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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
#!/usr/bin/env python3
from io import open
import os
import re
import sys
import shlex
import locale
import hashlib
import argparse
import subprocess
from difflib import unified_diff
class DocTests:
def __init__(self, args):
scriptpath = os.path.dirname(os.path.realpath(__file__))
self.ledger = os.path.realpath(args.ledger)
self.sourcepath = os.path.realpath(args.file)
self.verbose = args.verbose
self.tests = args.examples
self.examples = dict()
self.test_files = list()
self.testin_token = 'command'
self.testout_token = 'output'
self.testdat_token = 'input'
self.testfile_token = 'file'
self.validate_token = 'validate'
self.validate_cmd_token = 'validate-command'
self.validate_dat_token = 'validate-data'
self.testwithdat_token = 'with_input'
self.testwithfile_token = 'with_file'
def read_example(self):
endexample = re.compile(r'^@end\s+smallexample\s*$')
example = str()
while True:
line = self.file.readline()
self.current_line += 1
if len(line) <= 0 or endexample.match(line): break
# Replace special texinfo character sequences with their ASCII counterpart
example += re.sub(r'@([@{}])', r'\1', line)
return example
def test_id(self, example):
example_id = example.rstrip().encode('utf-8')
return hashlib.sha1(example_id).hexdigest()[0:7].upper()
def find_examples(self):
startexample = re.compile(r'^@smallexample\s+@c\s+(%s|%s|%s|%s)(?::([\dA-Fa-f]+|validate))?(?:,(.*))?'
% (self.testin_token, self.testout_token, self.testdat_token, self.testfile_token))
while True:
line = self.file.readline()
self.current_line += 1
if len(line) <= 0: break
startmatch = startexample.match(line)
if (startmatch):
test_begin_pos = self.file.tell()
test_begin_line = self.current_line
test_kind = startmatch.group(1)
test_id = startmatch.group(2)
test_options = dict()
for pair in re.split(r',\s*', str(startmatch.group(3))):
kv = re.split(r':\s*', pair, 2)
try:
test_options[kv[0]] = kv[1]
except IndexError:
pass
example = self.read_example()
test_end_pos = self.file.tell()
test_end_line = self.current_line
if not test_id:
print('Example', test_kind, 'in line', test_begin_line, 'is missing id.', file=sys.stderr)
test_id = self.test_id(example)
if test_kind == self.testin_token:
print('Use', self.test_id(example), file=sys.stderr)
elif test_kind == self.testin_token and test_id != self.validate_token and test_id != self.test_id(example):
print('Expected test id', test_id, 'for example' \
, test_kind, 'on line', test_begin_line, 'to be', self.test_id(example), file=sys.stderr)
if test_id == self.validate_token:
test_id = "Val-" + str(test_begin_line)
if test_kind == self.testin_token:
test_kind = self.validate_cmd_token
elif test_kind == self.testdat_token:
test_kind = self.validate_dat_token
try:
self.examples[test_id]
except KeyError:
self.examples[test_id] = dict()
try:
example = self.examples[test_id][test_kind][test_kind] + example
except KeyError:
pass
self.examples[test_id][test_kind] = {
'bpos': test_begin_pos,
'epos': test_end_pos,
'blin': test_begin_line,
'elin': test_end_line,
'opts': test_options,
test_kind: example,
}
def parse_command(self, test_id, example):
validate_command = False
try:
command = example[self.testin_token][self.testin_token]
command = re.sub(r'\\\n', '', command)
except KeyError:
if self.validate_dat_token in example:
command = '$ ledger bal'
elif self.validate_cmd_token in example:
validate_command = True
command = example[self.validate_cmd_token][self.validate_cmd_token]
else:
return None
if sys.version_info.major == 2:
command = command.encode(locale.getpreferredencoding())
command_parts = shlex.split(command)
command = filter(lambda x: x != '\n', command_parts)
if sys.version_info.major > 2:
command = list(command)
if command[0] == '$': command.remove('$')
index = command.index('ledger')
command[index] = self.ledger
for i,argument in enumerate(shlex.split('--args-only --columns 80')):
command.insert(index+i+1, argument)
try:
findex = command.index('-f')
except ValueError:
try:
findex = command.index('--file')
except ValueError:
findex = index+1
command.insert(findex, '--file')
if validate_command:
command.insert(findex+1, 'sample.dat')
else:
command.insert(findex+1, test_id + '.dat')
return (command, findex+1)
def test_examples(self):
failed = set()
tests = self.examples.keys()
if self.tests:
tests = list(set(self.tests).intersection(tests))
temp = list(set(self.tests).difference(tests))
if len(temp) > 0:
print('Skipping non-existent examples: %s' % ', '.join(temp), file=sys.stderr)
for test_id in tests:
validation = False
if self.validate_dat_token in self.examples[test_id] or self.validate_cmd_token in self.examples[test_id]:
validation = True
example = self.examples[test_id]
try:
(command, findex) = self.parse_command(test_id, example)
except TypeError:
failed.add(test_id)
continue
output = example.get(self.testout_token, {}).get(self.testout_token)
input = example.get(self.testdat_token, {}).get(self.testdat_token)
if not input:
with_input = example.get(self.testin_token, {}).get('opts', {}).get(self.testwithdat_token)
input = self.examples.get(with_input, {}).get(self.testdat_token, {}).get(self.testdat_token)
if not input:
input = example.get(self.validate_dat_token, {}).get(self.validate_dat_token)
if command and (output != None or validation):
test_file_created = False
if findex:
scriptpath = os.path.dirname(os.path.realpath(__file__))
test_input_dir = os.path.join(scriptpath, '..', 'test', 'input')
test_file = command[findex]
if not os.path.exists(test_file):
if input:
test_file_created = True
with open(test_file, 'w', encoding='utf-8') as f:
f.write(input)
elif os.path.exists(os.path.join(test_input_dir, test_file)):
command[findex] = os.path.join(test_input_dir, test_file)
try:
convert_idx = command.index(str('convert'))
convert_file = command[convert_idx+1]
convert_data = example[self.testfile_token][self.testfile_token]
if not os.path.exists(convert_file):
with open(convert_file, 'w', encoding='utf-8') as f:
f.write(convert_data)
except ValueError:
pass
error = None
try:
verify = subprocess.check_output(command, stderr=subprocess.STDOUT)
verify = verify.decode('utf-8')
if sys.platform == 'win32':
verify = verify.replace('\r\n', '\n')
valid = (output == verify) or (not error and validation)
except subprocess.CalledProcessError as e:
error = e.output
valid = False
failed.add(test_id)
if valid and test_file_created:
os.remove(test_file)
if self.verbose > 0:
print(test_id, ':', 'Passed' if valid else 'FAILED: {}'.format(error) if error else 'FAILED')
else:
sys.stdout.write('.' if valid else 'E')
sys.stdout.flush()
if not (valid or error):
failed.add(test_id)
if self.verbose > 1:
print(' '.join(command))
if not validation:
for line in unified_diff(output.split('\n'), verify.split('\n'), fromfile='generated', tofile='expected'):
print(line)
print()
else:
if self.verbose > 0:
print(test_id, ':', 'Skipped')
else:
sys.stdout.write('X')
if self.verbose == 0:
print()
if len(failed) > 0:
print("\nThe following examples failed:")
print(" ", "\n ".join(failed))
return len(failed)
def main(self):
self.file = open(self.sourcepath, encoding='utf-8')
self.current_line = 0
self.find_examples()
failed_examples = self.test_examples()
self.file.close()
return failed_examples
if __name__ == "__main__":
def getargs():
parser = argparse.ArgumentParser(prog='DocTests',
description='Test and validate ledger examples from the texinfo manual')
parser.add_argument('-v', '--verbose',
dest='verbose',
action='count',
default=0,
help='be verbose. Add -vv for more verbosity')
parser.add_argument('-l', '--ledger',
dest='ledger',
type=str,
action='store',
required=True,
help='the path to the ledger executable to test with')
parser.add_argument('-f', '--file',
dest='file',
type=str,
action='store',
required=True,
help='the texinfo documentation file to run the examples from')
parser.add_argument('examples',
metavar='EXAMPLE',
type=str,
nargs='*',
help='the examples to test')
return parser.parse_args()
args = getargs()
script = DocTests(args)
status = script.main()
sys.exit(status)
|