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