From efe687e263647b1efa2673847372389ea90961eb Mon Sep 17 00:00:00 2001 From: Tolga Eren <794719+tolgaeren@users.noreply.github.com> Date: Tue, 21 Feb 2023 00:57:14 +0100 Subject: [PATCH] fix(serializer): handling of multi-part file extensions in SingleFileExtension (#710) Co-authored-by: tolga.eren Co-authored-by: noahnu --- src/syrupy/extensions/single_file.py | 5 ++- .../test_single_file_multiple_extensions.py | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/integration/test_single_file_multiple_extensions.py diff --git a/src/syrupy/extensions/single_file.py b/src/syrupy/extensions/single_file.py index af53ea4d..8e1a7c5d 100644 --- a/src/syrupy/extensions/single_file.py +++ b/src/syrupy/extensions/single_file.py @@ -80,8 +80,11 @@ def dirname(cls, *, test_location: "PyTestLocation") -> str: def _read_snapshot_collection( self, *, snapshot_location: str ) -> "SnapshotCollection": + file_ext_len = len(self._file_extension) + 1 if self._file_extension else 0 + filename_wo_ext = snapshot_location[:-file_ext_len] + snapshot_collection = SnapshotCollection(location=snapshot_location) - snapshot_collection.add(Snapshot(name=Path(snapshot_location).stem)) + snapshot_collection.add(Snapshot(name=Path(filename_wo_ext).stem)) return snapshot_collection def _read_snapshot_data_from_location( diff --git a/tests/integration/test_single_file_multiple_extensions.py b/tests/integration/test_single_file_multiple_extensions.py new file mode 100644 index 00000000..b93f2876 --- /dev/null +++ b/tests/integration/test_single_file_multiple_extensions.py @@ -0,0 +1,40 @@ +from pathlib import Path + + +def test_multiple_file_extensions(testdir): + file_extension = "ext2.ext1" + + testcase = f""" + import pytest + from syrupy.extensions.single_file import SingleFileSnapshotExtension + + class DotInFileExtension(SingleFileSnapshotExtension): + _file_extension = "{file_extension}" + + @pytest.fixture + def snapshot(snapshot): + return snapshot.use_extension(DotInFileExtension) + + def test_dot_in_filename(snapshot): + assert b"expected_data" == snapshot + """ + + test_file: Path = testdir.makepyfile(test_file=testcase) + + result = testdir.runpytest("-v", "--snapshot-update") + result.stdout.re_match_lines((r"1 snapshot generated\.")) + assert "snapshots unused" not in result.stdout.str() + assert result.ret == 0 + + snapshot_file = ( + Path(test_file).parent + / "__snapshots__" + / "test_file" + / f"test_dot_in_filename.{file_extension}" + ) + assert snapshot_file.exists() + + result = testdir.runpytest("-v") + result.stdout.re_match_lines((r"1 snapshot passed\.")) + assert "snapshots unused" not in result.stdout.str() + assert result.ret == 0