Skip to content

Commit

Permalink
fix: only perform session finish on test items ran (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamogbz committed Oct 30, 2020
1 parent cf83a9a commit 61a670f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
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

0 comments on commit 61a670f

Please sign in to comment.