forked from Technologicat/unpythonic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntests.py
67 lines (57 loc) · 2.29 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import re
import subprocess
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
# https://stackoverflow.com/questions/287871/print-in-terminal-with-colors
CHEAD = "\033[32m" # dark green
CPASS = "\033[92m" # light green
CFAIL = "\033[91m" # light red
CEND = "\033[39m" # reset FG color to default
def listtestmodules(path):
testfiles = listtestfiles(path)
testmodules = [modname(path, fn) for fn in testfiles]
return list(sorted(testmodules))
def listtestfiles(path, prefix="test_", suffix=".py"):
return [fn for fn in os.listdir(path) if fn.startswith(prefix) and fn.endswith(suffix)]
def modname(path, filename): # some/dir/mod.py --> some.dir.mod
modpath = re.sub(os.path.sep, r".", path)
themod = re.sub(r"\.py$", r"", filename)
return ".".join([modpath, themod])
def runtests(testsetname, modules, command_prefix):
print(CHEAD + "*** Testing {} ***".format(testsetname) + CEND)
fails = 0
for mod in modules:
print(CHEAD + "*** Running {} ***".format(mod) + CEND)
# TODO: migrate to subprocess.run (Python 3.5+)
ret = subprocess.call(command_prefix + [mod])
if ret == 0:
print(CPASS + "*** PASS ***" + CEND)
else:
fails += 1
print(CFAIL + "*** FAIL ***" + CEND)
if not fails:
print(CPASS + "*** ALL OK in {} ***".format(testsetname) + CEND)
else:
print(CFAIL + "*** AT LEAST ONE FAIL in {} ***".format(testsetname))
return fails
def main():
totalfails = 0
totalfails += runtests("regular code",
listtestmodules(os.path.join("unpythonic", "test")),
["python3", "-m"])
try:
import macropy.activate
except ImportError:
print(CHEAD + "*** Could not initialize MacroPy, skipping macro tests. ***" + CEND)
else:
totalfails += runtests("macros",
listtestmodules(os.path.join("unpythonic", "syntax", "test")),
[os.path.join("macro_extras", "macropy3"), "-m"])
if not totalfails:
print(CPASS + "*** ALL OK ***" + CEND)
else:
print(CFAIL + "*** AT LEAST ONE FAIL ***" + CEND)
if __name__ == '__main__':
main()