diff options
-rw-r--r-- | src/corelib/Qt6CTestMacros.cmake | 93 | ||||
-rw-r--r-- | tests/auto/cmake/CMakeLists.txt | 22 |
2 files changed, 75 insertions, 40 deletions
diff --git a/src/corelib/Qt6CTestMacros.cmake b/src/corelib/Qt6CTestMacros.cmake index 5a96e15ea6..4e1abe904c 100644 --- a/src/corelib/Qt6CTestMacros.cmake +++ b/src/corelib/Qt6CTestMacros.cmake @@ -113,44 +113,95 @@ function(_qt_internal_set_up_test_run_environment testname) endfunction() -# Checks if the test project can be built successfully. +# Checks if the test project can be built successfully. Arguments: +# +# SIMULATE_IN_SOURCE: If the option is specified, the function copies sources of the tests to the +# CMAKE_CURRENT_BINARY_DIR directory, creates internal build directory in the +# copied sources and uses this directory to build and test the project. +# This makes possible to have relative paths to the source files in the +# generated ninja rules. +# +# BINARY: Path to the test artifact that will be executed after the build is complete. If a +# relative path is specified, it will be counted from the build directory. # # TESTNAME: a custom test name to use instead of the one derived from the source directory name +# # BUILD_OPTIONS: a list of -D style CMake definitions to pass to ctest's --build-options (which # are ultimately passed to the CMake invocation of the test project) macro(_qt_internal_test_expect_pass _dir) - cmake_parse_arguments(_ARGS "" "BINARY;TESTNAME" "BUILD_OPTIONS" ${ARGN}) - + cmake_parse_arguments(_ARGS "SIMULATE_IN_SOURCE" "BINARY;TESTNAME" "BUILD_OPTIONS" ${ARGN}) if(_ARGS_TESTNAME) set(testname "${_ARGS_TESTNAME}") else() string(REPLACE "(" "_" testname "${_dir}") string(REPLACE ")" "_" testname "${testname}") + string(REPLACE "/" "_" testname "${testname}") endif() - set(__expect_pass__prefixes "${CMAKE_PREFIX_PATH}") - string(REPLACE ";" "\;" __expect_pass__prefixes "${__expect_pass__prefixes}") - - set(ctest_command_args - --build-and-test - "${CMAKE_CURRENT_SOURCE_DIR}/${_dir}" - "${CMAKE_CURRENT_BINARY_DIR}/${_dir}" - --build-config "${CMAKE_BUILD_TYPE}" - --build-generator "${CMAKE_GENERATOR}" - --build-makeprogram "${CMAKE_MAKE_PROGRAM}" - --build-project "${_dir}" - --build-options "-DCMAKE_PREFIX_PATH=${__expect_pass__prefixes}" ${BUILD_OPTIONS_LIST} - ${_ARGS_BUILD_OPTIONS} - --test-command ${_ARGS_BINARY}) - add_test(${testname} ${CMAKE_CTEST_COMMAND} ${ctest_command_args}) - if(_ARGS_BINARY) - _qt_internal_set_up_test_run_environment("${testname}") - endif() + set(__expect_pass_prefixes "${CMAKE_PREFIX_PATH}") + string(REPLACE ";" "\;" __expect_pass_prefixes "${__expect_pass_prefixes}") + + set(__expect_pass_build_dir "${CMAKE_CURRENT_BINARY_DIR}/${_dir}") + set(__expect_pass_source_dir "${CMAKE_CURRENT_SOURCE_DIR}/${_dir}") + if(_ARGS_SIMULATE_IN_SOURCE) + set(__expect_pass_in_source_build_dir "${CMAKE_CURRENT_BINARY_DIR}/in_source") + set(__expect_pass_build_dir "${__expect_pass_in_source_build_dir}/${_dir}/build") + set(__expect_pass_source_dir "${__expect_pass_in_source_build_dir}/${_dir}") + + unset(__expect_pass_in_source_build_dir) + endif() + + if(_ARGS_BINARY AND NOT IS_ABSOLUTE "${_ARGS_BINARY}") + set(_ARGS_BINARY "${__expect_pass_build_dir}/${_ARGS_BINARY}") + endif() + + if(_ARGS_SIMULATE_IN_SOURCE) + add_test(NAME ${testname}_cleanup + COMMAND ${CMAKE_COMMAND} -E remove_directory "${__expect_pass_source_dir}" + ) + set_tests_properties(${testname}_cleanup PROPERTIES + FIXTURES_SETUP "${testname}SIMULATE_IN_SOURCE_FIXTURE" + ) + add_test(${testname}_copy_sources ${CMAKE_COMMAND} -E copy_directory + "${CMAKE_CURRENT_SOURCE_DIR}/${_dir}" "${__expect_pass_source_dir}" + ) + set_tests_properties(${testname}_copy_sources PROPERTIES + FIXTURES_SETUP "${testname}SIMULATE_IN_SOURCE_FIXTURE" + DEPENDS ${testname}_cleanup + ) + endif() + + set(ctest_command_args + --build-and-test + "${__expect_pass_source_dir}" + "${__expect_pass_build_dir}" + --build-config "${CMAKE_BUILD_TYPE}" + --build-generator "${CMAKE_GENERATOR}" + --build-makeprogram "${CMAKE_MAKE_PROGRAM}" + --build-project "${_dir}" + --build-options "-DCMAKE_PREFIX_PATH=${__expect_pass_prefixes}" ${BUILD_OPTIONS_LIST} + ${_ARGS_BUILD_OPTIONS} + --test-command ${_ARGS_BINARY} + ) + add_test(${testname} ${CMAKE_CTEST_COMMAND} ${ctest_command_args}) + if(_ARGS_SIMULATE_IN_SOURCE) + set_tests_properties(${testname} PROPERTIES + FIXTURES_REQUIRED "${testname}SIMULATE_IN_SOURCE_FIXTURE") + endif() + + if(_ARGS_BINARY) + _qt_internal_set_up_test_run_environment("${testname}") + endif() + + unset(__expect_pass_source_dir) + unset(__expect_pass_build_dir) + unset(__expect_pass_prefixes) endmacro() # Checks if the build of the test project fails. # This test passes if the test project fails either at the # configuring or build steps. +# Arguments: See _qt_internal_test_expect_pass macro(_qt_internal_test_expect_fail) _qt_internal_test_expect_pass(${ARGV}) set_tests_properties(${testname} PROPERTIES WILL_FAIL TRUE) diff --git a/tests/auto/cmake/CMakeLists.txt b/tests/auto/cmake/CMakeLists.txt index b43aa8f0a6..4fb6c24d8d 100644 --- a/tests/auto/cmake/CMakeLists.txt +++ b/tests/auto/cmake/CMakeLists.txt @@ -108,26 +108,10 @@ _qt_internal_test_expect_pass(test_platform_defs_include) _qt_internal_test_expect_pass(test_qtmainwin_library) if (CMAKE_GENERATOR STREQUAL Ninja AND UNIX AND NOT WIN32) - set(qfindtestdata_build_dir "${CMAKE_CURRENT_SOURCE_DIR}/test_QFINDTESTDATA/build") - add_test(test_QFINDTESTDATA ${CMAKE_CTEST_COMMAND} - --build-and-test - "${CMAKE_CURRENT_SOURCE_DIR}/test_QFINDTESTDATA" - # Build in a subdir of the source dir. - # This causes Ninja to use relative paths. - "${qfindtestdata_build_dir}" - --build-config "${CMAKE_BUILD_TYPE}" - --build-generator "${CMAKE_GENERATOR}" - --build-makeprogram "${CMAKE_MAKE_PROGRAM}" - --build-options "-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}" ${BUILD_OPTIONS_LIST} + _qt_internal_test_expect_pass(test_QFINDTESTDATA + BINARY "tests/test_QFINDTESTDATA" + SIMULATE_IN_SOURCE ) - set_tests_properties(test_QFINDTESTDATA PROPERTIES FIXTURES_SETUP QFINDTESTDATA) - - add_test(NAME run_test_QFINDTESTDATA COMMAND sh -c "cd \"${qfindtestdata_build_dir}/tests\" && ./test_QFINDTESTDATA -v2") - set_tests_properties(run_test_QFINDTESTDATA PROPERTIES FIXTURES_REQUIRED QFINDTESTDATA) - - # source dir should be untouched by build, so remove build results - add_test(NAME cleanup_test_QFINDTESTDATA COMMAND sh -c "rm -rf \"${qfindtestdata_build_dir}\"") - set_tests_properties(cleanup_test_QFINDTESTDATA PROPERTIES FIXTURES_CLEANUP QFINDTESTDATA) endif() if (NOT NO_DBUS) |