annotate makeitso/python_package/tests/run_doctests.py @ 232:66ca1a67a76c

one more time
author Jeff Hammel <k0scist@gmail.com>
date Mon, 14 Dec 2015 14:16:39 -0800
parents 44bd0cc9bbc3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
81
0d5fac58b1a8 add a template for doctests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
0d5fac58b1a8 add a template for doctests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
0d5fac58b1a8 add a template for doctests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
0d5fac58b1a8 add a template for doctests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 doctest runner
0d5fac58b1a8 add a template for doctests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 """
0d5fac58b1a8 add a template for doctests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
0d5fac58b1a8 add a template for doctests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 import doctest
0d5fac58b1a8 add a template for doctests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import os
123
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
9 import sys
125
19ffc278e35c do the import right
Jeff Hammel <jhammel@mozilla.com>
parents: 124
diff changeset
10 from optparse import OptionParser
19ffc278e35c do the import right
Jeff Hammel <jhammel@mozilla.com>
parents: 124
diff changeset
11
81
0d5fac58b1a8 add a template for doctests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12
123
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
13 def run_tests(raise_on_error=False, report_first=False):
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
14
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
15 # add results here
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
16 results = {}
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
17
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
18 # doctest arguments
81
0d5fac58b1a8 add a template for doctests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 directory = os.path.dirname(os.path.abspath(__file__))
0d5fac58b1a8 add a template for doctests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 extraglobs = {'here': directory}
123
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
21 doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error)
145
c07dd14d5842 we always want doctest.ELLIPSIS. why the hell wouldnt we?
Jeff Hammel <jhammel@mozilla.com>
parents: 127
diff changeset
22 doctest_args['optionsflags'] = doctest.ELLIPSIS
123
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
23 if report_first:
145
c07dd14d5842 we always want doctest.ELLIPSIS. why the hell wouldnt we?
Jeff Hammel <jhammel@mozilla.com>
parents: 127
diff changeset
24 doctest_args['optionflags'] |= doctest.REPORT_ONLY_FIRST_FAILURE
123
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
25
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
26 # gather tests
146
ebaee69c5ea4 whitespace
Jeff Hammel <jhammel@mozilla.com>
parents: 145
diff changeset
27 tests = [test for test in os.listdir(directory)
ebaee69c5ea4 whitespace
Jeff Hammel <jhammel@mozilla.com>
parents: 145
diff changeset
28 if test.endswith('.txt')]
123
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
29
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
30 # run the tests
81
0d5fac58b1a8 add a template for doctests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 for test in tests:
124
3eee157f4564 correct syntax
Jeff Hammel <jhammel@mozilla.com>
parents: 123
diff changeset
32 try:
3eee157f4564 correct syntax
Jeff Hammel <jhammel@mozilla.com>
parents: 123
diff changeset
33 results[test] = doctest.testfile(test, **doctest_args)
3eee157f4564 correct syntax
Jeff Hammel <jhammel@mozilla.com>
parents: 123
diff changeset
34 except doctest.DocTestFailure, failure:
3eee157f4564 correct syntax
Jeff Hammel <jhammel@mozilla.com>
parents: 123
diff changeset
35 raise
3eee157f4564 correct syntax
Jeff Hammel <jhammel@mozilla.com>
parents: 123
diff changeset
36 except doctest.UnexpectedException, failure:
3eee157f4564 correct syntax
Jeff Hammel <jhammel@mozilla.com>
parents: 123
diff changeset
37 raise failure.exc_info[0], failure.exc_info[1], failure.exc_info[2]
123
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
38
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
39 return results
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
40
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
41 def main(args=sys.argv[1:]):
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
42
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
43 # parse command line args
126
ea3225facbcf add a description to the OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents: 125
diff changeset
44 parser = OptionParser(description=__doc__)
123
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
45 parser.add_option('--raise', dest='raise_on_error',
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
46 default=False, action='store_true',
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
47 help="raise on first error")
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
48 parser.add_option('--report-first', dest='report_first',
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
49 default=False, action='store_true',
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
50 help="report the first error only (all tests will still run)")
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
51 options, args = parser.parse_args(args)
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
52
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
53 # run the tests
127
Jeff Hammel <jhammel@mozilla.com>
parents: 126
diff changeset
54 results = run_tests(**options.__dict__)
123
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
55 if sum([i.failed for i in results.values()]):
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
56 sys.exit(1) # error
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
57
81
0d5fac58b1a8 add a template for doctests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58
0d5fac58b1a8 add a template for doctests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 if __name__ == '__main__':
123
8db34885ebe4 tame that beast called doctest
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
60 main()