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