Debian Codegraph @

runtests

raw
1 #!/usr/bin/env python
2 import os
3 import pytest
4 import subprocess
5 import sys
6
7
8 PYTEST_ARGS = ['tests.py', '--tb=short']
9 FLAKE8_ARGS = ['itypes.py', 'tests.py', '--ignore=E501']
10 COVERAGE_OPTIONS = {
11 'include': ['itypes.py', 'tests.py'],
12 }
13
14
15 sys.path.append(os.path.dirname(__file__))
16
17
18 class NullFile(object):
19 def write(self, data):
20 pass
21
22
23 def exit_on_failure(ret, message=None):
24 if ret:
25 sys.exit(ret)
26
27
28 def flake8_main(args):
29 print('Running flake8 code linting')
30 ret = subprocess.call(['flake8'] + args)
31 print('flake8 failed' if ret else 'flake8 passed')
32 return ret
33
34
35 def fail_if_lacking_coverage(cov):
36 precent_covered = cov.report(
37 file=NullFile(), **COVERAGE_OPTIONS
38 )
39 if precent_covered == 100:
40 print('100% coverage')
41 return
42 print('Tests passed, but not 100% coverage.')
43 cov.report(**COVERAGE_OPTIONS)
44 cov.html_report(**COVERAGE_OPTIONS)
45 sys.exit(1)
46
47
48 def split_class_and_function(string):
49 class_string, function_string = string.split('.', 1)
50 return "%s and %s" % (class_string, function_string)
51
52
53 def is_function(string):
54 # `True` if it looks like a test function is included in the string.
55 return string.startswith('test_') or '.test_' in string
56
57
58 def is_class(string):
59 # `True` if first character is uppercase - assume it's a class name.
60 return string[0] == string[0].upper()
61
62
63 if __name__ == "__main__":
64 if len(sys.argv) > 1:
65 pytest_args = sys.argv[1:]
66 first_arg = pytest_args[0]
67 if first_arg.startswith('-'):
68 # `runtests.py [flags]`
69 pytest_args = PYTEST_ARGS + pytest_args
70 elif is_class(first_arg) and is_function(first_arg):
71 # `runtests.py TestCase.test_function [flags]`
72 expression = split_class_and_function(first_arg)
73 pytest_args = PYTEST_ARGS + ['-k', expression] + pytest_args[1:]
74 elif is_class(first_arg) or is_function(first_arg):
75 # `runtests.py TestCase [flags]`
76 # `runtests.py test_function [flags]`
77 pytest_args = PYTEST_ARGS + ['-k', pytest_args[0]] + pytest_args[1:]
78 else:
79 pytest_args = PYTEST_ARGS
80
81 # cov = coverage.coverage()
82 # cov.start()
83 exit_on_failure(pytest.main(pytest_args))
84 # cov.stop()
85 exit_on_failure(flake8_main(FLAKE8_ARGS))
86 # fail_if_lacking_coverage(cov)