forked from Cadasta/cadasta-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtests.py
executable file
·151 lines (117 loc) · 4.06 KB
/
runtests.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
#! /usr/bin/env python
from __future__ import print_function
import pytest
import sys
import os
import subprocess
BASE_PYTEST_ARGS = ['cadasta', '--cov=cadasta', '--cov-report=term-missing']
PYTEST_ARGS = {
'default': BASE_PYTEST_ARGS,
'fast': BASE_PYTEST_ARGS + ['-q']
}
BASE_PYTEST_ARGS_FUNCTIONAL = []
PYTEST_ARGS_FUNCTIONAL = {
'default': BASE_PYTEST_ARGS_FUNCTIONAL,
'fast': BASE_PYTEST_ARGS_FUNCTIONAL + ['-q'],
}
FLAKE8_ARGS = ['cadasta', 'functional_tests', '--exclude=migrations']
sys.path.append(os.path.dirname(__file__))
def exit_on_failure(ret, message=None):
if ret:
sys.exit(ret)
class tee:
def __init__(self, _fd1, _fd2):
self.fd1 = _fd1
self.fd2 = _fd2
def __del__(self):
if self.fd1 != sys.stdout and self.fd1 != sys.stderr:
self.fd1.close()
if self.fd2 != sys.stdout and self.fd2 != sys.stderr:
self.fd2.close()
def write(self, text):
self.fd1.write(text)
self.fd2.write(text)
def flush(self):
self.fd1.flush()
self.fd2.flush()
def isatty(self):
return True
def flake8_main(args):
print('Running flake8 code linting')
ret = subprocess.run(['flake8'] + args).returncode
print('flake8 failed' if ret else 'flake8 passed')
return ret
def functional_main(args):
print('Running functional tests')
ret = subprocess.run(['./run.py'] + args,
cwd='./functional_tests').returncode
print('Functional tests failed' if ret else 'Functional tests passed')
return ret
def split_class_and_function(string):
class_string, function_string = string.split('.', 1)
return "%s and %s" % (class_string, function_string)
def is_function(string):
# `True` if it looks like a test function is included in the string.
return string.startswith('test_') or '.test_' in string
def is_class(string):
# `True` if first character is uppercase - assume it's a class name.
return string[0] == string[0].upper()
if __name__ == "__main__":
run_flake8 = False
run_tests = True
run_functional = False
try:
sys.argv.remove('--lint')
except ValueError:
pass
else:
run_flake8 = True
run_tests = False
try:
sys.argv.remove('--fast')
except ValueError:
style = 'default'
else:
style = 'fast'
try:
sys.argv.remove('--functional')
except ValueError:
pass
else:
run_flake8 = False
run_tests = False
run_functional = True
if len(sys.argv) > 1:
pytest_args = sys.argv[1:]
first_arg = pytest_args[0]
if first_arg.startswith('-'):
# `runtests.py [flags]`
pytest_args = ['tests'] + pytest_args
elif is_class(first_arg) and is_function(first_arg):
# `runtests.py TestCase.test_function [flags]`
expression = split_class_and_function(first_arg)
pytest_args = ['tests', '-k', expression] + pytest_args[1:]
elif is_class(first_arg) or is_function(first_arg):
# `runtests.py TestCase [flags]`
# `runtests.py test_function [flags]`
pytest_args = ['tests', '-k', pytest_args[0]] + pytest_args[1:]
pytest_args_functional = pytest_args
else:
pytest_args = PYTEST_ARGS[style]
if os.environ['DJANGO_SETTINGS_MODULE'] == 'config.settings.travis':
pytest_args = pytest_args + ['--ds=config.settings.travis']
pytest_args_functional = PYTEST_ARGS_FUNCTIONAL[style]
if run_tests:
stdoutsav = sys.stdout
outputlog = open('pytest.txt', "w")
sys.stdout = tee(stdoutsav, outputlog)
exit_on_failure(pytest.main(pytest_args))
sys.stdout = stdoutsav
for l in open('pytest.txt'):
if l.startswith('TOTAL') and not l.endswith("100%\n"):
print('Test coverage < 100%')
exit(1)
if run_flake8:
exit_on_failure(flake8_main(FLAKE8_ARGS))
if run_functional:
exit_on_failure(functional_main(pytest_args_functional))