forked from djc/runa
/
test.py
148 lines (119 loc) · 3.38 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
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
from __future__ import print_function
import sys, os, unittest, subprocess, json
from runac import util
import runac
DIR = os.path.dirname(__file__)
TEST_DIR = os.path.join(DIR, 'tests')
TESTS = [i[:-4] for i in os.listdir(TEST_DIR) if i.endswith('.rns')]
def getspec(src):
with open(src) as f:
h = f.readline()
if h.startswith('# test: '):
return json.loads(h[8:])
else:
return {}
def compile(fn, bin):
try:
runac.compile(fn, bin)
return None
except util.Error as e:
return e.show()
except util.ParseError as e:
return e.show()
def run(self, key):
fullname = os.path.join(TEST_DIR, key + '.rns')
base = fullname.rsplit('.rns', 1)[0]
bin = base + '.test'
spec = getspec(fullname)
type = spec.get('type', 'test')
if type == 'show':
out = '\n'.join(runac.show(fullname, None)) + '\n'
else:
out = compile(fullname, bin)
if out and sys.version_info[0] > 2:
out = out.encode('utf-8')
if not out:
cmd = [bin] + spec.get('args', [])
opts = {'stdout': subprocess.PIPE, 'stderr': subprocess.PIPE}
proc = subprocess.Popen(cmd, **opts)
res = [proc.wait(), proc.stdout.read(), proc.stderr.read()]
proc.stdout.close()
proc.stderr.close()
elif type == 'show':
res = [0, out, bytes()]
else:
res = [0, bytes(), out]
expected = [spec.get('ret', 0), bytes(), bytes()]
for i, ext in enumerate(('.out', '.err')):
if os.path.exists(base + ext):
with open(base + ext, 'rb') as f:
expected[i + 1] = f.read()
if self is None:
return res == expected
elif res[1]:
self.assertEqual(expected[0], res[0])
self.assertEqual(expected[1], res[1])
self.assertEqual(expected[2], res[2])
elif res[2]:
self.assertEqual(expected[2], res[2])
self.assertEqual(expected[1], res[1])
self.assertEqual(expected[0], res[0])
def testfunc(key):
def do(self):
return self._do(key)
return do
attrs = {'_do': run}
for key in TESTS:
m = testfunc(key)
m.__name__ = 'test_%s' % key
attrs[m.__name__] = m
LangTests = type('LangTests', (unittest.TestCase,), attrs)
def suite():
return unittest.makeSuite(LangTests, 'test')
IGNORE = [
'Memcheck', 'WARNING:', 'HEAP SUMMARY:', 'LEAK SUMMARY:',
'All heap blocks', 'For counts',
]
def valgrind(bin, spec):
cmd = ['valgrind', '--leak-check=full', bin] + spec.get('args', [])
streams = {'stdout': subprocess.PIPE, 'stderr': subprocess.PIPE}
proc = subprocess.Popen(cmd, **streams)
proc.wait()
err = proc.stderr.read()
blocks, cur = [], []
for ln in err.splitlines():
if not ln.startswith('=='):
continue
ln = ln.split(' ', 1)[1]
if not ln.strip():
if cur:
blocks.append(cur)
cur = []
continue
cur.append(ln)
errors = []
for bl in blocks:
if not any(flag for flag in IGNORE if bl[0].startswith(flag)):
errors.append(bl)
return len(errors)
def leaks():
for fn in sorted(os.listdir('tests')):
if not fn.endswith('.rns'):
continue
test = os.path.join('tests', fn)
bin = test[:-4] + '.test'
compiled = os.path.exists(bin)
if not compiled or os.stat(test).st_mtime >= os.stat(bin).st_mtime:
out = compile(test, bin)
if out is not None:
continue
print('Running %s...' % bin, end=' ')
count = valgrind(bin, getspec(test))
print(' ' * (40 - len(bin)), '%3i' % count)
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == '--leaks':
leaks()
elif len(sys.argv) > 1:
print(run(None, sys.argv[1]))
else:
unittest.main(defaultTest='suite')