Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: expand single filename legal characters #398

Merged
merged 7 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/syrupy/extensions/single_file.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import re
from gettext import gettext
from pathlib import Path
from typing import (
Expand All @@ -7,6 +6,7 @@
Optional,
Set,
)
from unicodedata import category

from syrupy.data import (
Snapshot,
Expand Down Expand Up @@ -69,5 +69,12 @@ def _write_snapshot_fossil(self, *, snapshot_fossil: "SnapshotFossil") -> None:

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[:255] # maximum filename length
iamogbz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
orange
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.123~!@#$%^&*()/[]{}|
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a?
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
space space
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
apple
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
apple
30 changes: 30 additions & 0 deletions tests/syrupy/extensions/test_single_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand All @@ -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"