diff --git a/cmake/match.cmake b/cmake/match.cmake index d796adbc74..8891c617e9 100644 --- a/cmake/match.cmake +++ b/cmake/match.cmake @@ -61,7 +61,7 @@ elseif(${MODE} STREQUAL "file") endif() if(TEST_RESULT) - message(FATAL_ERROR "Failed: Test ${TEST_FILE} ${TEST_ARGS} returned non-zero (${TEST_RESULT}).") + message(FATAL_ERROR "Failed: Test command '${TEST_FILE} ${TEST_ARGS}' returned non-zero (${TEST_RESULT}).") endif() # Compare the output file contents with a match file contents @@ -71,13 +71,7 @@ execute_process( ) if(TEST_RESULT) - file(READ ${OUT_FILE} OUTPUT) - file(READ ${MATCH_FILE} MATCH_STRING) - message(FATAL_ERROR "Failed: The output of ${OUT_FILE}:\n ${OUTPUT}\n does not match ${MATCH_FILE}:\n ${MATCH_STRING} (${TEST_RESULT})") -elseif() - message("Passed: The output ${OUT_FILE} matches ${MATCH_FILE}") -endif() - -if(EXISTS ${OUT_FILE}) - file(REMOVE ${OUT_FILE}) -endif() + message(FATAL_ERROR "Failed (${TEST_RESULT}): The output of test command '${TEST_FILE} ${TEST_ARGS}' (stored in ${CMAKE_CURRENT_BINARY_DIR}/${OUT_FILE}) does not match '${MATCH_FILE}'") +else() + message("Passed: The output of test command '${TEST_FILE} ${TEST_ARGS}' (stored in ${CMAKE_CURRENT_BINARY_DIR}/${OUT_FILE}) matches '${MATCH_FILE}'") +endif() \ No newline at end of file diff --git a/cmake/match.py b/cmake/match.py index 74ec9b54b2..8445f1c558 100755 --- a/cmake/match.py +++ b/cmake/match.py @@ -11,6 +11,16 @@ import sys import re + +## @brief print the whole content of input and match files +def print_content(input_lines, match_lines): + print("--- Input Lines " + "-" * 64) + print("".join(input_lines).strip()) + print("--- Match Lines " + "-" * 64) + print("".join(match_lines).strip()) + print("-" * 80) + + if len(sys.argv) != 3: print("Usage: python match.py ") sys.exit(1) @@ -22,10 +32,20 @@ input_lines = input.readlines() match_lines = match.readlines() -if len(match_lines) != len(input_lines): - sys.exit(f"Line count doesn't match (is: {len(input_lines)}, expected: {len(match_lines)})") +if len(match_lines) < len(input_lines): + print(f"Match length < input length (input: {len(input_lines)}, match: {len(match_lines)})") + print_content(input_lines, match_lines) + sys.exit(1) +input_idx = 0 +opt = "{{OPT}}" for i, match_line in enumerate(match_lines): + if match_line.startswith(opt): + optional_line = True + match_line = match_line[len(opt):] + else: + optional_line = False + # split into parts at {{ }} match_parts = re.split(r'\{{(.*?)\}}', match_line.strip()) pattern = "" @@ -35,9 +55,27 @@ else: pattern += part - input_line = input_lines[i].strip() + # empty input file or end of input file, from now on match file must be optional + if not input_lines: + if optional_line is True: + continue + else: + print("End of input file or empty file.") + print("expected: " + match_line.strip()) + sys.exit(1) + + input_line = input_lines[input_idx].strip() if not re.fullmatch(pattern, input_line): - print(f"Line {i+1} does not match".format(i+1)) - print("is: " + input_line) - print("expected: " + match_line.strip()) - sys.exit(1) + if optional_line is True: + continue + else: + print("Line " + str(i+1) + " does not match") + print("is: " + input_line) + print("expected: " + match_line.strip()) + print_content(input_lines, match_lines) + sys.exit(1) + else: + if (input_idx == len(input_lines) - 1): + input_lines = [] + else: + input_idx += 1 \ No newline at end of file