Skip to content

Commit

Permalink
fix: parsing identifiers from snapshot names (#186) (#187)
Browse files Browse the repository at this point in the history
Co-authored-by: Jimmy Jia <tesrin@gmail.com>
  • Loading branch information
Noah and taion committed Apr 15, 2020
1 parent 235bbcf commit 45a2931
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
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):
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

0 comments on commit 45a2931

Please sign in to comment.