Mercurial > hg > simpypi
annotate tests/test.py @ 55:143adebe4caa
install package in a virtualenv and make sure importing is sane
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 01 Mar 2012 15:28:34 -0800 |
parents | 486a4640fc31 |
children | 2e60fb704454 |
rev | line source |
---|---|
6 | 1 #!/usr/bin/env python |
2 | |
3 """ | |
32 | 4 doctest runner for simpypi tests |
6 | 5 """ |
6 | |
7 import doctest | |
8 import os | |
8 | 9 import shutil |
6 | 10 import sys |
8 | 11 import tempfile |
55
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
12 import virtualenv |
6 | 13 from optparse import OptionParser |
9 | 14 |
55
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
15 def create_virtualenv(path): |
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
16 """create a virtualenv and return the path to the python interpreter therein""" |
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
17 virtualenv.create_environment(path) |
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
18 for python in (('bin', 'python'), ('Scripts', 'python.exe')): |
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
19 python = os.path.join(path, *python) |
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
20 if os.path.exists(python): |
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
21 return python |
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
22 raise Exception("Python binary not found in %s" % path) |
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
23 |
6 | 24 def run_tests(raise_on_error=False, report_first=False): |
25 | |
26 # add results here | |
27 results = {} | |
28 | |
29 # doctest arguments | |
30 directory = os.path.dirname(os.path.abspath(__file__)) | |
55
143adebe4caa
install package in a virtualenv and make sure importing is sane
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
31 extraglobs = {'here': directory, 'create_virtualenv': create_virtualenv} |
6 | 32 doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error) |
33 if report_first: | |
34 doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE | |
35 | |
36 # gather tests | |
37 tests = [ test for test in os.listdir(directory) | |
38 if test.endswith('.txt') ] | |
39 | |
40 # run the tests | |
41 for test in tests: | |
8 | 42 |
43 # make a temporary directory | |
44 tmpdir = tempfile.mkdtemp() | |
45 doctest_args['extraglobs']['directory'] = tmpdir | |
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 |