Re-sync with internal repository
[hiphop-php.git] / third-party / watchman / src / build / fbcode_builder / CMake / FBPythonTestAddTests.cmake
blobd73c055d8245a35c8f143eaa1ba375518144c0bb
1 # Copyright (c) Facebook, Inc. and its affiliates.
3 # Add a command to be emitted to the CTest file
4 set(ctest_script)
5 function(add_command CMD)
6   set(escaped_args "")
7   foreach(arg ${ARGN})
8     # Escape all arguments using "Bracket Argument" syntax
9     # We could skip this for argument that don't contain any special
10     # characters if we wanted to make the output slightly more human-friendly.
11     set(escaped_args "${escaped_args} [==[${arg}]==]")
12   endforeach()
13   set(ctest_script "${ctest_script}${CMD}(${escaped_args})\n" PARENT_SCOPE)
14 endfunction()
16 if(NOT EXISTS "${TEST_EXECUTABLE}")
17   message(FATAL_ERROR "Test executable does not exist: ${TEST_EXECUTABLE}")
18 endif()
19 execute_process(
20   COMMAND ${CMAKE_COMMAND} -E env ${TEST_ENV} "${TEST_INTERPRETER}" "${TEST_EXECUTABLE}" --list-tests
21   WORKING_DIRECTORY "${TEST_WORKING_DIR}"
22   OUTPUT_VARIABLE output
23   RESULT_VARIABLE result
25 if(NOT "${result}" EQUAL 0)
26   string(REPLACE "\n" "\n  " output "${output}")
27   message(
28     FATAL_ERROR
29     "Error running test executable: ${TEST_EXECUTABLE}\n"
30     "Output:\n"
31     "  ${output}\n"
32   )
33 endif()
35 # Parse output
36 string(REPLACE "\n" ";" tests_list "${output}")
37 foreach(test_name ${tests_list})
38   add_command(
39     add_test
40     "${TEST_PREFIX}${test_name}"
41     ${CMAKE_COMMAND} -E env ${TEST_ENV}
42     "${TEST_INTERPRETER}" "${TEST_EXECUTABLE}" "${test_name}"
43   )
44   add_command(
45     set_tests_properties
46     "${TEST_PREFIX}${test_name}"
47     PROPERTIES
48     WORKING_DIRECTORY "${TEST_WORKING_DIR}"
49     ${TEST_PROPERTIES}
50   )
51 endforeach()
53 # Set a list of discovered tests in the parent scope, in case users
54 # want access to this list as a CMake variable
55 if(TEST_LIST)
56   add_command(set ${TEST_LIST} ${tests_list})
57 endif()
59 file(WRITE "${CTEST_FILE}" "${ctest_script}")