Skip to content

Commit

Permalink
refactor: dirname property to method
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Negin-Ulster committed Dec 1, 2022
1 parent 943ffb1 commit adb266d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
15 changes: 10 additions & 5 deletions src/syrupy/extensions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 5 additions & 4 deletions src/syrupy/extensions/single_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions tests/examples/test_custom_snapshot_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/examples/test_custom_snapshot_directory_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)
)
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/test_snapshot_outside_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

2 comments on commit adb266d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: adb266d Previous: 23cca84 Ratio
benchmarks/test_1000x.py::test_1000x_reads 0.5768650110444813 iter/sec (stddev: 0.07560233369071932) 0.6754078596653935 iter/sec (stddev: 0.06391877117699159) 1.17
benchmarks/test_1000x.py::test_1000x_writes 0.21424657719623252 iter/sec (stddev: 0.16212295030378035) 0.6345993135561808 iter/sec (stddev: 0.23174880874067105) 2.96
benchmarks/test_standard.py::test_standard 0.5378388882967824 iter/sec (stddev: 0.10931639165997459) 0.6315599143065584 iter/sec (stddev: 0.0923523543680502) 1.17

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: adb266d Previous: 23cca84 Ratio
benchmarks/test_1000x.py::test_1000x_writes 0.21424657719623252 iter/sec (stddev: 0.16212295030378035) 0.6345993135561808 iter/sec (stddev: 0.23174880874067105) 2.96

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.