-
-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathtest_log.py
420 lines (338 loc) · 17.8 KB
/
test_log.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# -*- coding: utf-8 -*-
#
# Picard, the next-generation MusicBrainz tagger
#
# Copyright (C) 2021 Gabriel Ferreira
# Copyright (C) 2021, 2024 Philipp Wolfer
# Copyright (C) 2021-2022, 2024 Laurent Monin
# Copyright (C) 2024 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.
from collections import deque
from dataclasses import dataclass
from pathlib import (
PurePath,
PureWindowsPath,
)
import unittest
from unittest.mock import patch
from test.picardtestcase import PicardTestCase
from picard.const.sys import IS_WIN
from picard.debug_opts import DebugOpt
from picard.log import (
_calculate_bounds,
name_filter,
)
class MockLogItem:
def __init__(self, pos=0):
self.pos = pos
class MockLogItemQueue:
def __init__(self):
self._log_queue = deque(maxlen=10)
def contents(self, prev=-1):
if not self._log_queue:
return []
offset, length = _calculate_bounds(prev, self._log_queue[0].pos, self._log_queue[-1].pos, len(self._log_queue))
if offset >= 0:
return (self._log_queue[i] for i in range(offset, length))
# If offset < 0, there is a discontinuity in the queue positions
# Use a slower approach to get the new content.
else:
return (x for x in self._log_queue if x.pos > prev)
def push(self, item):
self._log_queue.append(item)
class LogQueueCommonTest(PicardTestCase):
def setUp(self):
super().setUp()
self.item_queue = MockLogItemQueue()
class LogQueueBoundsTestCase(LogQueueCommonTest):
def test_1(self):
# Common case where the item positions are within the max size of the queue
# [0,1,2,3,4,5,6,7], len = 8, maxlen = 10, offset = 0
for i in range(8):
self.item_queue.push(MockLogItem(i))
content_list = self.item_queue.contents()
self.assertListEqual([x.pos for x in content_list], list(range(0, 8)))
def test_2(self):
# Common case where the item positions are outside the max size of the queue
# Which means the positions do not match the index of items in the queue
# [5,6,7,8,9,10,11,12,13,14], len = 10, offset = len - (last - prev) = 10 - (14-7) = 3
for i in range(15):
self.item_queue.push(MockLogItem(i))
content_list = self.item_queue.contents(7) # prev value
self.assertListEqual([x.pos for x in content_list], list(range(8, 15)))
def test_3(self):
# Previous case but the previous item (2) was already removed from the queue
# So we pick the first item in the queue in its place
# [5,6,7,8,9,10,11,12,13,14], len = 10, maxlen = 10, prev = 5-1 = 4, offset = 0
for i in range(15):
self.item_queue.push(MockLogItem(i))
content_list = self.item_queue.contents(2)
self.assertListEqual([x.pos for x in content_list], list(range(5, 15)))
def test_4(self):
# In case we have only one element but use different prev values
self.item_queue.push(MockLogItem(10))
content_list = self.item_queue.contents() # prev = -1 is smaller than 10, so we update prev from -1 to 10-1 = 9
self.assertListEqual([x.pos for x in content_list], [10])
content_list = self.item_queue.contents(2) # prev = 2 is smaller than 10, so we update prev from 2 to 10-1 = 9
self.assertListEqual([x.pos for x in content_list], [10])
content_list = self.item_queue.contents(9) # prev = 9 is smaller than 10, so we update prev from 9 to 10-1 = 9
self.assertListEqual([x.pos for x in content_list], [10])
content_list = self.item_queue.contents(10) # prev = 10 is equal to 10, so we use it as is
self.assertListEqual([x.pos for x in content_list], [])
content_list = self.item_queue.contents(20) # prev = 20 is bigger than 10, so we use it as is
self.assertListEqual([x.pos for x in content_list], [])
def test_5(self):
# This shouldn't really happen, but here is a test for it
# In case of a discontinuity e.g. [4,5,11], we have len = 3, prev = 3, last_pos=11,
# which results in offset = 3 - (11-4) = -4, which is completely absurd offset, when the correct would be 0
self.item_queue.push(MockLogItem(4))
self.item_queue.push(MockLogItem(5))
self.item_queue.push(MockLogItem(11))
content_list = self.item_queue.contents(3)
self.assertListEqual([x.pos for x in content_list], [4, 5, 11])
@dataclass
class FakeRecord:
pathname: str
name: str
@unittest.skipIf(IS_WIN, "Posix test")
@patch('picard.log.picard_module_path', PurePath('/path1/path2'))
@patch('picard.log.USER_PLUGIN_DIR', PurePath('/user/picard/plugins'))
class NameFilterTestRel(PicardTestCase):
def test_1(self):
record = FakeRecord(name=None, pathname='/path1/path2/module/file.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'module/file')
def test_2(self):
record = FakeRecord(name=None, pathname='/path1/path2/module/__init__.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'module')
def test_3(self):
record = FakeRecord(name=None, pathname='/path1/path2/module/subpath/file.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'module/subpath/file')
def test_4(self):
record = FakeRecord(name=None, pathname='')
with self.assertRaises(ValueError):
name_filter(record)
def test_5(self):
record = FakeRecord(name=None, pathname='/path1/path2/__init__/module/__init__.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '__init__/module')
def test_plugin_path_long_1(self):
DebugOpt.PLUGIN_FULLPATH.enabled = True
record = FakeRecord(name=None, pathname='/user/picard/plugins/plugin.zip')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '/user/picard/plugins/plugin')
def test_plugin_path_long_2(self):
DebugOpt.PLUGIN_FULLPATH.enabled = True
record = FakeRecord(name=None, pathname='/user/picard/plugins/plugin.zip/xxx.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '/user/picard/plugins/plugin.zip/xxx')
def test_plugin_path_short_1(self):
DebugOpt.PLUGIN_FULLPATH.enabled = False
record = FakeRecord(name=None, pathname='/user/picard/plugins/plugin.zip')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'plugins/plugin')
def test_plugin_path_short_2(self):
DebugOpt.PLUGIN_FULLPATH.enabled = False
record = FakeRecord(name=None, pathname='/user/picard/plugins/plugin.zip/xxx.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'plugins/plugin.zip/xxx')
def test_plugin_path_short_3(self):
DebugOpt.PLUGIN_FULLPATH.enabled = False
record = FakeRecord(name=None, pathname='/user/picard/plugins/myplugin.zip/myplugin.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'plugins/myplugin.zip')
def test_plugin_path_short_4(self):
DebugOpt.PLUGIN_FULLPATH.enabled = False
record = FakeRecord(name=None, pathname='/user/picard/plugins/myplugin.zip/__init__.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'plugins/myplugin.zip')
@unittest.skipIf(IS_WIN, "Posix test")
@patch('picard.log.picard_module_path', PurePath('/picard'))
@patch('picard.log.USER_PLUGIN_DIR', PurePath('/user/picard/plugins/'))
class NameFilterTestAbs(PicardTestCase):
def test_1(self):
record = FakeRecord(name=None, pathname='/path/module/file.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '/path/module/file')
def test_2(self):
record = FakeRecord(name=None, pathname='/path/module/__init__.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '/path/module')
def test_3(self):
record = FakeRecord(name=None, pathname='/path/module/subpath/file.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '/path/module/subpath/file')
def test_4(self):
record = FakeRecord(name=None, pathname='')
with self.assertRaises(ValueError):
name_filter(record)
def test_plugin_path_long_1(self):
DebugOpt.PLUGIN_FULLPATH.enabled = True
record = FakeRecord(name=None, pathname='/path1/path2/plugins/plugin.zip')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '/path1/path2/plugins/plugin')
def test_plugin_path_long_2(self):
DebugOpt.PLUGIN_FULLPATH.enabled = True
record = FakeRecord(name=None, pathname='/path1/path2/plugins/plugin.zip/xxx.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '/path1/path2/plugins/plugin.zip/xxx')
def test_plugin_path_long_3(self):
DebugOpt.PLUGIN_FULLPATH.enabled = True
record = FakeRecord(name=None, pathname='/path1/path2/plugins/plugin.zip/__init__.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '/path1/path2/plugins/plugin.zip')
def test_plugin_path_short_1(self):
DebugOpt.PLUGIN_FULLPATH.enabled = False
record = FakeRecord(name=None, pathname='/path1/path2/plugins/plugin.zip')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '/path1/path2/plugins/plugin')
def test_plugin_path_short_2(self):
DebugOpt.PLUGIN_FULLPATH.enabled = False
record = FakeRecord(name=None, pathname='/path1/path2/plugins/plugin.zip/xxx.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '/path1/path2/plugins/plugin.zip/xxx')
def test_plugin_path_short_3(self):
DebugOpt.PLUGIN_FULLPATH.enabled = False
record = FakeRecord(name=None, pathname='/path1/path2/plugins/myplugin.zip/myplugin.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '/path1/path2/plugins/myplugin.zip')
def test_plugin_path_short_4(self):
DebugOpt.PLUGIN_FULLPATH.enabled = False
record = FakeRecord(name=None, pathname='/path1/path2/plugins/myplugin.zip/__init__.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '/path1/path2/plugins/myplugin.zip')
@unittest.skipIf(IS_WIN, "Posix test")
@patch('picard.log.picard_module_path', PurePath('/path1/path2/')) # incorrect, but testing anyway
@patch('picard.log.USER_PLUGIN_DIR', PurePath('/user/picard/plugins'))
class NameFilterTestEndingSlash(PicardTestCase):
def test_1(self):
record = FakeRecord(name=None, pathname='/path3/module/file.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '/path3/module/file')
@unittest.skipUnless(IS_WIN, "Windows test")
@patch('picard.log.picard_module_path', PureWindowsPath('C:\\path1\\path2'))
@patch('picard.log.USER_PLUGIN_DIR', PurePath('C:\\user\\picard\\plugins'))
class NameFilterTestRelWin(PicardTestCase):
def test_1(self):
record = FakeRecord(name=None, pathname='C:/path1/path2/module/file.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'module\\file')
def test_2(self):
record = FakeRecord(name=None, pathname='C:/path1/path2/module/__init__.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'module')
def test_3(self):
record = FakeRecord(name=None, pathname='C:/path1/path2/module/subpath/file.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'module\\subpath\\file')
def test_4(self):
record = FakeRecord(name=None, pathname='')
with self.assertRaises(ValueError):
name_filter(record)
def test_5(self):
record = FakeRecord(name=None, pathname='C:/path1/path2/__init__/module/__init__.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '__init__\\module')
def test_plugin_path_long_1(self):
DebugOpt.PLUGIN_FULLPATH.enabled = True
record = FakeRecord(name=None, pathname='C:/user/picard/plugins/path3/plugins/plugin.zip')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '\\user\\picard\\plugins\\path3\\plugins\\plugin')
def test_plugin_path_long_2(self):
DebugOpt.PLUGIN_FULLPATH.enabled = True
record = FakeRecord(name=None, pathname='C:/user/picard/plugins/path3/plugins/plugin.zip/xxx.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '\\user\\picard\\plugins\\path3\\plugins\\plugin.zip\\xxx')
def test_plugin_path_short_1(self):
DebugOpt.PLUGIN_FULLPATH.enabled = False
record = FakeRecord(name=None, pathname='C:/user/picard/plugins/path3/plugins/plugin.zip')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'plugins\\path3\\plugins\\plugin')
def test_plugin_path_short_2(self):
DebugOpt.PLUGIN_FULLPATH.enabled = False
record = FakeRecord(name=None, pathname='C:/user/picard/plugins/path3/plugins/plugin.zip/xxx.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'plugins\\path3\\plugins\\plugin.zip\\xxx')
def test_plugin_path_short_3(self):
DebugOpt.PLUGIN_FULLPATH.enabled = False
record = FakeRecord(name=None, pathname='C:/user/picard/plugins/path3/plugins/myplugin.zip/myplugin.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'plugins\\path3\\plugins\\myplugin.zip')
def test_plugin_path_short_4(self):
DebugOpt.PLUGIN_FULLPATH.enabled = False
record = FakeRecord(name=None, pathname='C:/user/picard/plugins/path3/plugins/myplugin.zip/__init__.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'plugins\\path3\\plugins\\myplugin.zip')
@unittest.skipUnless(IS_WIN, "Windows test")
@patch('picard.log.picard_module_path', PureWindowsPath('C:\\picard'))
@patch('picard.log.USER_PLUGIN_DIR', PurePath('C:\\user\\picard\\plugins'))
class NameFilterTestAbsWin(PicardTestCase):
def test_1(self):
record = FakeRecord(name=None, pathname='C:/path/module/file.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '\\path\\module\\file')
def test_2(self):
record = FakeRecord(name=None, pathname='C:/path/module/__init__.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '\\path\\module')
def test_3(self):
record = FakeRecord(name=None, pathname='C:/path/module/subpath/file.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '\\path\\module\\subpath\\file')
def test_4(self):
record = FakeRecord(name=None, pathname='')
with self.assertRaises(ValueError):
name_filter(record)
def test_plugin_path_long_1(self):
DebugOpt.PLUGIN_FULLPATH.enabled = True
record = FakeRecord(name=None, pathname='C:/path1/path2/plugins/plugin.zip')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '\\path1\\path2\\plugins\\plugin')
def test_plugin_path_long_2(self):
DebugOpt.PLUGIN_FULLPATH.enabled = True
record = FakeRecord(name=None, pathname='C:/path1/path2/plugins/plugin.zip/xxx.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '\\path1\\path2\\plugins\\plugin.zip\\xxx')
def test_plugin_path_short_1(self):
DebugOpt.PLUGIN_FULLPATH.enabled = False
record = FakeRecord(name=None, pathname='C:/path1/path2/plugins/plugin.zip')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '\\path1\\path2\\plugins\\plugin')
def test_plugin_path_short_2(self):
DebugOpt.PLUGIN_FULLPATH.enabled = False
record = FakeRecord(name=None, pathname='C:/path1/path2/plugins/plugin.zip/xxx.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '\\path1\\path2\\plugins\\plugin.zip\\xxx')
def test_plugin_path_short_3(self):
DebugOpt.PLUGIN_FULLPATH.enabled = False
record = FakeRecord(name=None, pathname='C:/path1/path2/plugins/myplugin.zip/myplugin.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '\\path1\\path2\\plugins\\myplugin.zip')
def test_plugin_path_short_4(self):
DebugOpt.PLUGIN_FULLPATH.enabled = False
record = FakeRecord(name=None, pathname='C:/path1/path2/plugins/myplugin.zip/__init__.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '\\path1\\path2\\plugins\\myplugin.zip')
@unittest.skipUnless(IS_WIN, "Windows test")
@patch('picard.log.picard_module_path', PureWindowsPath('C:\\path1\\path2\\')) # incorrect, but testing anyway
@patch('picard.log.USER_PLUGIN_DIR', PurePath('C:\\user\\picard\\plugins'))
class NameFilterTestEndingSlashWin(PicardTestCase):
def test_1(self):
record = FakeRecord(name=None, pathname='C:/path3/module/file.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, '\\path3\\module\\file')