Skip to content

Commit 1eaf9c3

Browse files
DenzellceWolf
authored andcommitted
feat(presets): support testPresets
1 parent 81e622d commit 1eaf9c3

8 files changed

Lines changed: 242 additions & 41 deletions

File tree

lua/cmake-tools/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ local Config = {
1818
kit = nil,
1919
configure_preset = nil,
2020
build_preset = nil,
21+
test_preset = nil,
2122
base_settings = {
2223
env = {},
2324
build_dir = "",

lua/cmake-tools/init.lua

Lines changed: 74 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,54 @@ function cmake.select_configure_preset(callback)
838838
end
839839
end
840840

841+
function cmake.select_test_preset(callback)
842+
callback = type(callback) == "function" and callback
843+
or function(result)
844+
if result:is_ok() then
845+
cmake.generate({ bang = false, fargs = {} }, nil)
846+
end
847+
end
848+
if check_active_job_and_notify(callback) then
849+
return
850+
end
851+
852+
if get_cmake_configuration_or_notify(callback) == nil then
853+
return
854+
end
855+
856+
-- if exists presets
857+
if Presets.exists(config.cwd) then
858+
local presets = Presets:parse(config.cwd)
859+
local test_preset_names = presets:get_test_preset_names()
860+
local format_preset_name = function(p_name)
861+
local p = presets:get_test_preset(p_name)
862+
return p.displayName or p.name
863+
end
864+
vim.ui.select(
865+
test_preset_names,
866+
{
867+
prompt = "Select cmake test preset",
868+
format_item = format_preset_name,
869+
},
870+
vim.schedule_wrap(function(choice)
871+
if not choice then
872+
callback(Result:new_error(Types.NOT_SELECT_PRESET, "No test preset selected"))
873+
return
874+
end
875+
if config.test_preset ~= choice then
876+
config.test_preset = choice
877+
end
878+
callback(Result:new(Types.SUCCESS, nil, nil))
879+
end)
880+
)
881+
else
882+
callback(
883+
Result:new_error(Types.CANNOT_FIND_PRESETS_FILE, "Cannot find CMake[User]Presets file")
884+
)
885+
log.error("Cannot find CMake[User]Presets.json at Root (" .. config.cwd .. ") !!")
886+
end
887+
end
888+
841889
function cmake.select_build_preset(callback)
842890
callback = type(callback) == "function" and callback
843891
or function(result)
@@ -1173,7 +1221,17 @@ function cmake.run_test(opt, callback)
11731221
end
11741222

11751223
local env = environment.get_build_environment(config)
1176-
ctest.list_all_tests(config:build_directory_path(), function(all_tests)
1224+
local preset_name = nil
1225+
if config.test_preset and Presets.exists(config.cwd) then
1226+
local presets = Presets:parse(config.cwd)
1227+
local test_preset = presets:get_test_preset(config.test_preset)
1228+
if test_preset and test_preset:isValid() then
1229+
preset_name = config.test_preset
1230+
end
1231+
end
1232+
local build_dir = config:build_directory_path()
1233+
1234+
ctest.list_all_tests(build_dir, preset_name, function(all_tests)
11771235
if #all_tests == 0 then
11781236
return
11791237
end
@@ -1211,27 +1269,18 @@ function cmake.run_test(opt, callback)
12111269
return
12121270
end
12131271
local selected = items[idx]
1214-
if selected.type == Type.ALL then
1215-
ctest.run(const.ctest_command, nil, config:build_directory_path(), env, config, opt)
1216-
elseif selected.type == Type.LABEL then
1217-
ctest.run(
1218-
const.ctest_command,
1219-
nil,
1220-
config:build_directory_path(),
1221-
env,
1222-
config,
1223-
vim.tbl_extend("force", opt, { label = selected.label })
1224-
)
1225-
else
1226-
ctest.run(
1227-
const.ctest_command,
1228-
selected.name,
1229-
config:build_directory_path(),
1230-
env,
1231-
config,
1232-
opt
1233-
)
1272+
local run_opt = vim.tbl_extend("force", opt, {
1273+
preset = preset_name,
1274+
build_dir = build_dir,
1275+
})
1276+
1277+
if selected.type == Type.LABEL then
1278+
run_opt.label = selected.label
1279+
elseif selected.type == Type.TEST then
1280+
run_opt.test_name = selected.name
12341281
end
1282+
1283+
ctest.run(const.ctest_command, env, config, run_opt)
12351284
end)
12361285
end)
12371286
end
@@ -1367,6 +1416,10 @@ function cmake.get_build_preset()
13671416
return config.build_preset
13681417
end
13691418

1419+
function cmake.get_test_preset()
1420+
return config.test_preset
1421+
end
1422+
13701423
function cmake.get_build_directory()
13711424
return config.build_directory
13721425
end

lua/cmake-tools/presets.lua

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local Path = require("plenary.path")
22
local Preset = require("cmake-tools.preset")
33
local BuildPreset = require("cmake-tools.build_preset")
4+
local TestPreset = require("cmake-tools.test_preset")
45

56
-- Extends (or creates a new) key-value pair in [dest] in which the
67
-- key is [key] and the value is the resulting list table of merging
@@ -138,23 +139,26 @@ function Presets:parse(cwd)
138139
local function getPreset(name)
139140
return instance:get_configure_preset(name, { include_hidden = true, include_disabled = true })
140141
end
141-
return Preset:new(cwd, obj, getPreset)
142+
Preset:new(cwd, obj, getPreset)
142143
end
143144

144-
local function createBuildPreset(obj)
145-
return BuildPreset:new(cwd, obj)
145+
for _, preset in ipairs(instance.configurePresets) do
146+
createPreset(preset)
146147
end
147148

148-
for _, preset in ipairs(instance.configurePresets) do
149-
preset = createPreset(preset)
149+
instance.testPresets = instance.testPresets or {}
150+
for _, test_preset in ipairs(instance.testPresets) do
151+
TestPreset.new(cwd, test_preset)
150152
end
151153

154+
table.insert(instance.testPresets, TestPreset.new(cwd, { name = "None", valid = false }))
155+
152156
instance.buildPresets = instance.buildPresets or {}
153157
for _, build_preset in ipairs(instance.buildPresets) do
154-
build_preset = createBuildPreset(build_preset)
158+
BuildPreset:new(cwd, build_preset)
155159
end
156160

157-
table.insert(instance.buildPresets, createBuildPreset({ name = "None", valid = false }))
161+
table.insert(instance.buildPresets, BuildPreset:new(cwd, { name = "None", valid = false }))
158162

159163
return instance
160164
end
@@ -185,6 +189,10 @@ function Presets:get_configure_preset_names(opts)
185189
return get_preset_names(self.configurePresets, opts)
186190
end
187191

192+
function Presets:get_test_preset_names(opts)
193+
return get_preset_names(self.testPresets, opts)
194+
end
195+
188196
function Presets:get_build_preset_names(opts)
189197
local presets = get_preset_names(self.buildPresets, opts)
190198
local ret = {}
@@ -227,6 +235,10 @@ function Presets:get_configure_preset(name, opts)
227235
return get_preset(name, self.configurePresets, opts)
228236
end
229237

238+
function Presets:get_test_preset(name, opts)
239+
return get_preset(name, self.testPresets, opts)
240+
end
241+
230242
function Presets:get_build_preset(name, opts)
231243
return get_preset(name, self.buildPresets, { include_hidden = true, include_disabled = true })
232244
end

lua/cmake-tools/test/ctest.lua

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@ local ctest = {
55
job = nil,
66
}
77

8-
function ctest.list_all_tests(build_dir, callback)
8+
function ctest.list_all_tests(build_dir, preset_name, callback)
99
local result = {}
1010

11+
local args
12+
if preset_name then
13+
args = { "--preset", preset_name, "--show-only=json-v1" }
14+
else
15+
args = { "--test-dir", build_dir, "--show-only=json-v1" }
16+
end
17+
1118
ctest.job = Job:new({
1219
command = "ctest",
13-
args = { "--test-dir", build_dir, "--show-only=json-v1" },
20+
args = args,
1421
on_exit = function(j, _, _)
1522
vim.schedule(function()
1623
local json_data = ""
@@ -63,22 +70,27 @@ function ctest.get_all_labels(tests)
6370
return labels
6471
end
6572

66-
function ctest.run(ctest_command, test_name, build_dir, env, config, opt)
67-
local cmd = ctest_command
73+
function ctest.run(ctest_command, env, config, opt)
6874
opt = opt or {}
6975

70-
local args = { "--test-dir", utils.transform_path(build_dir) }
76+
local args = {}
77+
if opt.preset then
78+
vim.list_extend(args, { "--preset", opt.preset })
79+
else
80+
vim.list_extend(args, { "--test-dir", utils.transform_path(opt.build_dir) })
81+
end
82+
7183
if opt.label then
72-
table.insert(args, "-L")
73-
table.insert(args, opt.label)
74-
elseif test_name then
75-
table.insert(args, "-R")
76-
table.insert(args, test_name)
84+
vim.list_extend(args, { "-L", opt.label })
85+
elseif opt.test_name then
86+
vim.list_extend(args, { "-R", opt.test_name })
7787
end
88+
7889
if opt.args then
7990
table.insert(args, opt.args)
8091
end
81-
utils.run(cmd, config.env_script, env, args, config.cwd, config.runner, nil)
92+
93+
utils.run(ctest_command, config.env_script, env, args, config.cwd, config.runner, nil)
8294
end
8395

8496
function ctest.stop()

lua/cmake-tools/test_preset.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---@class TestPreset: CMakeTestPreset
2+
local TestPreset = {}
3+
TestPreset.__index = TestPreset
4+
5+
---@return TestPreset
6+
function TestPreset.new(cwd, obj)
7+
local instance = setmetatable(obj or {}, TestPreset)
8+
instance.cwd = cwd
9+
10+
if instance.valid == nil then
11+
instance.valid = true
12+
end
13+
14+
return instance
15+
end
16+
17+
function TestPreset:isValid()
18+
return self.valid
19+
end
20+
21+
return TestPreset
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---@meta _
2+
3+
---@class CMakeCondition
4+
---@field type "const"|"equals"|"notEquals"|"inList"|"notInList"|"matches"|"notMatches"|"anyOf"|"allOf"|"not"
5+
---@field value boolean?
6+
---@field lhs string?
7+
---@field rhs string?
8+
---@field string string?
9+
---@field list string[]?
10+
---@field regex string?
11+
---@field conditions CMakeCondition[]?
12+
---@field condition CMakeCondition?
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---@meta _
2+
3+
---@class CMakeTestPresetOutputOpts
4+
---@field shortProgress boolean?
5+
---@field verbosity ("default"|"verbose"|"extra")?
6+
---@field debug boolean?
7+
---@field outputOnFailure boolean?
8+
---@field quiet boolean?
9+
---@field outputLogFile string?
10+
---@field outputJUnitFile string?
11+
---@field labelSummary boolean?
12+
---@field subprojectSummary boolean?
13+
---@field maxPassedTestOutputSize integer?
14+
---@field maxFailedTestOutputSize integer?
15+
---@field testOutputTruncation string?
16+
---@field maxTestNameWidth integer?
17+
18+
---@class CMakeTestPresetFilterIncludeIndex
19+
---@field start integer?
20+
---@field end integer?
21+
---@field stride integer?
22+
---@field specificTests integer[]?
23+
24+
---@class CMakeTestPresetFilterInclude
25+
---@field name string?
26+
---@field label string?
27+
---@field useUnion boolean?
28+
---@field index CMakeTestPresetFilterIncludeIndex|string?
29+
30+
---@class CMakeTestPresetFilterExcludeFixtures
31+
---@field any string?
32+
---@field setup string?
33+
---@field cleanup string?
34+
35+
---@class CMakeTestPresetFilterExclude
36+
---@field name string?
37+
---@field label string?
38+
---@field fixtures CMakeTestPresetFilterExcludeFixtures?
39+
40+
---@class CMakeTestPresetFilter
41+
---@field include CMakeTestPresetFilterInclude?
42+
---@field exclude CMakeTestPresetFilterExclude?
43+
44+
---@class CMakeTestPresetExecutionRepeat
45+
---@field mode "until-fail"|"until-pass"|"after-timeout"
46+
---@field count integer
47+
48+
---@class CMakeTestPresetExecution
49+
---@field stopOnFailure boolean?
50+
---@field enableFailover boolean?
51+
---@field jobs integer?
52+
---@field resourceSpecFile string?
53+
---@field testLoad integer?
54+
---@field showOnly ("human"|"json-v1")?
55+
---@field repeat CMakeTestPresetExecutionRepeat?
56+
---@field interactiveDebugging boolean?
57+
---@field scheduleRandom boolean?
58+
---@field timeout integer?
59+
---@field noTestsAction ("default"|"error"|"ignore")?
60+
61+
---@class CMakeTestPreset
62+
---@field name string
63+
---@field valid boolean
64+
---@field hidden boolean?
65+
---@field inherits string|string[]?
66+
---@field condition CMakeCondition?
67+
---@field vendor table?
68+
---@field displayName string?
69+
---@field description string?
70+
---@field environment table<string, string?>?
71+
---@field configurePreset string?
72+
---@field inheritConfigureEnvironment boolean?
73+
---@field configuration string?
74+
---@field overwriteConfigurationFile string[]?
75+
---@field output CMakeTestPresetOutputOpts?
76+
---@field filter CMakeTestPresetFilter?
77+
---@field execution CMakeTestPresetExecution?
78+
---@field disabled boolean?
79+
---@field cwd string

0 commit comments

Comments
 (0)