-
Notifications
You must be signed in to change notification settings - Fork 34
/
runtests.py
executable file
·148 lines (129 loc) · 4.5 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
#!/usr/bin/env python3
import os
import sys
import difflib
from io import StringIO
from traceback import print_exc
from itertools import chain
from subprocess import STDOUT
from subprocess import check_output
from subprocess import CalledProcessError
from pythonium.main import main
from pythonium.veloce.veloce import Veloce
from pythonium.compliant.compliant import Compliant
from pythonium.utils import pythonium_generate_js
ROOT = os.path.abspath(os.path.dirname(__file__))
TESTS_ROOT = os.path.join(ROOT, 'tests')
COMPLIANT_TESTS_ROOT = os.path.join(TESTS_ROOT, 'compliant')
PYTHON_TESTS_ROOT = os.path.join(TESTS_ROOT, 'python')
# generate pythonium compliant library
stdout = sys.stdout
sys.stdout = StringIO()
main(['--generate'])
COMPLIANTJS = sys.stdout.getvalue()
sys.stdout = stdout
# init counts
ok_ctr = test_ctr = 0
def compare_output(expected, result):
if isinstance(expected, bytes):
expected = expected.decode(errors='replace')
if isinstance(result, bytes):
result = result.decode(errors='replace')
expected = expected.strip().splitlines()
result = result.strip().splitlines()
diffs = difflib.unified_diff(expected, result, n=1, lineterm='',
fromfile='expected', tofile='result')
return list(diffs)
def run(test, filepath, mode):
global ok_ctr, test_ctr
print('> Running {} in {} mode.'.format(test, mode))
test_ctr += 1
ext = 'exec-{}.js'.format(mode)
exec_script = os.path.join(TMPDIR, test + ext)
with open(exec_script, 'w') as f:
if mode =='veloce':
translator=Veloce
else:
f.write(COMPLIANTJS)
translator=Compliant
try:
pythonium_generate_js(filepath, translator, output=f)
except Exception as exc:
print_exc()
print('< Translation failed with the above exception.')
return
try:
result = check_output(['node', '--harmony', exec_script], stderr=STDOUT)
except CalledProcessError as err:
print(err.output.decode(errors='replace'))
print('< ERROR :(')
return
expected_file = os.path.join(os.path.dirname(filepath), test+'.expected')
if os.path.exists(expected_file):
with open(expected_file, 'br') as f:
expected = f.read()
else:
try:
expected = check_output(['python3', filepath], stderr=STDOUT)
except CalledProcessError as err:
print(err.output.decode(errors='replace'))
print('< PYTHON ERROR :(')
return
diffs = compare_output(expected, result)
if diffs:
for line in diffs:
print(line)
print('< FAILED :(')
else:
ok_ctr += 1
print('< PASS :)')
def run_python(test, filepath):
global ok_ctr, test_ctr
test_ctr +=1
print('> Running python test {}.'.format(test))
try:
expected = check_output(['python3', filepath], stderr=STDOUT)
except CalledProcessError as err:
print(err.output.decode(errors='replace'))
print('< PYTHON ERROR :(')
return
else:
print('> PASS :)')
ok_ctr += 1
if __name__ == '__main__':
TMPDIR = os.path.join(TESTS_ROOT, 'tmp')
try:
os.mkdir(TMPDIR)
except OSError:
pass
# solo mode
if len(sys.argv) > 1:
for path in sys.argv[1:]:
if 'python' in path:
run_python(path, path)
else:
if 'compliant' in path:
modes = ('compliant',)
else:
modes = ('veloce', 'compliant')
name = os.path.basename(path)
for mode in modes:
run(name, path, mode)
else:
for mode in ('veloce', 'compliant'):
print('* Running tests for {} mode'.format(mode))
for test in os.listdir(TESTS_ROOT):
if test.endswith('.py'):
filepath = os.path.join(TESTS_ROOT, test)
run(test, filepath, mode)
for test in os.listdir(COMPLIANT_TESTS_ROOT):
if test.endswith('.py'):
filepath = os.path.join(COMPLIANT_TESTS_ROOT, test)
run(test, filepath, mode)
print('* Running python tests')
for test in os.listdir(PYTHON_TESTS_ROOT):
if test.endswith('.py'):
run_python(test, os.path.join(PYTHON_TESTS_ROOT, test))
print("= Passed {}/{} tests".format(ok_ctr, test_ctr))
if (ok_ctr - test_ctr) != 0:
sys.exit(1)