forked from servo/saltfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·60 lines (42 loc) · 1.79 KB
/
test.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
#!/usr/bin/env python3
import importlib
import os
import sys
from tests.util import color, GREEN, RED, Failure, project_path
def is_python_script(dir_entry):
return dir_entry.name.endswith('.py') and dir_entry.is_file()
def run_tests(tests):
any_failures = False
for test_spec in tests:
test_dir = os.path.join(project_path(), 'tests', *test_spec.split('.'))
python_scripts = filter(is_python_script, os.scandir(test_dir))
tests = sorted([entry.name for entry in python_scripts])
for test in tests:
test_mod_name = 'tests.{}.{}'.format(test_spec, test[:-3])
test_mod = importlib.import_module(test_mod_name)
if not hasattr(test_mod, 'run'): # Not a test script
continue
try:
result = test_mod.run()
except Exception as e:
message = 'Test \'{}\' raised an exception:'.format(test)
result = Failure(message, str(e))
if result.is_success():
print('[ {} ] {}'.format(color(GREEN, 'PASS'), result.message))
else:
any_failures = True
print('[ {} ] {}'.format(color(RED, 'FAIL'), result.message))
for line in result.output.splitlines():
print(' {}'.format(line))
return 1 if any_failures else 0
def main():
if sys.version_info < (3, 5): # We use features introduced in Python 3.5
sys.stderr.write('{}: Python 3.5 or later is needed for this script\n'
.format(__file__))
return 1
tests = ['lint'] # Only tests that are always safe and meaningful to run
if len(sys.argv) > 1:
tests = sys.argv[1:]
return run_tests(tests)
if __name__ == '__main__':
sys.exit(main())