6
|
1 #!/usr/bin/env python
|
|
2
|
|
3 """
|
|
4 doctest runner
|
|
5 """
|
|
6
|
|
7 import doctest
|
|
8 import os
|
8
|
9 import shutil
|
6
|
10 import sys
|
8
|
11 import tempfile
|
6
|
12 from optparse import OptionParser
|
8
|
13 from paste.fixture import TestApp
|
6
|
14
|
|
15 def run_tests(raise_on_error=False, report_first=False):
|
|
16
|
|
17 # add results here
|
|
18 results = {}
|
|
19
|
|
20 # doctest arguments
|
|
21 directory = os.path.dirname(os.path.abspath(__file__))
|
|
22 extraglobs = {'here': directory}
|
|
23 doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error)
|
|
24 if report_first:
|
|
25 doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE
|
|
26
|
|
27 # gather tests
|
|
28 tests = [ test for test in os.listdir(directory)
|
|
29 if test.endswith('.txt') ]
|
|
30
|
|
31 # run the tests
|
|
32 for test in tests:
|
8
|
33
|
|
34 # make a temporary directory
|
|
35 tmpdir = tempfile.mkdtemp()
|
|
36 doctest_args['extraglobs']['directory'] = tmpdir
|
|
37
|
6
|
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]
|
8
|
44 finally:
|
|
45 if os.path.exists(tmpdir):
|
|
46 shutil.rmtree(tmpdir)
|
6
|
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
|