diff --git a/.github/workflows/lua-test.yaml b/.github/workflows/lua-test.yaml index 40cdaf0c..bb1a5466 100644 --- a/.github/workflows/lua-test.yaml +++ b/.github/workflows/lua-test.yaml @@ -24,6 +24,9 @@ permissions: jobs: build-and-test: + strategy: + matrix: + lua_version: ["5.1", "5.2", "5.3", "5.4", "luajit-2.1"] defaults: run: working-directory: "lua" @@ -31,9 +34,11 @@ jobs: steps: - uses: actions/checkout@v6 - name: Setup lua toolchain - run: | - sudo apt-get update - sudo apt-get install -y lua-busted luarocks liblua5.2-dev + uses: leafo/gh-actions-lua@v13 + with: + luaVersion: "${{ matrix.lua_version }}" + - name: Setup luarocks + uses: leafo/gh-actions-luarocks@v6 - name: Install Rust uses: actions-rs/toolchain@v1 with: @@ -41,4 +46,6 @@ jobs: override: true components: clippy, rustfmt - name: Build - run: make + run: luarocks make ./*.rockspec + - name: Test + run: luarocks test ./*.rockspec diff --git a/lua/Cargo.toml b/lua/Cargo.toml index 6b8271b0..1bbe99b0 100644 --- a/lua/Cargo.toml +++ b/lua/Cargo.toml @@ -5,8 +5,12 @@ edition = "2024" publish = false [features] -default = ["mlua/lua52"] +lua51 = ["mlua", "mlua/lua51"] lua52 = ["mlua", "mlua/lua52"] +lua53 = ["mlua", "mlua/lua53"] +lua54 = ["mlua", "mlua/lua54"] +luajit = ["mlua", "mlua/luajit"] +luajit52 = ["mlua", "mlua/luajit52"] [lib] crate-type = ["cdylib"] diff --git a/lua/kcl_lib-0.12.3-1.rockspec b/lua/kcl_lib-0.12.3-1.rockspec index cf1e712b..f9f00abb 100644 --- a/lua/kcl_lib-0.12.3-1.rockspec +++ b/lua/kcl_lib-0.12.3-1.rockspec @@ -1,28 +1,35 @@ -package = "kcl_lib" -version = "0.12.3-1" +local package_version = "0.12.3" +local rockspec_revision = "1" +rockspec_format = "3.0" +package = "kcl_lib" +version = package_version .. "-" .. rockspec_revision source = { - url = "git+https://github.com/kcl-lang/kcl", + url = "git+https://github.com/kcl-lang/lib", + -- tag = "lua/v" .. version, } - description = { - summary = "KCL Lua Bindings", - detailed = [[ - KCL Lua Bindings + summary = "KCL Lua Bindings", + detailed = [[ + KCL Lua Bindings ]], - homepage = "https://kcl-lang.io/", - license = " Apache-2.0" + homepage = "https://kcl-lang.io/", + license = " Apache-2.0", } - dependencies = { - "lua >= 5.1", - "luarocks-build-rust-mlua = 0.2.0", + "lua >= 5.1", + "luarocks-build-rust-mlua = 0.2.0", +} +test_dependencies = { + "busted >= 2.2", +} +test = { + type = "busted", } - build = { - type = "rust-mlua", - modules = { - ["kcl_lib"] = "kcl_lib_lua", - }, - target_path = "target", -} \ No newline at end of file + type = "rust-mlua", + modules = { + ["kcl_lib"] = "kcl_lib_lua", + }, + target_path = "target", +} diff --git a/lua/test/exec_test.lua b/lua/spec/exec_spec.lua similarity index 70% rename from lua/test/exec_test.lua rename to lua/spec/exec_spec.lua index 4ab2d713..1ea73623 100644 --- a/lua/test/exec_test.lua +++ b/lua/spec/exec_spec.lua @@ -3,7 +3,9 @@ local kcl_lib = require("kcl_lib") describe("kcl lua lib unit test", function() describe("exec_program", function() it("operator function in fs schema", function() - local result, err = kcl_lib.exec_program({k_filename_list=["./test_data/schema.k"]}) + local result, err = kcl_lib.exec_program({ + k_filename_list = { "./spec/test_data/schema.k" }, + }) assert.is_nil(err) assert.are.equal(result.yaml_result, "app:\n replicas: 2") end) diff --git a/lua/test/test_data/schema.k b/lua/spec/test_data/schema.k similarity index 100% rename from lua/test/test_data/schema.k rename to lua/spec/test_data/schema.k diff --git a/lua/src/lib.rs b/lua/src/lib.rs index 2576ec42..e1ad06c4 100644 --- a/lua/src/lib.rs +++ b/lua/src/lib.rs @@ -5,9 +5,9 @@ use mlua::prelude::*; /// Execute KCL file with arguments and return the JSON/YAML result. fn exec_program<'a>(lua: &'a Lua, args: LuaTable<'a>) -> LuaResult> { let api = kcl_api::API::default(); - let work_dir: String = args.get("work_dir")?; - let k_filename_list: Vec = args.get("k_filename_list")?; - let k_code_list: Vec = args.get("k_code_list")?; + let work_dir: String = args.get("work_dir").unwrap_or_default(); + let k_filename_list: Vec = args.get("k_filename_list").unwrap_or_default(); + let k_code_list: Vec = args.get("k_code_list").unwrap_or_default(); let result = match api.exec_program(&kcl_api::ExecProgramArgs { work_dir, @@ -28,7 +28,7 @@ fn exec_program<'a>(lua: &'a Lua, args: LuaTable<'a>) -> LuaResult> } #[mlua::lua_module] -fn kcl_lib(lua: &Lua) -> LuaResult { +fn kcl_lib(lua: &Lua) -> LuaResult> { let module = lua.create_table()?; module.set("exec_program", lua.create_function(exec_program)?)?; Ok(module) diff --git a/lua/stylua.toml b/lua/stylua.toml new file mode 100644 index 00000000..3724db8e --- /dev/null +++ b/lua/stylua.toml @@ -0,0 +1,10 @@ +column_width = 80 +line_endings = "Unix" +indent_type = "Spaces" +indent_width = 2 +quote_style = "AutoPreferDouble" +call_parentheses = "Always" +collapse_simple_statement = "Never" + +[sort_requires] +enabled = true