summaryrefslogtreecommitdiffstats
path: root/Tests/CMakeCommands/build_command/RunCMake.cmake
blob: 55d93596089c478d6c24ac8574dce36a0a908c79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
if(NOT DEFINED CMake_SOURCE_DIR)
  message(FATAL_ERROR "CMake_SOURCE_DIR not defined")
endif()

if(NOT DEFINED dir)
  message(FATAL_ERROR "dir not defined")
endif()

if(NOT DEFINED gen)
  message(FATAL_ERROR "gen not defined")
endif()

message(STATUS "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)")

# Run cmake:
#
function(run_cmake build_dir extra_args expected_result expected_output expected_error)
  message(STATUS "run_cmake build_dir='${build_dir}' extra_args='${extra_args}'")

  # Ensure build_dir exists:
  #
  execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${build_dir})

  # Run cmake:
  #
  execute_process(COMMAND ${CMAKE_COMMAND}
    ${extra_args}
    -G ${gen} ${CMake_SOURCE_DIR}/Tests/CMakeCommands/build_command
    RESULT_VARIABLE result
    OUTPUT_VARIABLE stdout
    ERROR_VARIABLE stderr
    WORKING_DIRECTORY ${build_dir}
    )

  message(STATUS "result='${result}'")
  message(STATUS "stdout='${stdout}'")
  message(STATUS "stderr='${stderr}'")
  message(STATUS "")

  # Verify result and output match expectations:
  #
  if("0" STREQUAL "${expected_result}")
    if(NOT "${result}" STREQUAL "0")
      message(FATAL_ERROR
        "error: result='${result}' is non-zero and different than expected_result='${expected_result}'")
    endif()
  else()
    if("${result}" STREQUAL "0")
      message(FATAL_ERROR
        "error: result='${result}' is zero and different than expected_result='${expected_result}'")
    endif()
  endif()

  foreach(e ${expected_output})
    if(NOT stdout MATCHES "${e}")
      message(FATAL_ERROR
        "error: stdout does not match expected_output item e='${e}'")
    else()
      message(STATUS "info: stdout matches '${e}'")
    endif()
  endforeach()

  foreach(e ${expected_error})
    if(NOT stderr MATCHES "${e}")
      message(FATAL_ERROR
        "error: stderr does not match expected_error item e='${e}'")
    else()
      message(STATUS "info: stderr matches '${e}'")
    endif()
  endforeach()

  message(STATUS "result, stdout and stderr match all expectations: test passes")
  message(STATUS "")
endfunction()


# Expect this case to succeed:
run_cmake("${dir}/b1" "" 0
  "Build files have been written to:"
  "skipping cases 1, 2 and 3 because TEST_ERROR_CONDITIONS is OFF")


# Expect this one to fail:
run_cmake("${dir}/b2" "-DTEST_ERROR_CONDITIONS:BOOL=ON" 1
  "Configuring incomplete, errors occurred!"
  "build_command requires at least one argument naming a CMake variable;build_command unknown argument ")