From ad10f7fb89934b1f681b46151e214cdc877202ea Mon Sep 17 00:00:00 2001 From: Kalebe Alves Date: Sat, 8 Feb 2025 20:20:49 -0300 Subject: [PATCH 1/3] Option to pass ctest extra args --- README.md | 1 + lua/cmake-tools/config.lua | 7 ++++++- lua/cmake-tools/const.lua | 1 + lua/cmake-tools/test/ctest.lua | 3 +++ tests/config_spec.lua | 23 ++++++++++++++++++++++ tests/ctest_spec.lua | 35 ++++++++++++++++++++++++++++++++++ 6 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 tests/config_spec.lua create mode 100644 tests/ctest_spec.lua diff --git a/README.md b/README.md index 097affb5..ec77c7d5 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ local osys = require("cmake-tools.osys") require("cmake-tools").setup { cmake_command = "cmake", -- this is used to specify cmake command path ctest_command = "ctest", -- this is used to specify ctest command path + ctest_extra_args = {}, -- this is used to specify extra args for ctest cmake_use_preset = true, cmake_regenerate_on_save = true, -- auto generate when save CMakeLists.txt cmake_generate_options = { "-DCMAKE_EXPORT_COMPILE_COMMANDS=1" }, -- this will be passed when invoke `CMakeGenerate` diff --git a/lua/cmake-tools/config.lua b/lua/cmake-tools/config.lua index b744893e..faec5c49 100644 --- a/lua/cmake-tools/config.lua +++ b/lua/cmake-tools/config.lua @@ -11,6 +11,9 @@ local Config = { build_directory = nil, query_directory = nil, reply_directory = nil, + ctest = { + extra_args = {}, + }, build_type = nil, variant = nil, build_target = nil, @@ -48,7 +51,9 @@ function Config:new(const) obj.executor = const.cmake_executor obj.runner = const.cmake_runner - + for _,v in pairs(const.ctest_extra_args) do + table.insert(obj.ctest.extra_args, v) + end return obj end diff --git a/lua/cmake-tools/const.lua b/lua/cmake-tools/const.lua index 55f41d96..704efb46 100644 --- a/lua/cmake-tools/const.lua +++ b/lua/cmake-tools/const.lua @@ -2,6 +2,7 @@ local osys = require("cmake-tools.osys") local const = { cmake_command = "cmake", -- this is used to specify cmake command path ctest_command = "ctest", -- this is used to specify ctest command path + ctest_extra_args = {}, -- this is used to specify extra arguments for ctest cmake_use_preset = true, -- when `false`, this is used to define if the `--preset` option should be use on cmake commands cmake_regenerate_on_save = true, -- auto generate when save CMakeLists.txt cmake_generate_options = { "-DCMAKE_EXPORT_COMPILE_COMMANDS=1" }, -- this will be passed when invoke `CMakeGenerate` diff --git a/lua/cmake-tools/test/ctest.lua b/lua/cmake-tools/test/ctest.lua index e3575ee5..8977f762 100644 --- a/lua/cmake-tools/test/ctest.lua +++ b/lua/cmake-tools/test/ctest.lua @@ -39,6 +39,9 @@ function ctest.run(ctest_command, test_name, build_dir, env, config, opt) opt = opt or {} local args = { "--test-dir", utils.transform_path(build_dir), "-R", test_name, opt.args } + for _, v in pairs(config.ctest.extra_args) do + table.insert(args, v) + end utils.run(cmd, config.env_script, env, args, config.cwd, config.runner, nil) end diff --git a/tests/config_spec.lua b/tests/config_spec.lua new file mode 100644 index 00000000..7cb761d5 --- /dev/null +++ b/tests/config_spec.lua @@ -0,0 +1,23 @@ +local const = require("cmake-tools.const") + +describe('Config', function() + local Config + local local_const + + before_each(function() + package.loaded['cmake-tools.config'] = nil + Config = require('cmake-tools.config') + local_const = vim.deepcopy(const) + end) + + it('should parse user provided ctest arguments', function() + local_const.ctest_extra_args = { "-j", "6" } + local config = Config:new(local_const) + assert.are_same({ "-j", "6" }, config.ctest.extra_args) + end) + + it('should parse user ctest empty arguments', function() + local config = Config:new(const) + assert.are_same({}, config.ctest.extra_args) + end) +end) diff --git a/tests/ctest_spec.lua b/tests/ctest_spec.lua new file mode 100644 index 00000000..40b11b1b --- /dev/null +++ b/tests/ctest_spec.lua @@ -0,0 +1,35 @@ +local match = require('luassert.match') +local stub = require('luassert.stub') +local const = require('cmake-tools.const') +local ctest = require("cmake-tools.test.ctest") +local utils = require('cmake-tools.utils') + +describe("run", function() + local local_const + local Config + local expected + before_each(function() + package.loaded['cmake-tools.config'] = nil + Config = require('cmake-tools.config') + local_const = vim.deepcopy(const) + expected = { "--test-dir", "build_dir", "-R", "test_name" } + stub(utils, "run") + end) + + it("takes extra args from user config", function() + local_const.ctest_extra_args = { "-j", "6" } + local config = Config:new(local_const) + ctest:run("test_name", "build_dir", "env", config, {}) + table.insert(expected, "-j") + table.insert(expected, "6") + assert.stub(utils.run).was.called_with(match._, match._, match._, expected, + match._, match._, match._) + end) + + it("ignores extra args if not provided", function() + local config = Config:new(const) + ctest:run("test_name", "build_dir", "env", config, {}) + assert.stub(utils.run).was.called_with(match._, match._, match._, expected, + match._, match._, match._) + end) +end) From e76007b91dc12fc744f05b64ca7f3c2e8b365425 Mon Sep 17 00:00:00 2001 From: Kalebe Alves Date: Sun, 9 Feb 2025 17:08:08 -0300 Subject: [PATCH 2/3] Fix formatting issues --- lua/cmake-tools/config.lua | 2 +- tests/config_spec.lua | 10 +++++----- tests/ctest_spec.lua | 22 ++++++++++++---------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/lua/cmake-tools/config.lua b/lua/cmake-tools/config.lua index faec5c49..013b389e 100644 --- a/lua/cmake-tools/config.lua +++ b/lua/cmake-tools/config.lua @@ -51,7 +51,7 @@ function Config:new(const) obj.executor = const.cmake_executor obj.runner = const.cmake_runner - for _,v in pairs(const.ctest_extra_args) do + for _, v in pairs(const.ctest_extra_args) do table.insert(obj.ctest.extra_args, v) end return obj diff --git a/tests/config_spec.lua b/tests/config_spec.lua index 7cb761d5..7b62944e 100644 --- a/tests/config_spec.lua +++ b/tests/config_spec.lua @@ -1,22 +1,22 @@ local const = require("cmake-tools.const") -describe('Config', function() +describe("Config", function() local Config local local_const before_each(function() - package.loaded['cmake-tools.config'] = nil - Config = require('cmake-tools.config') + package.loaded["cmake-tools.config"] = nil + Config = require("cmake-tools.config") local_const = vim.deepcopy(const) end) - it('should parse user provided ctest arguments', function() + it("should parse user provided ctest arguments", function() local_const.ctest_extra_args = { "-j", "6" } local config = Config:new(local_const) assert.are_same({ "-j", "6" }, config.ctest.extra_args) end) - it('should parse user ctest empty arguments', function() + it("should parse user ctest empty arguments", function() local config = Config:new(const) assert.are_same({}, config.ctest.extra_args) end) diff --git a/tests/ctest_spec.lua b/tests/ctest_spec.lua index 40b11b1b..d827c056 100644 --- a/tests/ctest_spec.lua +++ b/tests/ctest_spec.lua @@ -1,16 +1,16 @@ -local match = require('luassert.match') -local stub = require('luassert.stub') -local const = require('cmake-tools.const') +local match = require("luassert.match") +local stub = require("luassert.stub") +local const = require("cmake-tools.const") local ctest = require("cmake-tools.test.ctest") -local utils = require('cmake-tools.utils') +local utils = require("cmake-tools.utils") describe("run", function() local local_const local Config local expected before_each(function() - package.loaded['cmake-tools.config'] = nil - Config = require('cmake-tools.config') + package.loaded["cmake-tools.config"] = nil + Config = require("cmake-tools.config") local_const = vim.deepcopy(const) expected = { "--test-dir", "build_dir", "-R", "test_name" } stub(utils, "run") @@ -22,14 +22,16 @@ describe("run", function() ctest:run("test_name", "build_dir", "env", config, {}) table.insert(expected, "-j") table.insert(expected, "6") - assert.stub(utils.run).was.called_with(match._, match._, match._, expected, - match._, match._, match._) + assert + .stub(utils.run).was + .called_with(match._, match._, match._, expected, match._, match._, match._) end) it("ignores extra args if not provided", function() local config = Config:new(const) ctest:run("test_name", "build_dir", "env", config, {}) - assert.stub(utils.run).was.called_with(match._, match._, match._, expected, - match._, match._, match._) + assert + .stub(utils.run).was + .called_with(match._, match._, match._, expected, match._, match._, match._) end) end) From 88f289531478494a588622b9110febe7b8d53d15 Mon Sep 17 00:00:00 2001 From: Kalebe Alves Date: Sat, 4 Oct 2025 01:59:23 -0300 Subject: [PATCH 3/3] Moving ctest extra args to Config.base_settings --- lua/cmake-tools/config.lua | 6 ++---- lua/cmake-tools/test/ctest.lua | 2 +- tests/config_spec.lua | 4 ++-- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lua/cmake-tools/config.lua b/lua/cmake-tools/config.lua index 013b389e..698cdc85 100644 --- a/lua/cmake-tools/config.lua +++ b/lua/cmake-tools/config.lua @@ -11,9 +11,6 @@ local Config = { build_directory = nil, query_directory = nil, reply_directory = nil, - ctest = { - extra_args = {}, - }, build_type = nil, variant = nil, build_target = nil, @@ -29,6 +26,7 @@ local Config = { generate_options = {}, build_options = {}, show_disabled_build_presets = true, + ctest_extra_args = {}, }, -- general config target_settings = {}, -- target specific config executor = nil, @@ -52,7 +50,7 @@ function Config:new(const) obj.executor = const.cmake_executor obj.runner = const.cmake_runner for _, v in pairs(const.ctest_extra_args) do - table.insert(obj.ctest.extra_args, v) + table.insert(obj.base_settings.ctest_extra_args, v) end return obj end diff --git a/lua/cmake-tools/test/ctest.lua b/lua/cmake-tools/test/ctest.lua index 8977f762..7f50e38a 100644 --- a/lua/cmake-tools/test/ctest.lua +++ b/lua/cmake-tools/test/ctest.lua @@ -39,7 +39,7 @@ function ctest.run(ctest_command, test_name, build_dir, env, config, opt) opt = opt or {} local args = { "--test-dir", utils.transform_path(build_dir), "-R", test_name, opt.args } - for _, v in pairs(config.ctest.extra_args) do + for _, v in pairs(config.base_settings.ctest_extra_args) do table.insert(args, v) end utils.run(cmd, config.env_script, env, args, config.cwd, config.runner, nil) diff --git a/tests/config_spec.lua b/tests/config_spec.lua index 7b62944e..40628fbc 100644 --- a/tests/config_spec.lua +++ b/tests/config_spec.lua @@ -13,11 +13,11 @@ describe("Config", function() it("should parse user provided ctest arguments", function() local_const.ctest_extra_args = { "-j", "6" } local config = Config:new(local_const) - assert.are_same({ "-j", "6" }, config.ctest.extra_args) + assert.are_same({ "-j", "6" }, config.base_settings.ctest_extra_args) end) it("should parse user ctest empty arguments", function() local config = Config:new(const) - assert.are_same({}, config.ctest.extra_args) + assert.are_same({}, config.base_settings.ctest_extra_args) end) end)