6
|
1 #!/usr/bin/env python
|
|
2
|
|
3 """
|
32
|
4 doctest runner for simpypi tests
|
6
|
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
|
9
|
13
|
6
|
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:
|
8
|
32
|
|
33 # make a temporary directory
|
|
34 tmpdir = tempfile.mkdtemp()
|
|
35 doctest_args['extraglobs']['directory'] = tmpdir
|
|
36
|
6
|
37 try:
|
|
38 results[test] = doctest.testfile(test, **doctest_args)
|
|
39 except doctest.DocTestFailure, failure:
|
|
40 raise
|
|
41 except doctest.UnexpectedException, failure:
|
|
42 raise failure.exc_info[0], failure.exc_info[1], failure.exc_info[2]
|
8
|
43 finally:
|
|
44 if os.path.exists(tmpdir):
|
|
45 shutil.rmtree(tmpdir)
|
6
|
46
|
|
47 return results
|
|
48
|
|
49 def main(args=sys.argv[1:]):
|
|
50
|
|
51 # parse command line args
|
|
52 parser = OptionParser(description=__doc__)
|
|
53 parser.add_option('--raise', dest='raise_on_error',
|
|
54 default=False, action='store_true',
|
|
55 help="raise on first error")
|
|
56 parser.add_option('--report-first', dest='report_first',
|
|
57 default=False, action='store_true',
|
|
58 help="report the first error only (all tests will still run)")
|
|
59 options, args = parser.parse_args(args)
|
|
60
|
|
61 # run the tests
|
|
62 results = run_tests(**options.__dict__)
|
|
63 if sum([i.failed for i in results.values()]):
|
|
64 sys.exit(1) # error
|
|
65
|
|
66 if __name__ == '__main__':
|
|
67 main()
|
|
68
|