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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
#!/usr/bin/env python
import sys
import os
import re
import tempfile
multiproc = False
try:
from multiprocessing import Pool
multiproc = True
except:
pass
from string import join
from difflib import unified_diff
from LedgerHarness import LedgerHarness
args = sys.argv
jobs = 1
match = re.match('-j([0-9]+)?', args[1])
if match:
args = [args[0]] + args[2:]
if match.group(1):
jobs = int(match.group(1))
if jobs == 1:
multiproc = False
harness = LedgerHarness(args)
tests = args[3]
if not os.path.isdir(tests) and not os.path.isfile(tests):
sys.exit(1)
class RegressFile(object):
def __init__(self, filename):
self.filename = filename
self.fd = open(self.filename)
def is_directive(self, line):
return line == "<<<\n" or \
line == ">>>\n" or \
line == ">>>1\n" or \
line == ">>>2\n" or \
line.startswith("===")
def transform_line(self, line):
line = re.sub('\$sourcepath', harness.sourcepath, line)
return line
def read_section(self):
lines = []
line = self.fd.readline()
while line and not self.is_directive(line):
lines.append(self.transform_line(line))
line = self.fd.readline()
return (lines, line)
def read_test(self, last_test = None):
test = {
'command': None,
'input': "",
'output': "",
'error': "",
'exitcode': 0
}
if last_test:
test['input'] = last_test['input']
line = self.fd.readline()
while line:
if line == "<<<\n":
(test['input'], line) = self.read_section()
elif line == ">>>\n" or line == ">>>1\n":
(test['output'], line) = self.read_section()
elif line == ">>>2\n":
(test['error'], line) = self.read_section()
elif line.startswith("==="):
match = re.match('=== ([0-9]+)', line)
assert match
test['exitcode'] = int(match.group(1))
return test
else:
test['command'] = self.transform_line(line)
line = self.fd.readline()
return test['command'] and test
def notify_user(self, msg, test):
print msg
print "--"
print test['command'],
print "--"
def run_test(self, test):
use_stdin = False
if test['command'].find("-f - ") != -1:
use_stdin = True
test['command'] = '$ledger ' + test['command']
else:
tempdata = tempfile.mkstemp()
os.write(tempdata[0], join(test['input'], ''))
os.close(tempdata[0])
test['command'] = (('$ledger -f "%s" ' % tempdata[1]) +
test['command'])
p = harness.run(test['command'],
columns=(not re.search('--columns', test['command'])))
if use_stdin:
p.stdin.write(join(test['input'], ''))
p.stdin.close()
success = True
printed = False
index = 0
if test['output'] is not None:
for line in unified_diff(test['output'], harness.readlines(p.stdout)):
index += 1
if index < 3:
continue
if not printed:
if success: print
self.notify_user("Regression failure in output from %s:" % self.filename, test)
success = False
printed = True
print " ", line,
printed = False
index = 0
if test['error'] is not None:
for line in unified_diff([re.sub('\$FILE', tempdata[1], line)
for line in test['error']],
harness.readlines(p.stderr)):
index += 1
if index < 3:
continue
if not printed:
if success: print
self.notify_user("Regression failure in error output from %s:" % self.filename, test)
success = False
printed = True
print " ", line,
if not use_stdin:
os.remove(tempdata[1])
if test['exitcode'] is None or test['exitcode'] == p.wait():
if success:
harness.success()
else:
harness.failure()
else:
if success: print
self.notify_user("Regression failure in exit code (%d (expected) != %d) from %s:"
% (test['exitcode'], p.returncode), test, self.filename)
harness.failure()
def run_tests(self):
test = self.read_test()
while test:
self.run_test(test)
test = self.read_test(test)
return harness.failed
def close(self):
self.fd.close()
def do_test(path):
entry = RegressFile(path)
failed = entry.run_tests()
entry.close()
return failed
if __name__ == '__main__':
if multiproc:
pool = Pool(jobs*2)
else:
pool = None
if os.path.isdir(tests):
tests = [os.path.join(tests, x)
for x in os.listdir(tests) if x.endswith('.test')]
if pool:
pool.map(do_test, tests, 1)
else:
map(do_test, tests)
else:
entry = RegressFile(tests)
entry.run_tests()
entry.close()
if pool:
pool.close()
pool.join()
harness.exit()
|