Skip to content
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

Ensure that untouched snapshots are not deleted in targeting mode #123

Merged
merged 23 commits into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ From v1.0.0 onwards, this project adheres to [Semantic Versioning](https://semve

- Up to date with releases.

## [v0.3.2](https://github.com/tophat/syrupy/compare/v.0.3.1...v0.3.2)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noahnu Is this the correct way to go about writing out the changelog manually?


- Fix bug there untargeted snapshots would be deleted when using pytest in targeted mode (#123)

## [v0.3.1](https://github.com/tophat/syrupy/compare/v0.3.0...v0.3.1)

- Fix bug where newline control characters were being translated based on platform (#113)
Expand Down
5 changes: 5 additions & 0 deletions src/syrupy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import glob
from typing import (
Any,
List,
Expand Down Expand Up @@ -63,6 +64,10 @@ def pytest_sessionstart(session: Any) -> None:
warn_unused_snapshots=config.option.warn_unused_snapshots,
update_snapshots=config.option.update_snapshots,
base_dir=config.rootdir,
is_providing_paths=any(
not arg.startswith("-") and glob.glob(arg)
for arg in config.invocation_params.args
),
)
session._syrupy.start()

Expand Down
2 changes: 2 additions & 0 deletions src/syrupy/extensions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ def discover_snapshots(self) -> "SnapshotFossils":
snapshot_fossil = SnapshotEmptyFossil(location=filepath)
else:
snapshot_fossil = SnapshotFossil(location=filepath)

discovered.add(snapshot_fossil)

return discovered

@final
Expand Down
22 changes: 21 additions & 1 deletion src/syrupy/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Any,
Generator,
List,
Optional,
Set,
)

Expand Down Expand Up @@ -39,6 +40,7 @@ class SnapshotReport(object):
all_items: Set[Any] = attr.ib()
ran_items: Set[Any] = attr.ib()
update_snapshots: bool = attr.ib()
is_providing_paths: bool = attr.ib()
warn_unused_snapshots: bool = attr.ib()
assertions: List["SnapshotAssertion"] = attr.ib()
discovered: "SnapshotFossils" = attr.ib(factory=SnapshotFossils)
Expand All @@ -48,9 +50,23 @@ class SnapshotReport(object):
updated: "SnapshotFossils" = attr.ib(factory=SnapshotFossils)
used: "SnapshotFossils" = attr.ib(factory=SnapshotFossils)

def filter_fossils(self, item: "SnapshotFossil") -> bool:
if not self.ran_test_files or not self.is_providing_paths:
return True

target_snaps = {snap.location for snap in self.used}

return item.location in target_snaps
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think item.location in self.used would have just worked here without needing to create target_snaps


def __attrs_post_init__(self) -> None:
for assertion in self.assertions:
self.discovered.merge(assertion.extension.discover_snapshots())
discovered_snaps = assertion.extension.discover_snapshots()
filtered_snaps = {
snap.location: snap
for snap in discovered_snaps
if self.filter_fossils(snap)
}
self.discovered.merge(SnapshotFossils(filtered_snaps))
for result in assertion.executions.values():
snapshot_fossil = SnapshotFossil(location=result.snapshot_location)
snapshot_fossil.add(
Expand All @@ -66,6 +82,10 @@ def __attrs_post_init__(self) -> None:
else:
self.failed.update(snapshot_fossil)

@property
def ran_test_files(self) -> Optional[List[str]]:
return [ran_item.location[0] for ran_item in self.ran_items]

@property
def num_created(self) -> int:
return self._count_snapshots(self.created)
Expand Down
10 changes: 9 additions & 1 deletion src/syrupy/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@

class SnapshotSession:
def __init__(
self, *, warn_unused_snapshots: bool, update_snapshots: bool, base_dir: str
self,
*,
warn_unused_snapshots: bool,
update_snapshots: bool,
base_dir: str,
is_providing_paths: bool,
):
self.warn_unused_snapshots = warn_unused_snapshots
self.update_snapshots = update_snapshots
self.base_dir = base_dir
self.is_providing_paths: bool = is_providing_paths
self.report: Optional["SnapshotReport"] = None
self._all_items: Set[Any] = set()
self._ran_items: Set[Any] = set()
Expand All @@ -47,6 +53,7 @@ def finish(self) -> int:
assertions=self._assertions,
update_snapshots=self.update_snapshots,
warn_unused_snapshots=self.warn_unused_snapshots,
is_providing_paths=self.is_providing_paths,
)
if self.report.num_unused:
if self.update_snapshots:
Expand Down Expand Up @@ -74,6 +81,7 @@ def remove_unused_snapshots(
) -> None:
for unused_snapshot_fossil in unused_snapshot_fossils:
snapshot_location = unused_snapshot_fossil.location

extension = self._extensions.get(snapshot_location)
if extension:
extension.delete_snapshots(
Expand Down
36 changes: 36 additions & 0 deletions tests/test_integration_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,42 @@ def test_unused_snapshots_warning(stubs):
assert result.ret == 0


def test_unused_snapshots_ignored_if_not_targeted_when_targeting_using_dash_k(stubs,):
_, testdir, tests, _ = stubs
testdir.makepyfile(test_file="\n\n".join(tests[k] for k in tests if k != "unused"))
testdir.makefile(
".ambr", **{"__snapshots__/other_snapfile": ""},
)
test_content = (
"def test_life_always_finds_a_way(snapshot):\n\tassert snapshot == snapshot"
)
testdir.makefile(
".py", **{"test_life_always_finds_a_way": test_content},
)
result = testdir.runpytest(
"-k", "life_always_finds_a_way", "-v", "--snapshot-update"
)
result_stdout = clean_output(result.stdout.str())
assert "1 snapshot generated" in result_stdout
assert result.ret == 0
assert os.path.isfile("__snapshots__/other_snapfile.ambr")


def test_unused_snapshots_ignored_if_not_targeted_wdhen_targeting_specific_testfiles(
stubs,
):
_, testdir, tests, _ = stubs
testdir.makepyfile(test_file="\n\n".join(tests[k] for k in tests if k != "unused"))
testdir.makefile(
".ambr", **{"__snapshots__/other_snapfile": ""},
)
result = testdir.runpytest("-v", "--snapshot-update", "test_file.py")
result_stdout = clean_output(result.stdout.str())
assert "1 unused snapshot deleted" in result_stdout
assert result.ret == 0
assert os.path.isfile("__snapshots__/other_snapfile.ambr")


def test_removed_snapshots(stubs):
_, testdir, tests, filepath = stubs
assert os.path.isfile(filepath)
Expand Down