Skip to content

Commit 79bb2c9

Browse files
authored
bpo-40275: Use new test.support helper submodules in tests (GH-21743)
1 parent 52f9842 commit 79bb2c9

20 files changed

+73
-56
lines changed

Lib/test/test_dbm_gnu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
gdbm = import_helper.import_module("dbm.gnu") #skip if not supported
44
import unittest
55
import os
6-
from test.support import TESTFN, TESTFN_NONASCII, unlink
6+
from test.support.os_helper import TESTFN, TESTFN_NONASCII, unlink
77

88

99
filename = TESTFN

Lib/test/test_faulthandler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import sysconfig
99
from test import support
10+
from test.support import os_helper
1011
from test.support import script_helper, is_android
1112
import tempfile
1213
import unittest
@@ -51,7 +52,7 @@ def temporary_filename():
5152
try:
5253
yield filename
5354
finally:
54-
support.unlink(filename)
55+
os_helper.unlink(filename)
5556

5657
class FaultHandlerTests(unittest.TestCase):
5758
def get_output(self, code, filename=None, fd=None):

Lib/test/test_grp.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Test script for the grp module."""
22

33
import unittest
4-
from test import support
4+
from test.support import import_helper
55

6-
grp = support.import_module('grp')
6+
7+
grp = import_helper.import_module('grp')
78

89
class GroupDatabaseTestCase(unittest.TestCase):
910

Lib/test/test_http_cookiejar.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import os
44
import re
55
import test.support
6+
from test.support import os_helper
7+
from test.support import warnings_helper
68
import time
79
import unittest
810
import urllib.request
@@ -328,12 +330,12 @@ def _interact(cookiejar, url, set_cookie_hdrs, hdr_name):
328330

329331
class FileCookieJarTests(unittest.TestCase):
330332
def test_constructor_with_str(self):
331-
filename = test.support.TESTFN
333+
filename = os_helper.TESTFN
332334
c = LWPCookieJar(filename)
333335
self.assertEqual(c.filename, filename)
334336

335337
def test_constructor_with_path_like(self):
336-
filename = pathlib.Path(test.support.TESTFN)
338+
filename = pathlib.Path(os_helper.TESTFN)
337339
c = LWPCookieJar(filename)
338340
self.assertEqual(c.filename, os.fspath(filename))
339341

@@ -353,7 +355,7 @@ class A:
353355

354356
def test_lwp_valueless_cookie(self):
355357
# cookies with no value should be saved and loaded consistently
356-
filename = test.support.TESTFN
358+
filename = os_helper.TESTFN
357359
c = LWPCookieJar()
358360
interact_netscape(c, "http://www.acme.com/", 'boo')
359361
self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)
@@ -368,7 +370,7 @@ def test_lwp_valueless_cookie(self):
368370

369371
def test_bad_magic(self):
370372
# OSErrors (eg. file doesn't exist) are allowed to propagate
371-
filename = test.support.TESTFN
373+
filename = os_helper.TESTFN
372374
for cookiejar_class in LWPCookieJar, MozillaCookieJar:
373375
c = cookiejar_class()
374376
try:
@@ -475,7 +477,7 @@ def test_domain_return_ok(self):
475477
def test_missing_value(self):
476478
# missing = sign in Cookie: header is regarded by Mozilla as a missing
477479
# name, and by http.cookiejar as a missing value
478-
filename = test.support.TESTFN
480+
filename = os_helper.TESTFN
479481
c = MozillaCookieJar(filename)
480482
interact_netscape(c, "http://www.acme.com/", 'eggs')
481483
interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/')
@@ -599,7 +601,7 @@ def test_expires(self):
599601
c = CookieJar()
600602
future = time2netscape(time.time()+3600)
601603

602-
with test.support.check_no_warnings(self):
604+
with warnings_helper.check_no_warnings(self):
603605
headers = [f"Set-Cookie: FOO=BAR; path=/; expires={future}"]
604606
req = urllib.request.Request("http://www.coyote.com/")
605607
res = FakeResponse(headers, "http://www.coyote.com/")
@@ -1713,7 +1715,7 @@ def test_rejection(self):
17131715
self.assertEqual(len(c), 6)
17141716

17151717
# save and restore
1716-
filename = test.support.TESTFN
1718+
filename = os_helper.TESTFN
17171719

17181720
try:
17191721
c.save(filename, ignore_discard=True)
@@ -1753,7 +1755,7 @@ def test_mozilla(self):
17531755
# Save / load Mozilla/Netscape cookie file format.
17541756
year_plus_one = time.localtime()[0] + 1
17551757

1756-
filename = test.support.TESTFN
1758+
filename = os_helper.TESTFN
17571759

17581760
c = MozillaCookieJar(filename,
17591761
policy=DefaultCookiePolicy(rfc2965=True))

Lib/test/test_httpservers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def stop(self):
6767
class BaseTestCase(unittest.TestCase):
6868
def setUp(self):
6969
self._threads = threading_helper.threading_setup()
70-
os.environ = support.EnvironmentVarGuard()
70+
os.environ = os_helper.EnvironmentVarGuard()
7171
self.server_started = threading.Event()
7272
self.thread = TestServerThread(self, self.request_handler)
7373
self.thread.start()
@@ -621,7 +621,7 @@ def setUp(self):
621621
# The shebang line should be pure ASCII: use symlink if possible.
622622
# See issue #7668.
623623
self._pythonexe_symlink = None
624-
if support.can_symlink():
624+
if os_helper.can_symlink():
625625
self.pythonexe = os.path.join(self.parent_dir, 'python')
626626
self._pythonexe_symlink = support.PythonSymlink(self.pythonexe).__enter__()
627627
else:

Lib/test/test_import/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import unittest
1919
from unittest import mock
2020

21-
import test.support
2221
from test.support import os_helper
2322
from test.support import (is_jython, swap_attr, swap_item, cpython_only)
2423
from test.support.import_helper import (
@@ -480,7 +479,7 @@ def test_dll_dependency_import(self):
480479
os.path.dirname(pydname),
481480
"sqlite3{}.dll".format("_d" if "_d" in pydname else ""))
482481

483-
with test.support.temp_dir() as tmp:
482+
with os_helper.temp_dir() as tmp:
484483
tmp2 = os.path.join(tmp, "DLLs")
485484
os.mkdir(tmp2)
486485

Lib/test/test_ntpath.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def test_realpath_relative(self):
285285
def test_realpath_broken_symlinks(self):
286286
ABSTFN = ntpath.abspath(os_helper.TESTFN)
287287
os.mkdir(ABSTFN)
288-
self.addCleanup(support.rmtree, ABSTFN)
288+
self.addCleanup(os_helper.rmtree, ABSTFN)
289289

290290
with support.change_cwd(ABSTFN):
291291
os.mkdir("subdir")
@@ -427,9 +427,9 @@ def test_realpath_cwd(self):
427427
ABSTFN = ntpath.abspath(os_helper.TESTFN)
428428

429429
os_helper.unlink(ABSTFN)
430-
support.rmtree(ABSTFN)
430+
os_helper.rmtree(ABSTFN)
431431
os.mkdir(ABSTFN)
432-
self.addCleanup(support.rmtree, ABSTFN)
432+
self.addCleanup(os_helper.rmtree, ABSTFN)
433433

434434
test_dir_long = ntpath.join(ABSTFN, "MyVeryLongDirectoryName")
435435
os.mkdir(test_dir_long)

Lib/test/test_site.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import unittest
88
import test.support
99
from test import support
10+
from test.support import os_helper
1011
from test.support import socket_helper
1112
from test.support import captured_stderr
1213
from test.support.os_helper import TESTFN, EnvironmentVarGuard, change_cwd
@@ -601,7 +602,7 @@ class _pthFileTests(unittest.TestCase):
601602
def _create_underpth_exe(self, lines, exe_pth=True):
602603
import _winapi
603604
temp_dir = tempfile.mkdtemp()
604-
self.addCleanup(test.support.rmtree, temp_dir)
605+
self.addCleanup(os_helper.rmtree, temp_dir)
605606
exe_file = os.path.join(temp_dir, os.path.split(sys.executable)[1])
606607
dll_src_file = _winapi.GetModuleFileName(sys.dllhandle)
607608
dll_file = os.path.join(temp_dir, os.path.split(dll_src_file)[1])

Lib/test/test_socketserver.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import test.support
1717
from test.support import reap_children, verbose
18+
from test.support import os_helper
1819
from test.support import socket_helper
1920
from test.support import threading_helper
2021

@@ -299,7 +300,7 @@ class ErrorHandlerTest(unittest.TestCase):
299300
KeyboardInterrupt are not passed."""
300301

