Skip to content

Commit

Permalink
Port match scripts from adapters branch
Browse files Browse the repository at this point in the history
  • Loading branch information
kswiecicki committed Nov 3, 2023
1 parent 08fee15 commit 4a84470
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
16 changes: 5 additions & 11 deletions cmake/match.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
52 changes: 45 additions & 7 deletions cmake/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <input_file> <match_file>")
sys.exit(1)
Expand All @@ -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 = ""
Expand All @@ -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

0 comments on commit 4a84470

Please sign in to comment.