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
|
|
14 def run_tests(raise_on_error=False, report_first=False):
|
|
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
|
|
46 shutil.rmtree(tempdir)
|
8
|
47
|
|
48 return results
|
|
49
|
|
50 def main(args=sys.argv[1:]):
|
|
51
|
|
52 # parse command line args
|
|
53 parser = OptionParser(description=__doc__)
|
|
54 parser.add_option('--raise', dest='raise_on_error',
|
|
55 default=False, action='store_true',
|
|
56 help="raise on first error")
|
|
57 parser.add_option('--report-first', dest='report_first',
|
|
58 default=False, action='store_true',
|
|
59 help="report the first error only (all tests will still run)")
|
|
60 options, args = parser.parse_args(args)
|
|
61
|
|
62 # run the tests
|
|
63 results = run_tests(**options.__dict__)
|
|
64 if sum([i.failed for i in results.values()]):
|
|
65 sys.exit(1) # error
|
|
66
|
|
67
|
|
68 if __name__ == '__main__':
|
|
69 main()
|
|
70
|