test.cmake 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. if(NOT DEFINED dir)
  2. message(FATAL_ERROR "dir not defined")
  3. endif()
  4. if(NOT DEFINED gen)
  5. message(FATAL_ERROR "gen not defined")
  6. endif()
  7. # TODO: Generalize this for other tests.
  8. function(run_test test)
  9. set(top_src "${CMAKE_CURRENT_LIST_DIR}")
  10. set(top_bin "${dir}")
  11. if(EXISTS ${top_src}/${test}-result.txt)
  12. file(READ ${top_src}/${test}-result.txt expect_result)
  13. string(REGEX REPLACE "\n+$" "" expect_result "${expect_result}")
  14. else()
  15. set(expect_result 0)
  16. endif()
  17. foreach(o out err)
  18. if(EXISTS ${top_src}/${test}-std${o}.txt)
  19. file(READ ${top_src}/${test}-std${o}.txt expect_std${o})
  20. string(REGEX REPLACE "\n+$" "" expect_std${o} "${expect_std${o}}")
  21. else()
  22. unset(expect_std${o})
  23. endif()
  24. endforeach()
  25. set(source_dir "${top_src}")
  26. set(binary_dir "${top_bin}/${test}-build")
  27. file(REMOVE_RECURSE "${binary_dir}")
  28. file(MAKE_DIRECTORY "${binary_dir}")
  29. execute_process(
  30. COMMAND ${CMAKE_COMMAND} "${source_dir}" -G "${gen}" -DTEST=${test}
  31. WORKING_DIRECTORY "${binary_dir}"
  32. OUTPUT_VARIABLE actual_stdout
  33. ERROR_VARIABLE actual_stderr
  34. RESULT_VARIABLE actual_result
  35. )
  36. set(msg "")
  37. if(NOT "${actual_result}" STREQUAL "${expect_result}")
  38. set(msg "${msg}Result is [${actual_result}], not [${expect_result}].\n")
  39. endif()
  40. foreach(o out err)
  41. string(REGEX REPLACE "\n+$" "" actual_std${o} "${actual_std${o}}")
  42. set(expect_${o} "")
  43. if(DEFINED expect_std${o})
  44. if(NOT "${actual_std${o}}" MATCHES "${expect_std${o}}")
  45. string(REGEX REPLACE "\n" "\n expect-${o}> " expect_${o}
  46. " expect-${o}> ${expect_std${o}}")
  47. set(expect_${o} "Expected std${o} to match:\n${expect_${o}}\n")
  48. set(msg "${msg}std${o} does not match that expected.\n")
  49. endif()
  50. endif()
  51. endforeach()
  52. if(msg)
  53. string(REGEX REPLACE "\n" "\n actual-out> " actual_out " actual-out> ${actual_stdout}")
  54. string(REGEX REPLACE "\n" "\n actual-err> " actual_err " actual-err> ${actual_stderr}")
  55. message(SEND_ERROR "${test} - FAILED:\n"
  56. "${msg}"
  57. "${expect_out}"
  58. "Actual stdout:\n${actual_out}\n"
  59. "${expect_err}"
  60. "Actual stderr:\n${actual_err}\n"
  61. )
  62. else()
  63. message(STATUS "${test} - PASSED")
  64. endif()
  65. endfunction()
  66. run_test(MissingNormal)
  67. run_test(MissingNormalRequired)
  68. run_test(MissingNormalVersion)
  69. run_test(MissingNormalWarnNoModuleOld)
  70. run_test(MissingNormalWarnNoModuleNew)
  71. run_test(MissingModule)
  72. run_test(MissingModuleRequired)
  73. run_test(MissingConfig)
  74. run_test(MissingConfigOneName)
  75. run_test(MissingConfigRequired)
  76. run_test(MissingConfigVersion)
  77. run_test(MixedModeOptions)