Skip to content

Commit

Permalink
Inject absolute paths into "expected output" files
Browse files Browse the repository at this point in the history
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
  • Loading branch information
phiwuu committed Dec 13, 2024
1 parent 3bf1317 commit 8d4e5c4
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions tests-system/run_tool_tests.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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__":
Expand Down

0 comments on commit 8d4e5c4

Please sign in to comment.