From 8d4e5c4984f17b63c86db2d564d030060a84de11 Mon Sep 17 00:00:00 2001 From: Philipp Wullstein-Kammler Date: Fri, 13 Dec 2024 12:54:01 +0100 Subject: [PATCH] Inject absolute paths into "expected output" files Add feature so that the system tests can inject absolute paths into *.lobster files, which represent the expected outcome of a test. It works like this: 1. If a *.lobster file in the `expected-output` folder contains the string `TEST_CASE_PATH`, then this string is replaced with the absolute path of the test case folder. 2. Afterwards the content of the actual output file is compared against this modified file. This way it is possible to compare *.lobster files that contain absolute paths against their expected content. `lobster-cpptest` needs this feature. simplification --- tests-system/run_tool_tests.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/tests-system/run_tool_tests.py b/tests-system/run_tool_tests.py index 8a133f7..064125d 100644 --- a/tests-system/run_tool_tests.py +++ b/tests-system/run_tool_tests.py @@ -1,4 +1,5 @@ import sys +from posixpath import normpath from os import scandir, DirEntry, remove from os.path import basename, dirname, join from pathlib import Path @@ -31,6 +32,11 @@ def __init__(self, test_case_path: str): self._expected_lobster_output_file_name = \ self._get_expected_lobster_output_file_name() + @property + def test_case_path(self) -> str: + """Returns the path to the directory which contains the test case""" + return self._test_case_path + @property def expected_lobster_output_file_name(self) -> str: return self._expected_lobster_output_file_name @@ -185,18 +191,27 @@ def _compare_results(setup: TestSetup, completed_process: CompletedProcess): with open(expected, "r", encoding="UTF-8") as expected_lobster_file: try: with open(actual, "r", encoding="UTF-8") as actual_lobster_file: - # Note: we replace Windows-like slashes \\ with one / in order to be - # able to compare the actual output on all OS against the expected - # output on Linux - assert actual_lobster_file.read().replace("\\\\", "/") \ - == expected_lobster_file.read(), \ + # Before comparing the actual text with the expected text, we do the + # following replacements: + # a) Replace Windows-like slashes \\ with / in order to be able to + # compare the actual output on all OS against the expected output on + # Linux + # b) Replace the fixed string TEST_CASE_PATH with the absolute path to + # the current test case directory. This is necessary for tools like + # lobster-cpptest which write absolute paths into their *.lobster + # output files. + modified_actual = actual_lobster_file.read().replace("\\\\", "/") + modified_expected = expected_lobster_file.read().replace( + "TEST_CASE_PATH", setup.test_case_path + ) + assert modified_actual == modified_expected, \ "Actual *.lobster file differs from expectation!" except FileNotFoundError as ex: assert True, f"File {ex.filename} was not generated by the tool under test!" def _get_directories( - start_directory: str, + start_directory: Path, startswith: Optional[str] = None, ) -> Iterator[DirEntry]: """Returns DirEntry instances for each subdirectory found in the given start @@ -225,7 +240,7 @@ def _delete_generated_files(setup: TestSetup): remove(generated) -def _run_tests(directory: str, tool: str) -> int: +def _run_tests(directory: Path, tool: str) -> int: """Runs all system tests in the given folder for the specified tool. :param directory: the path to the directory containing all test cases @@ -264,7 +279,7 @@ def _get_tool(test_dir: str) -> str: to the tool name :param test_dir: The path containing the requirements-based tests """ - return join("../../../../../", basename(test_dir)) + return normpath(Path(join("../", basename(test_dir))).absolute()) if __name__ == "__main__":