Home | History | Annotate | Download | only in cmake
      1 # ----------------------------------------------------------------------------
      2 # sanitize string stored in variable for use in regular expression.
      3 macro (sanitize_for_regex STRVAR)
      4   string (REGEX REPLACE "([.+*?^$()])" "\\\\\\1" ${STRVAR} "${${STRVAR}}")
      5 endmacro ()
      6 
      7 # ----------------------------------------------------------------------------
      8 # script arguments
      9 if (NOT COMMAND)
     10   message (FATAL_ERROR "Test command not specified!")
     11 endif ()
     12 if (NOT DEFINED EXPECTED_RC)
     13   set (EXPECTED_RC 0)
     14 endif ()
     15 if (EXPECTED_OUTPUT)
     16   sanitize_for_regex(EXPECTED_OUTPUT)
     17 endif ()
     18 if (UNEXPECTED_OUTPUT)
     19   sanitize_for_regex(UNEXPECTED_OUTPUT)
     20 endif ()
     21 
     22 # ----------------------------------------------------------------------------
     23 # set a few environment variables (useful for --tryfromenv)
     24 set (ENV{FLAGS_undefok} "foo,bar")
     25 set (ENV{FLAGS_weirdo}  "")
     26 set (ENV{FLAGS_version} "true")
     27 set (ENV{FLAGS_help}    "false")
     28 
     29 # ----------------------------------------------------------------------------
     30 # execute test command
     31 execute_process(
     32   COMMAND ${COMMAND}
     33   RESULT_VARIABLE RC
     34   OUTPUT_VARIABLE OUTPUT
     35   ERROR_VARIABLE  OUTPUT
     36 )
     37 
     38 if (OUTPUT)
     39   message ("${OUTPUT}")
     40 endif ()
     41 
     42 # ----------------------------------------------------------------------------
     43 # check test result
     44 if (NOT RC EQUAL EXPECTED_RC)
     45   string (REPLACE ";" " " COMMAND "${COMMAND}")
     46   message (FATAL_ERROR "Command:\n\t${COMMAND}\nExit status is ${RC}, expected ${EXPECTED_RC}")
     47 endif ()
     48 if (EXPECTED_OUTPUT AND NOT OUTPUT MATCHES "${EXPECTED_OUTPUT}")
     49   message (FATAL_ERROR "Test output does not match expected output: ${EXPECTED_OUTPUT}")
     50 endif ()
     51 if (UNEXPECTED_OUTPUT AND OUTPUT MATCHES "${UNEXPECTED_OUTPUT}")
     52   message (FATAL_ERROR "Test output matches unexpected output: ${UNEXPECTED_OUTPUT}")
     53 endif ()