Skip to content

Commit 921b7ed

Browse files
committed
Rewrite legalisation tests for readability
1 parent 6a7832f commit 921b7ed

File tree

2 files changed

+24
-62
lines changed

2 files changed

+24
-62
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ select = [
263263
]
264264
[tool.ruff.lint.per-file-ignores]
265265
"beets/**" = ["PT"]
266+
"test/test_util.py" = ["E501"]
266267

267268
[tool.ruff.lint.isort]
268269
split-on-trailing-comma = false

test/test_util.py

Lines changed: 23 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -190,66 +190,27 @@ def test_truncate(self, path, expected):
190190

191191
assert util.truncate_path(path) == expected
192192

193-
@pytest.mark.parametrize(
194-
"pre_trunc_repl, post_trunc_repl, expected",
195-
[
196-
pytest.param(
197-
[],
198-
[],
199-
("_abcd", False),
200-
id="default",
201-
),
202-
pytest.param(
203-
[(re.compile(r"abcdX$"), "PRE")],
204-
[],
205-
(":PRE", False),
206-
id="valid path after initial repl",
207-
),
208-
pytest.param(
209-
[(re.compile(r"abcdX$"), "PRE_LONG")],
210-
[],
211-
(":PRE_", False),
212-
id="too long path after initial repl is truncated",
213-
),
214-
pytest.param(
215-
[],
216-
[(re.compile(r"abcdX$"), "POST")],
217-
(":POST", False),
218-
id="valid path after post-trunc repl",
219-
),
220-
pytest.param(
221-
[],
222-
[(re.compile(r"abcdX$"), "POST_LONG")],
223-
(":POST", False),
224-
id="too long path after post-trunc repl is truncated",
225-
),
226-
pytest.param(
227-
[(re.compile(r"abcdX$"), "PRE")],
228-
[(re.compile(r"PRE$"), "POST")],
229-
(":POST", False),
230-
id="both replacements within filename length limit",
231-
),
232-
pytest.param(
233-
[(re.compile(r"abcdX$"), "PRE_LONG")],
234-
[(re.compile(r"PRE_$"), "POST")],
235-
(":POST", False),
236-
id="too long initial path is truncated and valid post-trunc repl",
237-
),
238-
pytest.param(
239-
[(re.compile(r"abcdX$"), "PRE")],
240-
[(re.compile(r"PRE$"), "POST_LONG")],
241-
(":POST", False),
242-
id="valid pre-trunc repl and too long post-trunc path is truncated",
243-
),
244-
pytest.param(
245-
[(re.compile(r"abcdX$"), "PRE_LONG")],
246-
[(re.compile(r"PRE_$"), "POST_LONG")],
247-
("_PRE_", True),
248-
id="too long repl both times force default ones to be applied",
249-
),
250-
],
251-
)
252-
def test_replacements(self, pre_trunc_repl, post_trunc_repl, expected):
253-
replacements = pre_trunc_repl + post_trunc_repl
193+
_p = pytest.param
254194

255-
assert util.legalize_path(":abcdX", replacements, "") == expected
195+
@pytest.mark.parametrize(
196+
"replacements, expected_path, expected_truncated",
197+
[ # [ repl before truncation, repl after truncation ]
198+
_p([ ], "_abcd", False, id="default"),
199+
_p([(r"abcdX$", "1ST"), ], ":1ST", False, id="1st_valid"),
200+
_p([(r"abcdX$", "TOO_LONG"), ], ":TOO_", False, id="1st_truncated"),
201+
_p([(r"abcdX$", "1ST"), (r"1ST$", "2ND") ], ":2ND", False, id="both_valid"),
202+
_p([(r"abcdX$", "TOO_LONG"), (r"TOO_$", "2ND") ], ":2ND", False, id="1st_truncated_2nd_valid"),
203+
_p([(r"abcdX$", "1ST"), (r"1ST$", "TOO_LONG") ], ":TOO_", False, id="1st_valid_2nd_truncated"),
204+
# if the logic truncates the path twice, it ends up applying the default replacements
205+
_p([(r"abcdX$", "TOO_LONG"), (r"TOO_$", "TOO_LONG") ], "_TOO_", True, id="both_truncated_default_repl_applied"),
206+
]
207+
) # fmt: skip
208+
def test_replacements(
209+
self, replacements, expected_path, expected_truncated
210+
):
211+
replacements = [(re.compile(pat), repl) for pat, repl in replacements]
212+
213+
assert util.legalize_path(":abcdX", replacements, "") == (
214+
expected_path,
215+
expected_truncated,
216+
)

0 commit comments

Comments
 (0)