-
-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathtest_util_filenaming.py
324 lines (265 loc) · 13.3 KB
/
test_util_filenaming.py
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# -*- coding: utf-8 -*-
#
# Picard, the next-generation MusicBrainz tagger
#
# Copyright (C) 2013-2014 Ionuț Ciocîrlan
# Copyright (C) 2016 Sambhav Kothari
# Copyright (C) 2018 Wieland Hoffmann
# Copyright (C) 2018-2022 Laurent Monin
# Copyright (C) 2019-2022 Philipp Wolfer
# Copyright (C) 2022 Bob Swift
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import os
import os.path
import sys
from tempfile import (
NamedTemporaryFile,
TemporaryDirectory,
)
import unittest
from test.picardtestcase import PicardTestCase
from picard.const.sys import (
IS_MACOS,
IS_WIN,
)
from picard.util.filenaming import (
ShortenMode,
WinPathTooLong,
get_available_filename,
make_save_path,
make_short_filename,
move_ensure_casing,
replace_extension,
samefile_different_casing,
shorten_filename,
shorten_path,
)
class ShortFilenameTest(PicardTestCase):
def __init__(self, *args, **kwargs):
self.maxDiff = None
self.root = os.path.join(IS_WIN and "X:\\" or "/", "x" * 10)
if IS_WIN:
self.max_len = 255
else:
self.max_len = os.statvfs("/").f_namemax
super().__init__(*args, **kwargs)
@unittest.skipUnless(IS_WIN or IS_MACOS, "windows / macOS test")
def test_bmp_unicode_on_unicode_fs(self):
char = "\N{LATIN SMALL LETTER SHARP S}"
fn = make_short_filename(self.root, os.path.join(*[char * 120] * 2))
self.assertEqual(fn, os.path.join(*[char * 120] * 2))
@unittest.skipIf(IS_WIN or IS_MACOS, "non-windows, non-osx test")
def test_bmp_unicode_on_nix(self):
char = "\N{LATIN SMALL LETTER SHARP S}"
max_len = self.max_len
divisor = len(char.encode(sys.getfilesystemencoding()))
fn = make_short_filename(self.root, os.path.join(*[char * 200] * 2))
self.assertEqual(fn, os.path.join(*[char * (max_len // divisor)] * 2))
@unittest.skipUnless(IS_MACOS, "macOS test")
def test_precomposed_unicode_on_osx(self):
char = "\N{LATIN SMALL LETTER A WITH BREVE}"
max_len = self.max_len
fn = make_short_filename(self.root, os.path.join(*[char * 200] * 2))
self.assertEqual(fn, os.path.join(*[char * (max_len // 2)] * 2))
@unittest.skipUnless(IS_WIN, "windows test")
def test_nonbmp_unicode_on_windows(self):
char = "\N{MUSICAL SYMBOL G CLEF}"
remaining = 259 - (3 + 10 + 1 + 200 + 1)
fn = make_short_filename(self.root, os.path.join(*[char * 100] * 2), win_shorten_path=True)
self.assertEqual(fn, os.path.join(char * 100, char * (remaining // 2)))
@unittest.skipUnless(IS_MACOS, "macOS test")
def test_nonbmp_unicode_on_osx(self):
char = "\N{MUSICAL SYMBOL G CLEF}"
max_len = self.max_len
fn = make_short_filename(self.root, os.path.join(*[char * 200] * 2))
self.assertEqual(fn, os.path.join(*[char * (max_len // 2)] * 2))
@unittest.skipIf(IS_WIN or IS_MACOS, "non-windows, non-osx test")
def test_nonbmp_unicode_on_nix(self):
char = "\N{MUSICAL SYMBOL G CLEF}"
max_len = self.max_len
divisor = len(char.encode(sys.getfilesystemencoding()))
fn = make_short_filename(self.root, os.path.join(*[char * 100] * 2))
self.assertEqual(fn, os.path.join(*[char * (max_len // divisor)] * 2))
@unittest.skipIf(IS_WIN or IS_MACOS, "non-windows, non-osx test")
def test_nonbmp_unicode_on_nix_with_windows_compat(self):
char = "\N{MUSICAL SYMBOL G CLEF}"
max_len = self.max_len
remaining = 259 - (3 + 10 + 1 + 200 + 1)
divisor = len(char.encode(sys.getfilesystemencoding()))
fn = make_short_filename(self.root, os.path.join(*[char * 100] * 2), win_shorten_path=True)
self.assertEqual(fn, os.path.join(char * (max_len // divisor), char * (remaining // 2)))
def test_windows_shortening(self):
fn = make_short_filename(self.root, os.path.join("a" * 200, "b" * 200, "c" * 200 + ".ext"), win_shorten_path=True)
self.assertEqual(fn, os.path.join("a" * 116, "b" * 116, "c" * 7 + ".ext"))
@unittest.skipIf(IS_WIN, "non-windows test")
def test_windows_shortening_with_ancestor_on_nix(self):
root = os.path.join(self.root, "w" * 10, "x" * 10, "y" * 9, "z" * 9)
fn = make_short_filename(
root, os.path.join("b" * 200, "c" * 200, "d" * 200 + ".ext"),
win_shorten_path=True, relative_to=self.root)
self.assertEqual(fn, os.path.join("b" * 100, "c" * 100, "d" * 7 + ".ext"))
def test_windows_node_maxlength_shortening(self):
max_len = 226
remaining = 259 - (3 + 10 + 1 + max_len + 1)
fn = make_short_filename(self.root, os.path.join("a" * 300, "b" * 100 + ".ext"), win_shorten_path=True)
self.assertEqual(fn, os.path.join("a" * max_len, "b" * (remaining - 4) + ".ext"))
def test_windows_selective_shortening(self):
root = self.root + "x" * (44 - 10 - 3)
fn = make_short_filename(root, os.path.join(
os.path.join(*["a" * 9] * 10 + ["b" * 15] * 10), "c" * 10), win_shorten_path=True)
self.assertEqual(fn, os.path.join(os.path.join(*["a" * 9] * 10 + ["b" * 9] * 10), "c" * 10))
def test_windows_shortening_not_needed(self):
root = self.root + "x" * 33
fn = make_short_filename(root, os.path.join(
os.path.join(*["a" * 9] * 20), "b" * 10), win_shorten_path=True)
self.assertEqual(fn, os.path.join(os.path.join(*["a" * 9] * 20), "b" * 10))
def test_windows_path_too_long(self):
root = self.root + "x" * 230
self.assertRaises(WinPathTooLong, make_short_filename,
root, os.path.join("a", "b", "c", "d"), win_shorten_path=True)
@unittest.skipUnless(IS_WIN, "windows test")
def test_windows_long_path_allowed(self):
char = "Ä"
path = os.path.join(*[char * self.max_len] * 3)
fn = make_short_filename(self.root, path, win_shorten_path=False)
self.assertEqual(fn, path)
@unittest.skipUnless(IS_WIN, "windows test")
def test_windows_long_path_node_limit(self):
char = "Ä"
path = os.path.join(*[char * (self.max_len + 1)] * 3)
fn = make_short_filename(self.root, path, win_shorten_path=False)
expected_path = os.path.join(*[char * self.max_len] * 3)
self.assertEqual(fn, expected_path)
def test_windows_path_not_too_long(self):
root = self.root + "x" * 230
fn = make_short_filename(root, os.path.join("a", "b", "c"), win_shorten_path=True)
self.assertEqual(fn, os.path.join("a", "b", "c"))
def test_whitespace(self):
fn = make_short_filename(self.root, os.path.join("a1234567890 ", " b1234567890 "))
self.assertEqual(fn, os.path.join("a1234567890", "b1234567890"))
class SamefileDifferentCasingTest(PicardTestCase):
@unittest.skipUnless(IS_WIN, "windows test")
def test_samefile_different_casing(self):
with NamedTemporaryFile(prefix='Foo') as f:
real_name = f.name
lower_name = real_name.lower()
self.assertFalse(samefile_different_casing(real_name, real_name))
self.assertFalse(samefile_different_casing(lower_name, lower_name))
self.assertTrue(samefile_different_casing(real_name, lower_name))
def test_samefile_different_casing_non_existant_file(self):
self.assertFalse(samefile_different_casing("/foo/bar", "/foo/BAR"))
def test_samefile_different_casing_identical_path(self):
self.assertFalse(samefile_different_casing("/foo/BAR", "/foo/BAR"))
class MoveEnsureCasingTest(PicardTestCase):
def test_move_ensure_casing(self):
with TemporaryDirectory() as d:
file_path = os.path.join(d, 'foo')
target_path = os.path.join(d, 'FOO')
open(file_path, 'a').close()
move_ensure_casing(file_path, target_path)
files = os.listdir(d)
self.assertIn('FOO', files)
def test_move_same_file(self):
# Having the same source and target path should do nothing.
# Just make sure the operation completes and nothing got raised.
path = '/foo/bar'
move_ensure_casing(path, path)
class MakeSavePathTest(PicardTestCase):
def test_replace_trailing_dots(self):
path = 'foo./bar.'
self.assertEqual(path, make_save_path(path))
self.assertEqual('foo_/bar_', make_save_path(path, win_compat=True))
def test_replace_leading_dots(self):
path = '.foo/.bar'
self.assertEqual('_foo/_bar', make_save_path(path))
def test_decompose_precomposed_chars(self):
path = 'foo/\u00E9bar' # é
self.assertEqual('foo/\u0065\u0301bar', make_save_path(path, mac_compat=True))
def test_remove_zero_length_space(self):
path = 'foo/\u200Bbar'
self.assertEqual('foo/bar', make_save_path(path))
class GetAvailableFilenameTest(PicardTestCase):
def _add_number(self, filename, number):
name, ext = os.path.splitext(filename)
return '%s (%i)%s' % (name, number, ext)
def test_append_number(self):
with NamedTemporaryFile(prefix='foo', suffix='.mp3') as f:
new_filename = get_available_filename(f.name)
self.assertEqual(self._add_number(f.name, 1), new_filename)
def test_do_not_append_number_on_same_file(self):
with NamedTemporaryFile(prefix='foo', suffix='.mp3') as f:
new_filename = get_available_filename(f.name, f.name)
self.assertEqual(f.name, new_filename)
def test_handle_non_existant_old_path(self):
with NamedTemporaryFile(prefix='foo', suffix='.mp3') as f:
new_filename = get_available_filename(f.name, '/foo/old.mp3')
self.assertEqual(self._add_number(f.name, 1), new_filename)
def test_append_additional_numbers(self):
with TemporaryDirectory() as d:
expected_number = 3
oldname = os.path.join(d, 'bar.mp3')
open(oldname, 'a').close()
filename = os.path.join(d, 'foo.mp3')
open(filename, 'a').close()
for i in range(1, expected_number):
open(self._add_number(filename, i), 'a').close()
new_filename = get_available_filename(filename, oldname)
self.assertEqual(self._add_number(filename, expected_number), new_filename)
def test_reuse_existing_number(self):
with TemporaryDirectory() as d:
expected_number = 2
filename = os.path.join(d, 'foo.mp3')
open(filename, 'a').close()
for i in range(1, 3):
open(self._add_number(filename, i), 'a').close()
oldname = self._add_number(filename, expected_number)
new_filename = get_available_filename(filename, oldname)
self.assertEqual(self._add_number(filename, expected_number), new_filename)
class ReplaceExtensionTest(PicardTestCase):
def test_replace(self):
self.assertEqual('foo/bar.wvc', replace_extension('foo/bar.wv', '.wvc'))
self.assertEqual('foo/bar.wvc', replace_extension('foo/bar.wv', 'wvc'))
self.assertEqual('foo/bar.wvc', replace_extension('foo/bar', 'wvc'))
class ShortenFilenameTest(PicardTestCase):
def test_shorten_bytes(self):
self.assertEqual(b'a' * 10, shorten_filename(b'a' * 11, 10, None))
self.assertEqual('a' * 10, shorten_filename('a' * 10, 10, ShortenMode.BYTES))
@unittest.skipUnless(
os.path.supports_unicode_filenames and not IS_MACOS,
'for filesystem with Unicode support',
)
def test_shorten_bytes_fs_unicode_support(self):
self.assertEqual('ä' * 10, shorten_filename('ä' * 11, 10, ShortenMode.BYTES))
@unittest.skipIf(
os.path.supports_unicode_filenames and not IS_MACOS,
'for filesystem without Unicode support',
)
def test_shorten_bytes_fs_no_unicode_support(self):
self.assertEqual('ä' * 5, shorten_filename('ä' * 11, 10, ShortenMode.BYTES))
self.assertEqual('ä' * 2, shorten_filename('ä' * 6, 5, ShortenMode.BYTES))
def test_shorten_filename_unicode(self):
self.assertEqual('ä' * 10, shorten_filename('ä' * 11, 10, ShortenMode.UTF16))
# In NFD mode, each "ä" is represented by two code points. Hence the shortened
# string is only 5 characters long.
self.assertEqual('ä' * 5, shorten_filename('ä' * 11, 10, ShortenMode.UTF16_NFD))
class ShortenPathTest(PicardTestCase):
def test_shorten_path(self):
self.assertEqual(
os.path.join('aaaaa', 'bbbbb', 'c.mp3'),
shorten_path(os.path.join('a' * 6, 'b' * 6, 'cccccc.mp3'), 5, ShortenMode.BYTES))
self.assertEqual(
os.path.join('ä' * 255, 'ö' * 255, 'ü' * 251 + '.ext'),
shorten_path(os.path.join('ä' * 256, 'ö' * 256, 'ü' * 256 + '.ext'), 255, ShortenMode.UTF16))