@@ -838,6 +838,54 @@ function cmake.select_configure_preset(callback)
838838 end
839839end
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+
841889function 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 )
12371286end
@@ -1367,6 +1416,10 @@ function cmake.get_build_preset()
13671416 return config .build_preset
13681417end
13691418
1419+ function cmake .get_test_preset ()
1420+ return config .test_preset
1421+ end
1422+
13701423function cmake .get_build_directory ()
13711424 return config .build_directory
13721425end
0 commit comments