From adb266d5d63eac6b46337c4326d9d1bb55b64b80 Mon Sep 17 00:00:00 2001 From: Noah Negin-Ulster Date: Thu, 1 Dec 2022 16:26:02 -0500 Subject: [PATCH] refactor: dirname property to method --- src/syrupy/extensions/base.py | 15 ++++++++++----- src/syrupy/extensions/single_file.py | 9 +++++---- tests/examples/test_custom_snapshot_directory.py | 8 +++----- .../examples/test_custom_snapshot_directory_2.py | 6 +++--- .../test_snapshot_outside_directory.py | 3 +-- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/syrupy/extensions/base.py b/src/syrupy/extensions/base.py index 7e0e87d1..3a77a6c3 100644 --- a/src/syrupy/extensions/base.py +++ b/src/syrupy/extensions/base.py @@ -100,7 +100,11 @@ def get_location(self, *, index: "SnapshotIndex") -> str: """Returns full location where snapshot data is stored.""" basename = self._get_file_basename(index=index) fileext = f".{self._file_extension}" if self._file_extension else "" - return str(Path(self._dirname).joinpath(f"{basename}{fileext}")) + return str( + Path(self.dirname(test_location=self.test_location)).joinpath( + f"{basename}{fileext}" + ) + ) def is_snapshot_location(self, *, location: str) -> bool: """Checks if supplied location is valid for this snapshot extension""" @@ -111,7 +115,9 @@ def discover_snapshots(self) -> "SnapshotCollections": Returns all snapshot collections in test site """ discovered: "SnapshotCollections" = SnapshotCollections() - for filepath in walk_snapshot_dir(self._dirname): + for filepath in walk_snapshot_dir( + self.dirname(test_location=self.test_location) + ): if self.is_snapshot_location(location=filepath): snapshot_collection = self._read_snapshot_collection( snapshot_location=filepath @@ -239,9 +245,8 @@ def _write_snapshot_collection( """ raise NotImplementedError - @property - def _dirname(self) -> str: - test_dir = Path(self.test_location.filepath).parent + def dirname(self, *, test_location: "PyTestLocation") -> str: + test_dir = Path(test_location.filepath).parent return str(test_dir.joinpath(SNAPSHOT_DIRNAME)) def _get_file_basename(self, *, index: "SnapshotIndex") -> str: diff --git a/src/syrupy/extensions/single_file.py b/src/syrupy/extensions/single_file.py index 9663422c..fd6494d5 100644 --- a/src/syrupy/extensions/single_file.py +++ b/src/syrupy/extensions/single_file.py @@ -69,10 +69,11 @@ def delete_snapshots( def _get_file_basename(self, *, index: "SnapshotIndex") -> str: return self.get_snapshot_name(test_location=self.test_location, index=index) - @property - def _dirname(self) -> str: - original_dirname = super(SingleFileSnapshotExtension, self)._dirname - return str(Path(original_dirname).joinpath(self.test_location.basename)) + def dirname(self, *, test_location: "PyTestLocation") -> str: + original_dirname = super(SingleFileSnapshotExtension, self).dirname( + test_location=test_location + ) + return str(Path(original_dirname).joinpath(test_location.basename)) def _read_snapshot_collection( self, *, snapshot_location: str diff --git a/tests/examples/test_custom_snapshot_directory.py b/tests/examples/test_custom_snapshot_directory.py index 305c27e7..345143a5 100644 --- a/tests/examples/test_custom_snapshot_directory.py +++ b/tests/examples/test_custom_snapshot_directory.py @@ -15,16 +15,14 @@ import pytest from syrupy.extensions.amber import AmberSnapshotExtension +from syrupy.location import PyTestLocation DIFFERENT_DIRECTORY = "__snaps_example__" class DifferentDirectoryExtension(AmberSnapshotExtension): - @property - def _dirname(self) -> str: - return str( - Path(self.test_location.filepath).parent.joinpath(DIFFERENT_DIRECTORY) - ) + def dirname(self, *, test_location: "PyTestLocation") -> str: + return str(Path(test_location.filepath).parent.joinpath(DIFFERENT_DIRECTORY)) @pytest.fixture diff --git a/tests/examples/test_custom_snapshot_directory_2.py b/tests/examples/test_custom_snapshot_directory_2.py index 496b8a7e..5bae0d80 100644 --- a/tests/examples/test_custom_snapshot_directory_2.py +++ b/tests/examples/test_custom_snapshot_directory_2.py @@ -15,14 +15,14 @@ import pytest from syrupy.extensions.json import JSONSnapshotExtension +from syrupy.location import PyTestLocation def create_versioned_fixture(version: int): class VersionedJSONExtension(JSONSnapshotExtension): - @property - def _dirname(self) -> str: + def dirname(self, *, test_location: "PyTestLocation") -> str: return str( - Path(self.test_location.filepath).parent.joinpath( + Path(test_location.filepath).parent.joinpath( "__snapshots__", f"v{version}" ) ) diff --git a/tests/integration/test_snapshot_outside_directory.py b/tests/integration/test_snapshot_outside_directory.py index b241c6f0..fb444a54 100644 --- a/tests/integration/test_snapshot_outside_directory.py +++ b/tests/integration/test_snapshot_outside_directory.py @@ -11,8 +11,7 @@ def testcases(testdir, tmp_path): from syrupy.extensions.amber import AmberSnapshotExtension class CustomSnapshotExtension(AmberSnapshotExtension): - @property - def _dirname(self): + def dirname(self, *, test_location): return {str(dirname)!r} @pytest.fixture