diff --git a/src/syrupy/extensions/single_file.py b/src/syrupy/extensions/single_file.py index 3f6f8388..edf64221 100644 --- a/src/syrupy/extensions/single_file.py +++ b/src/syrupy/extensions/single_file.py @@ -1,4 +1,3 @@ -import re from gettext import gettext from pathlib import Path from typing import ( @@ -7,6 +6,7 @@ Optional, Set, ) +from unicodedata import category from syrupy.data import ( Snapshot, @@ -68,6 +68,13 @@ def _write_snapshot_fossil(self, *, snapshot_fossil: "SnapshotFossil") -> None: f.write(data) def __clean_filename(self, filename: str) -> str: - filename = str(filename).strip().replace(" ", "_") max_filename_length = 255 - len(self._file_extension or "") - return re.sub(r"(?u)[^-\w.]", "", filename)[:max_filename_length] + exclude_chars = '\\/?*:|"<>' + exclude_categ = ("C",) + cleaned_filename = "".join( + c + for c in filename + if c not in exclude_chars + and not any(categ in category(c) for categ in exclude_categ) + ) + return cleaned_filename[:max_filename_length] diff --git a/tests/syrupy/extensions/__snapshots__/test_single_file/TestClass.test_class_method_parametrizedx.raw b/tests/syrupy/extensions/__snapshots__/test_single_file/TestClass.test_class_method_parametrized[x].raw similarity index 100% rename from tests/syrupy/extensions/__snapshots__/test_single_file/TestClass.test_class_method_parametrizedx.raw rename to tests/syrupy/extensions/__snapshots__/test_single_file/TestClass.test_class_method_parametrized[x].raw diff --git a/tests/syrupy/extensions/__snapshots__/test_single_file/TestClass.test_class_method_parametrizedy.raw b/tests/syrupy/extensions/__snapshots__/test_single_file/TestClass.test_class_method_parametrized[y].raw similarity index 100% rename from tests/syrupy/extensions/__snapshots__/test_single_file/TestClass.test_class_method_parametrizedy.raw rename to tests/syrupy/extensions/__snapshots__/test_single_file/TestClass.test_class_method_parametrized[y].raw diff --git a/tests/syrupy/extensions/__snapshots__/test_single_file/TestClass.test_class_method_parametrizedz.raw b/tests/syrupy/extensions/__snapshots__/test_single_file/TestClass.test_class_method_parametrized[z].raw similarity index 100% rename from tests/syrupy/extensions/__snapshots__/test_single_file/TestClass.test_class_method_parametrizedz.raw rename to tests/syrupy/extensions/__snapshots__/test_single_file/TestClass.test_class_method_parametrized[z].raw diff --git a/tests/syrupy/extensions/__snapshots__/test_single_file/test_____underscore.raw b/tests/syrupy/extensions/__snapshots__/test_single_file/test_____underscore.raw new file mode 100644 index 00000000..adb2ca6f --- /dev/null +++ b/tests/syrupy/extensions/__snapshots__/test_single_file/test_____underscore.raw @@ -0,0 +1 @@ +orange \ No newline at end of file diff --git a/tests/syrupy/extensions/__snapshots__/test_single_file/test_special_characters[.123~!@#$%^&()[]{}].raw b/tests/syrupy/extensions/__snapshots__/test_single_file/test_special_characters[.123~!@#$%^&()[]{}].raw new file mode 100644 index 00000000..42ae95ed --- /dev/null +++ b/tests/syrupy/extensions/__snapshots__/test_single_file/test_special_characters[.123~!@#$%^&()[]{}].raw @@ -0,0 +1 @@ +.123~!@#$%^&*()/[]{}| \ No newline at end of file diff --git a/tests/syrupy/extensions/__snapshots__/test_single_file/test_special_characters[].raw b/tests/syrupy/extensions/__snapshots__/test_single_file/test_special_characters[].raw new file mode 100644 index 00000000..e69de29b diff --git a/tests/syrupy/extensions/__snapshots__/test_single_file/test_special_characters[_].raw b/tests/syrupy/extensions/__snapshots__/test_single_file/test_special_characters[_].raw new file mode 100644 index 00000000..c9cdc63b --- /dev/null +++ b/tests/syrupy/extensions/__snapshots__/test_single_file/test_special_characters[_].raw @@ -0,0 +1 @@ +_ \ No newline at end of file diff --git a/tests/syrupy/extensions/__snapshots__/test_single_file/test_special_characters[a].raw b/tests/syrupy/extensions/__snapshots__/test_single_file/test_special_characters[a].raw new file mode 100644 index 00000000..621e0acd --- /dev/null +++ b/tests/syrupy/extensions/__snapshots__/test_single_file/test_special_characters[a].raw @@ -0,0 +1 @@ +a? \ No newline at end of file diff --git a/tests/syrupy/extensions/__snapshots__/test_single_file/test_special_characters[space space].raw b/tests/syrupy/extensions/__snapshots__/test_single_file/test_special_characters[space space].raw new file mode 100644 index 00000000..fce4c500 --- /dev/null +++ b/tests/syrupy/extensions/__snapshots__/test_single_file/test_special_characters[space space].raw @@ -0,0 +1 @@ +space space \ No newline at end of file diff --git a/tests/syrupy/extensions/__snapshots__/test_single_file/test_underscore.raw b/tests/syrupy/extensions/__snapshots__/test_single_file/test_underscore.raw new file mode 100644 index 00000000..0e37c889 --- /dev/null +++ b/tests/syrupy/extensions/__snapshots__/test_single_file/test_underscore.raw @@ -0,0 +1 @@ +apple \ No newline at end of file diff --git a/tests/syrupy/extensions/__snapshots__/test_single_file/test_unicode[greek u1ff4].raw b/tests/syrupy/extensions/__snapshots__/test_single_file/test_unicode[greek u1ff4].raw new file mode 100644 index 00000000..0e37c889 --- /dev/null +++ b/tests/syrupy/extensions/__snapshots__/test_single_file/test_unicode[greek u1ff4].raw @@ -0,0 +1 @@ +apple \ No newline at end of file diff --git a/tests/syrupy/extensions/test_single_file.py b/tests/syrupy/extensions/test_single_file.py index d81ca851..6aa6b58d 100644 --- a/tests/syrupy/extensions/test_single_file.py +++ b/tests/syrupy/extensions/test_single_file.py @@ -18,6 +18,16 @@ def snapshot_single(snapshot): return snapshot.use_extension(SingleFileSnapshotExtension) +class SingleFileUTF8SnapshotExtension(SingleFileSnapshotExtension): + def serialize(self, data, **kwargs) -> bytes: + return bytes(data, "utf8") + + +@pytest.fixture +def snapshot_utf8(snapshot): + return snapshot.use_extension(SingleFileUTF8SnapshotExtension) + + def test_does_not_write_non_binary(testdir, snapshot_single: "SnapshotAssertion"): snapshot_fossil = SnapshotFossil( location=str(Path(testdir.tmpdir).joinpath("snapshot_fossil.raw")), @@ -37,3 +47,23 @@ def test_class_method_name(self, snapshot_single): @pytest.mark.parametrize("content", [b"x", b"y", b"z"]) def test_class_method_parametrized(self, snapshot_single, content): assert snapshot_single == content + + +def test_underscore(snapshot_single): + assert snapshot_single == b"apple" + + +def test_____underscore(snapshot_single): + assert snapshot_single == b"orange" + + +@pytest.mark.parametrize( + "content", [b"", b"_", b"a?", b"space space", b".123~!@#$%^&*()/[]{}|"] +) +def test_special_characters(snapshot_single, content): + assert snapshot_single == content + + +@pytest.mark.parametrize("content", ["greek ῴ"]) +def test_unicode(snapshot_utf8, content): + assert snapshot_utf8 == "apple"