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