-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_choose_next.py
578 lines (502 loc) · 27.7 KB
/
test_choose_next.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# -*- coding: utf-8 -*-
# Copyright (C) 2010-2021 Johannes Weißl
# License GPLv3+:
# GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
# This is free software: you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.
"""Test module for choose_next.py."""
from __future__ import print_function
import sys
import os
import re
import unittest
import tempfile
import shutil
from io import StringIO
import subprocess
import stat
import multiprocessing
import choose_next
def mkdir_p(path):
"""Like os.makedirs(), but ignores existing directories."""
try:
os.makedirs(path)
except OSError:
if not os.path.isdir(path):
raise
def fake_sys_exit(arg=0):
"""Raise exception instead of exiting, for testing."""
raise Exception('sys.exit({!r})'.format(arg))
def choose_next_main(*args):
"""Call choose_next.py main function, return captured standard output."""
original_stdout = sys.stdout
stdout_buffer = StringIO()
sys.stdout = stdout_buffer
try:
choose_next.main_throws(list(args))
return stdout_buffer.getvalue().replace(os.linesep, '\n')
finally:
sys.stdout = original_stdout
def choose_next_external(*args):
"""Call choose_next.py as external process."""
here = os.path.abspath(os.path.dirname(__file__))
prog = os.path.join(here, 'choose_next.py')
shell = False
if os.name == 'nt':
shell = True
return subprocess.check_output([prog] + list(args), stderr=subprocess.STDOUT, shell=shell,
universal_newlines=True)
def put_file(dirname, filename):
"""Put file into directory and return absolute path."""
path = os.path.realpath(os.path.join(dirname, filename))
mkdir_p(os.path.dirname(path))
with open(path, 'w') as stream:
stream.write(filename)
return path
def put_files(dirname, *filenames):
"""Put files into directory and return absolute paths."""
return [put_file(dirname, filename) for filename in filenames]
class ChooseNextTestCase(unittest.TestCase):
# pylint: disable=too-many-public-methods
# pylint: disable=deprecated-method
"""Main choose_next.py test class."""
@classmethod
def setUpClass(cls):
"""Add renamed member functions for Python 2.7."""
if sys.version_info < (3, 2):
cls.assertRegex = cls.assertRegexpMatches
cls.assertRaisesRegex = cls.assertRaisesRegexp
def setUp(self):
"""Create temporary directory."""
self.tmpdir = os.path.realpath(tempfile.mkdtemp())
self.logdir = os.path.realpath(tempfile.mkdtemp())
os.environ['CHOOSE_NEXT_LOGDIR'] = self.logdir
def tearDown(self):
"""Remove temporary directory."""
del os.environ['CHOOSE_NEXT_LOGDIR']
shutil.rmtree(self.logdir)
shutil.rmtree(self.tmpdir)
def put_file(self, filename):
"""Put file into the temporary directory and return absolute path."""
return put_file(self.tmpdir, filename)
def put_files(self, *filenames):
"""Put files into the temporary directory and return absolute paths."""
return [self.put_file(filename) for filename in filenames]
def put_dir(self, dirname):
"""Put directory into the temporary directory and return absolute path."""
path = os.path.realpath(os.path.join(self.tmpdir, dirname))
mkdir_p(path)
return path
def put_dirs(self, *dirnames):
"""Put directories into the temporary directory and return absolute paths."""
return [self.put_dir(dirname) for dirname in dirnames]
def test_mkdir_p(self):
"""Test internal function for coverage."""
file1, file2 = self.put_files('a/b', 'a/c')
dir1, dir2 = self.put_dirs('c', 'c/d')
self.assertTrue(os.path.isfile(file1))
self.assertTrue(os.path.isfile(file2))
self.assertTrue(os.path.isdir(dir1))
self.assertTrue(os.path.isdir(dir2))
with self.assertRaises(OSError):
self.put_dirs('a/b')
def test_basic(self):
"""Test basic functionality."""
file_a, file_b, file_c = self.put_files('a', 'b', 'c')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
def test_empty(self):
"""Raise error if called on empty directory."""
with self.assertRaisesRegex(choose_next.Error, 'no files available'):
choose_next_main(self.tmpdir)
def test_main(self):
"""Test main function for coverage."""
original_sys_exit = sys.exit
sys.exit = fake_sys_exit
with self.assertRaisesRegex(Exception, r'^sys.exit\(0\)$'):
choose_next.main(['--help'])
sys.exit = original_sys_exit
def test_main_error(self):
"""Test main function error for coverage."""
original_sys_exit = sys.exit
sys.exit = fake_sys_exit
with self.assertRaisesRegex(Exception, r'^sys.exit\(1\)$'):
choose_next.main([os.path.join(self.tmpdir, 'nonexist')])
sys.exit = original_sys_exit
def test_nodirectory_specified(self):
"""Raise error if directory is not specified."""
original_sys_exit = sys.exit
sys.exit = fake_sys_exit
with self.assertRaisesRegex(Exception, r'^sys.exit\(2\)$'):
choose_next_main()
sys.exit = original_sys_exit
def test_nonexisting(self):
"""Raise error if directory does not exist."""
with self.assertRaisesRegex(choose_next.Error, 'error listing'):
choose_next_main(os.path.join(self.tmpdir, 'nonexist'))
def test_nodirectory(self):
"""Raise error if path is no directory."""
self.put_files('nodirectory')
with self.assertRaisesRegex(choose_next.Error, 'error listing'):
choose_next_main(os.path.join(self.tmpdir, 'nodirectory'))
def test_help(self):
"""Check that '-h' and '--help' options work."""
help_output1 = choose_next_external('-h')
help_output2 = choose_next_external('--help')
self.assertRegex(help_output1, '^usage: choose_next')
self.assertEqual(help_output1, help_output2)
def test_version(self):
"""Check that the '--version' option works."""
here = os.path.abspath(os.path.dirname(__file__))
setup_prog = os.path.join(here, 'setup.py')
version = subprocess.check_output(['python', setup_prog, '--version'],
universal_newlines=True)
version_output = choose_next_external('--version')
self.assertRegex(version_output, '^choose_next.py ' + re.escape(version))
def test_dump(self):
"""Check that the '--dump' option works."""
file_a, file_b = self.put_files('a', 'b')
self.assertEqual('', choose_next_main(self.tmpdir, '--dump'))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual('a\n', choose_next_main(self.tmpdir, '--dump'))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
self.assertEqual('a\nb\n', choose_next_main(self.tmpdir, '--dump'))
def test_dump0(self):
"""Check that the '--dump0' option works."""
file_a, file_b = self.put_files('a', 'b')
self.assertEqual('', choose_next_main(self.tmpdir, '--dump0'))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual('a\0', choose_next_main(self.tmpdir, '--dump0'))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
self.assertEqual('a\0b\0', choose_next_main(self.tmpdir, '--dump0'))
def test_quiet(self):
"""Check that '-q' and '--quiet' options work."""
self.put_files('a', 'b')
self.assertEqual('', choose_next_main(self.tmpdir, '-q'))
self.assertEqual('', choose_next_main(self.tmpdir, '--quiet'))
self.assertEqual('a\nb\n', choose_next_main(self.tmpdir, '--dump'))
def test_verbose(self):
"""Check that '-v' and '--verbose' options work."""
file_a, file_b = self.put_files('a', 'b')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '--verbose'))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir, '-v', '-v', '-v', '-v'))
def test_command(self):
"""Check that '-c' and '--command' options work."""
file_a1, file_a2 = self.put_files('a1 x', "a2'x")
pysed = (
'python -c "'
'from sys import argv as a;'
's = open(a[1]).read();'
"open(a[1], 'w').write(s.replace('a', 'b'))"
'"'
)
self.assertEqual(file_a1 + '\n', choose_next_main(self.tmpdir, '-c', pysed))
self.assertEqual(file_a2 + '\n', choose_next_main(self.tmpdir, '--command', pysed))
with open(file_a1) as stream:
self.assertEqual('b1 x', stream.read())
with open(file_a2) as stream:
self.assertEqual("b2'x", stream.read())
with self.assertRaisesRegex(choose_next.Error, 'command failed'):
choose_next_main(self.tmpdir, '-c' 'python -c "exit(1)"')
def test_no_write(self):
"""Check that '-w' and '--no-write' options work."""
file_a, file_b = self.put_files('a', 'b')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '-w'))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '--no-write'))
self.assertEqual('', choose_next_main(self.tmpdir, '--dump'))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
def test_no_read(self):
"""Check that '-i' and '--no-read' options work."""
file_a, file_b = self.put_files('a', 'b')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '-i'))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '--no-read'))
self.assertEqual('a\n', choose_next_main(self.tmpdir, '--dump'))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
def test_last(self):
"""Check that '-l' and '--last' options work."""
file_a, file_b, file_c = self.put_files('a', 'b', 'c')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir, '-l'))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir, '--last'))
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir))
def test_clear(self):
"""Check that the '--clear' option works."""
file_a, file_b, _unused = self.put_files('a', 'b', 'c')
choose_next_main(self.tmpdir, '--clear')
self.assertEqual('', choose_next_main(self.tmpdir, '--dump'))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
self.assertEqual('a\nb\n', choose_next_main(self.tmpdir, '--dump'))
choose_next_main(self.tmpdir, '--clear')
self.assertEqual('', choose_next_main(self.tmpdir, '--dump'))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
def test_clear_error(self):
"""Check that the '--clear' option can handle errors."""
file_a, _unused, _unused = self.put_files('a', 'b', 'c')
logdir = tempfile.mkdtemp()
logfile = os.path.join(logdir, 'logfile')
choose_next_main(self.tmpdir, '--logfile', logfile, '--clear')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '--logfile', logfile))
os.chmod(logfile, stat.S_IREAD)
os.chmod(logdir, stat.S_IREAD)
with self.assertRaisesRegex(choose_next.Error, 'error removing logfile'):
choose_next_main(self.tmpdir, '--logfile', logfile, '--clear')
os.chmod(logdir, stat.S_IWRITE | stat.S_IEXEC | stat.S_IREAD)
os.chmod(logfile, stat.S_IWRITE | stat.S_IREAD)
shutil.rmtree(logdir)
def test_clear_last(self):
"""Check that the '--clear-last' option works."""
file_a, file_b, _unused = self.put_files('a', 'b', 'c')
choose_next_main(self.tmpdir, '--clear-last')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
choose_next_main(self.tmpdir, '--clear-last')
self.assertEqual('a\n', choose_next_main(self.tmpdir, '--dump'))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
def test_clear_first(self):
"""Check that the '--clear-first' option works."""
file_a, file_b, file_c = self.put_files('a', 'b', 'c')
choose_next_main(self.tmpdir, '--clear-first')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
choose_next_main(self.tmpdir, '--clear-first')
self.assertEqual('b\n', choose_next_main(self.tmpdir, '--dump'))
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir))
def test_recursive_default(self):
"""Check that recursive directory reading works by default."""
file_a, file_b, file_c, file_d = self.put_files('x/y/a', 'x/y/b', 'y/c', 'z')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_d + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
def test_no_recursive(self):
"""Check that '-N' and '--no-recursive' options work."""
_unused, _unused, _unused, file_d = self.put_files('x/y/a', 'x/y/b', 'y/c', 'z')
self.assertEqual(file_d + '\n', choose_next_main(self.tmpdir, '-N'))
self.assertEqual(file_d + '\n', choose_next_main(self.tmpdir, '--no-recursive'))
def test_recursive(self):
"""Check that '-R' and '--recursive' options work."""
file_a, file_b, file_c, file_d = self.put_files('x/y/a', 'x/y/b', 'y/c', 'z')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '-N', '--recursive'))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir, '-N', '-R'))
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir, '--recursive'))
self.assertEqual(file_d + '\n', choose_next_main(self.tmpdir, '-R'))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '-R'))
def test_prepend(self):
"""Check that '-p' and '--prepend' options work."""
file_a, file_b, file_c = self.put_files('a', 'b', 'c')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '-p'))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir, '-p'))
self.assertEqual('b\na\n', choose_next_main(self.tmpdir, '--dump'))
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir, '--prepend'))
self.assertEqual('c\nb\na\n', choose_next_main(self.tmpdir, '--dump'))
def test_number(self):
"""Check that '-n' and '--number' options work."""
file_a, file_b = self.put_files('a', 'b')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
choose_next_main(self.tmpdir, '--clear')
self.assertEqual('', choose_next_main(self.tmpdir, '-n', '0'))
choose_next_main(self.tmpdir, '--clear')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '-n', '1'))
choose_next_main(self.tmpdir, '--clear')
self.assertEqual(file_a + '\n' + file_b + '\n', choose_next_main(self.tmpdir, '-n', '2'))
choose_next_main(self.tmpdir, '--clear')
self.assertEqual(file_a + '\n' + file_b + '\n' + file_a + '\n',
choose_next_main(self.tmpdir, '-n', '3'))
choose_next_main(self.tmpdir, '--clear')
self.assertEqual(file_a + '\n' + file_b + '\n',
choose_next_main(self.tmpdir, file_a, file_b))
choose_next_main(self.tmpdir, '--clear')
self.assertEqual(file_a + '\n', choose_next_main('-n', '1', self.tmpdir, file_a, file_b))
choose_next_main(self.tmpdir, '--clear')
process = multiprocessing.Process(target=choose_next_main, args=(self.tmpdir, '-n', '-1'))
process.start()
process.join(timeout=3)
self.assertTrue(process.is_alive())
process.terminate()
process.join()
def test_include_directories(self):
"""Check that '-d' and '--include-directories' options work."""
file_b, file_c = self.put_dirs('b', 'c')
file_a, file_bb = self.put_files('a', 'b/b')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '-d'))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir, '--include-directories'))
self.assertEqual(file_bb + '\n', choose_next_main(self.tmpdir, '-d'))
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir, '-d'))
def test_exclude(self):
"""Check that the '--exclude' option works."""
file_a, file_b = self.put_files('a.mp3', 'b.wav')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '--exclude', '*.ogg'))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir, '--exclude', '*.mp3'))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir, '--exclude', '*.mp3'))
def test_include(self):
"""Check that the '--include' option works."""
file_a, file_b = self.put_files('a.mp3', 'b.mp3')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '--include', '*.mp3'))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir, '--include', '*.wav'))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir,
'--exclude', '*.mp3',
'--include', '*/b.*'))
def test_files(self):
"""Check that filename arguments work."""
file_a, file_b, file_c = self.put_files('a', 'b', 'c')
file_nonexist = os.path.join(self.tmpdir, 'x')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, file_a))
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir, file_c))
self.assertEqual('a\nc\n', choose_next_main(self.tmpdir, '--dump'))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_nonexist + '\n', choose_next_main(self.tmpdir, file_nonexist))
with self.assertRaisesRegex(choose_next.Error, 'leads outside given directory'):
choose_next_main(self.tmpdir, os.path.join(self.logdir, 'outside'))
def test_repeat(self):
"""Check that repeating works even if no remaining files follow."""
file_a, file_b, file_c = self.put_files('a', 'b', 'c')
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir, file_c))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir, file_b))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
def test_random(self):
"""Check that '-r' and '--random' options work."""
names = ['{:02d}'.format(i) for i in range(0, 50)]
files = self.put_files(*names)
def random_files(*args):
"""Choose files in random fashion."""
return [choose_next_main(self.tmpdir, '-r', *args).rstrip('\n') for _name in files]
self.assertEqual(files, sorted(random_files()))
self.assertNotEqual(random_files(), random_files()) # very unlikely to fail
def test_numeric_sort(self):
"""Check that files are sorted numerically."""
file_a, file_b, file_c = self.put_files('2/3 e', '2/20 e', '10/5 e')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir))
def test_logfile(self):
"""Check that '-L' and '--logfile' options work."""
logdir = tempfile.mkdtemp()
logfile = os.path.join(logdir, 'logfile')
file_a, file_b, _unused = self.put_files('a', 'b', 'c')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '-L', logfile))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir, '--logfile', logfile))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
with self.assertRaisesRegex(choose_next.Error, 'error reading logfile'):
choose_next_main(self.tmpdir, '--logfile', self.tmpdir)
entries = choose_next.read_logfile(logfile, self.tmpdir)
choose_next.write_logfile(logfile, entries + [os.path.join(self.logdir, 'outside')])
with self.assertRaisesRegex(choose_next.Error, 'leads outside given directory'):
choose_next_main(self.tmpdir, '--logfile', logfile)
shutil.rmtree(logdir)
def test_nonexist_logdir(self):
"""Check non-existing logdir for coverage."""
shutil.rmtree(self.logdir)
file_a, file_b = self.put_files('a', 'b')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
def test_nonexist_logdir_error(self):
"""Check handling of error when creating logdir for coverage."""
shutil.rmtree(self.logdir)
with open(self.logdir, 'w') as stream:
stream.write('logdir\n')
self.put_files('a', 'b')
with self.assertRaisesRegex(choose_next.Error, 'error creating logdir'):
choose_next_main(self.tmpdir)
os.unlink(self.logdir)
os.makedirs(self.logdir)
def test_hidden(self):
"""Check that hidden files are not chosen."""
file_a, _unused, file_c = self.put_files('a', '.b', 'c')
_unused, file_e = self.put_dirs('.d', 'e')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '-d'))
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir, '-d'))
self.assertEqual(file_e + '\n', choose_next_main(self.tmpdir, '-d'))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '-d'))
def test_hidden_recursive(self):
"""Check that hidden files are not chosen in recursive mode."""
file_a, _unused, _unused = self.put_files('x/y/a', 'x/.y/b', 'x/y/.c')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '-r'))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '-r'))
def test_multibyte_error(self):
"""Check that multibyte error message works."""
with self.assertRaisesRegex(choose_next.Error, 'error listing'):
choose_next_main(os.path.join(self.tmpdir, '\xc3\xa4'))
with self.assertRaisesRegex(choose_next.Error, 'error listing'):
choose_next_main(os.path.join(self.tmpdir, '\xe4'))
def test_logfile_name_clash(self):
"""Check that mapping of directory to logfile name has no clash."""
tmpdir1 = os.path.join(self.tmpdir, 'foo_bar')
tmpdir2 = os.path.join(self.tmpdir, 'foo', 'bar')
file_a1, _unused = put_files(tmpdir1, 'a', 'b')
file_a2, _unused = put_files(tmpdir2, 'a', 'b')
self.assertEqual(file_a1 + '\n', choose_next_main(tmpdir1))
self.assertEqual(file_a2 + '\n', choose_next_main(tmpdir2))
def test_logfile_name_migration(self):
"""Check that migration of logfile name works."""
file_a, file_b, file_c = self.put_files('a', 'b', 'c')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
old_logfile = os.path.join(self.logdir, self.tmpdir.replace(os.path.sep, '_'))
new_logfile = os.path.join(self.logdir, choose_next.quote_plus(self.tmpdir))
os.rename(new_logfile, old_logfile)
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
def test_wrap(self):
"""Check that wrapping works multiple times (got broken during development)."""
file_a, file_b, file_c = self.put_files('a', 'b', 'c')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir))
def test_logfile_prune(self):
"""Check that logfile does not grow too much."""
file_a, file_b, file_c = self.put_files('a', 'b', 'c')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual('a\n', choose_next_main(self.tmpdir, '--dump'))
def test_logfile_prune_stale_files(self):
"""Check that stale logfile entries are pruned."""
file_a, file_b, file_c, file_d = self.put_files('a', 'b', 'c', 'd')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
os.remove(file_a)
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir))
# We could prune 'a' here already:
self.assertEqual('a\nb\nc\n', choose_next_main(self.tmpdir, '--dump'))
self.assertEqual(file_d + '\n', choose_next_main(self.tmpdir))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
self.assertEqual('b\n', choose_next_main(self.tmpdir, '--dump'))
@unittest.skipIf(os.name == 'nt', 'newlines in files not supported on Windows')
def test_newlines(self):
"""Check that files containing newlines are supported."""
file_a, file_b = self.put_files('a\n', 'b\nc')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual('a\n\0', choose_next_main(self.tmpdir, '--dump0'))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir))
self.assertEqual('a\n\0b\nc\0', choose_next_main(self.tmpdir, '--dump0'))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir))
self.assertEqual('a\n\0', choose_next_main(self.tmpdir, '--dump0'))
def test_logfile_migration(self):
"""Check that migration of logfile works."""
logdir = tempfile.mkdtemp()
logfile = os.path.join(logdir, 'logfile')
file_a, file_b, file_c = self.put_files('a', 'b', 'c')
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '-L', logfile))
self.assertEqual(file_b + '\n', choose_next_main(self.tmpdir, '-L', logfile))
entries = choose_next.read_logfile(logfile, self.tmpdir)
with open(logfile, 'wb') as stream:
stream.write(b''.join([os.fsencode(e) + os.linesep.encode() for e in entries]))
self.assertEqual(file_c + '\n', choose_next_main(self.tmpdir, '-L', logfile))
self.assertEqual(file_a + '\n', choose_next_main(self.tmpdir, '-L', logfile))
shutil.rmtree(logdir)
if __name__ == '__main__':
unittest.main(buffer=True, catchbreak=True)