301302
def tearDown(self):
302-
test.support.unlink(test.support.TESTFN)
303+
os_helper.unlink(os_helper.TESTFN)
303304

304305
def test_sync_handled(self):
305306
BaseErrorTestServer(ValueError)
@@ -329,7 +330,7 @@ def test_forking_not_handled(self):
329330
self.check_result(handled=False)
330331

331332
def check_result(self, handled):
332-
with open(test.support.TESTFN) as log:
333+
with open(os_helper.TESTFN) as log:
333334
expected = 'Handler called\n' + 'Error handled\n' * handled
334335
self.assertEqual(log.read(), expected)
335336

@@ -347,7 +348,7 @@ def __init__(self, exception):
347348
self.wait_done()
348349

349350
def handle_error(self, request, client_address):
350-
with open(test.support.TESTFN, 'a') as log:
351+
with open(os_helper.TESTFN, 'a') as log:
351352
log.write('Error handled\n')
352353

353354
def wait_done(self):
@@ -356,7 +357,7 @@ def wait_done(self):
356357

357358
class BadHandler(socketserver.BaseRequestHandler):
358359
def handle(self):
359-
with open(test.support.TESTFN, 'a') as log:
360+
with open(os_helper.TESTFN, 'a') as log:
360361
log.write('Handler called\n')
361362
raise self.server.exception('Test error')
362363

Lib/test/test_statistics.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
import sys
1616
import unittest
1717
from test import support
18+
from test.support import import_helper
1819

1920
from decimal import Decimal
2021
from fractions import Fraction
21-
from test import support
2222

2323

2424
# Module to be tested.
@@ -179,8 +179,10 @@ class _DoNothing:
179179
# We prefer this for testing numeric values that may not be exactly equal,
180180
# and avoid using TestCase.assertAlmostEqual, because it sucks :-)
181181

182-
py_statistics = support.import_fresh_module('statistics', blocked=['_statistics'])
183-
c_statistics = support.import_fresh_module('statistics', fresh=['_statistics'])
182+
py_statistics = import_helper.import_fresh_module('statistics',
183+
blocked=['_statistics'])
184+
c_statistics = import_helper.import_fresh_module('statistics',
185+
fresh=['_statistics'])
184186

185187

186188
class TestModules(unittest.TestCase):

0 commit comments

Comments
 (0)