Skip to content

Commit

Permalink
fix: improve pytest-xdist compatibility
Browse files Browse the repository at this point in the history
NOTE: When pytest-xdist is detected, we do not remove unused snapshots, since that requires coordination between the workers and the controller. A disclaimer has been added to the README and TODO comments added to the source code to help solve this problem at some point.
  • Loading branch information
Noah Negin-Ulster committed Dec 30, 2022
1 parent e8ed9f2 commit 9b9090f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
5 changes: 1 addition & 4 deletions src/syrupy/extensions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,7 @@ def write_snapshot(
warnings.warn(warning_msg)

# Ensures the folder path for the snapshot file exists.
try:
Path(snapshot_location).parent.mkdir(parents=True)
except FileExistsError:
pass
Path(snapshot_location).parent.mkdir(parents=True, exist_ok=True)

cls._write_snapshot_collection(snapshot_collection=snapshot_collection)

Expand Down
13 changes: 7 additions & 6 deletions src/syrupy/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
dataclass,
field,
)
from functools import cached_property
from gettext import (
gettext,
ngettext,
Expand Down Expand Up @@ -69,9 +70,6 @@ class SnapshotReport:
used: "SnapshotCollections" = field(default_factory=SnapshotCollections)
_provided_test_paths: Dict[str, List[str]] = field(default_factory=dict)
_keyword_expressions: Set["Expression"] = field(default_factory=set)
_collected_items_by_nodeid: Dict[str, "pytest.Item"] = field(
default_factory=dict, init=False
)

@property
def update_snapshots(self) -> bool:
Expand All @@ -85,12 +83,15 @@ def warn_unused_snapshots(self) -> bool:
def include_snapshot_details(self) -> bool:
return bool(self.options.include_snapshot_details)

def __post_init__(self) -> None:
self.__parse_invocation_args()
self._collected_items_by_nodeid = {
@cached_property
def _collected_items_by_nodeid(self) -> Dict[str, "pytest.Item"]:
return {
getattr(item, "nodeid"): item for item in self.collected_items # noqa: B009
}

def __post_init__(self) -> None:
self.__parse_invocation_args()

# We only need to discover snapshots once per test file, not once per assertion.
locations_discovered: DefaultDict[str, Set[Any]] = defaultdict(set)
for assertion in self.assertions:
Expand Down
4 changes: 4 additions & 0 deletions src/syrupy/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import pytest

from syrupy.location import PyTestLocation
from syrupy.utils import (
is_xdist_controller,
is_xdist_worker,
)

from .constants import EXIT_STATUS_FAIL_UNUSED
from .data import SnapshotCollections
Expand Down

0 comments on commit 9b9090f

Please sign in to comment.