-
Notifications
You must be signed in to change notification settings - Fork 0
/
doctest_runner.py
69 lines (52 loc) · 1.92 KB
/
doctest_runner.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
import argparse
import doctest
from re import sub
IGNORE_DOCKER = doctest.register_optionflag('IGNORE_DOCKER')
IGNORE_KOMPATIBLE = doctest.register_optionflag('IGNORE_KOMPATIBLE')
class UnfussyOutputChecker(doctest.OutputChecker):
def check_output(self, want, got, optionflags):
# Ignore differences in strings between 2 and 3:
got = sub(r"\bu'", "'", got) # python 2
got = sub(r"\bb'", "'", got) # python 3
# Allow different assertions for docker and kompatible:
if optionflags & IGNORE_KOMPATIBLE:
want = sub(r'.+# kompatible\s+', '', want)
if optionflags & IGNORE_DOCKER:
want = sub(r'.+# docker\s+', '', want)
want = sub(r'\s+# (docker|kompatible)', '', want)
# Handle any other optionflags
return doctest.OutputChecker.check_output(self, want, got, optionflags)
def get_args():
parser = argparse.ArgumentParser(description='Run doc tests')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--docker', action='store_true')
group.add_argument('--kompatible', action='store_true')
return parser.parse_args()
def get_sdk(args):
if args.docker:
import docker as sdk
else:
import kompatible as sdk
return sdk
def get_options(args):
if args.docker:
return IGNORE_KOMPATIBLE
else:
return IGNORE_DOCKER
def main():
filename = 'README.md'
with open(filename) as f:
readme = f.read()
args = get_args()
examples = doctest.DocTestParser().get_doctest(
string=readme, globs={'sdk': get_sdk(args)},
name=filename, filename=None, lineno=0)
runner = doctest.DocTestRunner(
checker=UnfussyOutputChecker(),
optionflags=get_options(args) | doctest.ELLIPSIS)
runner.run(examples)
(failed, attempted) = runner.summarize()
if failed > 0:
exit(1)
if __name__ == '__main__':
main()