diff options
author | Adam Weisi <adam.weisi@intel.com> | 2017-04-13 17:10:04 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2017-06-10 12:03:06 (GMT) |
commit | d6051ca39e2ac8e5afc8f6308fd1bda7d1e8c17b (patch) | |
tree | 231f86c5da5035fb62d94c2484de56785b3bf968 /Tests/RunCMake | |
parent | c095e90f3af78fb3421c80b725ebc6ad9ec85999 (diff) | |
download | CMake-d6051ca39e2ac8e5afc8f6308fd1bda7d1e8c17b.zip CMake-d6051ca39e2ac8e5afc8f6308fd1bda7d1e8c17b.tar.gz CMake-d6051ca39e2ac8e5afc8f6308fd1bda7d1e8c17b.tar.bz2 |
execute_process: Add option to get results of every child
Add a `RESULTS_VARIABLE` option to get the results of all children
in a pipeline of one or more `COMMAND`s.
Diffstat (limited to 'Tests/RunCMake')
-rw-r--r-- | Tests/RunCMake/CMakeLists.txt | 4 | ||||
-rw-r--r-- | Tests/RunCMake/execute_process/ExitValues-stdout.txt | 14 | ||||
-rw-r--r-- | Tests/RunCMake/execute_process/ExitValues.cmake | 120 | ||||
-rw-r--r-- | Tests/RunCMake/execute_process/RunCMakeTest.cmake | 4 | ||||
-rw-r--r-- | Tests/RunCMake/exit_code.c | 30 |
5 files changed, 171 insertions, 1 deletions
diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 79f487d..91e0cdd 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -181,8 +181,10 @@ add_RunCMake_test(add_custom_target) add_RunCMake_test(add_dependencies) add_RunCMake_test(add_subdirectory) add_RunCMake_test(build_command) +add_executable(exit_code exit_code.c) +set(execute_process_ARGS -DEXIT_CODE_EXE=$<TARGET_FILE:exit_code>) if(NOT CMake_TEST_EXTERNAL_CMAKE) - set(execute_process_ARGS -DTEST_ENCODING_EXE=$<TARGET_FILE:testEncoding>) + list(APPEND execute_process_ARGS -DTEST_ENCODING_EXE=$<TARGET_FILE:testEncoding>) endif() add_RunCMake_test(execute_process) add_RunCMake_test(export) diff --git a/Tests/RunCMake/execute_process/ExitValues-stdout.txt b/Tests/RunCMake/execute_process/ExitValues-stdout.txt new file mode 100644 index 0000000..3bcaf46 --- /dev/null +++ b/Tests/RunCMake/execute_process/ExitValues-stdout.txt @@ -0,0 +1,14 @@ +^-- 1 - 1 RESULT_VARIABLE: 0 +-- 1 - 2 RESULT_VARIABLE: [^0].* +-- 2 - 1 RESULT_VARIABLE: 0 +-- 2 - 1 RESULTS_VARIABLE: 0 +-- 2 - 2 RESULT_VARIABLE: [^0].* +-- 2 - 2 RESULTS_VARIABLE: [^0].* +-- 3 - 1 RESULTS_VARIABLE: 0 +-- 3 - 2 RESULTS_VARIABLE: [^0].* +-- 4 - 1 RESULT_VARIABLE: 0 +-- 4 - 1 RESULTS_VARIABLE: [^0].*;0;[^0].*;0;[^0].*;0 +-- 4 - 1 RESULTS_VARIABLE_LENGTH: 6 +-- 5 - 1 RESULT_VARIABLE: [^0].* +-- 5 - 1 RESULTS_VARIABLE: 0;0;[^0].* +-- 5 - 1 RESULTS_VARIABLE_LENGTH: 3$ diff --git a/Tests/RunCMake/execute_process/ExitValues.cmake b/Tests/RunCMake/execute_process/ExitValues.cmake new file mode 100644 index 0000000..d80a57b --- /dev/null +++ b/Tests/RunCMake/execute_process/ExitValues.cmake @@ -0,0 +1,120 @@ +#1st TEST RESULT_VARIABLE ONLY +execute_process(COMMAND ${EXIT_CODE_EXE} "zero_exit" + RESULT_VARIABLE r0 + ) +message(STATUS " 1 - 1 RESULT_VARIABLE: ${r0}") +if(NOT r0 EQUAL 0) + message(FATAL_ERROR "zero exit code expected") +endif() +execute_process(COMMAND ${EXIT_CODE_EXE} "non_zero_exit" + RESULT_VARIABLE r01 + ERROR_QUIET + ) +message(STATUS " 1 - 2 RESULT_VARIABLE: ${r01}") +if(r01 EQUAL 0) + message(FATAL_ERROR "non-zero exit code expected") +endif() +#2nd TEST RESULT_VARIABLE and RESULTS_VARIABLE +execute_process(COMMAND ${EXIT_CODE_EXE} "zero_exit" + RESULT_VARIABLE r1 + RESULTS_VARIABLE r1s + ) +message(STATUS " 2 - 1 RESULT_VARIABLE: ${r1}") +message(STATUS " 2 - 1 RESULTS_VARIABLE: ${r1s}") +if(NOT r1 EQUAL 0 OR NOT r1s EQUAL 0) + message(FATAL_ERROR "zero exit code expected") +endif() +execute_process(COMMAND ${EXIT_CODE_EXE} "non_zero_exit" + RESULT_VARIABLE r11 + RESULTS_VARIABLE r11s + ERROR_QUIET + ) +message(STATUS " 2 - 2 RESULT_VARIABLE: ${r11}") +message(STATUS " 2 - 2 RESULTS_VARIABLE: ${r11s}") +if(r11 EQUAL 0 OR r11s EQUAL 0) + message(FATAL_ERROR "non-zero exit code expected") +endif() +#3rd TEST RESULTS_VARIABLE +execute_process(COMMAND ${EXIT_CODE_EXE} "zero_exit" + RESULTS_VARIABLE r2s + ) +message(STATUS " 3 - 1 RESULTS_VARIABLE: ${r2s}") +if(NOT r2s EQUAL 0) + message(FATAL_ERROR "zero exit code expected") +endif() +execute_process(COMMAND ${EXIT_CODE_EXE} "non_zero_exit" + RESULTS_VARIABLE r21s + ERROR_QUIET + ) +message(STATUS " 3 - 2 RESULTS_VARIABLE: ${r21s}") +if(r21s EQUAL 0) + message(FATAL_ERROR "non-zero exit code expected") +endif() +#4th TEST RESULT_VARIABLE and RESULTS_VARIABLE WITH MULTICOMMAND +execute_process(COMMAND ${EXIT_CODE_EXE} "non_zero_exit" + COMMAND ${EXIT_CODE_EXE} "zero_exit" + COMMAND ${EXIT_CODE_EXE} "non_zero_exit" + COMMAND ${EXIT_CODE_EXE} "zero_exit" + COMMAND ${EXIT_CODE_EXE} "non_zero_exit" + COMMAND ${EXIT_CODE_EXE} "zero_exit" + RESULT_VARIABLE r31 + RESULTS_VARIABLE r31s + OUTPUT_QUIET + ERROR_QUIET + ) +message(STATUS " 4 - 1 RESULT_VARIABLE: ${r31}") +message(STATUS " 4 - 1 RESULTS_VARIABLE: ${r31s}") +if(NOT r31 EQUAL 0) + message(FATAL_ERROR "zero exit code expected for last command") +endif() +list(LENGTH r31s r31sLen) +message(STATUS " 4 - 1 RESULTS_VARIABLE_LENGTH: ${r31sLen}") +if(NOT r31sLen EQUAL 6) + message(FATAL_ERROR "length of RESULTS_VARIABLE is not as expected") +else() + foreach(loop_var RANGE 5) + list(GET r31s ${loop_var} rsLocal) + math(EXPR isOdd "${loop_var} % 2") + if(isOdd) + if(NOT rsLocal EQUAL 0) + message(FATAL_ERROR "zero exit code expected") + endif() + else() + if(rsLocal EQUAL 0) + message(FATAL_ERROR "non-zero exit code expected") + endif() + endif() + endforeach() +endif() +#5th TEST RESULT_VARIABLE and RESULTS_VARIABLE WITH MULTICOMMAND +execute_process(COMMAND ${EXIT_CODE_EXE} "zero_exit" + COMMAND ${EXIT_CODE_EXE} "zero_exit" + COMMAND ${EXIT_CODE_EXE} "non_zero_exit" + RESULT_VARIABLE r41 + RESULTS_VARIABLE r41s + OUTPUT_QUIET + ERROR_QUIET + ) +message(STATUS " 5 - 1 RESULT_VARIABLE: ${r41}") +message(STATUS " 5 - 1 RESULTS_VARIABLE: ${r41s}") +if(r41 EQUAL 0) + message(FATAL_ERROR "non-zero exit code expected for last command") +endif() +list(LENGTH r41s r41sLen) +message(STATUS " 5 - 1 RESULTS_VARIABLE_LENGTH: ${r41sLen}") +if(NOT r31sLen EQUAL 6) + message(FATAL_ERROR "length of RESULTS_VARIABLE is not as expected") +else() + list(GET r41s 0 rsLocal) + if(NOT rsLocal EQUAL 0) + message(FATAL_ERROR "zero exit code expected") + endif() + list(GET r41s 1 rsLocal) + if(NOT rsLocal EQUAL 0) + message(FATAL_ERROR "zero exit code expected") + endif() + list(GET r41s 2 rsLocal) + if(rsLocal EQUAL 0) + message(FATAL_ERROR "non-zero exit code expected") + endif() +endif() diff --git a/Tests/RunCMake/execute_process/RunCMakeTest.cmake b/Tests/RunCMake/execute_process/RunCMakeTest.cmake index 62e18c6..83589bb 100644 --- a/Tests/RunCMake/execute_process/RunCMakeTest.cmake +++ b/Tests/RunCMake/execute_process/RunCMakeTest.cmake @@ -11,3 +11,7 @@ run_cmake(EncodingMissing) if(TEST_ENCODING_EXE) run_cmake_command(EncodingUTF8 ${CMAKE_COMMAND} -DTEST_ENCODING=UTF8 -DTEST_ENCODING_EXE=${TEST_ENCODING_EXE} -P ${RunCMake_SOURCE_DIR}/Encoding.cmake) endif() + +if(EXIT_CODE_EXE) + run_cmake_command(ExitValues ${CMAKE_COMMAND} -DEXIT_CODE_EXE=${EXIT_CODE_EXE} -P ${RunCMake_SOURCE_DIR}/ExitValues.cmake) +endif() diff --git a/Tests/RunCMake/exit_code.c b/Tests/RunCMake/exit_code.c new file mode 100644 index 0000000..3eba019 --- /dev/null +++ b/Tests/RunCMake/exit_code.c @@ -0,0 +1,30 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +// Usage: +// +// /path/to/program arg1 [arg2 [...]] +// +// Return EXIT_SUCCESS if 'zero_exit' +// string was found in <arg1>. +// Return EXIT_FAILURE if 'non_zero_exit' +// string was found in <arg1>. + +int main(int argc, const char* argv[]) +{ + const char* substring_failure = "non_zero_exit"; + const char* substring_success = "zero_exit"; + const char* str = argv[1]; + if (argc < 2) { + return EXIT_FAILURE; + } + if (strcmp(str, substring_success) == 0) { + return EXIT_SUCCESS; + } else if (strcmp(str, substring_failure) == 0) { + return EXIT_FAILURE; + } + fprintf(stderr, "Failed to find string '%s' in '%s'\n", substring_success, + str); + return EXIT_FAILURE; +} |