test.cmake 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. if(NOT DEFINED CMake_SOURCE_DIR)
  2. message(FATAL_ERROR "CMake_SOURCE_DIR not defined")
  3. endif()
  4. if(NOT DEFINED dir)
  5. message(FATAL_ERROR "dir not defined")
  6. endif()
  7. if(NOT DEFINED gen)
  8. message(FATAL_ERROR "gen not defined")
  9. endif()
  10. message(STATUS "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)")
  11. # Run cmake:
  12. #
  13. function(run_cmake build_dir extra_args expected_result expected_output expected_error)
  14. message(STATUS "run_cmake build_dir='${build_dir}' extra_args='${extra_args}'")
  15. # Ensure build_dir exists:
  16. #
  17. execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${build_dir})
  18. # Run cmake:
  19. #
  20. execute_process(COMMAND ${CMAKE_COMMAND}
  21. ${extra_args}
  22. -G ${gen} ${CMake_SOURCE_DIR}/Tests/CMakeCommands/build_command
  23. RESULT_VARIABLE result
  24. OUTPUT_VARIABLE stdout
  25. ERROR_VARIABLE stderr
  26. WORKING_DIRECTORY ${build_dir}
  27. )
  28. message(STATUS "result='${result}'")
  29. message(STATUS "stdout='${stdout}'")
  30. message(STATUS "stderr='${stderr}'")
  31. message(STATUS "")
  32. # Verify result and output match expectations:
  33. #
  34. if("0" STREQUAL "${expected_result}")
  35. if(NOT "${result}" STREQUAL "0")
  36. message(FATAL_ERROR
  37. "error: result='${result}' is non-zero and different than expected_result='${expected_result}'")
  38. endif()
  39. else()
  40. if("${result}" STREQUAL "0")
  41. message(FATAL_ERROR
  42. "error: result='${result}' is zero and different than expected_result='${expected_result}'")
  43. endif()
  44. endif()
  45. foreach(e ${expected_output})
  46. if(NOT stdout MATCHES "${e}")
  47. message(FATAL_ERROR
  48. "error: stdout does not match expected_output item e='${e}'")
  49. else()
  50. message(STATUS "info: stdout matches '${e}'")
  51. endif()
  52. endforeach()
  53. foreach(e ${expected_error})
  54. if(NOT stderr MATCHES "${e}")
  55. message(FATAL_ERROR
  56. "error: stderr does not match expected_error item e='${e}'")
  57. else()
  58. message(STATUS "info: stderr matches '${e}'")
  59. endif()
  60. endforeach()
  61. message(STATUS "result, stdout and stderr match all expectations: test passes")
  62. message(STATUS "")
  63. endfunction()
  64. # Expect this case to succeed:
  65. run_cmake("${dir}/b1" "" 0
  66. "Build files have been written to:"
  67. "skipping cases 1, 2 and 3 because TEST_ERROR_CONDITIONS is OFF")
  68. # Expect this one to fail:
  69. run_cmake("${dir}/b2" "-DTEST_ERROR_CONDITIONS:BOOL=ON" 1
  70. "Configuring incomplete, errors occurred!"
  71. "build_command requires at least one argument naming a CMake variable;build_command unknown argument ")