Skip to content

Commit 3ddc634

Browse files
authored
bpo-40275: Use new test.support helper submodules in tests (GH-21219)
1 parent 3fa4799 commit 3ddc634

16 files changed

+116
-94
lines changed

Lib/test/audiotests.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from test.support import findfile, TESTFN, unlink
1+
from test.support import findfile
2+
from test.support.os_helper import TESTFN, unlink
23
import array
34
import io
45
import pickle

Lib/test/libregrtest/cmdline.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import sys
44
from test import support
5+
from test.support import os_helper
56

67

78
USAGE = """\
@@ -291,7 +292,7 @@ def _create_parser():
291292
def relative_filename(string):
292293
# CWD is replaced with a temporary dir before calling main(), so we
293294
# join it with the saved CWD so it ends up where the user expects.
294-
return os.path.join(support.SAVEDCWD, string)
295+
return os.path.join(os_helper.SAVEDCWD, string)
295296

296297

297298
def huntrleaks(string):

Lib/test/libregrtest/main.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def find_tests(self, tests):
216216
# regex to match 'test_builtin' in line:
217217
# '0:00:00 [ 4/400] test_builtin -- test_dict took 1 sec'
218218
regex = re.compile(r'\btest_[a-zA-Z0-9_]+\b')
219-
with open(os.path.join(support.SAVEDCWD, self.ns.fromfile)) as fp:
219+
with open(os.path.join(os_helper.SAVEDCWD, self.ns.fromfile)) as fp:
220220
for line in fp:
221221
line = line.split('#', 1)[0]
222222
line = line.strip()
@@ -559,7 +559,7 @@ def save_xml_result(self):
559559
for k, v in totals.items():
560560
root.set(k, str(v))
561561

562-
xmlpath = os.path.join(support.SAVEDCWD, self.ns.xmlpath)
562+
xmlpath = os.path.join(os_helper.SAVEDCWD, self.ns.xmlpath)
563563
with open(xmlpath, 'wb') as f:
564564
for s in ET.tostringlist(root):
565565
f.write(s)
@@ -597,7 +597,7 @@ def create_temp_dir(self):
597597
test_cwd = 'test_python_worker_{}'.format(pid)
598598
else:
599599
test_cwd = 'test_python_{}'.format(pid)
600-
test_cwd += support.FS_NONASCII
600+
test_cwd += os_helper.FS_NONASCII
601601
test_cwd = os.path.join(self.tmp_dir, test_cwd)
602602
return test_cwd
603603

@@ -609,10 +609,10 @@ def cleanup(self):
609609
for name in glob.glob(path):
610610
if os.path.isdir(name):
611611
print("Remove directory: %s" % name)
612-
support.rmtree(name)
612+
os_helper.rmtree(name)
613613
else:
614614
print("Remove file: %s" % name)
615-
support.unlink(name)
615+
os_helper.unlink(name)
616616

617617
def main(self, tests=None, **kwargs):
618618
self.parse_args(kwargs)
@@ -629,7 +629,7 @@ def main(self, tests=None, **kwargs):
629629
# Run the tests in a context manager that temporarily changes the CWD
630630
# to a temporary and writable directory. If it's not possible to
631631
# create or change the CWD, the original CWD will be used.
632-
# The original CWD is available from support.SAVEDCWD.
632+
# The original CWD is available from os_helper.SAVEDCWD.
633633
with os_helper.temp_cwd(test_cwd, quiet=True):
634634
# When using multiprocessing, worker processes will use test_cwd
635635
# as their parent temporary directory. So when the main process

Lib/test/libregrtest/runtest_mp.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import traceback
1212
import types
1313
from test import support
14+
from test.support import os_helper
1415

1516
from test.libregrtest.runtest import (
1617
runtest, INTERRUPTED, CHILD_ERROR, PROGRESS_MIN_TIME,
@@ -70,7 +71,7 @@ def run_test_in_subprocess(testname, ns):
7071
stderr=subprocess.PIPE,
7172
universal_newlines=True,
7273
close_fds=(os.name != 'nt'),
73-
cwd=support.SAVEDCWD,
74+
cwd=os_helper.SAVEDCWD,
7475
**kw)
7576

7677

Lib/test/test_aifc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from test.support import check_no_resource_warning, findfile, TESTFN, unlink
1+
from test.support import findfile
2+
from test.support.os_helper import TESTFN, unlink
3+
from test.support.warnings_helper import check_no_resource_warning
24
import unittest
35
from unittest import mock
46
from test import audiotests

Lib/test/test_bz2.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
import shutil
1313
import subprocess
1414
import threading
15+
from test.support import import_helper
1516
from test.support import threading_helper
16-
from test.support import unlink
17+
from test.support.os_helper import unlink
1718
import _compression
1819
import sys
1920

2021

2122
# Skip tests if the bz2 module doesn't exist.
22-
bz2 = support.import_module('bz2')
23+
bz2 = import_helper.import_module('bz2')
2324
from bz2 import BZ2File, BZ2Compressor, BZ2Decompressor
2425

2526
has_cmdline_bunzip2 = None

Lib/test/test_functools.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@
1919
from weakref import proxy
2020
import contextlib
2121

22+
from test.support import import_helper
2223
from test.support import threading_helper
2324
from test.support.script_helper import assert_python_ok
2425

2526
import functools
2627

27-
py_functools = support.import_fresh_module('functools', blocked=['_functools'])
28-
c_functools = support.import_fresh_module('functools', fresh=['_functools'])
28+
py_functools = import_helper.import_fresh_module('functools',
29+
blocked=['_functools'])
30+
c_functools = import_helper.import_fresh_module('functools',
31+
fresh=['_functools'])
2932

30-
decimal = support.import_fresh_module('decimal', fresh=['_decimal'])
33+
decimal = import_helper.import_fresh_module('decimal', fresh=['_decimal'])
3134

3235
@contextlib.contextmanager
3336
def replaced_module(name, replacement):

Lib/test/test_future.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import ast
55
import unittest
66
from test import support
7+
from test.support import import_helper
78
from textwrap import dedent
89
import os
910
import re
@@ -24,17 +25,17 @@ def check_syntax_error(self, err, basename, lineno, offset=1):
2425
self.assertEqual(err.offset, offset)
2526

2627
def test_future1(self):
27-
with support.CleanImport('future_test1'):
28+
with import_helper.CleanImport('future_test1'):
2829
from test import future_test1
2930
self.assertEqual(future_test1.result, 6)
3031

3132
def test_future2(self):
32-
with support.CleanImport('future_test2'):
33+
with import_helper.CleanImport('future_test2'):
3334
from test import future_test2
3435
self.assertEqual(future_test2.result, 6)
3536

3637
def test_future3(self):
37-
with support.CleanImport('test_future3'):
38+
with import_helper.CleanImport('test_future3'):
3839
from test import test_future3
3940

4041
def test_badfuture3(self):
@@ -113,7 +114,7 @@ def test_parserhack(self):
113114
self.fail("syntax error didn't occur")
114115

115116
def test_multiple_features(self):
116-
with support.CleanImport("test.test_future5"):
117+
with import_helper.CleanImport("test.test_future5"):
117118
from test import test_future5
118119

119120
def test_unicode_literals_exec(self):

Lib/test/test_logging.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@
4242
import tempfile
4343
from test.support.script_helper import assert_python_ok, assert_python_failure
4444
from test import support
45+
from test.support import os_helper
4546
from test.support import socket_helper
4647
from test.support import threading_helper
48+
from test.support import warnings_helper
4749
from test.support.logging_helper import TestHandler
4850
import textwrap
4951
import threading
@@ -1169,7 +1171,7 @@ class ConfigFileTest(BaseTest):
11691171

11701172
"""Reading logging config from a .ini-style config file."""
11711173

1172-
check_no_resource_warning = support.check_no_resource_warning
1174+
check_no_resource_warning = warnings_helper.check_no_resource_warning
11731175
expected_log_pat = r"^(\w+) \+\+ (\w+)$"
11741176

11751177
# config0 is a standard configuration.
@@ -1756,7 +1758,7 @@ def setUp(self):
17561758

17571759
def tearDown(self):
17581760
SocketHandlerTest.tearDown(self)
1759-
support.unlink(self.address)
1761+
os_helper.unlink(self.address)
17601762

17611763
class DatagramHandlerTest(BaseTest):
17621764

@@ -1837,7 +1839,7 @@ def setUp(self):
18371839

18381840
def tearDown(self):
18391841
DatagramHandlerTest.tearDown(self)
1840-
support.unlink(self.address)
1842+
os_helper.unlink(self.address)
18411843

18421844
class SysLogHandlerTest(BaseTest):
18431845

@@ -1921,7 +1923,7 @@ def setUp(self):
19211923

19221924
def tearDown(self):
19231925
SysLogHandlerTest.tearDown(self)
1924-
support.unlink(self.address)
1926+
os_helper.unlink(self.address)
19251927

19261928
@unittest.skipUnless(socket_helper.IPV6_ENABLED,
19271929
'IPv6 support required for this test.')
@@ -2175,7 +2177,7 @@ class ConfigDictTest(BaseTest):
21752177

21762178
"""Reading logging config from a dictionary."""
21772179

2178-
check_no_resource_warning = support.check_no_resource_warning
2180+
check_no_resource_warning = warnings_helper.check_no_resource_warning
21792181
expected_log_pat = r"^(\w+) \+\+ (\w+)$"
21802182

21812183
# config0 is a standard configuration.

Lib/test/test_poll.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import threading
88
import time
99
import unittest
10-
from test.support import TESTFN, run_unittest, cpython_only
10+
from test.support import run_unittest, cpython_only
1111
from test.support import threading_helper
12+
from test.support.os_helper import TESTFN
13+
1214

1315
try:
1416
select.poll

Lib/test/test_regrtest.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import unittest
1919
from test import libregrtest
2020
from test import support
21+
from test.support import os_helper
2122
from test.libregrtest import utils
2223

2324

@@ -161,12 +162,12 @@ def test_ignore(self):
161162
self.assertEqual(ns.ignore_tests, ['pattern'])
162163
self.checkError([opt], 'expected one argument')
163164

164-
self.addCleanup(support.unlink, support.TESTFN)
165-
with open(support.TESTFN, "w") as fp:
165+
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
166+
with open(os_helper.TESTFN, "w") as fp:
166167
print('matchfile1', file=fp)
167168
print('matchfile2', file=fp)
168169

169-
filename = os.path.abspath(support.TESTFN)
170+
filename = os.path.abspath(os_helper.TESTFN)
170171
ns = libregrtest._parse_args(['-m', 'match',
171172
'--ignorefile', filename])
172173
self.assertEqual(ns.ignore_tests,
@@ -183,12 +184,12 @@ def test_match(self):
183184
'-m', 'pattern2'])
184185
self.assertEqual(ns.match_tests, ['pattern1', 'pattern2'])
185186

186-
self.addCleanup(support.unlink, support.TESTFN)
187-
with open(support.TESTFN, "w") as fp:
187+
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
188+
with open(os_helper.TESTFN, "w") as fp:
188189
print('matchfile1', file=fp)
189190
print('matchfile2', file=fp)
190191

191-
filename = os.path.abspath(support.TESTFN)
192+
filename = os.path.abspath(os_helper.TESTFN)
192193
ns = libregrtest._parse_args(['-m', 'match',
193194
'--matchfile', filename])
194195
self.assertEqual(ns.match_tests,
@@ -237,7 +238,7 @@ def test_memlimit(self):
237238

238239
def test_testdir(self):
239240
ns = libregrtest._parse_args(['--testdir', 'foo'])
240-
self.assertEqual(ns.testdir, os.path.join(support.SAVEDCWD, 'foo'))
241+
self.assertEqual(ns.testdir, os.path.join(os_helper.SAVEDCWD, 'foo'))
241242
self.checkError(['--testdir'], 'expected one argument')
242243

243244
def test_runleaks(self):
@@ -284,7 +285,7 @@ def test_coverdir(self):
284285
with self.subTest(opt=opt):
285286
ns = libregrtest._parse_args([opt, 'foo'])
286287
self.assertEqual(ns.coverdir,
287-
os.path.join(support.SAVEDCWD, 'foo'))
288+
os.path.join(os_helper.SAVEDCWD, 'foo'))
288289
self.checkError([opt], 'expected one argument')
289290

290291
def test_nocoverdir(self):
@@ -363,7 +364,7 @@ def setUp(self):
363364
self.testdir = os.path.realpath(os.path.dirname(__file__))
364365

365366
self.tmptestdir = tempfile.mkdtemp()
366-
self.addCleanup(support.rmtree, self.tmptestdir)
367+
self.addCleanup(os_helper.rmtree, self.tmptestdir)
367368

368369
def create_test(self, name=None, code=None):
369370
if not name:
@@ -384,7 +385,7 @@ def test_empty_test(self):
384385
name = self.TESTNAME_PREFIX + name
385386
path = os.path.join(self.tmptestdir, name + '.py')
386387

387-
self.addCleanup(support.unlink, path)
388+
self.addCleanup(os_helper.unlink, path)
388389
# Use 'x' mode to ensure that we do not override existing tests
389390
try:
390391
with open(path, 'x', encoding='utf-8') as fp:
@@ -770,8 +771,8 @@ def test_fromfile(self):
770771
# Write the list of files using a format similar to regrtest output:
771772
# [1/2] test_1
772773
# [2/2] test_2
773-
filename = support.TESTFN
774-
self.addCleanup(support.unlink, filename)
774+
filename = os_helper.TESTFN
775+
self.addCleanup(os_helper.unlink, filename)
775776

776777
# test format '0:00:00 [2/7] test_opcodes -- test_grammar took 0 sec'
777778
with open(filename, "w") as fp:
@@ -886,7 +887,7 @@ def check_leak(self, code, what):
886887
test = self.create_test('huntrleaks', code=code)
887888

888889
filename = 'reflog.txt'
889-
self.addCleanup(support.unlink, filename)
890+
self.addCleanup(os_helper.unlink, filename)
890891
output = self.run_tests('--huntrleaks', '3:3:', test,
891892
exitcode=2,
892893
stderr=subprocess.STDOUT)
@@ -997,8 +998,8 @@ def test_method4(self):
997998
testname = self.create_test(code=code)
998999

9991000
# only run a subset
1000-
filename = support.TESTFN
1001-
self.addCleanup(support.unlink, filename)
1001+
filename = os_helper.TESTFN
1002+
self.addCleanup(os_helper.unlink, filename)
10021003

10031004
subset = [
10041005
# only ignore the method name
@@ -1038,8 +1039,8 @@ def test_method4(self):
10381039
self.assertEqual(methods, all_methods)
10391040

10401041
# only run a subset
1041-
filename = support.TESTFN
1042-
self.addCleanup(support.unlink, filename)
1042+
filename = os_helper.TESTFN
1043+
self.addCleanup(os_helper.unlink, filename)
10431044

10441045
subset = [
10451046
# only match the method name

0 commit comments

Comments
 (0)