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

fix: only perform session finish on test items actually ran #401

Merged
merged 1 commit into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 20 additions & 1 deletion src/syrupy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import_module_member,
)

# Global to have access to the session in `pytest_runtest_logfinish` hook
_syrupy: Optional["SnapshotSession"] = None


def __default_extension_option(value: str) -> Any:
try:
Expand Down Expand Up @@ -112,6 +115,8 @@ def pytest_sessionstart(session: Any) -> None:
base_dir=config.rootdir,
invocation_args=config.invocation_params.args,
)
global _syrupy
_syrupy = config._syrupy
config._syrupy.start()


Expand All @@ -132,7 +137,21 @@ def pytest_collection_finish(session: Any) -> None:
https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_collection_finish
"""
for item in session.config._syrupy.filter_valid_items(session.items):
session.config._syrupy._ran_items[item] = True
session.config._syrupy._ran_items[item] = False


def pytest_runtest_logfinish(nodeid: str) -> None:
"""
At the end of running the runtest protocol for a single item.
https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_runtest_logfinish
"""
global _syrupy
if not _syrupy:
return
for item in _syrupy._ran_items:
if getattr(item, "nodeid", None) == nodeid:
_syrupy._ran_items[item] = True
return


def pytest_sessionfinish(session: Any, exitstatus: int) -> None:
Expand Down
2 changes: 2 additions & 0 deletions src/syrupy/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ def _ran_items_match_name(self, snapshot_name: str) -> bool:
return any(
PyTestLocation(item).matches_snapshot_name(snapshot_name)
for item in self.ran_items
if self.ran_items[item]
)

def _selected_items_match_name(self, snapshot_name: str) -> bool:
Expand All @@ -378,6 +379,7 @@ def _selected_items_match_location(self, snapshot_location: str) -> bool:
return any(
PyTestLocation(item).matches_snapshot_location(snapshot_location)
for item in self.ran_items
if self.ran_items[item]
)


Expand Down
2 changes: 2 additions & 0 deletions src/syrupy/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class SnapshotSession:
warn_unused_snapshots: bool = attr.ib()
_invocation_args: Tuple[str, ...] = attr.ib(factory=tuple)
report: Optional["SnapshotReport"] = attr.ib(default=None)
# All the collected test items
_all_items: Dict["pytest.Item", bool] = attr.ib(factory=dict)
# All the selected test items. Will be set to False until the test item is run.
_ran_items: Dict["pytest.Item", bool] = attr.ib(factory=dict)
_assertions: List["SnapshotAssertion"] = attr.ib(factory=list)
_extensions: Dict[str, "AbstractSyrupyExtension"] = attr.ib(factory=dict)
Expand Down