Skip to content

Commit

Permalink
Inject absolute paths into "expected output" files (#157)
Browse files Browse the repository at this point in the history
Add feature to the system test framework to generate absolute paths in
output files based on template files.

It works like this:
If a folder called `expected-output-template` exists, then
1. it takes each file from that folder
2. creates a copy in folder `expected-output`
3. and replaces the string `TESTS-SYSTEM` with an absolute path to the
`tests-system` folder.

This way it is possible to compare *.lobster files that contain absolute
paths against their expected content. `lobster-cpptest` needs this
feature.
  • Loading branch information
phiwuu authored Dec 16, 2024
1 parent 3bf1317 commit 5423c4e
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 5423c4e

Please sign in to comment.