RunCMakeTest.cmake 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. include(RunCMake)
  2. set(RunCMake_TEST_TIMEOUT 60)
  3. unset(ENV{CTEST_PARALLEL_LEVEL})
  4. unset(ENV{CTEST_OUTPUT_ON_FAILURE})
  5. run_cmake_command(repeat-until-fail-bad1
  6. ${CMAKE_CTEST_COMMAND} --repeat-until-fail
  7. )
  8. run_cmake_command(repeat-until-fail-bad2
  9. ${CMAKE_CTEST_COMMAND} --repeat-until-fail foo
  10. )
  11. run_cmake_command(repeat-until-fail-good
  12. ${CMAKE_CTEST_COMMAND} --repeat-until-fail 2
  13. )
  14. function(run_repeat_until_fail_tests)
  15. # Use a single build tree for a few tests without cleaning.
  16. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/repeat-until-fail-build)
  17. set(RunCMake_TEST_NO_CLEAN 1)
  18. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  19. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  20. run_cmake(repeat-until-fail-cmake)
  21. run_cmake_command(repeat-until-fail-ctest
  22. ${CMAKE_CTEST_COMMAND} -C Debug --repeat-until-fail 3
  23. )
  24. endfunction()
  25. run_repeat_until_fail_tests()
  26. function(run_BadCTestTestfile)
  27. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/BadCTestTestfile)
  28. set(RunCMake_TEST_NO_CLEAN 1)
  29. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  30. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  31. file(WRITE "${RunCMake_TEST_BINARY_DIR}/CTestTestfile.cmake" "
  32. subdirs()
  33. ")
  34. run_cmake_command(BadCTestTestfile ${CMAKE_CTEST_COMMAND})
  35. endfunction()
  36. run_BadCTestTestfile()
  37. function(run_MergeOutput)
  38. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/MergeOutput)
  39. set(RunCMake_TEST_NO_CLEAN 1)
  40. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  41. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  42. file(WRITE "${RunCMake_TEST_BINARY_DIR}/CTestTestfile.cmake" "
  43. add_test(MergeOutput \"${CMAKE_COMMAND}\" -P \"${RunCMake_SOURCE_DIR}/MergeOutput.cmake\")
  44. ")
  45. run_cmake_command(MergeOutput ${CMAKE_CTEST_COMMAND} -V)
  46. endfunction()
  47. run_MergeOutput()
  48. function(run_LabelCount)
  49. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/LabelCount)
  50. set(RunCMake_TEST_NO_CLEAN 1)
  51. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  52. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  53. file(WRITE "${RunCMake_TEST_BINARY_DIR}/CTestTestfile.cmake" "
  54. add_test(test1 \"${CMAKE_COMMAND}\" -E echo \"test1\")
  55. set_tests_properties(test1 PROPERTIES LABELS 'bar')
  56. add_test(test2 \"${CMAKE_COMMAND}\" -E echo \"test2\")
  57. set_tests_properties(test2 PROPERTIES LABELS 'bar')
  58. add_test(test3 \"${CMAKE_COMMAND}\" -E echo \"test3\")
  59. set_tests_properties(test3 PROPERTIES LABELS 'foo')
  60. add_test(test4 \"${CMAKE_COMMAND}\" -E echo \"test4\")
  61. set_tests_properties(test4 PROPERTIES LABELS 'bar')
  62. ")
  63. run_cmake_command(LabelCount ${CMAKE_CTEST_COMMAND} -V)
  64. endfunction()
  65. run_LabelCount()
  66. function(run_SerialFailed)
  67. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/SerialFailed)
  68. set(RunCMake_TEST_NO_CLEAN 1)
  69. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  70. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  71. file(WRITE "${RunCMake_TEST_BINARY_DIR}/CTestTestfile.cmake" "
  72. add_test(NoSuchCommand no_such_command)
  73. set_tests_properties(NoSuchCommand PROPERTIES RUN_SERIAL ON)
  74. add_test(Echo \"${CMAKE_COMMAND}\" -E echo \"EchoTest\")
  75. ")
  76. run_cmake_command(SerialFailed ${CMAKE_CTEST_COMMAND} -V)
  77. endfunction()
  78. run_SerialFailed()
  79. function(run_TestLoad name load)
  80. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/TestLoad)
  81. set(RunCMake_TEST_NO_CLEAN 1)
  82. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  83. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  84. file(WRITE "${RunCMake_TEST_BINARY_DIR}/CTestTestfile.cmake" "
  85. add_test(TestLoad1 \"${CMAKE_COMMAND}\" -E echo \"test of --test-load\")
  86. add_test(TestLoad2 \"${CMAKE_COMMAND}\" -E echo \"test of --test-load\")
  87. ")
  88. run_cmake_command(${name} ${CMAKE_CTEST_COMMAND} -j2 --test-load ${load})
  89. endfunction()
  90. # Tests for the --test-load feature of ctest
  91. #
  92. # Spoof a load average value to make these tests more reliable.
  93. set(ENV{__CTEST_FAKE_LOAD_AVERAGE_FOR_TESTING} 5)
  94. # Verify that new tests are not started when the load average exceeds
  95. # our threshold and that they then run once the load average drops.
  96. run_TestLoad(test-load-wait 3)
  97. # Verify that warning message is displayed but tests still start when
  98. # an invalid argument is given.
  99. run_TestLoad(test-load-invalid 'two')
  100. # Verify that new tests are started when the load average falls below
  101. # our threshold.
  102. run_TestLoad(test-load-pass 10)
  103. unset(ENV{__CTEST_FAKE_LOAD_AVERAGE_FOR_TESTING})
  104. function(run_TestOutputSize)
  105. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/TestOutputSize)
  106. set(RunCMake_TEST_NO_CLEAN 1)
  107. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  108. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  109. file(WRITE "${RunCMake_TEST_BINARY_DIR}/CTestTestfile.cmake" "
  110. add_test(PassingTest \"${CMAKE_COMMAND}\" -E echo PassingTestOutput)
  111. add_test(FailingTest \"${CMAKE_COMMAND}\" -E no_such_command)
  112. ")
  113. run_cmake_command(TestOutputSize
  114. ${CMAKE_CTEST_COMMAND} -M Experimental -T Test
  115. --no-compress-output
  116. --test-output-size-passed 10
  117. --test-output-size-failed 12
  118. )
  119. endfunction()
  120. run_TestOutputSize()
  121. function(run_TestAffinity)
  122. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/TestAffinity)
  123. set(RunCMake_TEST_NO_CLEAN 1)
  124. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  125. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  126. # Create a test with affinity enabled. The default PROCESSORS
  127. # value is 1, so our expected output checks that this is the
  128. # number of processors in the mask.
  129. file(WRITE "${RunCMake_TEST_BINARY_DIR}/CTestTestfile.cmake" "
  130. add_test(Affinity \"${TEST_AFFINITY}\")
  131. set_tests_properties(Affinity PROPERTIES PROCESSOR_AFFINITY ON)
  132. ")
  133. # Run ctest with a large parallel level so that the value is
  134. # not responsible for capping the number of processors available.
  135. run_cmake_command(TestAffinity ${CMAKE_CTEST_COMMAND} -V -j 64)
  136. endfunction()
  137. if(TEST_AFFINITY)
  138. run_TestAffinity()
  139. endif()
  140. function(run_TestStdin)
  141. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/TestStdin)
  142. set(RunCMake_TEST_NO_CLEAN 1)
  143. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  144. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  145. file(WRITE "${RunCMake_TEST_BINARY_DIR}/CTestTestfile.cmake" "
  146. add_test(TestStdin \"${TEST_PRINT_STDIN}\")
  147. ")
  148. run_cmake_command(TestStdin ${CMAKE_CTEST_COMMAND} -V)
  149. endfunction()
  150. run_TestStdin()
  151. function(show_only_json_check_python v)
  152. if(RunCMake_TEST_FAILED OR NOT PYTHON_EXECUTABLE)
  153. return()
  154. endif()
  155. set(json_file "${RunCMake_TEST_BINARY_DIR}/ctest.json")
  156. file(WRITE "${json_file}" "${actual_stdout}")
  157. set(actual_stdout "" PARENT_SCOPE)
  158. execute_process(
  159. COMMAND ${PYTHON_EXECUTABLE} "${RunCMake_SOURCE_DIR}/show-only_json-v${v}_check.py" "${json_file}"
  160. RESULT_VARIABLE result
  161. OUTPUT_VARIABLE output
  162. ERROR_VARIABLE output
  163. )
  164. if(NOT result EQUAL 0)
  165. string(REPLACE "\n" "\n " output " ${output}")
  166. set(RunCMake_TEST_FAILED "Unexpected output:\n${output}" PARENT_SCOPE)
  167. endif()
  168. endfunction()
  169. function(run_ShowOnly)
  170. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/ShowOnly)
  171. set(RunCMake_TEST_NO_CLEAN 1)
  172. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  173. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  174. file(WRITE "${RunCMake_TEST_BINARY_DIR}/CTestTestfile.cmake" "
  175. add_test(ShowOnly \"${CMAKE_COMMAND}\" -E echo)
  176. set_tests_properties(ShowOnly PROPERTIES WILL_FAIL true _BACKTRACE_TRIPLES \"file1;1;add_test;file0;;\")
  177. add_test(ShowOnlyNotAvailable NOT_AVAILABLE)
  178. ")
  179. run_cmake_command(show-only_human ${CMAKE_CTEST_COMMAND} --show-only=human)
  180. run_cmake_command(show-only_bad ${CMAKE_CTEST_COMMAND} --show-only=bad)
  181. run_cmake_command(show-only_json-v1 ${CMAKE_CTEST_COMMAND} --show-only=json-v1)
  182. endfunction()
  183. run_ShowOnly()