Skip to content

Commit 95ffbcb

Browse files
authored
Merge pull request #1026 from syrupy-project/rename_methods
refactor: remove incorrect private underscore prefix from public methods
2 parents e1dbf5d + 27135c7 commit 95ffbcb

17 files changed

+151
-56
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ These are the cli options exposed to `pytest` by the plugin.
135135
| `--snapshot-patch-pycharm-diff`| Override PyCharm's default diffs viewer when looking at snapshot diffs. See [IDE Integrations](#ide-integrations) | `False` |
136136
| `--snapshot-diff-mode` | Configures how diffs are displayed on assertion failure. If working with very large snapshots, disabling the diff can improve performance. | `detailed` |
137137
| `--snapshot-ignore-file-extensions` | Comma separated list of file extensions to ignore when walking the file tree and discovering used/unused snapshots. | No extensions are ignored by default. |
138+
| `--snapshot-dirname` | Directory name to store snapshots in. | `__snapshots__` |
138139

139140
### Assertion Options
140141

src/syrupy/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import pytest
1313

14+
from syrupy.extensions.base import SnapshotCollectionStorage
15+
1416
from .assertion import DiffMode, SnapshotAssertion
1517
from .constants import DISABLE_COLOR_ENV_VAR
1618
from .exceptions import FailedToLoadModuleMember
@@ -108,6 +110,12 @@ def pytest_addoption(parser: "pytest.Parser") -> None:
108110
help="Comma separated list of file extensions to ignore when discovering snapshots",
109111
type=lambda v: v.split(","),
110112
)
113+
group.addoption(
114+
"--snapshot-dirname",
115+
dest="snapshot_dirname",
116+
default="__snapshots__",
117+
help="Directory name to use to store snapshots",
118+
)
111119

112120

