Skip to content

Commit

Permalink
fix(serializer): handling of multi-part file extensions in SingleFile…
Browse files Browse the repository at this point in the history
…Extension (#710)

Co-authored-by: tolga.eren <tolga.eren@adevinta.com>
Co-authored-by: noahnu <noahnu@gmail.com>
  • Loading branch information
3 people authored Feb 20, 2023
1 parent b831ee2 commit efe687e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/syrupy/extensions/single_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
40 changes: 40 additions & 0 deletions tests/integration/test_single_file_multiple_extensions.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit efe687e

Please sign in to comment.