From 74131233cb08063fbcd06afcd0f317de49ba0a43 Mon Sep 17 00:00:00 2001 From: Noah Negin-Ulster Date: Thu, 1 Dec 2022 16:01:14 -0500 Subject: [PATCH] refactor: rename Fossil to Collection BREAKING CHANGE: The term 'fossil' has been replaced by the clearer term 'collection'. --- src/syrupy/constants.py | 4 +- src/syrupy/data.py | 52 ++++---- src/syrupy/extensions/amber/__init__.py | 20 +-- src/syrupy/extensions/amber/serializer.py | 22 +-- src/syrupy/extensions/base.py | 50 ++++--- src/syrupy/extensions/single_file.py | 21 ++- src/syrupy/report.py | 125 ++++++++++-------- src/syrupy/session.py | 20 +-- .../test_snapshot_option_update.py | 9 +- tests/syrupy/extensions/test_single_file.py | 14 +- 10 files changed, 183 insertions(+), 154 deletions(-) diff --git a/src/syrupy/constants.py b/src/syrupy/constants.py index 7c7db338c..0503ff168 100644 --- a/src/syrupy/constants.py +++ b/src/syrupy/constants.py @@ -1,6 +1,6 @@ SNAPSHOT_DIRNAME = "__snapshots__" -SNAPSHOT_EMPTY_FOSSIL_KEY = "empty snapshot fossil" -SNAPSHOT_UNKNOWN_FOSSIL_KEY = "unknown snapshot fossil" +SNAPSHOT_EMPTY_FOSSIL_KEY = "empty snapshot collection" +SNAPSHOT_UNKNOWN_FOSSIL_KEY = "unknown snapshot collection" EXIT_STATUS_FAIL_UNUSED = 1 diff --git a/src/syrupy/data.py b/src/syrupy/data.py index 18fb41b83..b74f21411 100644 --- a/src/syrupy/data.py +++ b/src/syrupy/data.py @@ -36,7 +36,7 @@ class SnapshotUnknown(Snapshot): @dataclass -class SnapshotFossil: +class SnapshotCollection: """A collection of snapshots at a save location""" location: str @@ -54,8 +54,8 @@ def add(self, snapshot: "Snapshot") -> None: if snapshot.name != SNAPSHOT_EMPTY_FOSSIL_KEY: self.remove(SNAPSHOT_EMPTY_FOSSIL_KEY) - def merge(self, snapshot_fossil: "SnapshotFossil") -> None: - for snapshot in snapshot_fossil: + def merge(self, snapshot_collection: "SnapshotCollection") -> None: + for snapshot in snapshot_collection: self.add(snapshot) def remove(self, snapshot_name: str) -> None: @@ -69,8 +69,8 @@ def __iter__(self) -> Iterator["Snapshot"]: @dataclass -class SnapshotEmptyFossil(SnapshotFossil): - """This is a saved fossil that is known to be empty and thus can be removed""" +class SnapshotEmptyCollection(SnapshotCollection): + """This is a saved collection that is known to be empty and thus can be removed""" _snapshots: Dict[str, "Snapshot"] = field( default_factory=lambda: {SnapshotEmpty().name: SnapshotEmpty()} @@ -82,8 +82,8 @@ def has_snapshots(self) -> bool: @dataclass -class SnapshotUnknownFossil(SnapshotFossil): - """This is a saved fossil that is unclaimed by any extension currently in use""" +class SnapshotUnknownCollection(SnapshotCollection): + """This is a saved collection that is unclaimed by any extension currently in use""" _snapshots: Dict[str, "Snapshot"] = field( default_factory=lambda: {SnapshotUnknown().name: SnapshotUnknown()} @@ -91,33 +91,33 @@ class SnapshotUnknownFossil(SnapshotFossil): @dataclass -class SnapshotFossils: - _snapshot_fossils: Dict[str, "SnapshotFossil"] = field(default_factory=dict) +class SnapshotCollections: + _snapshot_collections: Dict[str, "SnapshotCollection"] = field(default_factory=dict) - def get(self, location: str) -> Optional["SnapshotFossil"]: - return self._snapshot_fossils.get(location) + def get(self, location: str) -> Optional["SnapshotCollection"]: + return self._snapshot_collections.get(location) - def add(self, snapshot_fossil: "SnapshotFossil") -> None: - self._snapshot_fossils[snapshot_fossil.location] = snapshot_fossil + def add(self, snapshot_collection: "SnapshotCollection") -> None: + self._snapshot_collections[snapshot_collection.location] = snapshot_collection - def update(self, snapshot_fossil: "SnapshotFossil") -> None: - snapshot_fossil_to_update = self.get(snapshot_fossil.location) - if snapshot_fossil_to_update is None: - snapshot_fossil_to_update = SnapshotFossil( - location=snapshot_fossil.location + def update(self, snapshot_collection: "SnapshotCollection") -> None: + snapshot_collection_to_update = self.get(snapshot_collection.location) + if snapshot_collection_to_update is None: + snapshot_collection_to_update = SnapshotCollection( + location=snapshot_collection.location ) - self.add(snapshot_fossil_to_update) - snapshot_fossil_to_update.merge(snapshot_fossil) + self.add(snapshot_collection_to_update) + snapshot_collection_to_update.merge(snapshot_collection) - def merge(self, snapshot_fossils: "SnapshotFossils") -> None: - for snapshot_fossil in snapshot_fossils: - self.update(snapshot_fossil) + def merge(self, snapshot_collections: "SnapshotCollections") -> None: + for snapshot_collection in snapshot_collections: + self.update(snapshot_collection) - def __iter__(self) -> Iterator["SnapshotFossil"]: - return iter(self._snapshot_fossils.values()) + def __iter__(self) -> Iterator["SnapshotCollection"]: + return iter(self._snapshot_collections.values()) def __contains__(self, key: str) -> bool: - return key in self._snapshot_fossils + return key in self._snapshot_collections @dataclass diff --git a/src/syrupy/extensions/amber/__init__.py b/src/syrupy/extensions/amber/__init__.py index 7f51d157f..d8559d846 100644 --- a/src/syrupy/extensions/amber/__init__.py +++ b/src/syrupy/extensions/amber/__init__.py @@ -7,7 +7,7 @@ Set, ) -from syrupy.data import SnapshotFossil +from syrupy.data import SnapshotCollection from syrupy.extensions.base import AbstractSyrupyExtension from .serializer import DataSerializer @@ -33,23 +33,23 @@ def serialize(self, data: "SerializableData", **kwargs: Any) -> str: def delete_snapshots( self, snapshot_location: str, snapshot_names: Set[str] ) -> None: - snapshot_fossil_to_update = DataSerializer.read_file(snapshot_location) + snapshot_collection_to_update = DataSerializer.read_file(snapshot_location) for snapshot_name in snapshot_names: - snapshot_fossil_to_update.remove(snapshot_name) + snapshot_collection_to_update.remove(snapshot_name) - if snapshot_fossil_to_update.has_snapshots: - DataSerializer.write_file(snapshot_fossil_to_update) + if snapshot_collection_to_update.has_snapshots: + DataSerializer.write_file(snapshot_collection_to_update) else: Path(snapshot_location).unlink() - def _read_snapshot_fossil(self, snapshot_location: str) -> "SnapshotFossil": + def _read_snapshot_collection(self, snapshot_location: str) -> "SnapshotCollection": return DataSerializer.read_file(snapshot_location) @staticmethod @lru_cache() def __cacheable_read_snapshot( snapshot_location: str, cache_key: str - ) -> "SnapshotFossil": + ) -> "SnapshotCollection": return DataSerializer.read_file(snapshot_location) def _read_snapshot_data_from_location( @@ -61,8 +61,10 @@ def _read_snapshot_data_from_location( snapshot = snapshots.get(snapshot_name) return snapshot.data if snapshot else None - def _write_snapshot_fossil(self, *, snapshot_fossil: "SnapshotFossil") -> None: - DataSerializer.write_file(snapshot_fossil, merge=True) + def _write_snapshot_collection( + self, *, snapshot_collection: "SnapshotCollection" + ) -> None: + DataSerializer.write_file(snapshot_collection, merge=True) __all__ = ["AmberSnapshotExtension", "DataSerializer"] diff --git a/src/syrupy/extensions/amber/serializer.py b/src/syrupy/extensions/amber/serializer.py index 31f009071..fd996ed4f 100644 --- a/src/syrupy/extensions/amber/serializer.py +++ b/src/syrupy/extensions/amber/serializer.py @@ -22,7 +22,7 @@ ) from syrupy.data import ( Snapshot, - SnapshotFossil, + SnapshotCollection, ) if TYPE_CHECKING: @@ -70,18 +70,20 @@ class DataSerializer: _marker_crn: str = "\r\n" @classmethod - def write_file(cls, snapshot_fossil: "SnapshotFossil", merge: bool = False) -> None: + def write_file( + cls, snapshot_collection: "SnapshotCollection", merge: bool = False + ) -> None: """ Writes the snapshot data into the snapshot file that can be read later. """ - filepath = snapshot_fossil.location + filepath = snapshot_collection.location if merge: base_snapshot = cls.read_file(filepath) - base_snapshot.merge(snapshot_fossil) - snapshot_fossil = base_snapshot + base_snapshot.merge(snapshot_collection) + snapshot_collection = base_snapshot with open(filepath, "w", encoding=TEXT_ENCODING, newline=None) as f: - for snapshot in sorted(snapshot_fossil, key=lambda s: s.name): + for snapshot in sorted(snapshot_collection, key=lambda s: s.name): snapshot_data = str(snapshot.data) if snapshot_data is not None: f.write(f"{cls._marker_name} {snapshot.name}\n") @@ -90,7 +92,7 @@ def write_file(cls, snapshot_fossil: "SnapshotFossil", merge: bool = False) -> N f.write(f"\n{cls._marker_divider}\n") @classmethod - def read_file(cls, filepath: str) -> "SnapshotFossil": + def read_file(cls, filepath: str) -> "SnapshotCollection": """ Read the raw snapshot data (str) from the snapshot file into a dict of snapshot name to raw data. This does not attempt any deserialization @@ -98,7 +100,7 @@ def read_file(cls, filepath: str) -> "SnapshotFossil": """ name_marker_len = len(cls._marker_name) indent_len = len(cls._indent) - snapshot_fossil = SnapshotFossil(location=filepath) + snapshot_collection = SnapshotCollection(location=filepath) try: with open(filepath, "r", encoding=TEXT_ENCODING, newline=None) as f: test_name = None @@ -112,7 +114,7 @@ def read_file(cls, filepath: str) -> "SnapshotFossil": if line.startswith(cls._indent): snapshot_data += line[indent_len:] elif line.startswith(cls._marker_divider) and snapshot_data: - snapshot_fossil.add( + snapshot_collection.add( Snapshot( name=test_name, data=snapshot_data.rstrip(os.linesep), @@ -121,7 +123,7 @@ def read_file(cls, filepath: str) -> "SnapshotFossil": except FileNotFoundError: pass - return snapshot_fossil + return snapshot_collection @classmethod def serialize( diff --git a/src/syrupy/extensions/base.py b/src/syrupy/extensions/base.py index d8827ca27..87ae11834 100644 --- a/src/syrupy/extensions/base.py +++ b/src/syrupy/extensions/base.py @@ -30,9 +30,9 @@ from syrupy.data import ( DiffedLine, Snapshot, - SnapshotEmptyFossil, - SnapshotFossil, - SnapshotFossils, + SnapshotCollection, + SnapshotCollections, + SnapshotEmptyCollection, ) from syrupy.exceptions import SnapshotDoesNotExist from syrupy.terminal import ( @@ -76,7 +76,7 @@ def serialize( raise NotImplementedError -class SnapshotFossilizer(ABC): +class SnapshotCollectionizer(ABC): _file_extension = "" @property @@ -106,20 +106,22 @@ def is_snapshot_location(self, *, location: str) -> bool: """Checks if supplied location is valid for this snapshot extension""" return location.endswith(self._file_extension) - def discover_snapshots(self) -> "SnapshotFossils": + def discover_snapshots(self) -> "SnapshotCollections": """ - Returns all snapshot fossils in test site + Returns all snapshot collections in test site """ - discovered: "SnapshotFossils" = SnapshotFossils() + discovered: "SnapshotCollections" = SnapshotCollections() for filepath in walk_snapshot_dir(self._dirname): if self.is_snapshot_location(location=filepath): - snapshot_fossil = self._read_snapshot_fossil(snapshot_location=filepath) - if not snapshot_fossil.has_snapshots: - snapshot_fossil = SnapshotEmptyFossil(location=filepath) + snapshot_collection = self._read_snapshot_collection( + snapshot_location=filepath + ) + if not snapshot_collection.has_snapshots: + snapshot_collection = SnapshotEmptyCollection(location=filepath) else: - snapshot_fossil = SnapshotFossil(location=filepath) + snapshot_collection = SnapshotCollection(location=filepath) - discovered.add(snapshot_fossil) + discovered.add(snapshot_collection) return discovered @@ -146,7 +148,7 @@ def read_snapshot( def write_snapshot(self, *, data: "SerializedData", index: "SnapshotIndex") -> None: """ This method is _final_, do not override. You can override - `_write_snapshot_fossil` in a subclass to change behaviour. + `_write_snapshot_collection` in a subclass to change behaviour. """ self.write_snapshot_batch(snapshots=[(data, index)]) @@ -155,7 +157,7 @@ def write_snapshot_batch( ) -> None: """ This method is _final_, do not override. You can override - `_write_snapshot_fossil` in a subclass to change behaviour. + `_write_snapshot_collection` in a subclass to change behaviour. """ # First we group by location since it'll let us batch by file on disk. # Not as useful for single file snapshots, but useful for the standard @@ -171,7 +173,7 @@ def write_snapshot_batch( self.__ensure_snapshot_dir(index=index) for location, location_snapshots in locations.items(): - snapshot_fossil = SnapshotFossil(location=location) + snapshot_collection = SnapshotCollection(location=location) if not self.test_location.matches_snapshot_location(location): warning_msg = gettext( @@ -186,7 +188,7 @@ def write_snapshot_batch( warnings.warn(warning_msg) for snapshot in location_snapshots: - snapshot_fossil.add(snapshot) + snapshot_collection.add(snapshot) if not self.test_location.matches_snapshot_name(snapshot.name): warning_msg = gettext( @@ -200,7 +202,7 @@ def write_snapshot_batch( ) warnings.warn(warning_msg) - self._write_snapshot_fossil(snapshot_fossil=snapshot_fossil) + self._write_snapshot_collection(snapshot_collection=snapshot_collection) @abstractmethod def delete_snapshots( @@ -213,9 +215,11 @@ def delete_snapshots( raise NotImplementedError @abstractmethod - def _read_snapshot_fossil(self, *, snapshot_location: str) -> "SnapshotFossil": + def _read_snapshot_collection( + self, *, snapshot_location: str + ) -> "SnapshotCollection": """ - Read the snapshot location and construct a snapshot fossil object + Read the snapshot location and construct a snapshot collection object """ raise NotImplementedError @@ -229,9 +233,11 @@ def _read_snapshot_data_from_location( raise NotImplementedError @abstractmethod - def _write_snapshot_fossil(self, *, snapshot_fossil: "SnapshotFossil") -> None: + def _write_snapshot_collection( + self, *, snapshot_collection: "SnapshotCollection" + ) -> None: """ - Adds the snapshot data to the snapshots in fossil location + Adds the snapshot data to the snapshots in collection location """ raise NotImplementedError @@ -415,7 +421,7 @@ def matches( class AbstractSyrupyExtension( - SnapshotSerializer, SnapshotFossilizer, SnapshotReporter, SnapshotComparator + SnapshotSerializer, SnapshotCollectionizer, SnapshotReporter, SnapshotComparator ): def __init__(self, test_location: "PyTestLocation"): self._test_location = test_location diff --git a/src/syrupy/extensions/single_file.py b/src/syrupy/extensions/single_file.py index 201c850f9..9663422cc 100644 --- a/src/syrupy/extensions/single_file.py +++ b/src/syrupy/extensions/single_file.py @@ -13,7 +13,7 @@ from syrupy.constants import TEXT_ENCODING from syrupy.data import ( Snapshot, - SnapshotFossil, + SnapshotCollection, ) from syrupy.location import PyTestLocation @@ -74,10 +74,12 @@ def _dirname(self) -> str: original_dirname = super(SingleFileSnapshotExtension, self)._dirname return str(Path(original_dirname).joinpath(self.test_location.basename)) - def _read_snapshot_fossil(self, *, snapshot_location: str) -> "SnapshotFossil": - snapshot_fossil = SnapshotFossil(location=snapshot_location) - snapshot_fossil.add(Snapshot(name=Path(snapshot_location).stem)) - return snapshot_fossil + def _read_snapshot_collection( + self, *, snapshot_location: str + ) -> "SnapshotCollection": + snapshot_collection = SnapshotCollection(location=snapshot_location) + snapshot_collection.add(Snapshot(name=Path(snapshot_location).stem)) + return snapshot_collection def _read_snapshot_data_from_location( self, *, snapshot_location: str, snapshot_name: str, session_id: str @@ -102,8 +104,13 @@ def _write_encoding(self) -> Optional[str]: return TEXT_ENCODING return None - def _write_snapshot_fossil(self, *, snapshot_fossil: "SnapshotFossil") -> None: - filepath, data = snapshot_fossil.location, next(iter(snapshot_fossil)).data + def _write_snapshot_collection( + self, *, snapshot_collection: "SnapshotCollection" + ) -> None: + filepath, data = ( + snapshot_collection.location, + next(iter(snapshot_collection)).data, + ) if not isinstance(data, self._supported_dataclass): error_text = gettext( "Can't write non supported data. Expected '{}', got '{}'" diff --git a/src/syrupy/report.py b/src/syrupy/report.py index 09b3991f5..ff3090468 100644 --- a/src/syrupy/report.py +++ b/src/syrupy/report.py @@ -24,9 +24,9 @@ from .constants import PYTEST_NODE_SEP from .data import ( Snapshot, - SnapshotFossil, - SnapshotFossils, - SnapshotUnknownFossil, + SnapshotCollection, + SnapshotCollections, + SnapshotUnknownCollection, ) from .location import PyTestLocation from .terminal import ( @@ -50,7 +50,7 @@ class SnapshotReport: """ This class is responsible for determining the test summary and post execution results. It will provide the lines of the report to be printed as well as the - information used for removal of unused or orphaned snapshots and fossils. + information used for removal of unused or orphaned snapshots and collections. """ base_dir: str @@ -58,12 +58,12 @@ class SnapshotReport: selected_items: Dict[str, bool] options: "argparse.Namespace" assertions: List["SnapshotAssertion"] - discovered: "SnapshotFossils" = field(default_factory=SnapshotFossils) - created: "SnapshotFossils" = field(default_factory=SnapshotFossils) - failed: "SnapshotFossils" = field(default_factory=SnapshotFossils) - matched: "SnapshotFossils" = field(default_factory=SnapshotFossils) - updated: "SnapshotFossils" = field(default_factory=SnapshotFossils) - used: "SnapshotFossils" = field(default_factory=SnapshotFossils) + discovered: "SnapshotCollections" = field(default_factory=SnapshotCollections) + created: "SnapshotCollections" = field(default_factory=SnapshotCollections) + failed: "SnapshotCollections" = field(default_factory=SnapshotCollections) + matched: "SnapshotCollections" = field(default_factory=SnapshotCollections) + updated: "SnapshotCollections" = field(default_factory=SnapshotCollections) + 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( @@ -98,19 +98,21 @@ def __post_init__(self) -> None: self.discovered.merge(assertion.extension.discover_snapshots()) for result in assertion.executions.values(): - snapshot_fossil = SnapshotFossil(location=result.snapshot_location) - snapshot_fossil.add( + snapshot_collection = SnapshotCollection( + location=result.snapshot_location + ) + snapshot_collection.add( Snapshot(name=result.snapshot_name, data=result.final_data) ) - self.used.update(snapshot_fossil) + self.used.update(snapshot_collection) if result.created: - self.created.update(snapshot_fossil) + self.created.update(snapshot_collection) elif result.updated: - self.updated.update(snapshot_fossil) + self.updated.update(snapshot_collection) elif result.success: - self.matched.update(snapshot_fossil) + self.matched.update(snapshot_collection) else: - self.failed.update(snapshot_fossil) + self.failed.update(snapshot_collection) def __parse_invocation_args(self) -> None: """ @@ -180,7 +182,7 @@ def ran_items(self) -> Iterator["pytest.Item"]: ) @property - def unused(self) -> "SnapshotFossils": + def unused(self) -> "SnapshotCollections": """ Iterate over each snapshot that was discovered but never used and compute if the snapshot was unused because the test attached to it was never run, @@ -189,11 +191,11 @@ def unused(self) -> "SnapshotFossils": Summary, if a snapshot was supposed to be run based on the invocation args and it was not, then it should be marked as unused otherwise ignored. """ - unused_fossils = SnapshotFossils() - for unused_snapshot_fossil in self._diff_snapshot_fossils( + unused_collections = SnapshotCollections() + for unused_snapshot_collection in self._diff_snapshot_collections( self.discovered, self.used ): - snapshot_location = unused_snapshot_fossil.location + snapshot_location = unused_snapshot_collection.location if self._provided_test_paths and not self._ran_items_match_location( snapshot_location ): @@ -204,13 +206,13 @@ def unused(self) -> "SnapshotFossils": provided_nodes = self._get_matching_path_nodes(snapshot_location) if self.selected_all_collected_items and not any(provided_nodes): # All collected tests were run and files were not filtered by ::node - # therefore the snapshot fossil file at this location can be deleted - unused_snapshots = {*unused_snapshot_fossil} + # therefore the snapshot collection file at this location can be deleted + unused_snapshots = {*unused_snapshot_collection} mark_for_removal = snapshot_location not in self.used else: unused_snapshots = { snapshot - for snapshot in unused_snapshot_fossil + for snapshot in unused_snapshot_collection if self._selected_items_match_name( snapshot_location=snapshot_location, snapshot_name=snapshot.name ) @@ -223,15 +225,17 @@ def unused(self) -> "SnapshotFossils": mark_for_removal = False if unused_snapshots: - marked_unused_snapshot_fossil = SnapshotFossil( + marked_unused_snapshot_collection = SnapshotCollection( location=snapshot_location ) for snapshot in unused_snapshots: - marked_unused_snapshot_fossil.add(snapshot) - unused_fossils.add(marked_unused_snapshot_fossil) + marked_unused_snapshot_collection.add(snapshot) + unused_collections.add(marked_unused_snapshot_collection) elif mark_for_removal: - unused_fossils.add(SnapshotUnknownFossil(location=snapshot_location)) - return unused_fossils + unused_collections.add( + SnapshotUnknownCollection(location=snapshot_location) + ) + return unused_collections @property def lines(self) -> Iterator[str]: @@ -296,9 +300,9 @@ def lines(self) -> Iterator[str]: yield "" if self.update_snapshots or self.include_snapshot_details: base_message = "Deleted" if self.update_snapshots else "Unused" - for snapshot_fossil in self.unused: - filepath = snapshot_fossil.location - snapshots = (snapshot.name for snapshot in snapshot_fossil) + for snapshot_collection in self.unused: + filepath = snapshot_collection.location + snapshots = (snapshot.name for snapshot in snapshot_collection) try: path_to_file = str(Path(filepath).relative_to(self.base_dir)) @@ -320,33 +324,40 @@ def lines(self) -> Iterator[str]: else: yield error_style(message) - def _diff_snapshot_fossils( - self, snapshot_fossils1: "SnapshotFossils", snapshot_fossils2: "SnapshotFossils" - ) -> "SnapshotFossils": + def _diff_snapshot_collections( + self, + snapshot_collections1: "SnapshotCollections", + snapshot_collections2: "SnapshotCollections", + ) -> "SnapshotCollections": """ - Find the difference between two collections of snapshot fossils. While - preserving the location site to all fossils in the first collections. That is - a collection with fossil sites {A{1,2}, B{3,4}, C{5,6}} with snapshot fossils - when diffed with another collection with snapshots {A{1,2}, B{3,4}, D{7,8}} - will result in a collection with the contents {A{}, B{}, C{5,6}}. + Find the difference between two collections of snapshot collections. While + preserving the location site to all collections in the first collections. + That is a collection with collection sites {A{1,2}, B{3,4}, C{5,6}} with + snapshot collections when diffed with another collection with snapshots + {A{1,2}, B{3,4}, D{7,8}} will result in a collection with the contents + {A{}, B{}, C{5,6}}. """ - diffed_snapshot_fossils: "SnapshotFossils" = SnapshotFossils() - for snapshot_fossil1 in snapshot_fossils1: - snapshot_fossil2 = snapshot_fossils2.get( - snapshot_fossil1.location - ) or SnapshotFossil(location=snapshot_fossil1.location) - diffed_snapshot_fossil = SnapshotFossil(location=snapshot_fossil1.location) - for snapshot in snapshot_fossil1: - if not snapshot_fossil2.get(snapshot.name): - diffed_snapshot_fossil.add(snapshot) - diffed_snapshot_fossils.add(diffed_snapshot_fossil) - return diffed_snapshot_fossils - - def _count_snapshots(self, snapshot_fossils: "SnapshotFossils") -> int: + diffed_snapshot_collections: "SnapshotCollections" = SnapshotCollections() + for snapshot_collection1 in snapshot_collections1: + snapshot_collection2 = snapshot_collections2.get( + snapshot_collection1.location + ) or SnapshotCollection(location=snapshot_collection1.location) + diffed_snapshot_collection = SnapshotCollection( + location=snapshot_collection1.location + ) + for snapshot in snapshot_collection1: + if not snapshot_collection2.get(snapshot.name): + diffed_snapshot_collection.add(snapshot) + diffed_snapshot_collections.add(diffed_snapshot_collection) + return diffed_snapshot_collections + + def _count_snapshots(self, snapshot_collections: "SnapshotCollections") -> int: """ - Count all the snapshots at all the locations in the snapshot fossil collection + Count all the snapshots at all the locations in the snapshot collections """ - return sum(len(snapshot_fossil) for snapshot_fossil in snapshot_fossils) + return sum( + len(snapshot_collection) for snapshot_collection in snapshot_collections + ) def _is_matching_path(self, snapshot_location: str, provided_path: str) -> bool: """ @@ -425,8 +436,8 @@ def _selected_items_match_name( def _ran_items_match_location(self, snapshot_location: str) -> bool: """ Check if any test run in the current session should match the snapshot location - This being true means that if no snapshot in the fossil was used then it should - be discarded as obsolete + This being true means that if no snapshot in the collection was used then it + should be discarded as obsolete """ return any( PyTestLocation(item).matches_snapshot_location(snapshot_location) diff --git a/src/syrupy/session.py b/src/syrupy/session.py index 9f5caf544..f18dcda1d 100644 --- a/src/syrupy/session.py +++ b/src/syrupy/session.py @@ -19,7 +19,7 @@ import pytest from .constants import EXIT_STATUS_FAIL_UNUSED -from .data import SnapshotFossils +from .data import SnapshotCollections from .report import SnapshotReport from .types import ( SerializedData, @@ -108,8 +108,8 @@ def finish(self) -> int: if self.report.num_unused: if self.update_snapshots: self.remove_unused_snapshots( - unused_snapshot_fossils=self.report.unused, - used_snapshot_fossils=self.report.used, + unused_snapshot_collections=self.report.unused, + used_snapshot_collections=self.report.used, ) elif not self.warn_unused_snapshots: exitstatus |= EXIT_STATUS_FAIL_UNUSED @@ -131,25 +131,25 @@ def register_request(self, assertion: "SnapshotAssertion") -> None: def remove_unused_snapshots( self, - unused_snapshot_fossils: "SnapshotFossils", - used_snapshot_fossils: "SnapshotFossils", + unused_snapshot_collections: "SnapshotCollections", + used_snapshot_collections: "SnapshotCollections", ) -> None: """ - Remove all unused snapshots using the registed extension for the fossil file + Remove all unused snapshots using the registed extension for the collection file If there is not registered extension and the location is unused delete the file """ - for unused_snapshot_fossil in unused_snapshot_fossils: - snapshot_location = unused_snapshot_fossil.location + for unused_snapshot_collection in unused_snapshot_collections: + snapshot_location = unused_snapshot_collection.location extension = self._extensions.get(snapshot_location) if extension: extension.delete_snapshots( snapshot_location=snapshot_location, snapshot_names={ - snapshot.name for snapshot in unused_snapshot_fossil + snapshot.name for snapshot in unused_snapshot_collection }, ) - elif snapshot_location not in used_snapshot_fossils: + elif snapshot_location not in used_snapshot_collections: Path(snapshot_location).unlink() @staticmethod diff --git a/tests/integration/test_snapshot_option_update.py b/tests/integration/test_snapshot_option_update.py index 12f4533de..b898c9a8f 100644 --- a/tests/integration/test_snapshot_option_update.py +++ b/tests/integration/test_snapshot_option_update.py @@ -394,7 +394,7 @@ def test_used(snapshot): assert not Path("__snapshots__", "test_used.ambr").exists() -def test_update_removes_empty_snapshot_fossil_only(run_testcases): +def test_update_removes_empty_snapshot_collection_only(run_testcases): testdir = run_testcases[1] snapfile_empty = Path("__snapshots__", "empty_snapfile.ambr") testdir.makefile(".ambr", **{str(snapfile_empty): ""}) @@ -403,7 +403,8 @@ def test_update_removes_empty_snapshot_fossil_only(run_testcases): result.stdout.re_match_lines( ( r"10 snapshots passed\. 1 unused snapshot deleted\.", - r"Deleted empty snapshot fossil \(__snapshots__[\\/]empty_snapfile\.ambr\)", + r"Deleted empty snapshot collection " + r"\(__snapshots__[\\/]empty_snapfile\.ambr\)", ) ) assert result.ret == 0 @@ -411,7 +412,7 @@ def test_update_removes_empty_snapshot_fossil_only(run_testcases): assert Path("__snapshots__", "test_used.ambr").exists() -def test_update_removes_hanging_snapshot_fossil_file(run_testcases): +def test_update_removes_hanging_snapshot_collection_file(run_testcases): testdir = run_testcases[1] snapfile_used = Path("__snapshots__", "test_used.ambr") snapfile_hanging = Path("__snapshots__", "hanging_snapfile.abc") @@ -421,7 +422,7 @@ def test_update_removes_hanging_snapshot_fossil_file(run_testcases): result.stdout.re_match_lines( ( r"10 snapshots passed\. 1 unused snapshot deleted\.", - r"Deleted unknown snapshot fossil " + r"Deleted unknown snapshot collection " r"\(__snapshots__[\\/]hanging_snapfile\.abc\)", ) ) diff --git a/tests/syrupy/extensions/test_single_file.py b/tests/syrupy/extensions/test_single_file.py index da7c22807..6adcda722 100644 --- a/tests/syrupy/extensions/test_single_file.py +++ b/tests/syrupy/extensions/test_single_file.py @@ -5,7 +5,7 @@ from syrupy.data import ( Snapshot, - SnapshotFossil, + SnapshotCollection, ) from syrupy.extensions.single_file import ( SingleFileSnapshotExtension, @@ -31,15 +31,15 @@ def snapshot_utf8(snapshot): def test_does_not_write_non_binary(testdir, snapshot_single: "SnapshotAssertion"): - snapshot_fossil = SnapshotFossil( - location=str(Path(testdir.tmpdir).joinpath("snapshot_fossil.raw")), + snapshot_collection = SnapshotCollection( + location=str(Path(testdir.tmpdir).joinpath("snapshot_collection.raw")), ) - snapshot_fossil.add(Snapshot(name="snapshot_name", data="non binary data")) + snapshot_collection.add(Snapshot(name="snapshot_name", data="non binary data")) with pytest.raises(TypeError, match="Expected 'bytes', got 'str'"): - snapshot_single.extension._write_snapshot_fossil( - snapshot_fossil=snapshot_fossil + snapshot_single.extension._write_snapshot_collection( + snapshot_collection=snapshot_collection ) - assert not Path(snapshot_fossil.location).exists() + assert not Path(snapshot_collection.location).exists() class TestClass: