Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | """Supporting definitions for the Python regression tests.""" |
2 | ||
3 | if __name__ != 'test.test_support': | |
4 | raise ImportError, 'test_support must be imported from the test package' | |
5 | ||
6 | import sys | |
7 | ||
8 | class Error(Exception): | |
9 | """Base class for regression test exceptions.""" | |
10 | ||
11 | class TestFailed(Error): | |
12 | """Test failed.""" | |
13 | ||
14 | class TestSkipped(Error): | |
15 | """Test skipped. | |
16 | ||
17 | This can be raised to indicate that a test was deliberatly | |
18 | skipped, but not because a feature wasn't available. For | |
19 | example, if some resource can't be used, such as the network | |
20 | appears to be unavailable, this should be raised instead of | |
21 | TestFailed. | |
22 | """ | |
23 | ||
24 | class ResourceDenied(TestSkipped): | |
25 | """Test skipped because it requested a disallowed resource. | |
26 | ||
27 | This is raised when a test calls requires() for a resource that | |
28 | has not be enabled. It is used to distinguish between expected | |
29 | and unexpected skips. | |
30 | """ | |
31 | ||
32 | verbose = 1 # Flag set to 0 by regrtest.py | |
33 | use_resources = None # Flag set to [] by regrtest.py | |
34 | ||
35 | # _original_stdout is meant to hold stdout at the time regrtest began. | |
36 | # This may be "the real" stdout, or IDLE's emulation of stdout, or whatever. | |
37 | # The point is to have some flavor of stdout the user can actually see. | |
38 | _original_stdout = None | |
39 | def record_original_stdout(stdout): | |
40 | global _original_stdout | |
41 | _original_stdout = stdout | |
42 | ||
43 | def get_original_stdout(): | |
44 | return _original_stdout or sys.stdout | |
45 | ||
46 | def unload(name): | |
47 | try: | |
48 | del sys.modules[name] | |
49 | except KeyError: | |
50 | pass | |
51 | ||
52 | def forget(modname): | |
53 | '''"Forget" a module was ever imported by removing it from sys.modules and | |
54 | deleting any .pyc and .pyo files.''' | |
55 | unload(modname) | |
56 | import os | |
57 | for dirname in sys.path: | |
58 | try: | |
59 | os.unlink(os.path.join(dirname, modname + os.extsep + 'pyc')) | |
60 | except os.error: | |
61 | pass | |
62 | # Deleting the .pyo file cannot be within the 'try' for the .pyc since | |
63 | # the chance exists that there is no .pyc (and thus the 'try' statement | |
64 | # is exited) but there is a .pyo file. | |
65 | try: | |
66 | os.unlink(os.path.join(dirname, modname + os.extsep + 'pyo')) | |
67 | except os.error: | |
68 | pass | |
69 | ||
70 | def is_resource_enabled(resource): | |
71 | """Test whether a resource is enabled. Known resources are set by | |
72 | regrtest.py.""" | |
73 | return use_resources is not None and resource in use_resources | |
74 | ||
75 | def requires(resource, msg=None): | |
76 | """Raise ResourceDenied if the specified resource is not available. | |
77 | ||
78 | If the caller's module is __main__ then automatically return True. The | |
79 | possibility of False being returned occurs when regrtest.py is executing.""" | |
80 | # see if the caller's module is __main__ - if so, treat as if | |
81 | # the resource was set | |
82 | if sys._getframe().f_back.f_globals.get("__name__") == "__main__": | |
83 | return | |
84 | if not is_resource_enabled(resource): | |
85 | if msg is None: | |
86 | msg = "Use of the `%s' resource not enabled" % resource | |
87 | raise ResourceDenied(msg) | |
88 | ||
89 | FUZZ = 1e-6 | |
90 | ||
91 | def fcmp(x, y): # fuzzy comparison function | |
92 | if type(x) == type(0.0) or type(y) == type(0.0): | |
93 | try: | |
94 | x, y = coerce(x, y) | |
95 | fuzz = (abs(x) + abs(y)) * FUZZ | |
96 | if abs(x-y) <= fuzz: | |
97 | return 0 | |
98 | except: | |
99 | pass | |
100 | elif type(x) == type(y) and type(x) in (type(()), type([])): | |
101 | for i in range(min(len(x), len(y))): | |
102 | outcome = fcmp(x[i], y[i]) | |
103 | if outcome != 0: | |
104 | return outcome | |
105 | return cmp(len(x), len(y)) | |
106 | return cmp(x, y) | |
107 | ||
108 | try: | |
109 | unicode | |
110 | have_unicode = 1 | |
111 | except NameError: | |
112 | have_unicode = 0 | |
113 | ||
114 | is_jython = sys.platform.startswith('java') | |
115 | ||
116 | import os | |
117 | # Filename used for testing | |
118 | if os.name == 'java': | |
119 | # Jython disallows @ in module names | |
120 | TESTFN = '$test' | |
121 | elif os.name == 'riscos': | |
122 | TESTFN = 'testfile' | |
123 | else: | |
124 | TESTFN = '@test' | |
125 | # Unicode name only used if TEST_FN_ENCODING exists for the platform. | |
126 | if have_unicode: | |
127 | # Assuming sys.getfilesystemencoding()!=sys.getdefaultencoding() | |
128 | # TESTFN_UNICODE is a filename that can be encoded using the | |
129 | # file system encoding, but *not* with the default (ascii) encoding | |
130 | if isinstance('', unicode): | |
131 | # python -U | |
132 | # XXX perhaps unicode() should accept Unicode strings? | |
133 | TESTFN_UNICODE = "@test-\xe0\xf2" | |
134 | else: | |
135 | # 2 latin characters. | |
136 | TESTFN_UNICODE = unicode("@test-\xe0\xf2", "latin-1") | |
137 | TESTFN_ENCODING = sys.getfilesystemencoding() | |
138 | # TESTFN_UNICODE_UNENCODEABLE is a filename that should *not* be | |
139 | # able to be encoded by *either* the default or filesystem encoding. | |
140 | # This test really only makes sense on Windows NT platforms | |
141 | # which have special Unicode support in posixmodule. | |
142 | if (not hasattr(sys, "getwindowsversion") or | |
143 | sys.getwindowsversion()[3] < 2): # 0=win32s or 1=9x/ME | |
144 | TESTFN_UNICODE_UNENCODEABLE = None | |
145 | else: | |
146 | # Japanese characters (I think - from bug 846133) | |
147 | TESTFN_UNICODE_UNENCODEABLE = eval('u"@test-\u5171\u6709\u3055\u308c\u308b"') | |
148 | try: | |
149 | # XXX - Note - should be using TESTFN_ENCODING here - but for | |
150 | # Windows, "mbcs" currently always operates as if in | |
151 | # errors=ignore' mode - hence we get '?' characters rather than | |
152 | # the exception. 'Latin1' operates as we expect - ie, fails. | |
153 | # See [ 850997 ] mbcs encoding ignores errors | |
154 | TESTFN_UNICODE_UNENCODEABLE.encode("Latin1") | |
155 | except UnicodeEncodeError: | |
156 | pass | |
157 | else: | |
158 | print \ | |
159 | 'WARNING: The filename %r CAN be encoded by the filesystem. ' \ | |
160 | 'Unicode filename tests may not be effective' \ | |
161 | % TESTFN_UNICODE_UNENCODEABLE | |
162 | ||
163 | # Make sure we can write to TESTFN, try in /tmp if we can't | |
164 | fp = None | |
165 | try: | |
166 | fp = open(TESTFN, 'w+') | |
167 | except IOError: | |
168 | TMP_TESTFN = os.path.join('/tmp', TESTFN) | |
169 | try: | |
170 | fp = open(TMP_TESTFN, 'w+') | |
171 | TESTFN = TMP_TESTFN | |
172 | del TMP_TESTFN | |
173 | except IOError: | |
174 | print ('WARNING: tests will fail, unable to write to: %s or %s' % | |
175 | (TESTFN, TMP_TESTFN)) | |
176 | if fp is not None: | |
177 | fp.close() | |
178 | try: | |
179 | os.unlink(TESTFN) | |
180 | except: | |
181 | pass | |
182 | del os, fp | |
183 | ||
184 | from os import unlink | |
185 | ||
186 | def findfile(file, here=__file__): | |
187 | """Try to find a file on sys.path and the working directory. If it is not | |
188 | found the argument passed to the function is returned (this does not | |
189 | necessarily signal failure; could still be the legitimate path).""" | |
190 | import os | |
191 | if os.path.isabs(file): | |
192 | return file | |
193 | path = sys.path | |
194 | path = [os.path.dirname(here)] + path | |
195 | for dn in path: | |
196 | fn = os.path.join(dn, file) | |
197 | if os.path.exists(fn): return fn | |
198 | return file | |
199 | ||
200 | def verify(condition, reason='test failed'): | |
201 | """Verify that condition is true. If not, raise TestFailed. | |
202 | ||
203 | The optional argument reason can be given to provide | |
204 | a better error text. | |
205 | """ | |
206 | ||
207 | if not condition: | |
208 | raise TestFailed(reason) | |
209 | ||
210 | def vereq(a, b): | |
211 | """Raise TestFailed if a == b is false. | |
212 | ||
213 | This is better than verify(a == b) because, in case of failure, the | |
214 | error message incorporates repr(a) and repr(b) so you can see the | |
215 | inputs. | |
216 | ||
217 | Note that "not (a == b)" isn't necessarily the same as "a != b"; the | |
218 | former is tested. | |
219 | """ | |
220 | ||
221 | if not (a == b): | |
222 | raise TestFailed, "%r == %r" % (a, b) | |
223 | ||
224 | def sortdict(dict): | |
225 | "Like repr(dict), but in sorted order." | |
226 | items = dict.items() | |
227 | items.sort() | |
228 | reprpairs = ["%r: %r" % pair for pair in items] | |
229 | withcommas = ", ".join(reprpairs) | |
230 | return "{%s}" % withcommas | |
231 | ||
232 | def check_syntax(statement): | |
233 | try: | |
234 | compile(statement, '<string>', 'exec') | |
235 | except SyntaxError: | |
236 | pass | |
237 | else: | |
238 | print 'Missing SyntaxError: "%s"' % statement | |
239 | ||
240 | ||
241 | ||
242 | #======================================================================= | |
243 | # Preliminary PyUNIT integration. | |
244 | ||
245 | import unittest | |
246 | ||
247 | ||
248 | class BasicTestRunner: | |
249 | def run(self, test): | |
250 | result = unittest.TestResult() | |
251 | test(result) | |
252 | return result | |
253 | ||
254 | ||
255 | def run_suite(suite, testclass=None): | |
256 | """Run tests from a unittest.TestSuite-derived class.""" | |
257 | if verbose: | |
258 | runner = unittest.TextTestRunner(sys.stdout, verbosity=2) | |
259 | else: | |
260 | runner = BasicTestRunner() | |
261 | ||
262 | result = runner.run(suite) | |
263 | if not result.wasSuccessful(): | |
264 | if len(result.errors) == 1 and not result.failures: | |
265 | err = result.errors[0][1] | |
266 | elif len(result.failures) == 1 and not result.errors: | |
267 | err = result.failures[0][1] | |
268 | else: | |
269 | if testclass is None: | |
270 | msg = "errors occurred; run in verbose mode for details" | |
271 | else: | |
272 | msg = "errors occurred in %s.%s" \ | |
273 | % (testclass.__module__, testclass.__name__) | |
274 | raise TestFailed(msg) | |
275 | raise TestFailed(err) | |
276 | ||
277 | ||
278 | def run_unittest(*classes): | |
279 | """Run tests from unittest.TestCase-derived classes.""" | |
280 | suite = unittest.TestSuite() | |
281 | for cls in classes: | |
282 | if isinstance(cls, (unittest.TestSuite, unittest.TestCase)): | |
283 | suite.addTest(cls) | |
284 | else: | |
285 | suite.addTest(unittest.makeSuite(cls)) | |
286 | if len(classes)==1: | |
287 | testclass = classes[0] | |
288 | else: | |
289 | testclass = None | |
290 | run_suite(suite, testclass) | |
291 | ||
292 | ||
293 | #======================================================================= | |
294 | # doctest driver. | |
295 | ||
296 | def run_doctest(module, verbosity=None): | |
297 | """Run doctest on the given module. Return (#failures, #tests). | |
298 | ||
299 | If optional argument verbosity is not specified (or is None), pass | |
300 | test_support's belief about verbosity on to doctest. Else doctest's | |
301 | usual behavior is used (it searches sys.argv for -v). | |
302 | """ | |
303 | ||
304 | import doctest | |
305 | ||
306 | if verbosity is None: | |
307 | verbosity = verbose | |
308 | else: | |
309 | verbosity = None | |
310 | ||
311 | # Direct doctest output (normally just errors) to real stdout; doctest | |
312 | # output shouldn't be compared by regrtest. | |
313 | save_stdout = sys.stdout | |
314 | sys.stdout = get_original_stdout() | |
315 | try: | |
316 | f, t = doctest.testmod(module, verbose=verbosity) | |
317 | if f: | |
318 | raise TestFailed("%d of %d doctests failed" % (f, t)) | |
319 | finally: | |
320 | sys.stdout = save_stdout | |
321 | if verbose: | |
322 | print 'doctest (%s) ... %d tests with zero failures' % (module.__name__, t) | |
323 | return f, t |