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