diff --git a/src/syrupy/location.py b/src/syrupy/location.py index 31207e75..ca4aa264 100644 --- a/src/syrupy/location.py +++ b/src/syrupy/location.py @@ -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 diff --git a/tests/integration/test_snapshot_similar_names_default.py b/tests/integration/test_snapshot_similar_names_default.py index abc06a6b..5ba1e18b 100644 --- a/tests/integration/test_snapshot_similar_names_default.py +++ b/tests/integration/test_snapshot_similar_names_default.py @@ -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 @@ -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 diff --git a/tests/integration/test_snapshot_similar_names_file_extension.py b/tests/integration/test_snapshot_similar_names_file_extension.py index 19d11310..458d4075 100644 --- a/tests/integration/test_snapshot_similar_names_file_extension.py +++ b/tests/integration/test_snapshot_similar_names_file_extension.py @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/tests/syrupy/test_location.py b/tests/syrupy/test_location.py index 71625597..6da7f9ab 100644 --- a/tests/syrupy/test_location.py +++ b/tests/syrupy/test_location.py @@ -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]", @@ -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]",