1#
2# Copyright by The HDF Group.
3# All rights reserved.
4#
5# This file is part of HDF5.  The full HDF5 copyright notice, including
6# terms governing use, modification, and redistribution, is contained in
7# the COPYING file, which can be found at the root of the source code
8# distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.
9# If you do not have access to either file, you may request a copy from
10# help@hdfgroup.org.
11#
12# grepTest.cmake executes a command and captures the output in a file. File is then compared
13# against a reference file. Exit status of command can also be compared.
14
15# arguments checking
16if (NOT TEST_PROGRAM)
17  message (FATAL_ERROR "Require TEST_PROGRAM to be defined")
18endif ()
19#if (NOT TEST_ARGS)
20#  message (STATUS "Require TEST_ARGS to be defined")
21#endif ()
22if (NOT TEST_FOLDER)
23  message ( FATAL_ERROR "Require TEST_FOLDER to be defined")
24endif ()
25if (NOT TEST_OUTPUT)
26  message (FATAL_ERROR "Require TEST_OUTPUT to be defined")
27endif ()
28#if (NOT TEST_EXPECT)
29#  message (STATUS "Require TEST_EXPECT to be defined")
30#endif ()
31if (NOT TEST_FILTER)
32  message (STATUS "Require TEST_FILTER to be defined")
33endif ()
34if (NOT TEST_REFERENCE)
35  message (FATAL_ERROR "Require TEST_REFERENCE to be defined")
36endif ()
37
38message (STATUS "COMMAND: ${TEST_PROGRAM} ${TEST_ARGS}")
39
40# run the test program, capture the stdout/stderr and the result var
41execute_process (
42    COMMAND ${TEST_PROGRAM} ${TEST_ARGS}
43    WORKING_DIRECTORY ${TEST_FOLDER}
44    RESULT_VARIABLE TEST_RESULT
45    OUTPUT_FILE ${TEST_OUTPUT}
46    ERROR_FILE ${TEST_OUTPUT}.err
47    OUTPUT_VARIABLE TEST_OUT
48    ERROR_VARIABLE TEST_ERROR
49)
50
51message (STATUS "COMMAND Result: ${TEST_RESULT}")
52message (STATUS "COMMAND Error: ${TEST_ERROR}")
53
54# now grep the output with the reference
55file (READ ${TEST_FOLDER}/${TEST_OUTPUT} TEST_STREAM)
56
57# TEST_REFERENCE should always be matched
58string (REGEX MATCH "${TEST_REFERENCE}" TEST_MATCH ${TEST_STREAM})
59string (COMPARE EQUAL "${TEST_REFERENCE}" "${TEST_MATCH}" TEST_RESULT)
60if ("${TEST_RESULT}" STREQUAL "0")
61  message (FATAL_ERROR "Failed: The output of ${TEST_PROGRAM} did not contain ${TEST_REFERENCE}")
62endif ()
63
64string (REGEX MATCH "${TEST_FILTER}" TEST_MATCH ${TEST_STREAM})
65if ("${TEST_EXPECT}" STREQUAL "1")
66  # TEST_EXPECT (1) interperts TEST_FILTER as NOT to match
67  string (LENGTH "${TEST_MATCH}" TEST_RESULT)
68  if (NOT "${TEST_RESULT}" STREQUAL "0")
69    message (FATAL_ERROR "Failed: The output of ${TEST_PROGRAM} did contain ${TEST_FILTER}")
70  endif ()
71endif ()
72
73# everything went fine...
74message ("Passed: The output of ${TEST_PROGRAM} matched")
75
76