Repository: tophat/syrupy

Issue:
Syrupy is recognizing snapshot as not being used (4.0.1)
**Describe the bug**

This is probably a regression in the latest release. I didn't observe the behavior in 4.0.0:

1. On a new test with no snapshot (using the `json` extension), I get the expected behaviour:
> 1 snapshot failed
> Snapshot 'TestFoo.test_foo' does not exist!
2. With the first `pytest --snapshot-update` (this is where the first signs of the bug is):
> 1 snapshot generated. 1 unused snapshot deleted.
Deleted TestFoo (tests/foo/__snapshots__/test_foo/TestFoo.test_foo.json)

Why does it output `Deleted ....`, that isn't the expected behaviour (the deletion doesn't happen though)
3. Another follow up when running `pytest`:
> 1 snapshot passed. 1 snapshot unused.
Re-run pytest with --snapshot-update to delete unused snapshots.

Again, that isn't correct. It gets worse though:
4. `pytest --snapshot-update`:
This time the deletion *will* happen hence the next run of `pytest` will fail since it won't find a snapshot.

# Env: 

Syrupy 4.0.1
Python 3.10


Going back to 4.0.0 solved the issue.


Comments:

Patch:
diff --git a/src/syrupy/extensions/single_file.py b/src/syrupy/extensions/single_file.py
index 8e1a7c5..19b9838 100644
--- a/src/syrupy/extensions/single_file.py
+++ b/src/syrupy/extensions/single_file.py
@@ -82,9 +82,10 @@ class SingleFileSnapshotExtension(AbstractSyrupyExtension):
     ) -> "SnapshotCollection":
         file_ext_len = len(self._file_extension) + 1 if self._file_extension else 0
         filename_wo_ext = snapshot_location[:-file_ext_len]
+        basename = Path(filename_wo_ext).parts[-1]
 
         snapshot_collection = SnapshotCollection(location=snapshot_location)
-        snapshot_collection.add(Snapshot(name=Path(filename_wo_ext).stem))
+        snapshot_collection.add(Snapshot(name=basename))
         return snapshot_collection
 
     def _read_snapshot_data_from_location(


Test patch:
diff --git a/tests/integration/test_single_file_multiple_extensions.py b/tests/integration/test_single_file_multiple_extensions.py
index b93f287..bcee53d 100644
--- a/tests/integration/test_single_file_multiple_extensions.py
+++ b/tests/integration/test_single_file_multiple_extensions.py
@@ -38,3 +38,39 @@ def test_multiple_file_extensions(testdir):
     result.stdout.re_match_lines((r"1 snapshot passed\."))
     assert "snapshots unused" not in result.stdout.str()
     assert result.ret == 0
+
+
+def test_class_style(testdir):
+    """
+    Regression test for https://github.com/tophat/syrupy/issues/717
+    """
+
+    testcase = """
+    import pytest
+    from syrupy.extensions.json import JSONSnapshotExtension
+
+    @pytest.fixture
+    def snapshot(snapshot):
+        return snapshot.use_extension(JSONSnapshotExtension)
+
+    class TestFoo:
+        def test_foo(self, snapshot):
+            assert { 'key': 'value' } == 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 "deleted" not in result.stdout.str()
+    assert result.ret == 0
+
+    snapshot_file = (
+        Path(test_file).parent / "__snapshots__" / "test_file" / "TestFoo.test_foo.json"
+    )
+    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