annotate tests/test.py @ 27:7e0c931a201d

web licenser now works
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 25 Nov 2011 00:57:38 -0800
parents d3e3a506dd29
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 doctest runner
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 """
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 import doctest
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import os
15
cf920f85fb98 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
9 import shutil
8
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 import sys
15
cf920f85fb98 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
11 import tempfile
8
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 from optparse import OptionParser
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13
24
d3e3a506dd29 tests now pass
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
14 def run_tests(raise_on_error=False, report_first=False, cleanup=True):
8
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 # add results here
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 results = {}
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 # doctest arguments
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 directory = os.path.dirname(os.path.abspath(__file__))
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 extraglobs = {'here': directory}
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error)
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 if report_first:
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 # gather tests
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 tests = [ test for test in os.listdir(directory)
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 if test.endswith('.txt') ]
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 # run the tests
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 for test in tests:
15
cf920f85fb98 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
32
cf920f85fb98 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
33 # make a temporary directory for example files
cf920f85fb98 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
34 tempdir = tempfile.mktemp()
cf920f85fb98 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
35 shutil.copytree(os.path.join(directory, 'example'), tempdir)
cf920f85fb98 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
36 doctest_args['extraglobs']['directory'] = tempdir
cf920f85fb98 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
37
8
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 try:
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 results[test] = doctest.testfile(test, **doctest_args)
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 except doctest.DocTestFailure, failure:
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 raise
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 except doctest.UnexpectedException, failure:
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 raise failure.exc_info[0], failure.exc_info[1], failure.exc_info[2]
15
cf920f85fb98 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
44 finally:
cf920f85fb98 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
45 # cleanup
24
d3e3a506dd29 tests now pass
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
46 if cleanup:
d3e3a506dd29 tests now pass
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
47 shutil.rmtree(tempdir)
d3e3a506dd29 tests now pass
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
48 else:
d3e3a506dd29 tests now pass
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
49 print '%s: %s' % (test, tempdir)
8
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 return results
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 def main(args=sys.argv[1:]):
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 # parse command line args
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 parser = OptionParser(description=__doc__)
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 parser.add_option('--raise', dest='raise_on_error',
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 default=False, action='store_true',
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 help="raise on first error")
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 parser.add_option('--report-first', dest='report_first',
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 default=False, action='store_true',
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 help="report the first error only (all tests will still run)")
24
d3e3a506dd29 tests now pass
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
63 parser.add_option('--no-cleanup', dest='cleanup',
d3e3a506dd29 tests now pass
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
64 action='store_false', default=True,
d3e3a506dd29 tests now pass
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
65 help="don't cleanup following the tests")
8
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66 options, args = parser.parse_args(args)
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
67
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68 # run the tests
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69 results = run_tests(**options.__dict__)
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
70 if sum([i.failed for i in results.values()]):
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71 sys.exit(1) # error
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
72
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
73
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74 if __name__ == '__main__':
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
75 main()
c3c65fb15ab3 stub tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
76