Skip to content

Commit 4fcb148

Browse files
committed
Add test for legalization logic
1 parent 40fbc8e commit 4fcb148

File tree

2 files changed

+80
-43
lines changed

2 files changed

+80
-43
lines changed

test/test_library.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -487,37 +487,6 @@ def test_destination_with_empty_final_component(self):
487487
self.i.path = "foo.mp3"
488488
assert self.i.destination() == np("base/one/_.mp3")
489489

490-
def test_legalize_path_one_for_one_replacement(self):
491-
# Use a replacement that should always replace the last X in any
492-
# path component with a Z.
493-
self.lib.replacements = [
494-
(re.compile(r"X$"), "Z"),
495-
]
496-
497-
# Construct an item whose untruncated path ends with a Y but whose
498-
# truncated version ends with an X.
499-
self.i.title = "X" * 300 + "Y"
500-
501-
# The final path should reflect the replacement.
502-
dest = self.i.destination()
503-
assert dest[-2:] == b"XZ"
504-
505-
def test_legalize_path_one_for_many_replacement(self):
506-
# Use a replacement that should always replace the last X in any
507-
# path component with four Zs.
508-
self.lib.replacements = [
509-
(re.compile(r"X$"), "ZZZZ"),
510-
]
511-
512-
# Construct an item whose untruncated path ends with a Y but whose
513-
# truncated version ends with an X.
514-
self.i.title = "X" * 300 + "Y"
515-
516-
# The final path should ignore the user replacement and create a path
517-
# of the correct length, containing Xs.
518-
dest = self.i.destination()
519-
assert dest[-2:] == b"XX"
520-
521490
def test_album_field_query(self):
522491
self.lib.directory = b"one"
523492
self.lib.path_formats = [("default", "two"), ("flex:foo", "three")]

test/test_util.py

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,83 @@ def test_bytesting_path_windows_removes_magic_prefix(self):
175175
assert outpath == "C:\\caf\xe9".encode()
176176

177177

178-
@patch("beets.util.get_max_filename_length", lambda: 5)
179-
@pytest.mark.parametrize(
180-
"path, expected",
181-
[
182-
("abcdeX/fgh", "abcde/fgh"),
183-
("abcde/fXX.ext", "abcde/f.ext"),
184-
("a🎹/a.ext", "a🎹/a.ext"),
185-
("ab🎹/a.ext", "ab/a.ext"),
186-
],
187-
)
188-
def test_truncate_path(path, expected):
189-
assert util.truncate_path(path) == expected
178+
class TestPathLegalization:
179+
@pytest.fixture(autouse=True)
180+
def _patch_max_filename_length(self, monkeypatch):
181+
monkeypatch.setattr("beets.util.get_max_filename_length", lambda: 5)
182+
183+
@pytest.mark.parametrize(
184+
"path, expected",
185+
[
186+
("abcdeX/fgh", "abcde/fgh"),
187+
("abcde/fXX.ext", "abcde/f.ext"),
188+
("a🎹/a.ext", "a🎹/a.ext"),
189+
("ab🎹/a.ext", "ab/a.ext"),
190+
],
191+
)
192+
def test_truncate(self, path, expected):
193+
assert util.truncate_path(path) == expected
194+
195+
@pytest.mark.parametrize(
196+
"pre_trunc_repl, post_trunc_repl, expected",
197+
[
198+
pytest.param(
199+
[],
200+
[],
201+
("_abcd", False),
202+
id="default",
203+
),
204+
pytest.param(
205+
[(re.compile(r"abcdX$"), "PRE")],
206+
[],
207+
(":PRE", False),
208+
id="valid path after initial repl",
209+
),
210+
pytest.param(
211+
[(re.compile(r"abcdX$"), "PRE_LONG")],
212+
[],
213+
(":PRE_", False),
214+
id="too long path after initial repl is truncated",
215+
),
216+
pytest.param(
217+
[],
218+
[(re.compile(r"abcdX$"), "POST")],
219+
(":POST", False),
220+
id="valid path after post-trunc repl",
221+
),
222+
pytest.param(
223+
[],
224+
[(re.compile(r"abcdX$"), "POST_LONG")],
225+
(":POST", False),
226+
id="too long path after post-trunc repl is truncated",
227+
),
228+
pytest.param(
229+
[(re.compile(r"abcdX$"), "PRE")],
230+
[(re.compile(r"PRE$"), "POST")],
231+
(":POST", False),
232+
id="both replacements within filename length limit",
233+
),
234+
pytest.param(
235+
[(re.compile(r"abcdX$"), "PRE_LONG")],
236+
[(re.compile(r"PRE_$"), "POST")],
237+
(":POST", False),
238+
id="too long initial path is truncated and valid post-trunc repl",
239+
),
240+
pytest.param(
241+
[(re.compile(r"abcdX$"), "PRE")],
242+
[(re.compile(r"PRE$"), "POST_LONG")],
243+
(":POST", False),
244+
id="valid pre-trunc repl and too long post-trunc path is truncated",
245+
),
246+
pytest.param(
247+
[(re.compile(r"abcdX$"), "PRE_LONG")],
248+
[(re.compile(r"PRE_$"), "POST_LONG")],
249+
("_PRE_", True),
250+
id="too long repl both times force default ones to be applied",
251+
),
252+
],
253+
)
254+
def test_replacements(self, pre_trunc_repl, post_trunc_repl, expected):
255+
replacements = pre_trunc_repl + post_trunc_repl
256+
257+
assert util.legalize_path(":abcdX", replacements, "") == expected

0 commit comments

Comments
 (0)