Skip to content

Commit 25d37ef

Browse files
committed
feat: include details about created/updated snapshots in detailed report (#942)
(cherry picked from commit 9736efc)
1 parent 4bc6d13 commit 25d37ef

File tree

3 files changed

+84
-12
lines changed

3 files changed

+84
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ These are the cli options exposed to `pytest` by the plugin.
134134
| Option | Description | Default |
135135
| ------------------------------ |--------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------|
136136
| `--snapshot-update` | Snapshots will be updated to match assertions and unused snapshots will be deleted. | `False` |
137-
| `--snapshot-details` | Includes details of unused snapshots (test name and snapshot location) in the final report. | `False` |
137+
| `--snapshot-details` | Includes details of unused, generated, and updated snapshots (test name and snapshot location) in the final report. | `False` |
138138
| `--snapshot-warn-unused` | Prints a warning on unused snapshots rather than fail the test suite. | `False` |
139139
| `--snapshot-default-extension` | Use to change the default snapshot extension class. | [AmberSnapshotExtension](https://github.com/syrupy-project/syrupy/blob/main/src/syrupy/extensions/amber/__init__.py) |
140140
| `--snapshot-no-colors` | Disable test results output highlighting. Equivalent to setting the environment variables `ANSI_COLORS_DISABLED` or `NO_COLOR` | Disabled by default if not in terminal. |

src/syrupy/report.py

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
DefaultDict,
1818
Dict,
1919
FrozenSet,
20+
Generator,
2021
Iterator,
2122
List,
2223
Set,
@@ -347,17 +348,9 @@ def lines(self) -> Iterator[str]:
347348
yield ""
348349
if self.update_snapshots or self.include_snapshot_details:
349350
base_message = "Deleted" if self.update_snapshots else "Unused"
350-
for snapshot_collection in self.unused:
351-
filepath = snapshot_collection.location
352-
snapshots = (snapshot.name for snapshot in snapshot_collection)
353-
354-
try:
355-
path_to_file = str(Path(filepath).relative_to(self.base_dir))
356-
except ValueError:
357-
# this is just used for display, so better to fallback to
358-
# something vaguely reasonable (the full path) than give up
359-
path_to_file = filepath
360-
351+
for snapshots, path_to_file in self.__iterate_snapshot_collection(
352+
self.unused
353+
):
361354
unused_snapshots = ", ".join(map(bold, sorted(snapshots)))
362355
yield (
363356
warning_style(gettext(base_message))
@@ -372,6 +365,44 @@ def lines(self) -> Iterator[str]:
372365
else:
373366
yield error_style(message)
374367

368+
if self.num_created and self.update_snapshots and self.include_snapshot_details:
369+
yield ""
370+
for snapshots, path_to_file in self.__iterate_snapshot_collection(
371+
self.created
372+
):
373+
created_snapshots = ", ".join(map(bold, sorted(snapshots)))
374+
yield (
375+
warning_style(gettext("Generated"))
376+
+ f" {created_snapshots} ({path_to_file})"
377+
)
378+
379+
if self.num_updated and self.update_snapshots and self.include_snapshot_details:
380+
yield ""
381+
for snapshots, path_to_file in self.__iterate_snapshot_collection(
382+
self.updated
383+
):
384+
updated_snapshots = ", ".join(map(bold, sorted(snapshots)))
385+
yield (
386+
warning_style(gettext("Updated"))
387+
+ f" {updated_snapshots} ({path_to_file})"
388+
)
389+
390+
def __iterate_snapshot_collection(
391+
self, collection: "SnapshotCollections"
392+
) -> Generator[tuple[Generator[str, None, None], str], Any, None]:
393+
for snapshot_collection in collection:
394+
filepath = snapshot_collection.location
395+
snapshots = (snapshot.name for snapshot in snapshot_collection)
396+
397+
try:
398+
path_to_file = str(Path(filepath).relative_to(self.base_dir))
399+
except ValueError:
400+
# this is just used for display, so better to fallback to
401+
# something vaguely reasonable (the full path) than give up
402+
path_to_file = filepath
403+
404+
yield (snapshots, path_to_file)
405+
375406
def _diff_snapshot_collections(
376407
self,
377408
snapshot_collections1: "SnapshotCollections",

tests/integration/test_snapshot_option_include_details.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,44 @@ def test_unused_snapshots_details_no_details_on_deletion(
146146
)
147147
)
148148
assert result.ret == 0
149+
150+
151+
def test_created_and_updates_details(testdir, plugin_args_fails_xdist):
152+
# Generate initial snapshots.
153+
testdir.makepyfile(
154+
test_generated="""
155+
def test_generated_1(snapshot):
156+
assert snapshot == "1"
157+
158+
def test_generated_2(snapshot):
159+
assert snapshot == "2"
160+
"""
161+
)
162+
result = testdir.runpytest("-v", "--snapshot-update")
163+
result.stdout.re_match_lines((r"2 snapshots generated\.",))
164+
165+
# Update one of the snapshots and generate a new one
166+
testdir.makepyfile(
167+
test_generated="""
168+
def test_generated_1(snapshot):
169+
assert snapshot == "1"
170+
171+
def test_generated_2(snapshot):
172+
assert snapshot == "2-update"
173+
174+
def test_generated_3(snapshot):
175+
assert snapshot == "3"
176+
"""
177+
)
178+
179+
result = testdir.runpytest(
180+
"-v", "--snapshot-details", "--snapshot-update", *plugin_args_fails_xdist
181+
)
182+
result.stdout.re_match_lines(
183+
(
184+
r"1 snapshot passed\. 1 snapshot generated\. 1 snapshot updated\.",
185+
r"Generated test_generated_3 \(__snapshots__[\\/]test_generated.ambr\)",
186+
r"Updated test_generated_2 \(__snapshots__[\\/]test_generated.ambr\)",
187+
)
188+
)
189+
assert result.ret == 0

0 commit comments

Comments
 (0)