| 1 |
from functools import lru_cache |
| 2 |
from pathlib import Path |
| 3 |
from typing import ( |
| 4 |
TYPE_CHECKING, |
| 5 |
Any, |
| 6 |
Optional, |
| 7 |
) |
| 8 |
|
| 9 |
from syrupy.data import SnapshotCollection |
| 10 |
from syrupy.exceptions import TaintedSnapshotError |
| 11 |
from syrupy.extensions.base import AbstractSyrupyExtension |
| 12 |
|
| 13 |
from .serializer import ( # noqa: F401 |
| 14 |
AmberDataSerializer, |
| 15 |
AmberDataSerializerPlugin, |
| 16 |
AmberDataSerializerSorted, # re-exported |
| 17 |
) |
| 18 |
|
| 19 |
if TYPE_CHECKING: |
| 20 |
from syrupy.types import SerializableData |
| 21 |
|
| 22 |
|
| 23 |
class AmberSnapshotExtension(AbstractSyrupyExtension): |
| 24 |
""" |
| 25 |
An amber snapshot file stores data in the following format: |
| 26 |
""" |
| 27 |
|
| 28 |
file_extension = "ambr" |
| 29 |
|
| 30 |
serializer_class: type["AmberDataSerializer"] = AmberDataSerializer |
| 31 |
|
| 32 |
def serialize(self, data: "SerializableData", **kwargs: Any) -> str: |
| 33 |
""" |
| 34 |
Returns the serialized form of 'data' to be compared |
| 35 |
with the snapshot data written to disk. |
| 36 |
""" |
| 37 |
return self.serializer_class.serialize(data, **kwargs) |
| 38 |
|
| 39 |
def delete_snapshots( |
| 40 |
self, snapshot_location: str, snapshot_names: set[str] |
| 41 |
) -> None: |
| 42 |
snapshot_collection_to_update = AmberDataSerializer.read_file(snapshot_location) |
| 43 |
for snapshot_name in snapshot_names: |
| 44 |
snapshot_collection_to_update.remove(snapshot_name) |
| 45 |
|
| 46 |
if snapshot_collection_to_update.has_snapshots: |
| 47 |
self.serializer_class.write_file(snapshot_collection_to_update) |
| 48 |
else: |
| 49 |
Path(snapshot_location).unlink() |
| 50 |
|
| 51 |
def read_snapshot_collection(self, snapshot_location: str) -> "SnapshotCollection": |
| 52 |
return self.serializer_class.read_file(snapshot_location) |
| 53 |
|
| 54 |
@classmethod |
| 55 |
@lru_cache |
| 56 |
def __cacheable_read_snapshot( |
| 57 |
cls, snapshot_location: str, cache_key: str |
| 58 |
) -> "SnapshotCollection": |
| 59 |
return cls.serializer_class.read_file(snapshot_location) |
| 60 |
|
| 61 |
def read_snapshot_data_from_location( |
| 62 |
self, snapshot_location: str, snapshot_name: str, session_id: str |
| 63 |
) -> Optional["SerializableData"]: |
| 64 |
snapshots = self.__cacheable_read_snapshot( |
| 65 |
snapshot_location=snapshot_location, cache_key=session_id |
| 66 |
) |
| 67 |
snapshot = snapshots.get(snapshot_name) |
| 68 |
tainted = bool(snapshots.tainted or (snapshot and snapshot.tainted)) |
| 69 |
data = snapshot.data if snapshot else None |
| 70 |
if tainted: |
| 71 |
raise TaintedSnapshotError(snapshot_data=data) |
| 72 |
return data |
| 73 |
|
| 74 |
@classmethod |
| 75 |
def write_snapshot_collection( |
| 76 |
cls, *, snapshot_collection: "SnapshotCollection" |
| 77 |
) -> None: |
| 78 |
cls.serializer_class.write_file(snapshot_collection, merge=True) |
| 79 |
|
| 80 |
|
| 81 |
__all__ = ["AmberSnapshotExtension", "AmberDataSerializer", "AmberDataSerializerPlugin"] |