Skip to content

Commit 81e622d

Browse files
DenzellceWolf
authored andcommitted
feat(ctest): add option to include labels into test selection
1 parent 5fc40e9 commit 81e622d

5 files changed

Lines changed: 85 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ local osys = require("cmake-tools.osys")
2828
require("cmake-tools").setup {
2929
cmake_command = "cmake", -- this is used to specify cmake command path
3030
ctest_command = "ctest", -- this is used to specify ctest command path
31+
ctest_show_labels = false, -- also show labels in the test picker
3132
cmake_use_preset = true,
3233
cmake_regenerate_on_save = true, -- auto generate when save CMakeLists.txt
3334
cmake_generate_options = { "-DCMAKE_EXPORT_COMPILE_COMMANDS=1" }, -- this will be passed when invoke `CMakeGenerate`

lua/cmake-tools/config.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ local Config = {
2626
generate_options = {},
2727
build_options = {},
2828
show_disabled_build_presets = true,
29+
ctest_show_labels = false,
2930
}, -- general config
3031
target_settings = {}, -- target specific config
3132
executor = nil,
@@ -45,6 +46,7 @@ function Config:new(const)
4546
obj.base_settings.use_preset = const.cmake_use_preset
4647

4748
obj.base_settings.show_disabled_build_presets = const.cmake_show_disabled_build_presets
49+
obj.base_settings.ctest_show_labels = const.ctest_show_labels
4850

4951
obj.executor = const.cmake_executor
5052
obj.runner = const.cmake_runner
@@ -148,6 +150,10 @@ function Config:show_disabled_build_presets()
148150
return self.base_settings.show_disabled_build_presets
149151
end
150152

153+
function Config:ctest_show_labels()
154+
return self.base_settings.ctest_show_labels
155+
end
156+
151157
function Config:generate_build_directory()
152158
local build_directory = Path:new(self.build_directory)
153159

lua/cmake-tools/const.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local osys = require("cmake-tools.osys")
22
local const = {
33
cmake_command = "cmake", -- this is used to specify cmake command path
44
ctest_command = "ctest", -- this is used to specify ctest command path
5+
ctest_show_labels = false, -- show test labels in the test picker (when true, labels from ctest are shown as filterable entries)
56
cmake_use_preset = true, -- when `false`, this is used to define if the `--preset` option should be use on cmake commands
67
cmake_regenerate_on_save = true, -- auto generate when save CMakeLists.txt
78
cmake_generate_options = { "-DCMAKE_EXPORT_COMPILE_COMMANDS=1" }, -- this will be passed when invoke `CMakeGenerate`

lua/cmake-tools/init.lua

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,17 +1177,55 @@ function cmake.run_test(opt, callback)
11771177
if #all_tests == 0 then
11781178
return
11791179
end
1180-
table.insert(all_tests, 1, "all")
1181-
vim.ui.select(all_tests, { prompt = "select test to run" }, function(_, idx)
1180+
1181+
local items = {}
1182+
local display = {}
1183+
1184+
local Type = {
1185+
ALL = 1,
1186+
LABEL = 2,
1187+
TEST = 3,
1188+
}
1189+
1190+
table.insert(items, { type = Type.ALL })
1191+
table.insert(display, "all")
1192+
1193+
if config:ctest_show_labels() then
1194+
local labels = ctest.get_all_labels(all_tests)
1195+
for _, entry in ipairs(labels) do
1196+
table.insert(items, { type = Type.LABEL, label = entry.label })
1197+
table.insert(
1198+
display,
1199+
table.concat({ "[label]", entry.label, "(" .. entry.count, "tests)" }, " ")
1200+
)
1201+
end
1202+
end
1203+
1204+
for _, test in ipairs(all_tests) do
1205+
table.insert(items, { type = Type.TEST, name = test.name })
1206+
table.insert(display, test.name)
1207+
end
1208+
1209+
vim.ui.select(display, { prompt = "select test to run" }, function(_, idx)
11821210
if not idx then
11831211
return
11841212
end
1185-
if idx == 1 then
1213+
local selected = items[idx]
1214+
if selected.type == Type.ALL then
11861215
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+
)
11871225
else
11881226
ctest.run(
11891227
const.ctest_command,
1190-
all_tests[idx],
1228+
selected.name,
11911229
config:build_directory_path(),
11921230
env,
11931231
config,

lua/cmake-tools/test/ctest.lua

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local Job = require("plenary.job")
22
local utils = require("cmake-tools.utils")
3-
local const = require("cmake-tools.const")
43

54
local ctest = {
65
job = nil,
@@ -24,7 +23,16 @@ function ctest.list_all_tests(build_dir, callback)
2423

2524
local tests = {}
2625
for _, item in ipairs(result.tests) do
27-
table.insert(tests, item["name"])
26+
local labels = {}
27+
if item["properties"] then
28+
for _, prop in ipairs(item["properties"]) do
29+
if prop["name"] == "LABELS" then
30+
labels = prop["value"] or {}
31+
break
32+
end
33+
end
34+
end
35+
table.insert(tests, { name = item["name"], labels = labels })
2836
end
2937
callback(tests)
3038
end)
@@ -34,12 +42,36 @@ function ctest.list_all_tests(build_dir, callback)
3442
ctest.job:start()
3543
end
3644

45+
--- Collect all labels from test objects with counts
46+
--- @param tests { name: string, labels: string[] }[]
47+
--- @return { label: string, count: number }[] sorted list of labels with their test counts
48+
function ctest.get_all_labels(tests)
49+
local label_counts = {}
50+
for _, test in ipairs(tests) do
51+
for _, label in ipairs(test.labels) do
52+
label_counts[label] = (label_counts[label] or 0) + 1
53+
end
54+
end
55+
56+
local labels = {}
57+
for label, count in pairs(label_counts) do
58+
table.insert(labels, { label = label, count = count })
59+
end
60+
table.sort(labels, function(a, b)
61+
return a.label < b.label
62+
end)
63+
return labels
64+
end
65+
3766
function ctest.run(ctest_command, test_name, build_dir, env, config, opt)
3867
local cmd = ctest_command
3968
opt = opt or {}
4069

4170
local args = { "--test-dir", utils.transform_path(build_dir) }
42-
if test_name then
71+
if opt.label then
72+
table.insert(args, "-L")
73+
table.insert(args, opt.label)
74+
elseif test_name then
4375
table.insert(args, "-R")
4476
table.insert(args, test_name)
4577
end

0 commit comments

Comments
 (0)