forked from mgedmin/check-manifest
/
tests.py
579 lines (500 loc) · 20.1 KB
/
tests.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
579
import doctest
import os
import shutil
import subprocess
import sys
import tempfile
import textwrap
import unittest
try:
from cStringIO import StringIO # Python 2.x
except ImportError:
from io import StringIO # Python 3.x
import mock
class Tests(unittest.TestCase):
def setUp(self):
import check_manifest
self.warnings = []
self._real_warning = check_manifest.warning
check_manifest.warning = self.warnings.append
def tearDown(self):
import check_manifest
check_manifest.warning = self._real_warning
def test_run_success(self):
from check_manifest import run
self.assertEqual(run(["true"]), "")
def test_run_failure(self):
from check_manifest import run, CommandFailed
with self.assertRaises(CommandFailed) as cm:
run(["false"])
self.assertEqual(str(cm.exception),
"['false'] failed (status 1):\n")
def test_run_no_such_program(self):
from check_manifest import run, Failure
with self.assertRaises(Failure) as cm:
run(["there-is-really-no-such-program"])
self.assertTrue(
str(cm.exception).startswith(
"could not run ['there-is-really-no-such-program']:"
" [Errno 2] No such file or directory"))
def test_copy_files(self):
from check_manifest import copy_files
actions = []
with mock.patch('os.path.isdir', lambda d: d in ('b', '/dest/dir')), \
mock.patch('os.makedirs', lambda d: actions.append('makedirs %s' % d)), \
mock.patch('os.mkdir', lambda d: actions.append('mkdir %s' % d)), \
mock.patch('shutil.copy2', lambda s, d: actions.append('cp %s %s' % (s, d))):
copy_files(['a', 'b', 'c/d/e'], '/dest/dir')
self.assertEqual(
actions,
[
'cp a /dest/dir/a',
'mkdir /dest/dir/b',
'makedirs /dest/dir/c/d',
'cp c/d/e /dest/dir/c/d/e',
])
def test_get_one_file_in(self):
from check_manifest import get_one_file_in
with mock.patch('os.listdir', lambda dir: ['a']):
self.assertEqual(get_one_file_in('/some/dir'),
'/some/dir/a')
def test_get_one_file_in_empty_directory(self):
from check_manifest import get_one_file_in, Failure
with mock.patch('os.listdir', lambda dir: []):
with self.assertRaises(Failure) as cm:
get_one_file_in('/some/dir')
self.assertEqual(str(cm.exception),
"No files found in /some/dir")
def test_get_one_file_in_too_many(self):
from check_manifest import get_one_file_in, Failure
with mock.patch('os.listdir', lambda dir: ['b', 'a']):
with self.assertRaises(Failure) as cm:
get_one_file_in('/some/dir')
self.assertEqual(str(cm.exception),
"More than one file exists in /some/dir:\na\nb")
def test_get_archive_file_list_unrecognized_archive(self):
from check_manifest import get_archive_file_list, Failure
with self.assertRaises(Failure) as cm:
get_archive_file_list('archive.rar')
self.assertEqual(str(cm.exception), 'Unrecognized archive type: .rar')
def test_format_list(self):
from check_manifest import format_list
self.assertEqual(format_list([]), "")
self.assertEqual(format_list(['a']), " a")
self.assertEqual(format_list(['a', 'b']), " a\n b")
def test_format_difference(self):
from check_manifest import format_difference
self.assertEqual(
format_difference(["a", "b"], ["a", "b"], "1st", "2nd"),
"")
self.assertEqual(
format_difference(["a", "b"], ["b", "c"], "1st", "2nd"),
"missing from 1st:\n"
" c\n"
"missing from 2nd:\n"
" a")
def test_strip_toplevel_name_empty_list(self):
from check_manifest import strip_toplevel_name
self.assertEqual(strip_toplevel_name([]), [])
def test_strip_toplevel_name_no_common_prefix(self):
from check_manifest import strip_toplevel_name, Failure
self.assertRaises(Failure, strip_toplevel_name, ["a/b", "c/d"])
def test_detect_vcs_no_vcs(self):
from check_manifest import detect_vcs, Failure
with mock.patch('check_manifest.VCS.detect', staticmethod(lambda *a: False)):
with self.assertRaises(Failure) as cm:
detect_vcs()
self.assertEqual(str(cm.exception),
"Couldn't find version control data"
" (git/hg/bzr/svn supported)")
def test_normalize_names(self):
from check_manifest import normalize_names
self.assertEqual(normalize_names(["a", "b/", "c/d", "e/f/", "g/h/../i"]),
["a", "b", "c/d", "e/f", "g/i"])
def test_add_directories(self):
from check_manifest import add_directories
self.assertEqual(add_directories(["a", "b", "c/d", "e/f"]),
["a", "b", "c", "c/d", "e", "e/f"])
def test_file_matches(self):
from check_manifest import file_matches
patterns = ['setup.cfg', '*.egg-info', '*.egg-info/*']
self.assertFalse(file_matches('setup.py', patterns))
self.assertTrue(file_matches('setup.cfg', patterns))
self.assertTrue(file_matches('src/zope.foo.egg-info', patterns))
self.assertTrue(file_matches('src/zope.foo.egg-info/SOURCES.txt',
patterns))
def test_strip_sdist_extras(self):
from check_manifest import strip_sdist_extras
filelist = [
'.gitignore',
'setup.py',
'setup.cfg',
'README.txt',
'src',
'src/zope',
'src/zope/__init__.py',
'src/zope/foo',
'src/zope/foo/__init__.py',
'src/zope/foo/language.po',
'src/zope/foo/language.mo',
'src/zope.foo.egg-info',
'src/zope.foo.egg-info/SOURCES.txt',
]
expected = [
'setup.py',
'README.txt',
'src',
'src/zope',
'src/zope/__init__.py',
'src/zope/foo',
'src/zope/foo/__init__.py',
'src/zope/foo/language.po',
]
self.assertEqual(strip_sdist_extras(filelist), expected)
def test_strip_sdist_extras_with_manifest(self):
import check_manifest
from check_manifest import strip_sdist_extras
from check_manifest import _get_ignore_from_manifest as parse
orig_ignore = check_manifest.IGNORE
orig_ignore_regexps = check_manifest.IGNORE_REGEXPS
manifest_in = """
graft src
exclude *.cfg
global-exclude *.mo
prune src/dump
recursive-exclude src/zope *.sh
"""
# Keep the indentation visually clear in the test, but remove
# leading whitespace programmatically.
manifest_in = textwrap.dedent(manifest_in)
filelist = [
'.gitignore',
'setup.py',
'setup.cfg',
'MANIFEST.in',
'README.txt',
'src',
'src/helper.sh',
'src/dump',
'src/dump/__init__.py',
'src/zope',
'src/zope/__init__.py',
'src/zope/zopehelper.sh',
'src/zope/foo',
'src/zope/foo/__init__.py',
'src/zope/foo/language.po',
'src/zope/foo/language.mo',
'src/zope/foo/config.cfg',
'src/zope/foo/foohelper.sh',
'src/zope.foo.egg-info',
'src/zope.foo.egg-info/SOURCES.txt',
]
expected = [
'setup.py',
'MANIFEST.in',
'README.txt',
'src',
'src/helper.sh',
'src/zope',
'src/zope/__init__.py',
'src/zope/foo',
'src/zope/foo/__init__.py',
'src/zope/foo/language.po',
'src/zope/foo/config.cfg',
]
# This will change the definitions.
try:
# This is normally done in read_manifest:
ignore, ignore_regexps = parse(manifest_in)
check_manifest.IGNORE.extend(ignore)
check_manifest.IGNORE_REGEXPS.extend(ignore_regexps)
# Filter the file list.
result = strip_sdist_extras(filelist)
finally:
# Restore the original definitions
check_manifest.IGNORE = orig_ignore
check_manifest.IGNORE_REGEXPS = orig_ignore_regexps
self.assertEqual(result, expected)
def test_find_bad_ideas(self):
from check_manifest import find_bad_ideas
filelist = [
'.gitignore',
'setup.py',
'setup.cfg',
'README.txt',
'src',
'src/zope',
'src/zope/__init__.py',
'src/zope/foo',
'src/zope/foo/__init__.py',
'src/zope/foo/language.po',
'src/zope/foo/language.mo',
'src/zope.foo.egg-info',
'src/zope.foo.egg-info/SOURCES.txt',
]
expected = [
'src/zope/foo/language.mo',
'src/zope.foo.egg-info',
]
self.assertEqual(find_bad_ideas(filelist), expected)
def test_find_suggestions(self):
from check_manifest import find_suggestions
self.assertEqual(find_suggestions(['buildout.cfg']),
(['include buildout.cfg'], []))
self.assertEqual(find_suggestions(['unknown.file~']),
([], ['unknown.file~']))
self.assertEqual(find_suggestions(['README.txt', 'CHANGES.txt']),
(['include *.txt'], []))
filelist = [
'docs/index.rst',
'docs/image.png',
'docs/Makefile',
'docs/unknown-file',
]
expected_rules = [
'recursive-include docs *.png',
'recursive-include docs *.rst',
'recursive-include docs Makefile',
]
expected_unknowns = ['docs/unknown-file']
self.assertEqual(find_suggestions(filelist),
(expected_rules, expected_unknowns))
def test_find_suggestions_generic_fallback_rules(self):
from check_manifest import find_suggestions
self.assertEqual(find_suggestions(['Changelog']),
(['include Changelog'], []))
self.assertEqual(find_suggestions(['id-lang.map']),
(['include *.map'], []))
self.assertEqual(find_suggestions(['src/id-lang.map']),
(['recursive-include src *.map'], []))
def test_get_ignore_from_manifest(self):
from check_manifest import _get_ignore_from_manifest as parse
# The return value is a tuple with two lists:
# ([<list of filename ignores>], [<list of regular expressions>])
self.assertEqual(parse(''),
([], []))
self.assertEqual(parse(' \n '),
([], []))
self.assertEqual(parse('exclude *.cfg'),
([], ['[^/]*\.cfg']))
self.assertEqual(parse('#exclude *.cfg'),
([], []))
self.assertEqual(parse('exclude *.cfg'),
([], ['[^/]*\.cfg']))
self.assertEqual(parse('\texclude\t*.cfg foo.* bar.txt'),
(['bar.txt'], ['[^/]*\.cfg', 'foo\.[^/]*']))
self.assertEqual(parse('exclude some/directory/*.cfg'),
([], ['some/directory/[^/]*\.cfg']))
self.assertEqual(parse('include *.cfg'),
([], []))
self.assertEqual(parse('global-exclude *.pyc'),
(['*.pyc'], []))
self.assertEqual(parse('global-exclude *.pyc *.sh'),
(['*.pyc', '*.sh'], []))
self.assertEqual(parse('recursive-exclude dir *.pyc'),
(['dir/*.pyc'], []))
self.assertEqual(parse('recursive-exclude dir *.pyc foo*.sh'),
(['dir/*.pyc', 'dir/foo*.sh', 'dir/*/foo*.sh'], []))
self.assertEqual(parse('recursive-exclude dir nopattern.xml'),
(['dir/nopattern.xml', 'dir/*/nopattern.xml'], []))
# We should not fail when a recursive-exclude line is wrong:
self.assertEqual(parse('recursive-exclude dirwithoutpattern'),
([], []))
self.assertEqual(parse('prune dir'),
(['dir', 'dir/*'], []))
# You should not add a slash at the end of a prune, but let's
# not fail over it or end up with double slashes.
self.assertEqual(parse('prune dir/'),
(['dir', 'dir/*'], []))
text = """
#exclude *.01
exclude *.02
exclude *.03 04.* bar.txt
exclude *.05
exclude some/directory/*.cfg
global-exclude *.10 *.11
global-exclude *.12
include *.20
prune 30
recursive-exclude 40 *.41
recursive-exclude 42 *.43 44.*
"""
# Keep the indentation visually clear in the test, but remove
# leading whitespace programmatically.
text = textwrap.dedent(text)
self.assertEqual(
parse(text),
([
'bar.txt',
'*.10',
'*.11',
'*.12',
'30',
'30/*',
'40/*.41',
'42/*.43',
'42/44.*',
'42/*/44.*',
], [
'[^/]*\.02',
'[^/]*\.03',
'04\.[^/]*',
'[^/]*\.05',
'some/directory/[^/]*\.cfg',
]))
class VCSMixin(object):
def setUp(self):
self.tmpdir = tempfile.mkdtemp(prefix='test-', suffix='-check-manifest')
self.olddir = os.getcwd()
os.chdir(self.tmpdir)
def tearDown(self):
os.chdir(self.olddir)
shutil.rmtree(self.tmpdir)
def _run(self, *command):
try:
subprocess.check_output(command, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print(' '.join(command))
print(e.output)
raise
def _create_file(self, filename):
assert not os.path.isabs(filename)
basedir = os.path.dirname(filename)
if basedir and not os.path.isdir(basedir):
os.makedirs(basedir)
open(filename, 'w').close()
def _create_files(self, filenames):
for filename in filenames:
self._create_file(filename)
def _create_and_add_to_vcs(self, filenames):
self._create_files(filenames)
self._add_to_vcs(filenames)
def test_get_vcs_files(self):
from check_manifest import get_vcs_files
self._init_vcs()
self._create_and_add_to_vcs(['a.txt', 'b/b.txt', 'b/c/d.txt'])
self._commit()
self._create_files(['b/x.txt', 'd/d.txt', 'i.txt'])
self.assertEqual(get_vcs_files(),
['a.txt', 'b', 'b/b.txt', 'b/c', 'b/c/d.txt'])
def test_get_vcs_files_added_but_uncommitted(self):
from check_manifest import get_vcs_files
self._init_vcs()
self._create_and_add_to_vcs(['a.txt', 'b/b.txt', 'b/c/d.txt'])
self._create_files(['b/x.txt', 'd/d.txt', 'i.txt'])
self.assertEqual(get_vcs_files(),
['a.txt', 'b', 'b/b.txt', 'b/c', 'b/c/d.txt'])
def test_get_vcs_files_in_a_subdir(self):
from check_manifest import get_vcs_files
self._init_vcs()
self._create_and_add_to_vcs(['a.txt', 'b/b.txt', 'b/c/d.txt'])
self._commit()
self._create_files(['b/x.txt', 'd/d.txt', 'i.txt'])
os.chdir('b')
self.assertEqual(get_vcs_files(), ['b.txt', 'c', 'c/d.txt'])
class TestGit(VCSMixin, unittest.TestCase):
def _init_vcs(self):
self._run('git', 'init')
self._run('git', 'config', 'user.name', 'Unit Test')
self._run('git', 'config', 'user.email', 'test@example.com')
def _add_to_vcs(self, filenames):
self._run('git', 'add', '--', *filenames)
def _commit(self):
self._run('git', 'commit', '-m', 'Initial')
class TestBzr(VCSMixin, unittest.TestCase):
def _init_vcs(self):
self._run('bzr', 'init')
self._run('bzr', 'whoami', '--branch', 'Unit Test <test@example.com>')
def _add_to_vcs(self, filenames):
self._run('bzr', 'add', '--', *filenames)
def _commit(self):
self._run('bzr', 'commit', '-m', 'Initial')
class TestHg(VCSMixin, unittest.TestCase):
def _init_vcs(self):
self._run('hg', 'init')
with open('.hg/hgrc', 'a') as f:
f.write('\n[ui]\nusername = Unit Test <test@example.com\n')
def _add_to_vcs(self, filenames):
self._run('hg', 'add', '--', *filenames)
def _commit(self):
self._run('hg', 'commit', '-m', 'Initial')
class TestSvn(VCSMixin, unittest.TestCase):
def _init_vcs(self):
self._run('svnadmin', 'create', 'repo')
self._run('svn', 'co', 'file:///' + os.path.abspath('repo'), 'checkout')
os.chdir('checkout')
def _add_to_vcs(self, filenames):
from check_manifest import add_directories
self._run('svn', 'add', '-N', '--', *add_directories(filenames))
def _commit(self):
self._run('svn', 'commit', '-m', 'Initial')
class TestUserInterface(unittest.TestCase):
def setUp(self):
self.real_stdout = sys.stdout
self.real_stderr = sys.stderr
sys.stdout = StringIO()
sys.stderr = StringIO()
def tearDown(self):
sys.stderr = self.real_stderr
sys.stdout = self.real_stdout
def test_info(self):
from check_manifest import info
info("Reticulating splines")
self.assertEqual(sys.stdout.getvalue(),
"Reticulating splines\n")
def test_info_begin_continue_end(self):
from check_manifest import info_begin, info_continue, info_end
info_begin("Reticulating splines...")
info_continue(" nearly done...")
info_continue(" almost done...")
info_end(" done!")
self.assertEqual(
sys.stdout.getvalue(),
"Reticulating splines... nearly done... almost done... done!\n")
def test_info_emits_newline_when_needed(self):
from check_manifest import info_begin, info
info_begin("Computering...")
info("Forgot to turn the gas off!")
self.assertEqual(
sys.stdout.getvalue(),
"Computering...\n"
"Forgot to turn the gas off!\n")
def test_warning(self):
from check_manifest import info_begin, warning
info_begin("Computering...")
warning("Forgot to turn the gas off!")
self.assertEqual(
sys.stdout.getvalue(),
"Computering...\n")
self.assertEqual(
sys.stderr.getvalue(),
"Forgot to turn the gas off!\n")
def test_error(self):
from check_manifest import info_begin, error
info_begin("Computering...")
error("Forgot to turn the gas off!")
self.assertEqual(
sys.stdout.getvalue(),
"Computering...\n")
self.assertEqual(
sys.stderr.getvalue(),
"Forgot to turn the gas off!\n")
def doctest_ui_messages():
r"""Tests for user interface messages.
>>> from check_manifest import (info, info_begin, info_continue,
... info_end, error, warning)
Here are the basic blocks:
>>> info_begin('About to do something'); \
... info_continue('...doing something...'); \
... info_end('done it!')
"""
def test_suite():
return unittest.TestSuite([
unittest.makeSuite(Tests),
unittest.makeSuite(TestGit),
unittest.makeSuite(TestBzr),
unittest.makeSuite(TestHg),
unittest.makeSuite(TestSvn),
unittest.makeSuite(TestUserInterface),
doctest.DocTestSuite('check_manifest'),
])