Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can put this variable into Config.base_settings? So we can edit it easily.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea. Done.

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`
Expand Down
5 changes: 4 additions & 1 deletion lua/cmake-tools/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a for loop here might not have the desired outcome. Or at least only once^^
It will add the args to the Config table inject of the created obj.
This is not an issue introduced by this PR, but the way new is currently implemented might cause the args to be appended multiple times.
It might be better to assign it directly (like cmake_build_options) until the Config class is fixed

additionally, ctest_extra_args is an array -- you should use ipairs instead of pairs

table.insert(obj.base_settings.ctest_extra_args, v)
end
return obj
end

Expand Down
1 change: 1 addition & 0 deletions lua/cmake-tools/const.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
3 changes: 3 additions & 0 deletions lua/cmake-tools/test/ctest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 23 additions & 0 deletions tests/config_spec.lua
Original file line number Diff line number Diff line change
@@ -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)
37 changes: 37 additions & 0 deletions tests/ctest_spec.lua
Original file line number Diff line number Diff line change
@@ -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, {})
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ctest is not actually an instance. it is a plain table. The colon syntax is not right and passes a wrong value to the ctest_command parameter

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)