Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorrect marking of TestClass.test_method as unused, close #717 #761

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/syrupy/extensions/single_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ def _read_snapshot_collection(
) -> "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))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're already stripping the suffix on L84. We should not be doing this twice, otherwise our class method naming convention TestClass.test_method.json will turn into TestClass.

snapshot_collection.add(Snapshot(name=basename))
return snapshot_collection

def _read_snapshot_data_from_location(
Expand Down
36 changes: 36 additions & 0 deletions tests/integration/test_single_file_multiple_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,39 @@ def test_dot_in_filename(snapshot):
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