From ba768963e5a8fc0feb46c7f0aae395fc4e4c3e06 Mon Sep 17 00:00:00 2001 From: Zack Galbreath Date: Thu, 28 May 2026 18:05:11 -0400 Subject: [PATCH] ctest: honor all -D definitions in command-line dashboard client mode Commit 63b6e7d785 (ctest: allow dashboard clients to specify presets via variables, 2026-05-26) added support for the CTEST_PRESET family of variables that provide users with an additional method of defining what preset to use (if any) for dashboard client steps. However, these variables were not being honored properly when defined on the command-line with ctest -D. This was caused by the fact that these variables do not have a corresponding CTest configuration setting. Instead, they are read directly using cmMakefile::GetDefinition(). To fix this, we now inject all variables defined with ctest -D into the cmMakefile used by CTest in ProcessSteps(). This more closely matches the existing behavior of cmCTestScriptHandler (ctest -S mode). In addition to fixing the newly added CTEST_PRESET variables, this also allow users to properly define CTEST_BUILD_CONFIGURATION and CTEST_CONFIGURATION_TYPE from the command-line. --- Source/cmCTest.cxx | 11 ++++++++ Tests/RunCMake/ctest_test/RunCMakeTest.cmake | 26 +++++++++++++++++++ .../ctest_test/TestPresetCLIVar-stdout.txt | 1 + 3 files changed, 38 insertions(+) create mode 100644 Tests/RunCMake/ctest_test/TestPresetCLIVar-stdout.txt diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx index 8a94dc5894..6272331165 100644 --- a/Source/cmCTest.cxx +++ b/Source/cmCTest.cxx @@ -735,6 +735,17 @@ int cmCTest::ProcessSteps() cmMakefile& mf = *script.GetMakefile(); this->ReadCustomConfigurationFileTree(this->Impl->BinaryDir, &mf); this->SetCMakeVariables(mf); + + // Inject variables passed via -D so that ctest_* commands can read them + // using mf.GetDefinition(). This matches what cmCTestScriptHandler does + // before running a -S script. SetCMakeVariables() (above) only defines + // variables that correspond to a CTest Configuration key. Other variables + // such as CTEST_PRESET are read directly from the makefile and would + // otherwise be invisible here. + for (auto const& def : this->GetDefinitions()) { + mf.AddDefinition(def.first, def.second); + } + // CTEST_TIME_LIMIT may come from CTestCustom.cmake (already in the makefile) // or from the config map (just populated by SetCMakeVariables above). this->SetTimeLimit(mf.GetDefinition("CTEST_TIME_LIMIT")); diff --git a/Tests/RunCMake/ctest_test/RunCMakeTest.cmake b/Tests/RunCMake/ctest_test/RunCMakeTest.cmake index ea399768b6..59cbb801ad 100644 --- a/Tests/RunCMake/ctest_test/RunCMakeTest.cmake +++ b/Tests/RunCMake/ctest_test/RunCMakeTest.cmake @@ -45,6 +45,32 @@ endforeach() run_ctest(TestPresetGenericVar) unset(CASE_TEST_PREFIX_CODE) + # Verify that CTEST_TEST_PRESET passed via -D on the command line reaches + # ctest_test() when ctest is run with -M/-T. + set(case_source_dir "${RunCMake_BINARY_DIR}/TestPresetCLIVar") + set(case_binary_dir "${RunCMake_BINARY_DIR}/TestPresetCLIVar-build") + configure_file( + "${RunCMake_SOURCE_DIR}/CMakePresets.json.in" + "${case_source_dir}/CMakePresets.json" + @ONLY) + file(REMOVE_RECURSE "${case_binary_dir}") + file(MAKE_DIRECTORY "${case_binary_dir}") + file(WRITE "${case_binary_dir}/DartConfiguration.tcl" + "BuildDirectory: ${case_binary_dir}\nSourceDirectory: ${case_source_dir}\n") + file(WRITE "${case_binary_dir}/CTestTestfile.cmake" + "add_test(test1 \"${CMAKE_COMMAND}\" -E true)\n" + "add_test(test2 \"${CMAKE_COMMAND}\" -E true)\n" + "add_test(test3 \"${CMAKE_COMMAND}\" -E true)\n") + set(RunCMake_TEST_BINARY_DIR "${case_binary_dir}") + set(RunCMake_TEST_NO_CLEAN 1) + run_cmake_command(TestPresetCLIVar + ${CMAKE_CTEST_COMMAND} + -C Debug + -M Experimental + -D "CTEST_TEST_PRESET=my-include-preset" + -T Test + -V) + set(custom_presets_file "${RunCMake_BINARY_DIR}/TestPresetFileInclude/custom-presets.json") configure_file( diff --git a/Tests/RunCMake/ctest_test/TestPresetCLIVar-stdout.txt b/Tests/RunCMake/ctest_test/TestPresetCLIVar-stdout.txt new file mode 100644 index 0000000000..913fad5104 --- /dev/null +++ b/Tests/RunCMake/ctest_test/TestPresetCLIVar-stdout.txt @@ -0,0 +1 @@ +1/1 Test #[0-9]+: test1 \.+ Passed +[0-9\.]+ sec