generated from tophat/new-project-kit
-
Notifications
You must be signed in to change notification settings - Fork 36
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: unused snapshot not filtered out when specific node #531
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -198,8 +198,14 @@ def unused(self) -> "SnapshotFossils": | |
unused_snapshots = { | ||
snapshot | ||
for snapshot in unused_snapshot_fossil | ||
if self._selected_items_match_name(snapshot.name) | ||
or self._provided_nodes_match_name(snapshot.name, provided_nodes) | ||
if self._selected_items_match_name( | ||
snapshot_location=snapshot_location, snapshot_name=snapshot.name | ||
) | ||
and self._provided_nodes_match_name( | ||
snapshot_location=snapshot_location, | ||
snapshot_name=snapshot.name, | ||
provided_nodes=provided_nodes, | ||
) | ||
} | ||
mark_for_removal = False | ||
|
||
|
@@ -343,12 +349,22 @@ def _get_matching_path_nodes(self, snapshot_location: str) -> List[List[str]]: | |
] | ||
|
||
def _provided_nodes_match_name( | ||
self, snapshot_name: str, provided_nodes: List[List[str]] | ||
self, | ||
snapshot_location: str, | ||
snapshot_name: str, | ||
provided_nodes: List[List[str]], | ||
) -> bool: | ||
""" | ||
Check that a snapshot name matches the node paths provided | ||
Check that a snapshot name matches the node paths provided. | ||
If no nodes are filtered, provided_nodes is empty, which means | ||
all nodes should be matched. | ||
noahnu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
return any(snapshot_name in ".".join(node_path) for node_path in provided_nodes) | ||
if not provided_nodes: | ||
return True | ||
for node_path in provided_nodes: | ||
if snapshot_name in ".".join(node_path): | ||
return True | ||
return False | ||
Comment on lines
+362
to
+367
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This syntax is easier to debug. |
||
|
||
def _provided_keywords_match_name(self, snapshot_name: str) -> bool: | ||
""" | ||
|
@@ -361,23 +377,30 @@ def _provided_keywords_match_name(self, snapshot_name: str) -> bool: | |
for expr in self._keyword_expressions | ||
) | ||
|
||
def _ran_items_match_name(self, snapshot_name: str) -> bool: | ||
def _ran_items_match_name(self, snapshot_location: str, snapshot_name: str) -> bool: | ||
""" | ||
Check that a snapshot name would match a test node using the Pytest location | ||
""" | ||
return any( | ||
PyTestLocation(item).matches_snapshot_name(snapshot_name) | ||
for item in self.ran_items | ||
) | ||
for item in self.ran_items: | ||
location = PyTestLocation(item) | ||
if location.matches_snapshot_location( | ||
snapshot_location | ||
) and location.matches_snapshot_name(snapshot_name): | ||
return True | ||
return False | ||
|
||
def _selected_items_match_name(self, snapshot_name: str) -> bool: | ||
def _selected_items_match_name( | ||
self, snapshot_location: str, snapshot_name: str | ||
) -> bool: | ||
""" | ||
Check that a snapshot name should be treated as selected by the current session | ||
This being true means that if the snapshot was not used then it will be deleted | ||
""" | ||
if self._keyword_expressions: | ||
return self._provided_keywords_match_name(snapshot_name) | ||
return self._ran_items_match_name(snapshot_name) | ||
return self._ran_items_match_name( | ||
snapshot_location=snapshot_location, snapshot_name=snapshot_name | ||
) | ||
|
||
def _ran_items_match_location(self, snapshot_location: str) -> bool: | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def testcases(): | ||
return { | ||
"a": ( | ||
""" | ||
def test_a(snapshot): | ||
assert snapshot == 'a' | ||
""" | ||
), | ||
"b": ( | ||
""" | ||
def test_b(snapshot): | ||
assert snapshot == 'b' | ||
""" | ||
), | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def run_testcases(testdir, testcases): | ||
pyfile_content = "\n\n".join(testcases.values()) | ||
testdir.makepyfile(test_1=pyfile_content, test_2=pyfile_content) | ||
result = testdir.runpytest("-v", "--snapshot-update") | ||
result.stdout.re_match_lines((r"4 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") | ||
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") | ||
assert result.ret == 0 | ||
|
||
|
||
def test_run_single_test_case_in_file(run_testcases): | ||
testdir, testcases = run_testcases | ||
result = testdir.runpytest("-v", "test_2.py::test_a") | ||
result.stdout.re_match_lines("1 snapshot passed") | ||
assert result.ret == 0 | ||
|
||
|
||
def test_run_all_but_one(run_testcases): | ||
testdir, testcases = run_testcases | ||
result = testdir.runpytest( | ||
"-v", "--snapshot-details", "test_1.py", "test_2.py::test_a" | ||
) | ||
result.stdout.re_match_lines("3 snapshots passed") | ||
assert result.ret == 0 | ||
|
||
|
||
def test_run_both_files_by_node(run_testcases): | ||
testdir, testcases = run_testcases | ||
result = testdir.runpytest( | ||
"-v", "--snapshot-details", "test_1.py::test_a", "test_2.py::test_a" | ||
) | ||
result.stdout.re_match_lines("2 snapshots passed") | ||
assert result.ret == 0 | ||
|
||
|
||
def test_run_both_files_by_node_2(run_testcases): | ||
testdir, testcases = run_testcases | ||
result = testdir.runpytest( | ||
"-v", "--snapshot-details", "test_1.py::test_b", "test_2.py::test_a" | ||
) | ||
result.stdout.re_match_lines("2 snapshots passed") | ||
assert result.ret == 0 |
110 changes: 110 additions & 0 deletions
110
tests/integration/test_snapshot_similar_names_file_extension.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def testcases(): | ||
return { | ||
"a": ( | ||
""" | ||
def test_a(snapshot): | ||
assert snapshot == b"a" | ||
""" | ||
), | ||
"b": ( | ||
""" | ||
def test_b(snapshot): | ||
assert snapshot == b"b" | ||
""" | ||
), | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def run_testcases(testdir, testcases): | ||
pyfile_content = "\n\n".join(testcases.values()) | ||
testdir.makepyfile(test_1=pyfile_content, test_2=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\.")) | ||
return testdir, testcases | ||
|
||
|
||
def test_run_all(run_testcases): | ||
testdir, testcases = run_testcases | ||
result = testdir.runpytest( | ||
"-v", | ||
"--snapshot-default-extension", | ||
"syrupy.extensions.single_file.SingleFileSnapshotExtension", | ||
) | ||
result.stdout.re_match_lines("4 snapshots passed") | ||
assert result.ret == 0 | ||
|
||
|
||
def test_run_single_file(run_testcases): | ||
testdir, testcases = run_testcases | ||
result = testdir.runpytest( | ||
"-v", | ||
"--snapshot-default-extension", | ||
"syrupy.extensions.single_file.SingleFileSnapshotExtension", | ||
"test_1.py", | ||
) | ||
result.stdout.re_match_lines("2 snapshots passed") | ||
assert result.ret == 0 | ||
|
||
|
||
def test_run_single_test_case_in_file(run_testcases): | ||
testdir, testcases = run_testcases | ||
result = testdir.runpytest( | ||
"-v", | ||
"--snapshot-default-extension", | ||
"syrupy.extensions.single_file.SingleFileSnapshotExtension", | ||
"test_2.py::test_a", | ||
) | ||
result.stdout.re_match_lines("1 snapshot passed") | ||
assert result.ret == 0 | ||
|
||
|
||
def test_run_all_but_one(run_testcases): | ||
testdir, testcases = run_testcases | ||
result = testdir.runpytest( | ||
"-v", | ||
"--snapshot-details", | ||
"--snapshot-default-extension", | ||
"syrupy.extensions.single_file.SingleFileSnapshotExtension", | ||
"test_1.py", | ||
"test_2.py::test_a", | ||
) | ||
result.stdout.re_match_lines("3 snapshots passed") | ||
assert result.ret == 0 | ||
|
||
|
||
def test_run_both_files_by_node(run_testcases): | ||
testdir, testcases = run_testcases | ||
result = testdir.runpytest( | ||
"-v", | ||
"--snapshot-details", | ||
"--snapshot-default-extension", | ||
"syrupy.extensions.single_file.SingleFileSnapshotExtension", | ||
"test_1.py::test_a", | ||
"test_2.py::test_a", | ||
) | ||
result.stdout.re_match_lines("2 snapshots passed") | ||
assert result.ret == 0 | ||
|
||
|
||
def test_run_both_files_by_node_2(run_testcases): | ||
testdir, testcases = run_testcases | ||
result = testdir.runpytest( | ||
"-v", | ||
"--snapshot-details", | ||
"--snapshot-default-extension", | ||
"syrupy.extensions.single_file.SingleFileSnapshotExtension", | ||
"test_1.py::test_b", | ||
"test_2.py::test_a", | ||
) | ||
result.stdout.re_match_lines("2 snapshots passed") | ||
assert result.ret == 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicated this test file since locations for images are handled a bit differently, better safe than sorry. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This bit might actually be dead code. It's never hit in tests. I don't think it's harmful at the moment, so will loop back to see if it can be removed, or if there's some edge case we're not testing.