Skip to content

Commit

Permalink
fix: ignore test_a_suffix snapshots when running test_a (#607)
Browse files Browse the repository at this point in the history
  • Loading branch information
huonw committed Jul 7, 2022
1 parent a472b44 commit 988a8ab
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/syrupy/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,7 @@ def matches_snapshot_name(self, snapshot_name: str) -> bool:
return self.__parse(self.snapshot_name) == self.__parse(snapshot_name)

def matches_snapshot_location(self, snapshot_location: str) -> bool:
return self.filename in snapshot_location
loc = Path(snapshot_location)
# "test_file" should match_"test_file.ext" or "test_file/whatever.ext", but not
# "test_file_suffix.ext"
return self.filename == loc.stem or self.filename == loc.parent.name
18 changes: 13 additions & 5 deletions tests/integration/test_snapshot_similar_names_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,37 @@ def test_b(snapshot):
assert snapshot == 'b'
"""
),
"a_suffix": (
"""
def test_a_suffix(snapshot):
assert snapshot == 'a_suffix'
"""
),
}


@pytest.fixture
def run_testcases(testdir, testcases):
pyfile_content = "\n\n".join(testcases.values())
testdir.makepyfile(test_1=pyfile_content, test_2=pyfile_content)
testdir.makepyfile(
test_1=pyfile_content, test_2=pyfile_content, test_1_with_suffix=pyfile_content
)
result = testdir.runpytest("-v", "--snapshot-update")
result.stdout.re_match_lines((r"4 snapshots generated\."))
result.stdout.re_match_lines((r"9 snapshots generated\."))
return testdir, testcases


def test_run_all(run_testcases):
testdir, testcases = run_testcases
result = testdir.runpytest("-v")
result.stdout.re_match_lines("4 snapshots passed")
result.stdout.re_match_lines("9 snapshots passed")
assert result.ret == 0


def test_run_single_file(run_testcases):
testdir, testcases = run_testcases
result = testdir.runpytest("-v", "test_1.py")
result.stdout.re_match_lines("2 snapshots passed")
result.stdout.re_match_lines("3 snapshots passed")
assert result.ret == 0


Expand All @@ -54,7 +62,7 @@ def test_run_all_but_one(run_testcases):
result = testdir.runpytest(
"-v", "--snapshot-details", "test_1.py", "test_2.py::test_a"
)
result.stdout.re_match_lines("3 snapshots passed")
result.stdout.re_match_lines("4 snapshots passed")
assert result.ret == 0


Expand Down
18 changes: 13 additions & 5 deletions tests/integration/test_snapshot_similar_names_file_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,28 @@ def test_b(snapshot):
assert snapshot == b"b"
"""
),
"a_suffix": (
"""
def test_a_suffix(snapshot):
assert snapshot == b"a_suffix"
"""
),
}


@pytest.fixture
def run_testcases(testdir, testcases):
pyfile_content = "\n\n".join(testcases.values())
testdir.makepyfile(test_1=pyfile_content, test_2=pyfile_content)
testdir.makepyfile(
test_1=pyfile_content, test_2=pyfile_content, test_1_suffix=pyfile_content
)
result = testdir.runpytest(
"-v",
"--snapshot-update",
"--snapshot-default-extension",
"syrupy.extensions.single_file.SingleFileSnapshotExtension",
)
result.stdout.re_match_lines((r"4 snapshots generated\."))
result.stdout.re_match_lines((r"9 snapshots generated\."))
return testdir, testcases


Expand All @@ -40,7 +48,7 @@ def test_run_all(run_testcases):
"--snapshot-default-extension",
"syrupy.extensions.single_file.SingleFileSnapshotExtension",
)
result.stdout.re_match_lines("4 snapshots passed")
result.stdout.re_match_lines("9 snapshots passed")
assert result.ret == 0


Expand All @@ -52,7 +60,7 @@ def test_run_single_file(run_testcases):
"syrupy.extensions.single_file.SingleFileSnapshotExtension",
"test_1.py",
)
result.stdout.re_match_lines("2 snapshots passed")
result.stdout.re_match_lines("3 snapshots passed")
assert result.ret == 0


Expand All @@ -78,7 +86,7 @@ def test_run_all_but_one(run_testcases):
"test_1.py",
"test_2.py::test_a",
)
result.stdout.re_match_lines("3 snapshots passed")
result.stdout.re_match_lines("4 snapshots passed")
assert result.ret == 0


Expand Down
20 changes: 18 additions & 2 deletions tests/syrupy/test_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ def test_location_properties(
"/tests/module/test_file.py::TestClass::method_name",
"method_name",
("test_file.snap", "__snapshots__/test_file", "test_file/1.snap"),
("test.snap", "__others__/test/file.snap"),
(
"test.snap",
"__others__/test/file.snap",
"test_file_extra.snap",
"__snapshots__/test_file_extra",
"test_file_extra/1.snap",
"test_file/extra/1.snap",
"__snapshots__/test_file/extra/even/more/1.snap",
),
(
"TestClass.method_name",
"TestClass.method_name[1]",
Expand All @@ -79,7 +87,15 @@ def test_location_properties(
"/tests/module/test_file.py::TestClass::method_name[1]",
"method_name",
("test_file.snap", "__snapshots__/test_file", "test_file/1.snap"),
("test.snap", "__others__/test/file.snap"),
(
"test.snap",
"__others__/test/file.snap",
"test_file_extra.snap",
"__snapshots__/test_file_extra",
"test_file_extra/1.snap",
"test_file/extra/1.snap",
"__snapshots__/test_file/extra/even/more/1.snap",
),
(
"TestClass.method_name",
"TestClass.method_name[1]",
Expand Down

0 comments on commit 988a8ab

Please sign in to comment.