annotate makeitso/python_module/tests/test.py @ 185:0fd0cc154153

STUB: makeitso/makeitso.py setup.py
author Jeff Hammel <k0scist@gmail.com>
date Wed, 26 Mar 2014 21:28:47 -0700
parents 4ee086606772
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
141
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 doctest runner
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 """
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 import doctest
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import os
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import sys
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 from optparse import OptionParser
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 def run_tests(raise_on_error=False, report_first=False):
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 # add results here
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 results = {}
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 # doctest arguments
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 directory = os.path.dirname(os.path.abspath(__file__))
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 extraglobs = {'here': directory}
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error)
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 if report_first:
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 # gather tests
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 tests = [ test for test in os.listdir(directory)
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 if test.endswith('.txt') ]
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 # run the tests
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 for test in tests:
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 try:
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 results[test] = doctest.testfile(test, **doctest_args)
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 except doctest.DocTestFailure, failure:
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 raise
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 except doctest.UnexpectedException, failure:
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 raise failure.exc_info[0], failure.exc_info[1], failure.exc_info[2]
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 return results
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 def main(args=sys.argv[1:]):
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 # parse command line args
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 parser = OptionParser(description=__doc__)
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 parser.add_option('--raise', dest='raise_on_error',
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 default=False, action='store_true',
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 help="raise on first error")
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 parser.add_option('--report-first', dest='report_first',
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 default=False, action='store_true',
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 help="report the first error only (all tests will still run)")
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 options, args = parser.parse_args(args)
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 # run the tests
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 results = run_tests(**options.__dict__)
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 if sum([i.failed for i in results.values()]):
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 sys.exit(1) # error
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 if __name__ == '__main__':
4ee086606772 add extra data
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 main()