Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1 | import importlib |
Nick Coghlan | 5517596 | 2013-07-28 22:11:50 +1000 | [diff] [blame] | 2 | import shutil |
Serhiy Storchaka | 6770f8a | 2016-11-20 16:16:06 +0200 | [diff] [blame] | 3 | import stat |
Eli Bendersky | 6c51999 | 2011-07-23 08:48:53 +0300 | [diff] [blame] | 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 |
Eli Bendersky | 6c51999 | 2011-07-23 08:48:53 +0300 | [diff] [blame] | 13 | |
| 14 | |
| 15 | class TestSupport(unittest.TestCase): |
Eli Bendersky | 6c51999 | 2011-07-23 08:48:53 +0300 | [diff] [blame] | 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 | |
Eli Bendersky | 7cd94a1 | 2011-07-23 15:00:31 +0300 | [diff] [blame] | 29 | @unittest.skip("failing buildbots") |
Eli Bendersky | 6c51999 | 2011-07-23 08:48:53 +0300 | [diff] [blame] | 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): |
Serhiy Storchaka | 6770f8a | 2016-11-20 16:16:06 +0200 | [diff] [blame] | 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) |
Martin Panter | 33623b1 | 2015-11-24 22:12:05 +0000 | [diff] [blame] | 55 | |
Serhiy Storchaka | 6770f8a | 2016-11-20 16:16:06 +0200 | [diff] [blame] | 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)) |
Eli Bendersky | 6c51999 | 2011-07-23 08:48:53 +0300 | [diff] [blame] | 69 | |
| 70 | def test_forget(self): |
Eli Bendersky | 8db645f | 2011-08-02 06:24:31 +0300 | [diff] [blame] | 71 | mod_filename = TESTFN + '.py' |
| 72 | with open(mod_filename, 'w') as f: |
| 73 | print('foo = 1', file=f) |
Eli Bendersky | 2b6ee28 | 2011-08-03 05:18:33 +0300 | [diff] [blame] | 74 | sys.path.insert(0, os.curdir) |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 75 | importlib.invalidate_caches() |
Eli Bendersky | 8db645f | 2011-08-02 06:24:31 +0300 | [diff] [blame] | 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: |
Eli Bendersky | 2b6ee28 | 2011-08-03 05:18:33 +0300 | [diff] [blame] | 83 | del sys.path[0] |
Eli Bendersky | 8db645f | 2011-08-02 06:24:31 +0300 | [diff] [blame] | 84 | support.unlink(mod_filename) |
Victor Stinner | 047b7ae | 2014-10-05 17:37:41 +0200 | [diff] [blame] | 85 | support.rmtree('__pycache__') |
Eli Bendersky | 6c51999 | 2011-07-23 08:48:53 +0300 | [diff] [blame] | 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) |
Charles-François Natali | 6e20460 | 2014-07-23 19:28:13 +0100 | [diff] [blame] | 101 | s.listen() |
Eli Bendersky | 6c51999 | 2011-07-23 08:48:53 +0300 | [diff] [blame] | 102 | s.close() |
| 103 | |
Nick Coghlan | 5517596 | 2013-07-28 22:11:50 +1000 | [diff] [blame] | 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: |
Benjamin Peterson | bb185ab | 2014-03-12 15:07:01 -0500 | [diff] [blame] | 119 | support.rmtree(parent_dir) |
Nick Coghlan | 5517596 | 2013-07-28 22:11:50 +1000 | [diff] [blame] | 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 | |
Eli Bendersky | 6c51999 | 2011-07-23 08:48:53 +0300 | [diff] [blame] | 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 | |
Nick Coghlan | 5517596 | 2013-07-28 22:11:50 +1000 | [diff] [blame] | 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) |
Ezio Melotti | 050a61f | 2012-09-21 16:53:07 +0300 | [diff] [blame] | 232 | |
Eli Bendersky | 6c51999 | 2011-07-23 08:48:53 +0300 | [diff] [blame] | 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): |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 243 | support.check_syntax_error(self, "def class", lineno=1, offset=9) |
Victor Stinner | 51d8c52 | 2016-02-08 17:57:02 +0100 | [diff] [blame] | 244 | with self.assertRaises(AssertionError): |
| 245 | support.check_syntax_error(self, "x=1") |
Eli Bendersky | 6c51999 | 2011-07-23 08:48:53 +0300 | [diff] [blame] | 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): |
R David Murray | 5a33f81 | 2013-07-11 12:28:40 -0400 | [diff] [blame] | 260 | with support.captured_stdout() as stdout: |
Eli Bendersky | d11c3e3 | 2011-07-23 08:51:14 +0300 | [diff] [blame] | 261 | print("hello") |
R David Murray | 5a33f81 | 2013-07-11 12:28:40 -0400 | [diff] [blame] | 262 | self.assertEqual(stdout.getvalue(), "hello\n") |
Eli Bendersky | 6c51999 | 2011-07-23 08:48:53 +0300 | [diff] [blame] | 263 | |
| 264 | def test_captured_stderr(self): |
R David Murray | 5a33f81 | 2013-07-11 12:28:40 -0400 | [diff] [blame] | 265 | with support.captured_stderr() as stderr: |
Eli Bendersky | d11c3e3 | 2011-07-23 08:51:14 +0300 | [diff] [blame] | 266 | print("hello", file=sys.stderr) |
R David Murray | 5a33f81 | 2013-07-11 12:28:40 -0400 | [diff] [blame] | 267 | self.assertEqual(stderr.getvalue(), "hello\n") |
Eli Bendersky | 6c51999 | 2011-07-23 08:48:53 +0300 | [diff] [blame] | 268 | |
| 269 | def test_captured_stdin(self): |
R David Murray | 5a33f81 | 2013-07-11 12:28:40 -0400 | [diff] [blame] | 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") |
Eli Bendersky | 6c51999 | 2011-07-23 08:48:53 +0300 | [diff] [blame] | 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 | |
Gregory P. Smith | 4e72cce | 2015-04-14 13:26:06 -0700 | [diff] [blame] | 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 | |
Gregory P. Smith | e334e3f | 2015-04-14 12:56:53 -0700 | [diff] [blame] | 309 | def test_detect_api_mismatch(self): |
Gregory P. Smith | 4e72cce | 2015-04-14 13:26:06 -0700 | [diff] [blame] | 310 | missing_items = support.detect_api_mismatch(self.RefClass, |
| 311 | self.OtherClass) |
Gregory P. Smith | e334e3f | 2015-04-14 12:56:53 -0700 | [diff] [blame] | 312 | self.assertEqual({'attribute1'}, missing_items) |
| 313 | |
Gregory P. Smith | 4e72cce | 2015-04-14 13:26:06 -0700 | [diff] [blame] | 314 | missing_items = support.detect_api_mismatch(self.OtherClass, |
| 315 | self.RefClass) |
Gregory P. Smith | e334e3f | 2015-04-14 12:56:53 -0700 | [diff] [blame] | 316 | self.assertEqual({'attribute3', '__magic_2__'}, missing_items) |
| 317 | |
| 318 | def test_detect_api_mismatch__ignore(self): |
Gregory P. Smith | e334e3f | 2015-04-14 12:56:53 -0700 | [diff] [blame] | 319 | ignore = ['attribute1', 'attribute3', '__magic_2__', 'not_in_either'] |
| 320 | |
Gregory P. Smith | 4e72cce | 2015-04-14 13:26:06 -0700 | [diff] [blame] | 321 | missing_items = support.detect_api_mismatch( |
| 322 | self.RefClass, self.OtherClass, ignore=ignore) |
Gregory P. Smith | e334e3f | 2015-04-14 12:56:53 -0700 | [diff] [blame] | 323 | self.assertEqual(set(), missing_items) |
| 324 | |
Gregory P. Smith | 4e72cce | 2015-04-14 13:26:06 -0700 | [diff] [blame] | 325 | missing_items = support.detect_api_mismatch( |
| 326 | self.OtherClass, self.RefClass, ignore=ignore) |
Gregory P. Smith | e334e3f | 2015-04-14 12:56:53 -0700 | [diff] [blame] | 327 | self.assertEqual(set(), missing_items) |
| 328 | |
Martin Panter | d226d30 | 2015-11-14 11:47:00 +0000 | [diff] [blame] | 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 | |
Eli Bendersky | 6c51999 | 2011-07-23 08:48:53 +0300 | [diff] [blame] | 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 |
Antoine Pitrou | 77e904e | 2013-10-08 23:04:32 +0200 | [diff] [blame] | 376 | # SuppressCrashReport |
Eli Bendersky | 6c51999 | 2011-07-23 08:48:53 +0300 | [diff] [blame] | 377 | |
| 378 | |
| 379 | def test_main(): |
| 380 | tests = [TestSupport] |
| 381 | support.run_unittest(*tests) |
| 382 | |
| 383 | if __name__ == '__main__': |
| 384 | test_main() |