8
|
1 #!/usr/bin/env python
|
|
2
|
|
3 """
|
|
4 doctest runner
|
|
5 """
|
|
6
|
|
7 import doctest
|
|
8 import os
|
15
|
9 import shutil
|
8
|
10 import sys
|
15
|
11 import tempfile
|
8
|
12 from optparse import OptionParser
|
|
13
|
24
|
14 def run_tests(raise_on_error=False, report_first=False, cleanup=True):
|
8
|
15
|
|
16 # add results here
|
|
17 results = {}
|
|
18
|
|
19 # doctest arguments
|
|
20 directory = os.path.dirname(os.path.abspath(__file__))
|
|
21 extraglobs = {'here': directory}
|
|
22 doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error)
|
|
23 if report_first:
|
|
24 doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE
|
|
25
|
|
26 # gather tests
|
|
27 tests = [ test for test in os.listdir(directory)
|
|
28 if test.endswith('.txt') ]
|
|
29
|
|
30 # run the tests
|
|
31 for test in tests:
|
15
|
32
|
|
33 # make a temporary directory for example files
|
|
34 tempdir = tempfile.mktemp()
|
|
35 shutil.copytree(os.path.join(directory, 'example'), tempdir)
|
|
36 doctest_args['extraglobs']['directory'] = tempdir
|
|
37
|
8
|
38 try:
|
|
39 results[test] = doctest.testfile(test, **doctest_args)
|
|
40 except doctest.DocTestFailure, failure:
|
|
41 raise
|
|
42 except doctest.UnexpectedException, failure:
|
|
43 raise failure.exc_info[0], failure.exc_info[1], failure.exc_info[2]
|
15
|
44 finally:
|
|
45 # cleanup
|
24
|
46 if cleanup:
|
|
47 shutil.rmtree(tempdir)
|
|
48 else:
|
|
49 print '%s: %s' % (test, tempdir)
|
8
|
50
|
|
51 return results
|
|
52
|
|
53 def main(args=sys.argv[1:]):
|
|
54
|
|
55 # parse command line args
|
|
56 parser = OptionParser(description=__doc__)
|
|
57 parser.add_option('--raise', dest='raise_on_error',
|
|
58 default=False, action='store_true',
|
|
59 help="raise on first error")
|
|
60 parser.add_option('--report-first', dest='report_first',
|
|
61 default=False, action='store_true',
|
|
62 help="report the first error only (all tests will still run)")
|
24
|
63 parser.add_option('--no-cleanup', dest='cleanup',
|
|
64 action='store_false', default=True,
|
|
65 help="don't cleanup following the tests")
|
8
|
66 options, args = parser.parse_args(args)
|
|
67
|
|
68 # run the tests
|
|
69 results = run_tests(**options.__dict__)
|
|
70 if sum([i.failed for i in results.values()]):
|
|
71 sys.exit(1) # error
|
|
72
|
|
73
|
|
74 if __name__ == '__main__':
|
|
75 main()
|
|
76
|