1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
from __future__ import annotations
from pathlib import Path
from string import ascii_lowercase as alphabet
import pytest
from streamlink_cli.utils.path import replace_chars, replace_path, truncate_path
@pytest.mark.parametrize("char", list(range(32)))
def test_replace_chars_unprintable(char: int):
assert replace_chars(f"foo{chr(char)}{chr(char)}bar") == "foo_bar", "Replaces unprintable characters"
@pytest.mark.posix_only()
@pytest.mark.parametrize("char", "/".split())
def test_replace_chars_posix(char: str):
assert replace_chars(f"foo{char}{char}bar") == "foo_bar", "Replaces multiple unsupported characters in a row"
@pytest.mark.windows_only()
@pytest.mark.parametrize("char", '\x7f"*/:<>?\\|'.split())
def test_replace_chars_windows(char: str):
assert replace_chars(f"foo{char}{char}bar") == "foo_bar", "Replaces multiple unsupported characters in a row"
@pytest.mark.posix_only()
def test_replace_chars_posix_all():
assert replace_chars("".join(chr(i) for i in range(32)) + "/") == "_"
@pytest.mark.windows_only()
def test_replace_chars_windows_all():
assert replace_chars("".join(chr(i) for i in range(32)) + '\x7f"*/:<>?\\|') == "_"
@pytest.mark.posix_only()
def test_replace_chars_posix_override():
all_chars = "".join(chr(i) for i in range(32)) + '\x7f"*:/<>?\\|'
assert replace_chars(all_chars) == '_\x7f"*:_<>?\\|'
assert replace_chars(all_chars, "posix") == '_\x7f"*:_<>?\\|'
assert replace_chars(all_chars, "unix") == '_\x7f"*:_<>?\\|'
assert replace_chars(all_chars, "windows") == "_"
assert replace_chars(all_chars, "win32") == "_"
@pytest.mark.windows_only()
def test_replace_chars_windows_override():
all_chars = "".join(chr(i) for i in range(32)) + '\x7f"*:/<>?\\|'
assert replace_chars(all_chars) == "_"
assert replace_chars(all_chars, "posix") == '_\x7f"*:_<>?\\|'
assert replace_chars(all_chars, "unix") == '_\x7f"*:_<>?\\|'
assert replace_chars(all_chars, "windows") == "_"
assert replace_chars(all_chars, "win32") == "_"
def test_replace_chars_replacement():
assert replace_chars("\x00", None, "+") == "+"
def test_replace_path():
def mapper(s, *_):
return dict(foo=".", bar="..").get(s, s)
path = Path("foo", ".", "bar", "..", "baz")
expected = Path("_", ".", "_", "..", "baz")
assert replace_path(path, mapper) == expected, "Only replaces mapped parts which are in the special parts tuple"
@pytest.mark.posix_only()
@pytest.mark.parametrize("os_environ", [pytest.param({"HOME": "/home/foo"}, id="posix")], indirect=True)
def test_replace_path_expanduser_posix(os_environ):
assert replace_path("~/bar", lambda s, *_: s) == Path("/home/foo/bar")
assert replace_path("foo/bar", lambda s, *_: dict(foo="~").get(s, s)) == Path("~/bar")
@pytest.mark.windows_only()
@pytest.mark.parametrize("os_environ", [pytest.param({"USERPROFILE": "C:\\Users\\foo"}, id="windows")], indirect=True)
def test_replace_path_expanduser_windows(os_environ):
assert replace_path("~\\bar", lambda s, *_: s) == Path("C:\\Users\\foo\\bar")
assert replace_path("foo\\bar", lambda s, *_: dict(foo="~").get(s, s)) == Path("~\\bar")
bear = "🐻" # Unicode character: "Bear Face" (U+1F43B)
@pytest.mark.parametrize(
("args", "expected"),
[
pytest.param(
(alphabet, 255, True),
alphabet,
id="text - no truncation",
),
pytest.param(
(alphabet * 10, 255, True),
(alphabet * 10)[:255],
id="text - truncate",
),
pytest.param(
(alphabet * 10, 50, True),
(alphabet * 10)[:50],
id="text - truncate at 50",
),
pytest.param(
(f"{alphabet}.ext", 255, True),
f"{alphabet}.ext",
id="text+ext1 - no truncation",
),
pytest.param(
(f"{alphabet * 10}.ext", 255, True),
f"{(alphabet * 10)[:251]}.ext",
id="text+ext1 - truncate",
),
pytest.param(
(f"{alphabet * 10}.ext", 50, True),
f"{(alphabet * 10)[:46]}.ext",
id="text+ext1 - truncate at 50",
),
pytest.param(
(f"{alphabet * 10}.ext", 255, False),
(alphabet * 10)[:255],
id="text+ext1+nokeep - truncate",
),
pytest.param(
(f"{alphabet * 10}.ext", 50, False),
(alphabet * 10)[:50],
id="text+ext1+nokeep - truncate at 50",
),
pytest.param(
(f"{alphabet * 10}.notafilenameextension", 255, True),
(alphabet * 10)[:255],
id="text+ext2 - truncate",
),
pytest.param(
(f"{alphabet * 10}.notafilenameextension", 50, True),
(alphabet * 10)[:50],
id="text+ext2 - truncate at 50",
),
pytest.param(
(bear * 63, 255, True),
bear * 63,
id="bear - no truncation",
),
pytest.param(
(bear * 64, 255, True),
bear * 63,
id="bear - truncate",
),
pytest.param(
(bear * 64, 50, True),
bear * 12,
id="bear - truncate at 50",
),
pytest.param(
(f"{bear}.ext", 255, True),
f"{bear}.ext",
id="bear+ext1 - no truncation",
),
pytest.param(
(f"{bear * 64}.ext", 255, True),
f"{bear * 62}.ext",
id="bear+ext1 - truncate",
),
pytest.param(
(f"{bear * 64}.ext", 50, True),
f"{bear * 11}.ext",
id="bear+ext1 - truncate at 50",
),
pytest.param(
(f"{bear * 64}.ext", 255, False),
bear * 63,
id="bear+ext1+nokeep - truncate",
),
pytest.param(
(f"{bear * 64}.ext", 50, False),
bear * 12,
id="bear+ext1+nokeep - truncate at 50",
),
pytest.param(
(f"{bear * 64}.notafilenameextension", 255, True),
bear * 63,
id="bear+ext2 - truncate",
),
pytest.param(
(f"{bear * 64}.notafilenameextension", 50, True),
bear * 12,
id="bear+ext2 - truncate at 50",
),
],
)
def test_truncate_path(args: tuple[str, int, bool], expected: str):
path, length, keep_extension = args
result = truncate_path(path, length, keep_extension)
assert len(result.encode("utf-8")) <= length
assert result == expected
|