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..698cdc85 100644 --- a/lua/cmake-tools/config.lua +++ b/lua/cmake-tools/config.lua @@ -26,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, @@ -48,7 +49,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.base_settings.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..7f50e38a 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.base_settings.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..40628fbc --- /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.base_settings.ctest_extra_args) + end) + + it("should parse user ctest empty arguments", function() + local config = Config:new(const) + assert.are_same({}, config.base_settings.ctest_extra_args) + end) +end) diff --git a/tests/ctest_spec.lua b/tests/ctest_spec.lua new file mode 100644 index 00000000..d827c056 --- /dev/null +++ b/tests/ctest_spec.lua @@ -0,0 +1,37 @@ +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)