Skip to content

Commit 2104e73

Browse files
test(approval-test): avoid forbidden chars for names
1 parent b3e8232 commit 2104e73

File tree

21 files changed

+13
-10
lines changed

21 files changed

+13
-10
lines changed

tests/integrationtests/utils.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,30 @@ def __init__(self, extension=None):
2121
"""An approval tests Namer for naming approved and received text files.
2222
2323
These files will get stored under:
24-
`tests/approval/{module}/test_file.py::TestClass::test_func[a]`
24+
`tests/approval/{module}/test_file.py--TestClass--test_func[a]`
2525
2626
This class utilizes the `PYTEST_CURRENT_TEST` environment variable, which
2727
consist of the nodeid and the current stage:
2828
`relative/path/to/test_file.py::TestClass::test_func[a] (call)`
29+
30+
During a pytest test session stages can be setup, teardown or call.
31+
Approval tests should only be used during the call stage and therefore
32+
the ` (call)` postfix is removed.
33+
34+
To avoid forbidden characters in system paths `::` is replaced by `-`.
2935
"""
30-
self.nodeid = os.environ["PYTEST_CURRENT_TEST"]
36+
self.nodeid: Path = Path(os.environ["PYTEST_CURRENT_TEST"])
3137
NamerBase.__init__(self, extension)
3238

33-
def get_file_name(self) -> str:
39+
def get_file_name(self) -> Path:
3440
"""File name is pytest nodeid w/out directory name and pytest stage."""
35-
# During a pytest test session stages can be setup, teardown or call.
36-
# Approval tests should only be used during the call stage and therefore
37-
# we replace the ` (call)` postfix.
38-
return self.nodeid.split("/")[-1].replace(" (call)", "")
41+
return Path(str(self.nodeid.name).replace(" (call)", "").replace("::", "-"))
3942

4043
def get_directory(self) -> Path:
4144
"""Directory is `tests/approval/{module}`."""
42-
parts = self.nodeid.split("/")[:-1]
43-
directory = "/".join(parts)
44-
return Path(APPROVED_DIR) / directory.replace("tests/integrationtests/", "")
45+
parts = self.nodeid.parent.parts
46+
raw = Path(*[p for p in parts if p not in ["tests", "integrationtests"]])
47+
return Path(APPROVED_DIR) / raw
4548

4649
def get_config(self) -> dict:
4750
return {}

0 commit comments

Comments
 (0)