From c50a9637e36e393b0f066dafe374a2a8bdaf8317 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Tue, 5 Jul 2022 14:30:59 +1000 Subject: [PATCH 1/3] fix: ignore test_a_suffix snapshots when running test_a --- src/syrupy/location.py | 7 ++++++- .../test_snapshot_similar_names_default.py | 8 +++++--- ...test_snapshot_similar_names_file_extension.py | 8 +++++--- tests/syrupy/test_location.py | 16 ++++++++++++++-- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/syrupy/location.py b/src/syrupy/location.py index 31207e75..beb934d4 100644 --- a/src/syrupy/location.py +++ b/src/syrupy/location.py @@ -114,4 +114,9 @@ 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 any( + self.filename == parent.name for parent in loc.parents + ) diff --git a/tests/integration/test_snapshot_similar_names_default.py b/tests/integration/test_snapshot_similar_names_default.py index abc06a6b..f880542b 100644 --- a/tests/integration/test_snapshot_similar_names_default.py +++ b/tests/integration/test_snapshot_similar_names_default.py @@ -22,16 +22,18 @@ def test_b(snapshot): @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"6 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("6 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..faee7de3 100644 --- a/tests/integration/test_snapshot_similar_names_file_extension.py +++ b/tests/integration/test_snapshot_similar_names_file_extension.py @@ -22,14 +22,16 @@ def test_b(snapshot): @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"6 snapshots generated\.")) return testdir, testcases @@ -40,7 +42,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("6 snapshots passed") assert result.ret == 0 diff --git a/tests/syrupy/test_location.py b/tests/syrupy/test_location.py index 71625597..d522d50e 100644 --- a/tests/syrupy/test_location.py +++ b/tests/syrupy/test_location.py @@ -67,7 +67,13 @@ 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", + ), ( "TestClass.method_name", "TestClass.method_name[1]", @@ -79,7 +85,13 @@ 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", + ), ( "TestClass.method_name", "TestClass.method_name[1]", From db037a7ad9de91df1a259cf7760edbaf38f6a314 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Tue, 5 Jul 2022 14:36:08 +1000 Subject: [PATCH 2/3] Also test tests with suffices --- .../test_snapshot_similar_names_default.py | 14 ++++++++++---- .../test_snapshot_similar_names_file_extension.py | 14 ++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/tests/integration/test_snapshot_similar_names_default.py b/tests/integration/test_snapshot_similar_names_default.py index f880542b..5ba1e18b 100644 --- a/tests/integration/test_snapshot_similar_names_default.py +++ b/tests/integration/test_snapshot_similar_names_default.py @@ -16,6 +16,12 @@ def test_b(snapshot): assert snapshot == 'b' """ ), + "a_suffix": ( + """ + def test_a_suffix(snapshot): + assert snapshot == 'a_suffix' + """ + ), } @@ -26,21 +32,21 @@ def run_testcases(testdir, testcases): 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"6 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("6 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 @@ -56,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 faee7de3..458d4075 100644 --- a/tests/integration/test_snapshot_similar_names_file_extension.py +++ b/tests/integration/test_snapshot_similar_names_file_extension.py @@ -16,6 +16,12 @@ def test_b(snapshot): assert snapshot == b"b" """ ), + "a_suffix": ( + """ + def test_a_suffix(snapshot): + assert snapshot == b"a_suffix" + """ + ), } @@ -31,7 +37,7 @@ def run_testcases(testdir, testcases): "--snapshot-default-extension", "syrupy.extensions.single_file.SingleFileSnapshotExtension", ) - result.stdout.re_match_lines((r"6 snapshots generated\.")) + result.stdout.re_match_lines((r"9 snapshots generated\.")) return testdir, testcases @@ -42,7 +48,7 @@ def test_run_all(run_testcases): "--snapshot-default-extension", "syrupy.extensions.single_file.SingleFileSnapshotExtension", ) - result.stdout.re_match_lines("6 snapshots passed") + result.stdout.re_match_lines("9 snapshots passed") assert result.ret == 0 @@ -54,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 @@ -80,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 From 161c60eb3cbf2d5f4b3c3031348a1e279a7fc2a6 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Fri, 8 Jul 2022 08:21:22 +1000 Subject: [PATCH 3/3] Only check immediate parent directory --- src/syrupy/location.py | 4 +--- tests/syrupy/test_location.py | 4 ++++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/syrupy/location.py b/src/syrupy/location.py index beb934d4..ca4aa264 100644 --- a/src/syrupy/location.py +++ b/src/syrupy/location.py @@ -117,6 +117,4 @@ def matches_snapshot_location(self, snapshot_location: str) -> bool: 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 any( - self.filename == parent.name for parent in loc.parents - ) + return self.filename == loc.stem or self.filename == loc.parent.name diff --git a/tests/syrupy/test_location.py b/tests/syrupy/test_location.py index d522d50e..6da7f9ab 100644 --- a/tests/syrupy/test_location.py +++ b/tests/syrupy/test_location.py @@ -73,6 +73,8 @@ def test_location_properties( "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", @@ -91,6 +93,8 @@ def test_location_properties( "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",