Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update test_support from CPython 3.10.5
  • Loading branch information
CPython developers authored and youknowone committed Jul 17, 2022
commit db213c31f9175081deb8bf7ca9f87b88f57e5c95
66 changes: 43 additions & 23 deletions Lib/test/test_support.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import contextlib
import errno
import importlib
import io
Expand All @@ -12,6 +11,8 @@
import textwrap
import time
import unittest
import warnings

from test import support
from test.support import import_helper
from test.support import os_helper
Expand All @@ -23,10 +24,38 @@


class TestSupport(unittest.TestCase):
@classmethod
def setUpClass(cls):
orig_filter_len = len(warnings.filters)
cls._warnings_helper_token = support.ignore_deprecations_from(
"test.support.warnings_helper", like=".*used in test_support.*"
)
cls._test_support_token = support.ignore_deprecations_from(
"test.test_support", like=".*You should NOT be seeing this.*"
)
assert len(warnings.filters) == orig_filter_len + 2

@classmethod
def tearDownClass(cls):
orig_filter_len = len(warnings.filters)
support.clear_ignored_deprecations(
cls._warnings_helper_token,
cls._test_support_token,
)
assert len(warnings.filters) == orig_filter_len - 2

def test_ignored_deprecations_are_silent(self):
"""Test support.ignore_deprecations_from() silences warnings"""
with warnings.catch_warnings(record=True) as warning_objs:
warnings_helper._warn_about_deprecation()
warnings.warn("You should NOT be seeing this.", DeprecationWarning)
messages = [str(w.message) for w in warning_objs]
self.assertEqual(len(messages), 0, messages)

def test_import_module(self):
import_helper.import_module("ftplib")
self.assertRaises(unittest.SkipTest, import_helper.import_module, "foo")
self.assertRaises(unittest.SkipTest,
import_helper.import_module, "foo")

def test_import_fresh_module(self):
import_helper.import_fresh_module("ftplib")
Expand All @@ -47,7 +76,7 @@ def test_unload(self):
self.assertNotIn("sched", sys.modules)

def test_unlink(self):
with open(TESTFN, "w") as f:
with open(TESTFN, "w", encoding="utf-8") as f:
pass
os_helper.unlink(TESTFN)
self.assertFalse(os.path.exists(TESTFN))
Expand Down Expand Up @@ -79,7 +108,7 @@ def test_rmtree(self):

def test_forget(self):
mod_filename = TESTFN + '.py'
with open(mod_filename, 'w') as f:
with open(mod_filename, 'w', encoding="utf-8") as f:
print('foo = 1', file=f)
sys.path.insert(0, os.curdir)
importlib.invalidate_caches()
Expand Down Expand Up @@ -177,16 +206,14 @@ def test_temp_dir__forked_child(self):
script_helper.assert_python_ok("-c", textwrap.dedent("""
import os
from test import support
from test.support import os_helper
with os_helper.temp_cwd() as temp_path:
pid = os.fork()
if pid != 0:
# parent process (child has pid == 0)
# parent process

# wait for the child to terminate
(pid, status) = os.waitpid(pid, 0)
if status != 0:
raise AssertionError(f"Child process failed with exit "
f"status indication 0x{status:x}.")
support.wait_process(pid, exitcode=0)

# Make sure that temp_path is still present. When the child
# process leaves the 'temp_cwd'-context, the __exit__()-
Expand Down Expand Up @@ -295,8 +322,8 @@ def test_check_syntax_error(self):

def test_CleanImport(self):
import importlib
with import_helper.CleanImport("asyncore"):
importlib.import_module("asyncore")
with import_helper.CleanImport("pprint"):
importlib.import_module("pprint")

def test_DirsOnSysPath(self):
with import_helper.DirsOnSysPath('foo', 'bar'):
Expand Down Expand Up @@ -414,8 +441,8 @@ def test_check__all__(self):

self.assertRaises(AssertionError, support.check__all__, self, unittest)

@unittest.skipUnless(hasattr(os, 'waitpid') and hasattr(os, 'WNOHANG') and hasattr(os, 'fork'),
'need os.waitpid() and os.WNOHANG and os.fork()')
@unittest.skipUnless(hasattr(os, 'waitpid') and hasattr(os, 'WNOHANG'),
'need os.waitpid() and os.WNOHANG')
def test_reap_children(self):
# Make sure that there is no other pending child process
support.reap_children()
Expand All @@ -427,7 +454,7 @@ def test_reap_children(self):
os._exit(0)

t0 = time.monotonic()
deadline = time.monotonic() + 60.0
deadline = time.monotonic() + support.SHORT_TIMEOUT

was_altered = support.environment_altered
try:
Expand Down Expand Up @@ -502,7 +529,6 @@ def test_args_from_interpreter_flags(self):
['-Wignore', '-X', 'dev'],
['-X', 'faulthandler'],
['-X', 'importtime'],
['-X', 'showalloccount'],
['-X', 'showrefcount'],
['-X', 'tracemalloc'],
['-X', 'tracemalloc=3'],
Expand Down Expand Up @@ -647,7 +673,7 @@ def test_fd_count(self):
def check_print_warning(self, msg, expected):
stderr = io.StringIO()

old_stderr = sys.__stderr__
old_stderr = sys.__stderr__
try:
sys.__stderr__ = stderr
support.print_warning(msg)
Expand All @@ -671,7 +697,6 @@ def test_print_warning(self):
# findfile
# check_warnings
# EnvironmentVarGuard
# TransientResource
# transient_internet
# run_with_locale
# set_memlimit
Expand All @@ -682,15 +707,10 @@ def test_print_warning(self):
# run_doctest
# threading_cleanup
# reap_threads
# strip_python_stderr
# can_symlink
# skip_unless_symlink
# SuppressCrashReport


def test_main():
tests = [TestSupport]
support.run_unittest(*tests)

if __name__ == '__main__':
test_main()
unittest.main()