-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
executable file
·106 lines (77 loc) · 2.6 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
#!/usr/bin/env python
import glob
import itertools
import fnmatch
import os
import os.path
import subprocess
import sys
PACKAGE_DIR = 'bashup'
def main(args):
try:
if args and args[0] == '--static-analysis':
run_static_analysis()
run_unit_tests()
except ProcessError as e:
print(str(e))
sys.exit(e.status)
def run_static_analysis():
analyze_rst_files()
analyze_setup_py()
analyze_source_with_flake8()
analyze_source_with_pylint()
def run_unit_tests():
run(('py.test', '--cov', PACKAGE_DIR, PACKAGE_DIR))
def analyze_rst_files():
rst_iter = itertools.chain(
glob.iglob('*.rst'),
recursive_glob(PACKAGE_DIR, '*.rst'))
for path in rst_iter:
run(('rst2html.py', '--exit-status=2', path), display_stdout=False)
def analyze_setup_py():
run((sys.executable,
'setup.py',
'check',
'--strict',
'--restructuredtext',
'--metadata'))
def analyze_source_with_flake8():
run(('flake8', 'setup.py', PACKAGE_DIR))
def analyze_source_with_pylint():
run(('pylint', '--reports=no', '--rcfile', '.pylintrc', PACKAGE_DIR))
def recursive_glob(top, pattern):
for dirpath, _, filenames in os.walk(top):
for f in fnmatch.filter(filenames, pattern):
yield os.path.join(dirpath, f)
# subprocess.CalledProcessError(...) has a different signature in Python 2.6
class ProcessError(Exception):
def __init__(self, args, status, stdout=None, stderr=None):
self.args = args
self.status = status
self.stdout = (stdout or b'').decode('utf-8', 'ignore')
self.stderr = (stderr or b'').decode('utf-8', 'ignore')
def __str__(self):
def format_output(o):
return '' if not o else '\n\n[stdout]\n{}'.format(indent(o))
return (
'The command {args!r} returned the non-zero exit code {status}.'
'{stdout}{stderr}'
).format(
args=self.args,
status=self.status,
stdout=format_output(self.stdout),
stderr=format_output(self.stderr))
def run(args, display_stdout=True):
p = subprocess.Popen(
args, stdout=None if display_stdout else subprocess.PIPE)
stdout, _ = p.communicate()
if p.returncode != 0:
raise ProcessError(
args=args,
status=p.returncode,
stdout=stdout if display_stdout else None)
# textwrap.indent() doesn't exist in Python 2
def indent(text, prefix=' '):
return prefix + prefix.join(text.splitlines(True))
if __name__ == '__main__':
main(args=sys.argv[1:])