1 | n/a | import importlib |
---|
2 | n/a | import shutil |
---|
3 | n/a | import stat |
---|
4 | n/a | import sys |
---|
5 | n/a | import os |
---|
6 | n/a | import unittest |
---|
7 | n/a | import socket |
---|
8 | n/a | import tempfile |
---|
9 | n/a | import errno |
---|
10 | n/a | from test import support |
---|
11 | n/a | |
---|
12 | n/a | TESTFN = support.TESTFN |
---|
13 | n/a | |
---|
14 | n/a | |
---|
15 | n/a | class TestSupport(unittest.TestCase): |
---|
16 | n/a | |
---|
17 | n/a | def test_import_module(self): |
---|
18 | n/a | support.import_module("ftplib") |
---|
19 | n/a | self.assertRaises(unittest.SkipTest, support.import_module, "foo") |
---|
20 | n/a | |
---|
21 | n/a | def test_import_fresh_module(self): |
---|
22 | n/a | support.import_fresh_module("ftplib") |
---|
23 | n/a | |
---|
24 | n/a | def test_get_attribute(self): |
---|
25 | n/a | self.assertEqual(support.get_attribute(self, "test_get_attribute"), |
---|
26 | n/a | self.test_get_attribute) |
---|
27 | n/a | self.assertRaises(unittest.SkipTest, support.get_attribute, self, "foo") |
---|
28 | n/a | |
---|
29 | n/a | @unittest.skip("failing buildbots") |
---|
30 | n/a | def test_get_original_stdout(self): |
---|
31 | n/a | self.assertEqual(support.get_original_stdout(), sys.stdout) |
---|
32 | n/a | |
---|
33 | n/a | def test_unload(self): |
---|
34 | n/a | import sched |
---|
35 | n/a | self.assertIn("sched", sys.modules) |
---|
36 | n/a | support.unload("sched") |
---|
37 | n/a | self.assertNotIn("sched", sys.modules) |
---|
38 | n/a | |
---|
39 | n/a | def test_unlink(self): |
---|
40 | n/a | with open(TESTFN, "w") as f: |
---|
41 | n/a | pass |
---|
42 | n/a | support.unlink(TESTFN) |
---|
43 | n/a | self.assertFalse(os.path.exists(TESTFN)) |
---|
44 | n/a | support.unlink(TESTFN) |
---|
45 | n/a | |
---|
46 | n/a | def test_rmtree(self): |
---|
47 | n/a | dirpath = support.TESTFN + 'd' |
---|
48 | n/a | subdirpath = os.path.join(dirpath, 'subdir') |
---|
49 | n/a | os.mkdir(dirpath) |
---|
50 | n/a | os.mkdir(subdirpath) |
---|
51 | n/a | support.rmtree(dirpath) |
---|
52 | n/a | self.assertFalse(os.path.exists(dirpath)) |
---|
53 | n/a | with support.swap_attr(support, 'verbose', 0): |
---|
54 | n/a | support.rmtree(dirpath) |
---|
55 | n/a | |
---|
56 | n/a | os.mkdir(dirpath) |
---|
57 | n/a | os.mkdir(subdirpath) |
---|
58 | n/a | os.chmod(dirpath, stat.S_IRUSR|stat.S_IXUSR) |
---|
59 | n/a | with support.swap_attr(support, 'verbose', 0): |
---|
60 | n/a | support.rmtree(dirpath) |
---|
61 | n/a | self.assertFalse(os.path.exists(dirpath)) |
---|
62 | n/a | |
---|
63 | n/a | os.mkdir(dirpath) |
---|
64 | n/a | os.mkdir(subdirpath) |
---|
65 | n/a | os.chmod(dirpath, 0) |
---|
66 | n/a | with support.swap_attr(support, 'verbose', 0): |
---|
67 | n/a | support.rmtree(dirpath) |
---|
68 | n/a | self.assertFalse(os.path.exists(dirpath)) |
---|
69 | n/a | |
---|
70 | n/a | def test_forget(self): |
---|
71 | n/a | mod_filename = TESTFN + '.py' |
---|
72 | n/a | with open(mod_filename, 'w') as f: |
---|
73 | n/a | print('foo = 1', file=f) |
---|
74 | n/a | sys.path.insert(0, os.curdir) |
---|
75 | n/a | importlib.invalidate_caches() |
---|
76 | n/a | try: |
---|
77 | n/a | mod = __import__(TESTFN) |
---|
78 | n/a | self.assertIn(TESTFN, sys.modules) |
---|
79 | n/a | |
---|
80 | n/a | support.forget(TESTFN) |
---|
81 | n/a | self.assertNotIn(TESTFN, sys.modules) |
---|
82 | n/a | finally: |
---|
83 | n/a | del sys.path[0] |
---|
84 | n/a | support.unlink(mod_filename) |
---|
85 | n/a | support.rmtree('__pycache__') |
---|
86 | n/a | |
---|
87 | n/a | def test_HOST(self): |
---|
88 | n/a | s = socket.socket() |
---|
89 | n/a | s.bind((support.HOST, 0)) |
---|
90 | n/a | s.close() |
---|
91 | n/a | |
---|
92 | n/a | def test_find_unused_port(self): |
---|
93 | n/a | port = support.find_unused_port() |
---|
94 | n/a | s = socket.socket() |
---|
95 | n/a | s.bind((support.HOST, port)) |
---|
96 | n/a | s.close() |
---|
97 | n/a | |
---|
98 | n/a | def test_bind_port(self): |
---|
99 | n/a | s = socket.socket() |
---|
100 | n/a | support.bind_port(s) |
---|
101 | n/a | s.listen() |
---|
102 | n/a | s.close() |
---|
103 | n/a | |
---|
104 | n/a | # Tests for temp_dir() |
---|
105 | n/a | |
---|
106 | n/a | def test_temp_dir(self): |
---|
107 | n/a | """Test that temp_dir() creates and destroys its directory.""" |
---|
108 | n/a | parent_dir = tempfile.mkdtemp() |
---|
109 | n/a | parent_dir = os.path.realpath(parent_dir) |
---|
110 | n/a | |
---|
111 | n/a | try: |
---|
112 | n/a | path = os.path.join(parent_dir, 'temp') |
---|
113 | n/a | self.assertFalse(os.path.isdir(path)) |
---|
114 | n/a | with support.temp_dir(path) as temp_path: |
---|
115 | n/a | self.assertEqual(temp_path, path) |
---|
116 | n/a | self.assertTrue(os.path.isdir(path)) |
---|
117 | n/a | self.assertFalse(os.path.isdir(path)) |
---|
118 | n/a | finally: |
---|
119 | n/a | support.rmtree(parent_dir) |
---|
120 | n/a | |
---|
121 | n/a | def test_temp_dir__path_none(self): |
---|
122 | n/a | """Test passing no path.""" |
---|
123 | n/a | with support.temp_dir() as temp_path: |
---|
124 | n/a | self.assertTrue(os.path.isdir(temp_path)) |
---|
125 | n/a | self.assertFalse(os.path.isdir(temp_path)) |
---|
126 | n/a | |
---|
127 | n/a | def test_temp_dir__existing_dir__quiet_default(self): |
---|
128 | n/a | """Test passing a directory that already exists.""" |
---|
129 | n/a | def call_temp_dir(path): |
---|
130 | n/a | with support.temp_dir(path) as temp_path: |
---|
131 | n/a | raise Exception("should not get here") |
---|
132 | n/a | |
---|
133 | n/a | path = tempfile.mkdtemp() |
---|
134 | n/a | path = os.path.realpath(path) |
---|
135 | n/a | try: |
---|
136 | n/a | self.assertTrue(os.path.isdir(path)) |
---|
137 | n/a | self.assertRaises(FileExistsError, call_temp_dir, path) |
---|
138 | n/a | # Make sure temp_dir did not delete the original directory. |
---|
139 | n/a | self.assertTrue(os.path.isdir(path)) |
---|
140 | n/a | finally: |
---|
141 | n/a | shutil.rmtree(path) |
---|
142 | n/a | |
---|
143 | n/a | def test_temp_dir__existing_dir__quiet_true(self): |
---|
144 | n/a | """Test passing a directory that already exists with quiet=True.""" |
---|
145 | n/a | path = tempfile.mkdtemp() |
---|
146 | n/a | path = os.path.realpath(path) |
---|
147 | n/a | |
---|
148 | n/a | try: |
---|
149 | n/a | with support.check_warnings() as recorder: |
---|
150 | n/a | with support.temp_dir(path, quiet=True) as temp_path: |
---|
151 | n/a | self.assertEqual(path, temp_path) |
---|
152 | n/a | warnings = [str(w.message) for w in recorder.warnings] |
---|
153 | n/a | # Make sure temp_dir did not delete the original directory. |
---|
154 | n/a | self.assertTrue(os.path.isdir(path)) |
---|
155 | n/a | finally: |
---|
156 | n/a | shutil.rmtree(path) |
---|
157 | n/a | |
---|
158 | n/a | self.assertEqual(len(warnings), 1, warnings) |
---|
159 | n/a | warn = warnings[0] |
---|
160 | n/a | self.assertTrue(warn.startswith(f'tests may fail, unable to create ' |
---|
161 | n/a | f'temporary directory {path!r}: '), |
---|
162 | n/a | warn) |
---|
163 | n/a | |
---|
164 | n/a | # Tests for change_cwd() |
---|
165 | n/a | |
---|
166 | n/a | def test_change_cwd(self): |
---|
167 | n/a | original_cwd = os.getcwd() |
---|
168 | n/a | |
---|
169 | n/a | with support.temp_dir() as temp_path: |
---|
170 | n/a | with support.change_cwd(temp_path) as new_cwd: |
---|
171 | n/a | self.assertEqual(new_cwd, temp_path) |
---|
172 | n/a | self.assertEqual(os.getcwd(), new_cwd) |
---|
173 | n/a | |
---|
174 | n/a | self.assertEqual(os.getcwd(), original_cwd) |
---|
175 | n/a | |
---|
176 | n/a | def test_change_cwd__non_existent_dir(self): |
---|
177 | n/a | """Test passing a non-existent directory.""" |
---|
178 | n/a | original_cwd = os.getcwd() |
---|
179 | n/a | |
---|
180 | n/a | def call_change_cwd(path): |
---|
181 | n/a | with support.change_cwd(path) as new_cwd: |
---|
182 | n/a | raise Exception("should not get here") |
---|
183 | n/a | |
---|
184 | n/a | with support.temp_dir() as parent_dir: |
---|
185 | n/a | non_existent_dir = os.path.join(parent_dir, 'does_not_exist') |
---|
186 | n/a | self.assertRaises(FileNotFoundError, call_change_cwd, |
---|
187 | n/a | non_existent_dir) |
---|
188 | n/a | |
---|
189 | n/a | self.assertEqual(os.getcwd(), original_cwd) |
---|
190 | n/a | |
---|
191 | n/a | def test_change_cwd__non_existent_dir__quiet_true(self): |
---|
192 | n/a | """Test passing a non-existent directory with quiet=True.""" |
---|
193 | n/a | original_cwd = os.getcwd() |
---|
194 | n/a | |
---|
195 | n/a | with support.temp_dir() as parent_dir: |
---|
196 | n/a | bad_dir = os.path.join(parent_dir, 'does_not_exist') |
---|
197 | n/a | with support.check_warnings() as recorder: |
---|
198 | n/a | with support.change_cwd(bad_dir, quiet=True) as new_cwd: |
---|
199 | n/a | self.assertEqual(new_cwd, original_cwd) |
---|
200 | n/a | self.assertEqual(os.getcwd(), new_cwd) |
---|
201 | n/a | warnings = [str(w.message) for w in recorder.warnings] |
---|
202 | n/a | |
---|
203 | n/a | self.assertEqual(len(warnings), 1, warnings) |
---|
204 | n/a | warn = warnings[0] |
---|
205 | n/a | self.assertTrue(warn.startswith(f'tests may fail, unable to change ' |
---|
206 | n/a | f'the current working directory ' |
---|
207 | n/a | f'to {bad_dir!r}: '), |
---|
208 | n/a | warn) |
---|
209 | n/a | |
---|
210 | n/a | # Tests for change_cwd() |
---|
211 | n/a | |
---|
212 | n/a | def test_change_cwd__chdir_warning(self): |
---|
213 | n/a | """Check the warning message when os.chdir() fails.""" |
---|
214 | n/a | path = TESTFN + '_does_not_exist' |
---|
215 | n/a | with support.check_warnings() as recorder: |
---|
216 | n/a | with support.change_cwd(path=path, quiet=True): |
---|
217 | n/a | pass |
---|
218 | n/a | messages = [str(w.message) for w in recorder.warnings] |
---|
219 | n/a | |
---|
220 | n/a | self.assertEqual(len(messages), 1, messages) |
---|
221 | n/a | msg = messages[0] |
---|
222 | n/a | self.assertTrue(msg.startswith(f'tests may fail, unable to change ' |
---|
223 | n/a | f'the current working directory ' |
---|
224 | n/a | f'to {path!r}: '), |
---|
225 | n/a | msg) |
---|
226 | n/a | |
---|
227 | n/a | # Tests for temp_cwd() |
---|
228 | n/a | |
---|
229 | n/a | def test_temp_cwd(self): |
---|
230 | n/a | here = os.getcwd() |
---|
231 | n/a | with support.temp_cwd(name=TESTFN): |
---|
232 | n/a | self.assertEqual(os.path.basename(os.getcwd()), TESTFN) |
---|
233 | n/a | self.assertFalse(os.path.exists(TESTFN)) |
---|
234 | n/a | self.assertTrue(os.path.basename(os.getcwd()), here) |
---|
235 | n/a | |
---|
236 | n/a | |
---|
237 | n/a | def test_temp_cwd__name_none(self): |
---|
238 | n/a | """Test passing None to temp_cwd().""" |
---|
239 | n/a | original_cwd = os.getcwd() |
---|
240 | n/a | with support.temp_cwd(name=None) as new_cwd: |
---|
241 | n/a | self.assertNotEqual(new_cwd, original_cwd) |
---|
242 | n/a | self.assertTrue(os.path.isdir(new_cwd)) |
---|
243 | n/a | self.assertEqual(os.getcwd(), new_cwd) |
---|
244 | n/a | self.assertEqual(os.getcwd(), original_cwd) |
---|
245 | n/a | |
---|
246 | n/a | def test_sortdict(self): |
---|
247 | n/a | self.assertEqual(support.sortdict({3:3, 2:2, 1:1}), "{1: 1, 2: 2, 3: 3}") |
---|
248 | n/a | |
---|
249 | n/a | def test_make_bad_fd(self): |
---|
250 | n/a | fd = support.make_bad_fd() |
---|
251 | n/a | with self.assertRaises(OSError) as cm: |
---|
252 | n/a | os.write(fd, b"foo") |
---|
253 | n/a | self.assertEqual(cm.exception.errno, errno.EBADF) |
---|
254 | n/a | |
---|
255 | n/a | def test_check_syntax_error(self): |
---|
256 | n/a | support.check_syntax_error(self, "def class", lineno=1, offset=9) |
---|
257 | n/a | with self.assertRaises(AssertionError): |
---|
258 | n/a | support.check_syntax_error(self, "x=1") |
---|
259 | n/a | |
---|
260 | n/a | def test_CleanImport(self): |
---|
261 | n/a | import importlib |
---|
262 | n/a | with support.CleanImport("asyncore"): |
---|
263 | n/a | importlib.import_module("asyncore") |
---|
264 | n/a | |
---|
265 | n/a | def test_DirsOnSysPath(self): |
---|
266 | n/a | with support.DirsOnSysPath('foo', 'bar'): |
---|
267 | n/a | self.assertIn("foo", sys.path) |
---|
268 | n/a | self.assertIn("bar", sys.path) |
---|
269 | n/a | self.assertNotIn("foo", sys.path) |
---|
270 | n/a | self.assertNotIn("bar", sys.path) |
---|
271 | n/a | |
---|
272 | n/a | def test_captured_stdout(self): |
---|
273 | n/a | with support.captured_stdout() as stdout: |
---|
274 | n/a | print("hello") |
---|
275 | n/a | self.assertEqual(stdout.getvalue(), "hello\n") |
---|
276 | n/a | |
---|
277 | n/a | def test_captured_stderr(self): |
---|
278 | n/a | with support.captured_stderr() as stderr: |
---|
279 | n/a | print("hello", file=sys.stderr) |
---|
280 | n/a | self.assertEqual(stderr.getvalue(), "hello\n") |
---|
281 | n/a | |
---|
282 | n/a | def test_captured_stdin(self): |
---|
283 | n/a | with support.captured_stdin() as stdin: |
---|
284 | n/a | stdin.write('hello\n') |
---|
285 | n/a | stdin.seek(0) |
---|
286 | n/a | # call test code that consumes from sys.stdin |
---|
287 | n/a | captured = input() |
---|
288 | n/a | self.assertEqual(captured, "hello") |
---|
289 | n/a | |
---|
290 | n/a | def test_gc_collect(self): |
---|
291 | n/a | support.gc_collect() |
---|
292 | n/a | |
---|
293 | n/a | def test_python_is_optimized(self): |
---|
294 | n/a | self.assertIsInstance(support.python_is_optimized(), bool) |
---|
295 | n/a | |
---|
296 | n/a | def test_swap_attr(self): |
---|
297 | n/a | class Obj: |
---|
298 | n/a | x = 1 |
---|
299 | n/a | obj = Obj() |
---|
300 | n/a | with support.swap_attr(obj, "x", 5): |
---|
301 | n/a | self.assertEqual(obj.x, 5) |
---|
302 | n/a | self.assertEqual(obj.x, 1) |
---|
303 | n/a | |
---|
304 | n/a | def test_swap_item(self): |
---|
305 | n/a | D = {"item":1} |
---|
306 | n/a | with support.swap_item(D, "item", 5): |
---|
307 | n/a | self.assertEqual(D["item"], 5) |
---|
308 | n/a | self.assertEqual(D["item"], 1) |
---|
309 | n/a | |
---|
310 | n/a | class RefClass: |
---|
311 | n/a | attribute1 = None |
---|
312 | n/a | attribute2 = None |
---|
313 | n/a | _hidden_attribute1 = None |
---|
314 | n/a | __magic_1__ = None |
---|
315 | n/a | |
---|
316 | n/a | class OtherClass: |
---|
317 | n/a | attribute2 = None |
---|
318 | n/a | attribute3 = None |
---|
319 | n/a | __magic_1__ = None |
---|
320 | n/a | __magic_2__ = None |
---|
321 | n/a | |
---|
322 | n/a | def test_detect_api_mismatch(self): |
---|
323 | n/a | missing_items = support.detect_api_mismatch(self.RefClass, |
---|
324 | n/a | self.OtherClass) |
---|
325 | n/a | self.assertEqual({'attribute1'}, missing_items) |
---|
326 | n/a | |
---|
327 | n/a | missing_items = support.detect_api_mismatch(self.OtherClass, |
---|
328 | n/a | self.RefClass) |
---|
329 | n/a | self.assertEqual({'attribute3', '__magic_2__'}, missing_items) |
---|
330 | n/a | |
---|
331 | n/a | def test_detect_api_mismatch__ignore(self): |
---|
332 | n/a | ignore = ['attribute1', 'attribute3', '__magic_2__', 'not_in_either'] |
---|
333 | n/a | |
---|
334 | n/a | missing_items = support.detect_api_mismatch( |
---|
335 | n/a | self.RefClass, self.OtherClass, ignore=ignore) |
---|
336 | n/a | self.assertEqual(set(), missing_items) |
---|
337 | n/a | |
---|
338 | n/a | missing_items = support.detect_api_mismatch( |
---|
339 | n/a | self.OtherClass, self.RefClass, ignore=ignore) |
---|
340 | n/a | self.assertEqual(set(), missing_items) |
---|
341 | n/a | |
---|
342 | n/a | def test_check__all__(self): |
---|
343 | n/a | extra = {'tempdir'} |
---|
344 | n/a | blacklist = {'template'} |
---|
345 | n/a | support.check__all__(self, |
---|
346 | n/a | tempfile, |
---|
347 | n/a | extra=extra, |
---|
348 | n/a | blacklist=blacklist) |
---|
349 | n/a | |
---|
350 | n/a | extra = {'TextTestResult', 'installHandler'} |
---|
351 | n/a | blacklist = {'load_tests', "TestProgram", "BaseTestSuite"} |
---|
352 | n/a | |
---|
353 | n/a | support.check__all__(self, |
---|
354 | n/a | unittest, |
---|
355 | n/a | ("unittest.result", "unittest.case", |
---|
356 | n/a | "unittest.suite", "unittest.loader", |
---|
357 | n/a | "unittest.main", "unittest.runner", |
---|
358 | n/a | "unittest.signals"), |
---|
359 | n/a | extra=extra, |
---|
360 | n/a | blacklist=blacklist) |
---|
361 | n/a | |
---|
362 | n/a | self.assertRaises(AssertionError, support.check__all__, self, unittest) |
---|
363 | n/a | |
---|
364 | n/a | # XXX -follows a list of untested API |
---|
365 | n/a | # make_legacy_pyc |
---|
366 | n/a | # is_resource_enabled |
---|
367 | n/a | # requires |
---|
368 | n/a | # fcmp |
---|
369 | n/a | # umaks |
---|
370 | n/a | # findfile |
---|
371 | n/a | # check_warnings |
---|
372 | n/a | # EnvironmentVarGuard |
---|
373 | n/a | # TransientResource |
---|
374 | n/a | # transient_internet |
---|
375 | n/a | # run_with_locale |
---|
376 | n/a | # set_memlimit |
---|
377 | n/a | # bigmemtest |
---|
378 | n/a | # precisionbigmemtest |
---|
379 | n/a | # bigaddrspacetest |
---|
380 | n/a | # requires_resource |
---|
381 | n/a | # run_doctest |
---|
382 | n/a | # threading_cleanup |
---|
383 | n/a | # reap_threads |
---|
384 | n/a | # reap_children |
---|
385 | n/a | # strip_python_stderr |
---|
386 | n/a | # args_from_interpreter_flags |
---|
387 | n/a | # can_symlink |
---|
388 | n/a | # skip_unless_symlink |
---|
389 | n/a | # SuppressCrashReport |
---|
390 | n/a | |
---|
391 | n/a | |
---|
392 | n/a | def test_main(): |
---|
393 | n/a | tests = [TestSupport] |
---|
394 | n/a | support.run_unittest(*tests) |
---|
395 | n/a | |
---|
396 | n/a | if __name__ == '__main__': |
---|
397 | n/a | test_main() |
---|