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