test_support.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. import errno
  2. import importlib
  3. import io
  4. import os
  5. import shutil
  6. import socket
  7. import stat
  8. import subprocess
  9. import sys
  10. import tempfile
  11. import textwrap
  12. import time
  13. import unittest
  14. import warnings
  15. from test import support
  16. from test.support import import_helper
  17. from test.support import os_helper
  18. from test.support import script_helper
  19. from test.support import socket_helper
  20. from test.support import warnings_helper
  21. TESTFN = os_helper.TESTFN
  22. class TestSupport(unittest.TestCase):
  23. @classmethod
  24. def setUpClass(cls):
  25. orig_filter_len = len(warnings.filters)
  26. cls._warnings_helper_token = support.ignore_deprecations_from(
  27. "test.support.warnings_helper", like=".*used in test_support.*"
  28. )
  29. cls._test_support_token = support.ignore_deprecations_from(
  30. "test.test_support", like=".*You should NOT be seeing this.*"
  31. )
  32. assert len(warnings.filters) == orig_filter_len + 2
  33. @classmethod
  34. def tearDownClass(cls):
  35. orig_filter_len = len(warnings.filters)
  36. support.clear_ignored_deprecations(
  37. cls._warnings_helper_token,
  38. cls._test_support_token,
  39. )
  40. assert len(warnings.filters) == orig_filter_len - 2
  41. def test_ignored_deprecations_are_silent(self):
  42. """Test support.ignore_deprecations_from() silences warnings"""
  43. with warnings.catch_warnings(record=True) as warning_objs:
  44. warnings_helper._warn_about_deprecation()
  45. warnings.warn("You should NOT be seeing this.", DeprecationWarning)
  46. messages = [str(w.message) for w in warning_objs]
  47. self.assertEqual(len(messages), 0, messages)
  48. def test_import_module(self):
  49. import_helper.import_module("ftplib")
  50. self.assertRaises(unittest.SkipTest,
  51. import_helper.import_module, "foo")
  52. def test_import_fresh_module(self):
  53. import_helper.import_fresh_module("ftplib")
  54. def test_get_attribute(self):
  55. self.assertEqual(support.get_attribute(self, "test_get_attribute"),
  56. self.test_get_attribute)
  57. self.assertRaises(unittest.SkipTest, support.get_attribute, self, "foo")
  58. @unittest.skip("failing buildbots")
  59. def test_get_original_stdout(self):
  60. self.assertEqual(support.get_original_stdout(), sys.stdout)
  61. def test_unload(self):
  62. import sched
  63. self.assertIn("sched", sys.modules)
  64. import_helper.unload("sched")
  65. self.assertNotIn("sched", sys.modules)
  66. def test_unlink(self):
  67. with open(TESTFN, "w", encoding="utf-8") as f:
  68. pass
  69. os_helper.unlink(TESTFN)
  70. self.assertFalse(os.path.exists(TESTFN))
  71. os_helper.unlink(TESTFN)
  72. def test_rmtree(self):
  73. dirpath = os_helper.TESTFN + 'd'
  74. subdirpath = os.path.join(dirpath, 'subdir')
  75. os.mkdir(dirpath)
  76. os.mkdir(subdirpath)
  77. os_helper.rmtree(dirpath)
  78. self.assertFalse(os.path.exists(dirpath))
  79. with support.swap_attr(support, 'verbose', 0):
  80. os_helper.rmtree(dirpath)
  81. os.mkdir(dirpath)
  82. os.mkdir(subdirpath)
  83. os.chmod(dirpath, stat.S_IRUSR|stat.S_IXUSR)
  84. with support.swap_attr(support, 'verbose', 0):
  85. os_helper.rmtree(dirpath)
  86. self.assertFalse(os.path.exists(dirpath))
  87. os.mkdir(dirpath)
  88. os.mkdir(subdirpath)
  89. os.chmod(dirpath, 0)
  90. with support.swap_attr(support, 'verbose', 0):
  91. os_helper.rmtree(dirpath)
  92. self.assertFalse(os.path.exists(dirpath))
  93. def test_forget(self):
  94. mod_filename = TESTFN + '.py'
  95. with open(mod_filename, 'w', encoding="utf-8") as f:
  96. print('foo = 1', file=f)
  97. sys.path.insert(0, os.curdir)
  98. importlib.invalidate_caches()
  99. try:
  100. mod = __import__(TESTFN)
  101. self.assertIn(TESTFN, sys.modules)
  102. import_helper.forget(TESTFN)
  103. self.assertNotIn(TESTFN, sys.modules)
  104. finally:
  105. del sys.path[0]
  106. os_helper.unlink(mod_filename)
  107. os_helper.rmtree('__pycache__')
  108. @support.requires_working_socket()
  109. def test_HOST(self):
  110. s = socket.create_server((socket_helper.HOST, 0))
  111. s.close()
  112. @support.requires_working_socket()
  113. def test_find_unused_port(self):
  114. port = socket_helper.find_unused_port()
  115. s = socket.create_server((socket_helper.HOST, port))
  116. s.close()
  117. @support.requires_working_socket()
  118. def test_bind_port(self):
  119. s = socket.socket()
  120. socket_helper.bind_port(s)
  121. s.listen()
  122. s.close()
  123. # Tests for temp_dir()
  124. def test_temp_dir(self):
  125. """Test that temp_dir() creates and destroys its directory."""
  126. parent_dir = tempfile.mkdtemp()
  127. parent_dir = os.path.realpath(parent_dir)
  128. try:
  129. path = os.path.join(parent_dir, 'temp')
  130. self.assertFalse(os.path.isdir(path))
  131. with os_helper.temp_dir(path) as temp_path:
  132. self.assertEqual(temp_path, path)
  133. self.assertTrue(os.path.isdir(path))
  134. self.assertFalse(os.path.isdir(path))
  135. finally:
  136. os_helper.rmtree(parent_dir)
  137. def test_temp_dir__path_none(self):
  138. """Test passing no path."""
  139. with os_helper.temp_dir() as temp_path:
  140. self.assertTrue(os.path.isdir(temp_path))
  141. self.assertFalse(os.path.isdir(temp_path))
  142. def test_temp_dir__existing_dir__quiet_default(self):
  143. """Test passing a directory that already exists."""
  144. def call_temp_dir(path):
  145. with os_helper.temp_dir(path) as temp_path:
  146. raise Exception("should not get here")
  147. path = tempfile.mkdtemp()
  148. path = os.path.realpath(path)
  149. try:
  150. self.assertTrue(os.path.isdir(path))
  151. self.assertRaises(FileExistsError, call_temp_dir, path)
  152. # Make sure temp_dir did not delete the original directory.
  153. self.assertTrue(os.path.isdir(path))
  154. finally:
  155. shutil.rmtree(path)
  156. def test_temp_dir__existing_dir__quiet_true(self):
  157. """Test passing a directory that already exists with quiet=True."""
  158. path = tempfile.mkdtemp()
  159. path = os.path.realpath(path)
  160. try:
  161. with warnings_helper.check_warnings() as recorder:
  162. with os_helper.temp_dir(path, quiet=True) as temp_path:
  163. self.assertEqual(path, temp_path)
  164. warnings = [str(w.message) for w in recorder.warnings]
  165. # Make sure temp_dir did not delete the original directory.
  166. self.assertTrue(os.path.isdir(path))
  167. finally:
  168. shutil.rmtree(path)
  169. self.assertEqual(len(warnings), 1, warnings)
  170. warn = warnings[0]
  171. self.assertTrue(warn.startswith(f'tests may fail, unable to create '
  172. f'temporary directory {path!r}: '),
  173. warn)
  174. @support.requires_fork()
  175. def test_temp_dir__forked_child(self):
  176. """Test that a forked child process does not remove the directory."""
  177. # See bpo-30028 for details.
  178. # Run the test as an external script, because it uses fork.
  179. script_helper.assert_python_ok("-c", textwrap.dedent("""
  180. import os
  181. from test import support
  182. from test.support import os_helper
  183. with os_helper.temp_cwd() as temp_path:
  184. pid = os.fork()
  185. if pid != 0:
  186. # parent process
  187. # wait for the child to terminate
  188. support.wait_process(pid, exitcode=0)
  189. # Make sure that temp_path is still present. When the child
  190. # process leaves the 'temp_cwd'-context, the __exit__()-
  191. # method of the context must not remove the temporary
  192. # directory.
  193. if not os.path.isdir(temp_path):
  194. raise AssertionError("Child removed temp_path.")
  195. """))
  196. # Tests for change_cwd()
  197. def test_change_cwd(self):
  198. original_cwd = os.getcwd()
  199. with os_helper.temp_dir() as temp_path:
  200. with os_helper.change_cwd(temp_path) as new_cwd:
  201. self.assertEqual(new_cwd, temp_path)
  202. self.assertEqual(os.getcwd(), new_cwd)
  203. self.assertEqual(os.getcwd(), original_cwd)
  204. def test_change_cwd__non_existent_dir(self):
  205. """Test passing a non-existent directory."""
  206. original_cwd = os.getcwd()
  207. def call_change_cwd(path):
  208. with os_helper.change_cwd(path) as new_cwd:
  209. raise Exception("should not get here")
  210. with os_helper.temp_dir() as parent_dir:
  211. non_existent_dir = os.path.join(parent_dir, 'does_not_exist')
  212. self.assertRaises(FileNotFoundError, call_change_cwd,
  213. non_existent_dir)
  214. self.assertEqual(os.getcwd(), original_cwd)
  215. def test_change_cwd__non_existent_dir__quiet_true(self):
  216. """Test passing a non-existent directory with quiet=True."""
  217. original_cwd = os.getcwd()
  218. with os_helper.temp_dir() as parent_dir:
  219. bad_dir = os.path.join(parent_dir, 'does_not_exist')
  220. with warnings_helper.check_warnings() as recorder:
  221. with os_helper.change_cwd(bad_dir, quiet=True) as new_cwd:
  222. self.assertEqual(new_cwd, original_cwd)
  223. self.assertEqual(os.getcwd(), new_cwd)
  224. warnings = [str(w.message) for w in recorder.warnings]
  225. self.assertEqual(len(warnings), 1, warnings)
  226. warn = warnings[0]
  227. self.assertTrue(warn.startswith(f'tests may fail, unable to change '
  228. f'the current working directory '
  229. f'to {bad_dir!r}: '),
  230. warn)
  231. # Tests for change_cwd()
  232. def test_change_cwd__chdir_warning(self):
  233. """Check the warning message when os.chdir() fails."""
  234. path = TESTFN + '_does_not_exist'
  235. with warnings_helper.check_warnings() as recorder:
  236. with os_helper.change_cwd(path=path, quiet=True):
  237. pass
  238. messages = [str(w.message) for w in recorder.warnings]
  239. self.assertEqual(len(messages), 1, messages)
  240. msg = messages[0]
  241. self.assertTrue(msg.startswith(f'tests may fail, unable to change '
  242. f'the current working directory '
  243. f'to {path!r}: '),
  244. msg)
  245. # Tests for temp_cwd()
  246. def test_temp_cwd(self):
  247. here = os.getcwd()
  248. with os_helper.temp_cwd(name=TESTFN):
  249. self.assertEqual(os.path.basename(os.getcwd()), TESTFN)
  250. self.assertFalse(os.path.exists(TESTFN))
  251. self.assertEqual(os.getcwd(), here)
  252. def test_temp_cwd__name_none(self):
  253. """Test passing None to temp_cwd()."""
  254. original_cwd = os.getcwd()
  255. with os_helper.temp_cwd(name=None) as new_cwd:
  256. self.assertNotEqual(new_cwd, original_cwd)
  257. self.assertTrue(os.path.isdir(new_cwd))
  258. self.assertEqual(os.getcwd(), new_cwd)
  259. self.assertEqual(os.getcwd(), original_cwd)
  260. def test_sortdict(self):
  261. self.assertEqual(support.sortdict({3:3, 2:2, 1:1}), "{1: 1, 2: 2, 3: 3}")
  262. def test_make_bad_fd(self):
  263. fd = os_helper.make_bad_fd()
  264. with self.assertRaises(OSError) as cm:
  265. os.write(fd, b"foo")
  266. self.assertEqual(cm.exception.errno, errno.EBADF)
  267. def test_check_syntax_error(self):
  268. support.check_syntax_error(self, "def class", lineno=1, offset=5)
  269. with self.assertRaises(AssertionError):
  270. support.check_syntax_error(self, "x=1")
  271. def test_CleanImport(self):
  272. import importlib
  273. with import_helper.CleanImport("pprint"):
  274. importlib.import_module("pprint")
  275. def test_DirsOnSysPath(self):
  276. with import_helper.DirsOnSysPath('foo', 'bar'):
  277. self.assertIn("foo", sys.path)
  278. self.assertIn("bar", sys.path)
  279. self.assertNotIn("foo", sys.path)
  280. self.assertNotIn("bar", sys.path)
  281. def test_captured_stdout(self):
  282. with support.captured_stdout() as stdout:
  283. print("hello")
  284. self.assertEqual(stdout.getvalue(), "hello\n")
  285. def test_captured_stderr(self):
  286. with support.captured_stderr() as stderr:
  287. print("hello", file=sys.stderr)
  288. self.assertEqual(stderr.getvalue(), "hello\n")
  289. def test_captured_stdin(self):
  290. with support.captured_stdin() as stdin:
  291. stdin.write('hello\n')
  292. stdin.seek(0)
  293. # call test code that consumes from sys.stdin
  294. captured = input()
  295. self.assertEqual(captured, "hello")
  296. def test_gc_collect(self):
  297. support.gc_collect()
  298. def test_python_is_optimized(self):
  299. self.assertIsInstance(support.python_is_optimized(), bool)
  300. def test_swap_attr(self):
  301. class Obj:
  302. pass
  303. obj = Obj()
  304. obj.x = 1
  305. with support.swap_attr(obj, "x", 5) as x:
  306. self.assertEqual(obj.x, 5)
  307. self.assertEqual(x, 1)
  308. self.assertEqual(obj.x, 1)
  309. with support.swap_attr(obj, "y", 5) as y:
  310. self.assertEqual(obj.y, 5)
  311. self.assertIsNone(y)
  312. self.assertFalse(hasattr(obj, 'y'))
  313. with support.swap_attr(obj, "y", 5):
  314. del obj.y
  315. self.assertFalse(hasattr(obj, 'y'))
  316. def test_swap_item(self):
  317. D = {"x":1}
  318. with support.swap_item(D, "x", 5) as x:
  319. self.assertEqual(D["x"], 5)
  320. self.assertEqual(x, 1)
  321. self.assertEqual(D["x"], 1)
  322. with support.swap_item(D, "y", 5) as y:
  323. self.assertEqual(D["y"], 5)
  324. self.assertIsNone(y)
  325. self.assertNotIn("y", D)
  326. with support.swap_item(D, "y", 5):
  327. del D["y"]
  328. self.assertNotIn("y", D)
  329. class RefClass:
  330. attribute1 = None
  331. attribute2 = None
  332. _hidden_attribute1 = None
  333. __magic_1__ = None
  334. class OtherClass:
  335. attribute2 = None
  336. attribute3 = None
  337. __magic_1__ = None
  338. __magic_2__ = None
  339. def test_detect_api_mismatch(self):
  340. missing_items = support.detect_api_mismatch(self.RefClass,
  341. self.OtherClass)
  342. self.assertEqual({'attribute1'}, missing_items)
  343. missing_items = support.detect_api_mismatch(self.OtherClass,
  344. self.RefClass)
  345. self.assertEqual({'attribute3', '__magic_2__'}, missing_items)
  346. def test_detect_api_mismatch__ignore(self):
  347. ignore = ['attribute1', 'attribute3', '__magic_2__', 'not_in_either']
  348. missing_items = support.detect_api_mismatch(
  349. self.RefClass, self.OtherClass, ignore=ignore)
  350. self.assertEqual(set(), missing_items)
  351. missing_items = support.detect_api_mismatch(
  352. self.OtherClass, self.RefClass, ignore=ignore)
  353. self.assertEqual(set(), missing_items)
  354. def test_check__all__(self):
  355. extra = {'tempdir'}
  356. not_exported = {'template'}
  357. support.check__all__(self,
  358. tempfile,
  359. extra=extra,
  360. not_exported=not_exported)
  361. extra = {
  362. 'TextTestResult',
  363. 'findTestCases',
  364. 'getTestCaseNames',
  365. 'installHandler',
  366. 'makeSuite',
  367. }
  368. not_exported = {'load_tests', "TestProgram", "BaseTestSuite"}
  369. support.check__all__(self,
  370. unittest,
  371. ("unittest.result", "unittest.case",
  372. "unittest.suite", "unittest.loader",
  373. "unittest.main", "unittest.runner",
  374. "unittest.signals", "unittest.async_case"),
  375. extra=extra,
  376. not_exported=not_exported)
  377. self.assertRaises(AssertionError, support.check__all__, self, unittest)
  378. @unittest.skipUnless(hasattr(os, 'waitpid') and hasattr(os, 'WNOHANG'),
  379. 'need os.waitpid() and os.WNOHANG')
  380. @support.requires_fork()
  381. def test_reap_children(self):
  382. # Make sure that there is no other pending child process
  383. support.reap_children()
  384. # Create a child process
  385. pid = os.fork()
  386. if pid == 0:
  387. # child process: do nothing, just exit
  388. os._exit(0)
  389. t0 = time.monotonic()
  390. deadline = time.monotonic() + support.SHORT_TIMEOUT
  391. was_altered = support.environment_altered
  392. try:
  393. support.environment_altered = False
  394. stderr = io.StringIO()
  395. while True:
  396. if time.monotonic() > deadline:
  397. self.fail("timeout")
  398. with support.swap_attr(support.print_warning, 'orig_stderr', stderr):
  399. support.reap_children()
  400. # Use environment_altered to check if reap_children() found
  401. # the child process
  402. if support.environment_altered:
  403. break
  404. # loop until the child process completed
  405. time.sleep(0.100)
  406. msg = "Warning -- reap_children() reaped child process %s" % pid
  407. self.assertIn(msg, stderr.getvalue())
  408. self.assertTrue(support.environment_altered)
  409. finally:
  410. support.environment_altered = was_altered
  411. # Just in case, check again that there is no other
  412. # pending child process
  413. support.reap_children()
  414. @support.requires_subprocess()
  415. def check_options(self, args, func, expected=None):
  416. code = f'from test.support import {func}; print(repr({func}()))'
  417. cmd = [sys.executable, *args, '-c', code]
  418. env = {key: value for key, value in os.environ.items()
  419. if not key.startswith('PYTHON')}
  420. proc = subprocess.run(cmd,
  421. stdout=subprocess.PIPE,
  422. stderr=subprocess.DEVNULL,
  423. universal_newlines=True,
  424. env=env)
  425. if expected is None:
  426. expected = args
  427. self.assertEqual(proc.stdout.rstrip(), repr(expected))
  428. self.assertEqual(proc.returncode, 0)
  429. def test_args_from_interpreter_flags(self):
  430. # Test test.support.args_from_interpreter_flags()
  431. for opts in (
  432. # no option
  433. [],
  434. # single option
  435. ['-B'],
  436. ['-s'],
  437. ['-S'],
  438. ['-E'],
  439. ['-v'],
  440. ['-b'],
  441. ['-P'],
  442. ['-q'],
  443. ['-I'],
  444. # same option multiple times
  445. ['-bb'],
  446. ['-vvv'],
  447. # -W options
  448. ['-Wignore'],
  449. # -X options
  450. ['-X', 'dev'],
  451. ['-Wignore', '-X', 'dev'],
  452. ['-X', 'faulthandler'],
  453. ['-X', 'importtime'],
  454. ['-X', 'showrefcount'],
  455. ['-X', 'tracemalloc'],
  456. ['-X', 'tracemalloc=3'],
  457. ):
  458. with self.subTest(opts=opts):
  459. self.check_options(opts, 'args_from_interpreter_flags')
  460. self.check_options(['-I', '-E', '-s', '-P'],
  461. 'args_from_interpreter_flags',
  462. ['-I'])
  463. def test_optim_args_from_interpreter_flags(self):
  464. # Test test.support.optim_args_from_interpreter_flags()
  465. for opts in (
  466. # no option
  467. [],
  468. ['-O'],
  469. ['-OO'],
  470. ['-OOOO'],
  471. ):
  472. with self.subTest(opts=opts):
  473. self.check_options(opts, 'optim_args_from_interpreter_flags')
  474. def test_match_test(self):
  475. class Test:
  476. def __init__(self, test_id):
  477. self.test_id = test_id
  478. def id(self):
  479. return self.test_id
  480. test_access = Test('test.test_os.FileTests.test_access')
  481. test_chdir = Test('test.test_os.Win32ErrorTests.test_chdir')
  482. # Test acceptance
  483. with support.swap_attr(support, '_match_test_func', None):
  484. # match all
  485. support.set_match_tests([])
  486. self.assertTrue(support.match_test(test_access))
  487. self.assertTrue(support.match_test(test_chdir))
  488. # match all using None
  489. support.set_match_tests(None, None)
  490. self.assertTrue(support.match_test(test_access))
  491. self.assertTrue(support.match_test(test_chdir))
  492. # match the full test identifier
  493. support.set_match_tests([test_access.id()], None)
  494. self.assertTrue(support.match_test(test_access))
  495. self.assertFalse(support.match_test(test_chdir))
  496. # match the module name
  497. support.set_match_tests(['test_os'], None)
  498. self.assertTrue(support.match_test(test_access))
  499. self.assertTrue(support.match_test(test_chdir))
  500. # Test '*' pattern
  501. support.set_match_tests(['test_*'], None)
  502. self.assertTrue(support.match_test(test_access))
  503. self.assertTrue(support.match_test(test_chdir))
  504. # Test case sensitivity
  505. support.set_match_tests(['filetests'], None)
  506. self.assertFalse(support.match_test(test_access))
  507. support.set_match_tests(['FileTests'], None)
  508. self.assertTrue(support.match_test(test_access))
  509. # Test pattern containing '.' and a '*' metacharacter
  510. support.set_match_tests(['*test_os.*.test_*'], None)
  511. self.assertTrue(support.match_test(test_access))
  512. self.assertTrue(support.match_test(test_chdir))
  513. # Multiple patterns
  514. support.set_match_tests([test_access.id(), test_chdir.id()], None)
  515. self.assertTrue(support.match_test(test_access))
  516. self.assertTrue(support.match_test(test_chdir))
  517. support.set_match_tests(['test_access', 'DONTMATCH'], None)
  518. self.assertTrue(support.match_test(test_access))
  519. self.assertFalse(support.match_test(test_chdir))
  520. # Test rejection
  521. with support.swap_attr(support, '_match_test_func', None):
  522. # match all
  523. support.set_match_tests(ignore_patterns=[])
  524. self.assertTrue(support.match_test(test_access))
  525. self.assertTrue(support.match_test(test_chdir))
  526. # match all using None
  527. support.set_match_tests(None, None)
  528. self.assertTrue(support.match_test(test_access))
  529. self.assertTrue(support.match_test(test_chdir))
  530. # match the full test identifier
  531. support.set_match_tests(None, [test_access.id()])
  532. self.assertFalse(support.match_test(test_access))
  533. self.assertTrue(support.match_test(test_chdir))
  534. # match the module name
  535. support.set_match_tests(None, ['test_os'])
  536. self.assertFalse(support.match_test(test_access))
  537. self.assertFalse(support.match_test(test_chdir))
  538. # Test '*' pattern
  539. support.set_match_tests(None, ['test_*'])
  540. self.assertFalse(support.match_test(test_access))
  541. self.assertFalse(support.match_test(test_chdir))
  542. # Test case sensitivity
  543. support.set_match_tests(None, ['filetests'])
  544. self.assertTrue(support.match_test(test_access))
  545. support.set_match_tests(None, ['FileTests'])
  546. self.assertFalse(support.match_test(test_access))
  547. # Test pattern containing '.' and a '*' metacharacter
  548. support.set_match_tests(None, ['*test_os.*.test_*'])
  549. self.assertFalse(support.match_test(test_access))
  550. self.assertFalse(support.match_test(test_chdir))
  551. # Multiple patterns
  552. support.set_match_tests(None, [test_access.id(), test_chdir.id()])
  553. self.assertFalse(support.match_test(test_access))
  554. self.assertFalse(support.match_test(test_chdir))
  555. support.set_match_tests(None, ['test_access', 'DONTMATCH'])
  556. self.assertFalse(support.match_test(test_access))
  557. self.assertTrue(support.match_test(test_chdir))
  558. @unittest.skipIf(support.is_emscripten, "Unstable in Emscripten")
  559. @unittest.skipIf(support.is_wasi, "Unavailable on WASI")
  560. def test_fd_count(self):
  561. # We cannot test the absolute value of fd_count(): on old Linux
  562. # kernel or glibc versions, os.urandom() keeps a FD open on
  563. # /dev/urandom device and Python has 4 FD opens instead of 3.
  564. # Test is unstable on Emscripten. The platform starts and stops
  565. # background threads that use pipes and epoll fds.
  566. start = os_helper.fd_count()
  567. fd = os.open(__file__, os.O_RDONLY)
  568. try:
  569. more = os_helper.fd_count()
  570. finally:
  571. os.close(fd)
  572. self.assertEqual(more - start, 1)
  573. def check_print_warning(self, msg, expected):
  574. stderr = io.StringIO()
  575. with support.swap_attr(support.print_warning, 'orig_stderr', stderr):
  576. support.print_warning(msg)
  577. self.assertEqual(stderr.getvalue(), expected)
  578. def test_print_warning(self):
  579. self.check_print_warning("msg",
  580. "Warning -- msg\n")
  581. self.check_print_warning("a\nb",
  582. 'Warning -- a\nWarning -- b\n')
  583. def test_has_strftime_extensions(self):
  584. if support.is_emscripten or sys.platform == "win32":
  585. self.assertFalse(support.has_strftime_extensions)
  586. else:
  587. self.assertTrue(support.has_strftime_extensions)
  588. # XXX -follows a list of untested API
  589. # make_legacy_pyc
  590. # is_resource_enabled
  591. # requires
  592. # fcmp
  593. # umaks
  594. # findfile
  595. # check_warnings
  596. # EnvironmentVarGuard
  597. # transient_internet
  598. # run_with_locale
  599. # set_memlimit
  600. # bigmemtest
  601. # precisionbigmemtest
  602. # bigaddrspacetest
  603. # requires_resource
  604. # run_doctest
  605. # threading_cleanup
  606. # reap_threads
  607. # can_symlink
  608. # skip_unless_symlink
  609. # SuppressCrashReport
  610. if __name__ == '__main__':
  611. unittest.main()