Skip to content

Commit 2a67756

Browse files
committed
fix(serializer): raise TypeError when serializing non-byte like object in binary mode (#951)
1 parent ab6558b commit 2a67756

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ Syrupy comes with a few built-in preset configurations for you to choose from. Y
354354

355355
- **`AmberSnapshotExtension`**: This is the default extension which generates `.ambr` files. Serialization of most data types are supported.
356356
- Line control characters are normalised when snapshots are generated i.e. `\r` and `\n` characters are all written as `\n`. This is to allow interoperability of snapshots between operating systems that use disparate line control characters.
357-
- **`SingleFileSnapshotExtension`**: Unlike the `AmberSnapshotExtension`, which groups all tests within a single test file into a singular snapshot file, this extension creates one `.raw` file per test case.
357+
- **`SingleFileSnapshotExtension`**: Unlike the `AmberSnapshotExtension`, which groups all tests within a single test file into a singular snapshot file, this extension creates one `.raw` file per test case. Note that the default behaviour of the SingleFileSnapshotExtension is to write raw bytes to disk. There is no further "serialization" that happens. For this reason, it is not a direct replacement for the Amber extension. The `SingleFileSnapshotExtension` is mostly used as a building block for other extensions such as the image extensions as well as the JSON extension. In the default "binary" mode, attempting to serialize a non-byte-like object will throw a TypeError.
358358
- **`PNGImageSnapshotExtension`**: An extension of single file, this should be used to produce `.png` files from a byte string.
359359
- **`SVGImageSnapshotExtension`**: Another extension of single file. This produces `.svg` files from an svg string.
360360
- **`JSONSnapshotExtension`**: Another extension of single file. This produces `.json` files from dictionaries and lists.

src/syrupy/extensions/single_file.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,17 @@ def serialize(
5050
include: Optional["PropertyFilter"] = None,
5151
matcher: Optional["PropertyMatcher"] = None,
5252
) -> "SerializedData":
53-
return self.get_supported_dataclass()(data)
53+
supported_dataclass = self.get_supported_dataclass()
54+
if supported_dataclass is bytes:
55+
try:
56+
memoryview(data)
57+
except TypeError:
58+
raise TypeError(
59+
gettext(
60+
"Can't serialize '{}' to '{}'. You must convert the data first."
61+
).format(type(data).__name__, supported_dataclass.__name__)
62+
) from None
63+
return supported_dataclass(data)
5464

5565
@classmethod
5666
def get_snapshot_name(
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def test_raises_informative_type_error_when_serializing_non_bytes(testdir):
2+
testdir.makepyfile(
3+
test_file="""
4+
from syrupy.extensions.single_file import SingleFileSnapshotExtension
5+
6+
def test_case(snapshot):
7+
assert {"x": 0} == snapshot(extension_class=SingleFileSnapshotExtension)
8+
"""
9+
)
10+
11+
result = testdir.runpytest("-v")
12+
assert result.ret == 1
13+
result.stdout.re_match_lines(
14+
(r".*TypeError: Can't serialize.*You must convert the data.*",)
15+
)

0 commit comments

Comments
 (0)