fix(import): survive file names that are not valid utf-8
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
"""Names that are not valid UTF-8 must not break the API."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from wordarr.fsnames import decode_path, encode_path, is_broken, readable
|
||||
|
||||
# a Latin-1 "ß" in an otherwise normal name, as it comes from older rips
|
||||
BROKEN = os.fsdecode(b"Die drei ??? - Die Fu\xdfball-Falle")
|
||||
|
||||
|
||||
def test_detects_and_cleans_broken_names():
|
||||
assert is_broken(BROKEN)
|
||||
assert not is_broken("Die drei ??? - Die Fußball-Falle")
|
||||
shown = readable(BROKEN)
|
||||
assert shown.startswith("Die drei ??? - Die Fu") and "�" in shown
|
||||
# the cleaned name survives the same encoding that used to raise
|
||||
json.dumps({"name": shown}, ensure_ascii=False).encode("utf-8")
|
||||
|
||||
|
||||
def test_encoded_paths_round_trip(tmp_path):
|
||||
folder = tmp_path / BROKEN
|
||||
folder.mkdir()
|
||||
(folder / "01.mp3").write_bytes(b"x")
|
||||
|
||||
encoded = encode_path(str(folder))
|
||||
json.dumps({"path": encoded}, ensure_ascii=False).encode("utf-8") # transportable
|
||||
assert not is_broken(encoded)
|
||||
assert Path(decode_path(encoded)) == folder
|
||||
assert (Path(decode_path(encoded)) / "01.mp3").read_bytes() == b"x"
|
||||
|
||||
|
||||
def test_plain_names_are_left_alone():
|
||||
plain = "/downloads/Die drei ??? - Die Fußball-Falle"
|
||||
assert encode_path(plain) == plain
|
||||
assert decode_path(plain) == plain
|
||||
assert readable(plain) == plain
|
||||
Reference in New Issue
Block a user