@@ -175,15 +175,83 @@ def test_bytesting_path_windows_removes_magic_prefix(self):
175
175
assert outpath == "C:\\ caf\xe9 " .encode ()
176
176
177
177
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