|
14 | 14 | import os |
15 | 15 | import sys |
16 | 16 | import unittest |
17 | | -from io import StringIO |
18 | 17 |
|
19 | | -# XXX move helpers to support, add tests for them, remove things that |
20 | | -# duplicate test.support (or keep them for the backport; needs thinking) |
21 | | - |
22 | | -here = os.path.dirname(__file__) or os.curdir |
23 | | -verbose = 1 |
24 | 18 |
|
25 | 19 | def test_suite(): |
26 | 20 | suite = unittest.TestSuite() |
| 21 | + here = os.path.dirname(__file__) or os.curdir |
27 | 22 | for fn in os.listdir(here): |
28 | 23 | if fn.startswith("test") and fn.endswith(".py"): |
29 | 24 | modname = "packaging.tests." + fn[:-3] |
30 | 25 | __import__(modname) |
31 | 26 | module = sys.modules[modname] |
32 | 27 | suite.addTest(module.test_suite()) |
33 | 28 | return suite |
34 | | - |
35 | | - |
36 | | -class Error(Exception): |
37 | | - """Base class for regression test exceptions.""" |
38 | | - |
39 | | - |
40 | | -class TestFailed(Error): |
41 | | - """Test failed.""" |
42 | | - |
43 | | - |
44 | | -class BasicTestRunner: |
45 | | - def run(self, test): |
46 | | - result = unittest.TestResult() |
47 | | - test(result) |
48 | | - return result |
49 | | - |
50 | | - |
51 | | -def _run_suite(suite, verbose_=1): |
52 | | - """Run tests from a unittest.TestSuite-derived class.""" |
53 | | - global verbose |
54 | | - verbose = verbose_ |
55 | | - if verbose_: |
56 | | - runner = unittest.TextTestRunner(sys.stdout, verbosity=2) |
57 | | - else: |
58 | | - runner = BasicTestRunner() |
59 | | - |
60 | | - result = runner.run(suite) |
61 | | - if not result.wasSuccessful(): |
62 | | - if len(result.errors) == 1 and not result.failures: |
63 | | - err = result.errors[0][1] |
64 | | - elif len(result.failures) == 1 and not result.errors: |
65 | | - err = result.failures[0][1] |
66 | | - else: |
67 | | - err = "errors occurred; run in verbose mode for details" |
68 | | - raise TestFailed(err) |
69 | | - |
70 | | - |
71 | | -def run_unittest(classes, verbose_=1): |
72 | | - """Run tests from unittest.TestCase-derived classes. |
73 | | -
|
74 | | - Originally extracted from stdlib test.test_support and modified to |
75 | | - support unittest2. |
76 | | - """ |
77 | | - valid_types = (unittest.TestSuite, unittest.TestCase) |
78 | | - suite = unittest.TestSuite() |
79 | | - for cls in classes: |
80 | | - if isinstance(cls, str): |
81 | | - if cls in sys.modules: |
82 | | - suite.addTest(unittest.findTestCases(sys.modules[cls])) |
83 | | - else: |
84 | | - raise ValueError("str arguments must be keys in sys.modules") |
85 | | - elif isinstance(cls, valid_types): |
86 | | - suite.addTest(cls) |
87 | | - else: |
88 | | - suite.addTest(unittest.makeSuite(cls)) |
89 | | - _run_suite(suite, verbose_) |
90 | | - |
91 | | - |
92 | | -def reap_children(): |
93 | | - """Use this function at the end of test_main() whenever sub-processes |
94 | | - are started. This will help ensure that no extra children (zombies) |
95 | | - stick around to hog resources and create problems when looking |
96 | | - for refleaks. |
97 | | -
|
98 | | - Extracted from stdlib test.support. |
99 | | - """ |
100 | | - |
101 | | - # Reap all our dead child processes so we don't leave zombies around. |
102 | | - # These hog resources and might be causing some of the buildbots to die. |
103 | | - if hasattr(os, 'waitpid'): |
104 | | - any_process = -1 |
105 | | - while True: |
106 | | - try: |
107 | | - # This will raise an exception on Windows. That's ok. |
108 | | - pid, status = os.waitpid(any_process, os.WNOHANG) |
109 | | - if pid == 0: |
110 | | - break |
111 | | - except: |
112 | | - break |
113 | | - |
114 | | - |
115 | | -def captured_stdout(func, *args, **kw): |
116 | | - orig_stdout = getattr(sys, 'stdout') |
117 | | - setattr(sys, 'stdout', StringIO()) |
118 | | - try: |
119 | | - res = func(*args, **kw) |
120 | | - sys.stdout.seek(0) |
121 | | - return res, sys.stdout.read() |
122 | | - finally: |
123 | | - setattr(sys, 'stdout', orig_stdout) |
124 | | - |
125 | | - |
126 | | -def unload(name): |
127 | | - try: |
128 | | - del sys.modules[name] |
129 | | - except KeyError: |
130 | | - pass |
0 commit comments