113121
def __terminal_color(
@@ -161,6 +169,10 @@ def pytest_sessionstart(session: Any) -> None:
161169
Initialize snapshot session before tests are collected and ran.
162170
https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_sessionstart
163171
"""
172+
173+
# Override the snapshot dirname in the base SnapshotCollectionStorage class with the pytest config.
174+
SnapshotCollectionStorage.snapshot_dirname = session.config.option.snapshot_dirname
175+
164176
session.config._syrupy = SnapshotSession(
165177
pytest_session=session,
166178
ignore_file_extensions=session.config.option.ignore_file_extensions,

src/syrupy/constants.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
SNAPSHOT_DIRNAME = "__snapshots__"
2-
SNAPSHOT_EMPTY_FOSSIL_KEY = "empty snapshot collection"
3-
SNAPSHOT_UNKNOWN_FOSSIL_KEY = "unknown snapshot collection"
1+
SNAPSHOT_EMPTY_COLLECTION_KEY = "empty snapshot collection"
2+
SNAPSHOT_UNKNOWN_COLLECTION_KEY = "unknown snapshot collection"
43

54
EXIT_STATUS_FAIL_UNUSED = 1
65

src/syrupy/data.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
)
1010

1111
from .constants import (
12-
SNAPSHOT_EMPTY_FOSSIL_KEY,
13-
SNAPSHOT_UNKNOWN_FOSSIL_KEY,
12+
SNAPSHOT_EMPTY_COLLECTION_KEY,
13+
SNAPSHOT_UNKNOWN_COLLECTION_KEY,
1414
)
1515

1616
if TYPE_CHECKING:
@@ -27,12 +27,12 @@ class Snapshot:
2727

2828
@dataclass(frozen=True)
2929
class SnapshotEmpty(Snapshot):
30-
name: str = SNAPSHOT_EMPTY_FOSSIL_KEY
30+
name: str = SNAPSHOT_EMPTY_COLLECTION_KEY
3131

3232

3333
@dataclass(frozen=True)
3434
class SnapshotUnknown(Snapshot):
35-
name: str = SNAPSHOT_UNKNOWN_FOSSIL_KEY
35+
name: str = SNAPSHOT_UNKNOWN_COLLECTION_KEY
3636

3737

3838
@dataclass
@@ -54,8 +54,8 @@ def get(self, snapshot_name: str) -> Optional["Snapshot"]:
5454

5555
def add(self, snapshot: "Snapshot") -> None:
5656
self._snapshots[snapshot.name] = snapshot
57-
if snapshot.name != SNAPSHOT_EMPTY_FOSSIL_KEY:
58-
self.remove(SNAPSHOT_EMPTY_FOSSIL_KEY)
57+
if snapshot.name != SNAPSHOT_EMPTY_COLLECTION_KEY:
58+
self.remove(SNAPSHOT_EMPTY_COLLECTION_KEY)
5959

6060
def merge(self, snapshot_collection: "SnapshotCollection") -> None:
6161
for snapshot in snapshot_collection:

src/syrupy/extensions/amber/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class AmberSnapshotExtension(AbstractSyrupyExtension):
2424
An amber snapshot file stores data in the following format:
2525
"""
2626

27-
_file_extension = "ambr"
27+
file_extension = "ambr"
2828

2929
serializer_class: type["AmberDataSerializer"] = AmberDataSerializer
3030

@@ -47,7 +47,7 @@ def delete_snapshots(
4747
else:
4848
Path(snapshot_location).unlink()
4949

50-
def _read_snapshot_collection(self, snapshot_location: str) -> "SnapshotCollection":
50+
def read_snapshot_collection(self, snapshot_location: str) -> "SnapshotCollection":
5151
return self.serializer_class.read_file(snapshot_location)
5252

5353
@classmethod
@@ -57,7 +57,7 @@ def __cacheable_read_snapshot(
5757
) -> "SnapshotCollection":
5858
return cls.serializer_class.read_file(snapshot_location)
5959

60-
def _read_snapshot_data_from_location(
60+
def read_snapshot_data_from_location(
6161
self, snapshot_location: str, snapshot_name: str, session_id: str
6262
) -> Optional["SerializableData"]:
6363
snapshots = self.__cacheable_read_snapshot(
@@ -71,7 +71,7 @@ def _read_snapshot_data_from_location(
7171
return data
7272

7373
@classmethod
74-
def _write_snapshot_collection(
74+
def write_snapshot_collection(
7575
cls, *, snapshot_collection: "SnapshotCollection"
7676
) -> None:
7777
cls.serializer_class.write_file(snapshot_collection, merge=True)

src/syrupy/extensions/amber/serializer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
IterableEntries = tuple[
4040
Iterable["PropertyName"],
4141
"PropertyValueGetter",
42-
"PropertyValueFilter" | None,
42+
"PropertyValueFilter | None",
4343
]
4444

4545

@@ -100,7 +100,7 @@ class Marker:
100100
Divider = "---"
101101

102102
@classmethod
103-
def _snapshot_sort_key(cls, snapshot: "Snapshot") -> Any:
103+
def snapshot_sort_key(cls, snapshot: "Snapshot") -> Any:
104104
return snapshot.name
105105

106106
@classmethod
@@ -120,7 +120,7 @@ def write_file(
120120
f.write(f"{cls._marker_prefix}{cls.Marker.Version}: {cls.VERSION}\n")
121121
for snapshot in sorted(
122122
snapshot_collection,
123-
key=cls._snapshot_sort_key, # noqa: E501
123+
key=cls.snapshot_sort_key, # noqa: E501
124124
):
125125
snapshot_data = str(snapshot.data)
126126
if snapshot_data is not None:
@@ -518,5 +518,5 @@ def __maybe_int(cls, part: str) -> tuple[int, str | int]:
518518
return (0, part)
519519

520520
@classmethod
521-
def _snapshot_sort_key(cls, snapshot: "Snapshot") -> Any:
521+
def snapshot_sort_key(cls, snapshot: "Snapshot") -> Any:
522522
return [cls.__maybe_int(part) for part in snapshot.name.split(".")]

src/syrupy/extensions/base.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from syrupy.constants import (
1616
DISABLE_COLOR_ENV_VAR,
17-
SNAPSHOT_DIRNAME,
1817
SYMBOL_CARRIAGE,
1918
SYMBOL_ELLIPSIS,
2019
SYMBOL_NEW_LINE,
@@ -71,7 +70,8 @@ def serialize(
7170

7271

7372
class SnapshotCollectionStorage(ABC):
74-
_file_extension = ""
73+
snapshot_dirname: str | Path = "__snapshots__"
74+
file_extension = ""
7575

7676
@classmethod
7777
def get_snapshot_name(
@@ -90,8 +90,8 @@ def get_location(
9090
cls, *, test_location: "PyTestLocation", index: "SnapshotIndex"
9191
) -> str:
9292
"""Returns full filepath where snapshot data is stored."""
93-
basename = cls._get_file_basename(test_location=test_location, index=index)
94-
fileext = f".{cls._file_extension}" if cls._file_extension else ""
93+
basename = cls.get_file_basename(test_location=test_location, index=index)
94+
fileext = f".{cls.file_extension}" if cls.file_extension else ""
9595
return str(
9696
Path(cls.dirname(test_location=test_location)).joinpath(
9797
f"{basename}{fileext}"
@@ -100,7 +100,7 @@ def get_location(
100100

101101
def is_snapshot_location(self, *, location: str) -> bool:
102102
"""Checks if supplied location is valid for this snapshot extension"""
103-
return location.endswith(self._file_extension)
103+
return location.endswith(self.file_extension)
104104

105105
def discover_snapshots(
106106
self,
@@ -117,7 +117,7 @@ def discover_snapshots(
117117
ignore_extensions=ignore_extensions,
118118
):
119119
if self.is_snapshot_location(location=filepath):
120-
snapshot_collection = self._read_snapshot_collection(
120+
snapshot_collection = self.read_snapshot_collection(
121121
snapshot_location=filepath
122122
)
123123
if not snapshot_collection.has_snapshots:
@@ -138,11 +138,11 @@ def read_snapshot(
138138
) -> "SerializedData":
139139
"""
140140
This method is _final_, do not override. You can override
141-
`_read_snapshot_data_from_location` in a subclass to change behaviour.
141+
`read_snapshot_data_from_location` in a subclass to change behaviour.
142142
"""
143143
snapshot_location = self.get_location(test_location=test_location, index=index)
144144
snapshot_name = self.get_snapshot_name(test_location=test_location, index=index)
145-
snapshot_data = self._read_snapshot_data_from_location(
145+
snapshot_data = self.read_snapshot_data_from_location(
146146
snapshot_location=snapshot_location,
147147
snapshot_name=snapshot_name,
148148
session_id=session_id,
@@ -160,7 +160,7 @@ def write_snapshot(
160160
) -> None:
161161
"""
162162
This method is _final_, do not override. You can override
163-
`_write_snapshot_collection` in a subclass to change behaviour.
163+
`write_snapshot_collection` in a subclass to change behaviour.
164164
"""
165165
if not snapshots:
166166
return
@@ -203,7 +203,7 @@ def write_snapshot(
203203
# Ensures the folder path for the snapshot file exists.
204204
Path(snapshot_location).parent.mkdir(parents=True, exist_ok=True)
205205

206-
cls._write_snapshot_collection(snapshot_collection=snapshot_collection)
206+
cls.write_snapshot_collection(snapshot_collection=snapshot_collection)
207207

208208
@abstractmethod
209209
def delete_snapshots(
@@ -216,7 +216,7 @@ def delete_snapshots(
216216
raise NotImplementedError
217217

218218
@abstractmethod
219-
def _read_snapshot_collection(
219+
def read_snapshot_collection(
220220
self, *, snapshot_location: str
221221
) -> "SnapshotCollection":
222222
"""
@@ -225,7 +225,7 @@ def _read_snapshot_collection(
225225
raise NotImplementedError
226226

227227
@abstractmethod
228-
def _read_snapshot_data_from_location(
228+
def read_snapshot_data_from_location(
229229
self, *, snapshot_location: str, snapshot_name: str, session_id: str
230230
) -> Optional["SerializedData"]:
231231
"""
@@ -235,7 +235,7 @@ def _read_snapshot_data_from_location(
235235

236236
@classmethod
237237
@abstractmethod
238-
def _write_snapshot_collection(
238+
def write_snapshot_collection(
239239
cls, *, snapshot_collection: "SnapshotCollection"
240240
) -> None:
241241
"""
@@ -246,10 +246,10 @@ def _write_snapshot_collection(
246246
@classmethod
247247
def dirname(cls, *, test_location: "PyTestLocation") -> str:
248248
test_dir = Path(test_location.filepath).parent
249-
return str(test_dir.joinpath(SNAPSHOT_DIRNAME))
249+
return str(test_dir.joinpath(cls.snapshot_dirname))
250250

251251
@classmethod
252-
def _get_file_basename(
252+
def get_file_basename(
253253
cls, *, test_location: "PyTestLocation", index: "SnapshotIndex"
254254
) -> str:
255255
"""Returns file basename without extension. Used to create full filepath."""

src/syrupy/extensions/image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313

1414
class PNGImageSnapshotExtension(SingleFileSnapshotExtension):
15-
_file_extension = "png"
15+
file_extension = "png"
1616

1717

1818
class SVGImageSnapshotExtension(SingleFileSnapshotExtension):
19-
_file_extension = "svg"
19+
file_extension = "svg"
2020

2121
def serialize(self, data: "SerializableData", **kwargs: Any) -> bytes:
2222
return str(data).encode(TEXT_ENCODING)

src/syrupy/extensions/json/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
class JSONSnapshotExtension(SingleFileSnapshotExtension):
3535
_max_depth: int = 99
3636
_write_mode = WriteMode.TEXT
37-
_file_extension = "json"
37+
file_extension = "json"
3838

3939
@classmethod
4040
def sort(cls, iterable: Iterable[Any]) -> Iterable[Any]:

src/syrupy/extensions/single_file.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def __str__(self) -> str:
4040
class SingleFileSnapshotExtension(AbstractSyrupyExtension):
4141
_text_encoding = TEXT_ENCODING
4242
_write_mode = WriteMode.BINARY
43-
_file_extension = "raw"
43+
file_extension = "raw"
4444

4545
def serialize(
4646
self,
@@ -78,7 +78,7 @@ def delete_snapshots(
7878
Path(snapshot_location).unlink()
7979

8080
@classmethod
81-
def _get_file_basename(
81+
def get_file_basename(
8282
cls, *, test_location: "PyTestLocation", index: "SnapshotIndex"
8383
) -> str:
8484
return cls.get_snapshot_name(test_location=test_location, index=index)
@@ -88,18 +88,18 @@ def dirname(cls, *, test_location: "PyTestLocation") -> str:
8888
original_dirname = AbstractSyrupyExtension.dirname(test_location=test_location)
8989
return str(Path(original_dirname).joinpath(test_location.basename))
9090

91-
def _read_snapshot_collection(
91+
def read_snapshot_collection(
9292
self, *, snapshot_location: str
9393
) -> "SnapshotCollection":
94-
file_ext_len = len(self._file_extension) + 1 if self._file_extension else 0
94+
file_ext_len = len(self.file_extension) + 1 if self.file_extension else 0
9595
filename_wo_ext = snapshot_location[:-file_ext_len]
9696
basename = Path(filename_wo_ext).parts[-1]
9797

9898
snapshot_collection = SnapshotCollection(location=snapshot_location)
9999
snapshot_collection.add(Snapshot(name=basename))
100100
return snapshot_collection
101101

102-
def _read_snapshot_data_from_location(
102+
def read_snapshot_data_from_location(
103103
self, *, snapshot_location: str, snapshot_name: str, session_id: str
104104
) -> Optional["SerializableData"]:
105105
try:
@@ -125,7 +125,7 @@ def get_write_encoding(cls) -> str | None:
125125
return None
126126

127127
@classmethod
128-
def _write_snapshot_collection(
128+
def write_snapshot_collection(
129129
cls, *, snapshot_collection: "SnapshotCollection"
130130
) -> None:
131131
filepath, data = (
@@ -148,7 +148,7 @@ def _write_snapshot_collection(
148148

149149
@classmethod
150150
def __clean_filename(cls, filename: str) -> str:
151-
max_filename_length = 255 - len(cls._file_extension or "")
151+
max_filename_length = 255 - len(cls.file_extension or "")
152152
exclude_chars = '\\/?*:|"<>'
153153
exclude_categ = ("C",)
154154
cleaned_filename = "".join(
@@ -161,7 +161,7 @@ def __clean_filename(cls, filename: str) -> str:
161161

162162

163163
class SingleFileAmberSnapshotExtension(SingleFileSnapshotExtension):
164-
_file_extension = "ambr"
164+
file_extension = "ambr"
165165
_write_mode = WriteMode.TEXT
166166

167167
def serialize(
@@ -176,7 +176,7 @@ def serialize(
176176
data, exclude=exclude, include=include, matcher=matcher
177177
)
178178

179-
def _read_snapshot_data_from_location(
179+
def read_snapshot_data_from_location(
180180
self, *, snapshot_location: str, snapshot_name: str, session_id: str
181181
) -> Optional["SerializableData"]:
182182
snapshot_collection = AmberDataSerializer.read_file(snapshot_location)
@@ -193,7 +193,7 @@ def _read_snapshot_data_from_location(
193193
return snapshot.data
194194

195195
@classmethod
196-
def _write_snapshot_collection(
196+
def write_snapshot_collection(
197197
cls, *, snapshot_collection: "SnapshotCollection"
198198
) -> None:
199199
AmberDataSerializer.write_file(snapshot_collection, merge=False)

0 commit comments

Comments
 (0)