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: Fix parsing identifiers from snapshot names #186

Merged
merged 1 commit into from
Apr 15, 2020
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
9 changes: 5 additions & 4 deletions src/syrupy/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ def snapshot_name(self) -> str:
return str(self.testname)

def __valid_id(self, name: str) -> str:
[valid_id, *rest] = name
while rest:
new_valid_id = f"{valid_id}{rest.pop(0)}"
valid_id = ""
for char in name:
new_valid_id = f"{valid_id}{char}"
if not new_valid_id.isidentifier():
break
valid_id = new_valid_id
return valid_id

def __parse(self, name: str) -> str:
return ".".join(self.__valid_id(n) for n in name.split("."))
valid_ids = (self.__valid_id(n) for n in name.split("."))
return ".".join(valid_id for valid_id in valid_ids if valid_id)

def matches_snapshot_name(self, snapshot_name: str) -> bool:
return self.__parse(self.snapshot_name) == self.__parse(snapshot_name)
Expand Down
20 changes: 18 additions & 2 deletions tests/test_integration_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_case_2(self, snapshot):
"""

testdir.makepyfile(test_content=test_content)
result = testdir.runpytest("-v", "--snapshot-update")
testdir.runpytest("-v", "--snapshot-update")

snapshot_path = Path(testdir.tmpdir, "__snapshots__")
assert snapshot_path.joinpath("test_content.ambr").exists()
Expand All @@ -137,7 +137,7 @@ def test_case_2(snapshot):
"""

testdir.makepyfile(test_content=test_content)
result = testdir.runpytest("-v", "--snapshot-update")
testdir.runpytest("-v", "--snapshot-update")

snapshot_path = Path(testdir.tmpdir, "__snapshots__")
assert snapshot_path.joinpath("test_content.ambr").exists()
Expand All @@ -149,6 +149,22 @@ def test_case_2(snapshot):
assert "snapshot unused" not in result_stdout


def test_multiple_snapshots(testdir):
test_content = """
import pytest

def test_case_1(snapshot):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
def test_case_1(snapshot):
def test_case(snapshot):

probably?

assert snapshot == 1
assert snapshot == 2
"""

testdir.makepyfile(test_content=test_content)
result = testdir.runpytest("-v", "--snapshot-update")

result_stdout = clean_output(result.stdout.str())
assert "Can not relate snapshot name" not in result_stdout


@pytest.fixture
def testcases():
return {
Expand Down