Skip to content

Commit 4660597

Browse files
authored
bpo-40275: Use new test.support helper submodules in tests (GH-21448)
1 parent bb0424b commit 4660597

20 files changed

+185
-154
lines changed

Lib/test/test_argparse.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from io import StringIO
1414

1515
from test import support
16+
from test.support import os_helper
1617
from unittest import mock
1718
class StdIOBuffer(StringIO):
1819
pass
@@ -23,7 +24,7 @@ def setUp(self):
2324
# The tests assume that line wrapping occurs at 80 columns, but this
2425
# behaviour can be overridden by setting the COLUMNS environment
2526
# variable. To ensure that this width is used, set COLUMNS to 80.
26-
env = support.EnvironmentVarGuard()
27+
env = os_helper.EnvironmentVarGuard()
2728
env['COLUMNS'] = '80'
2829
self.addCleanup(env.__exit__)
2930

@@ -3244,7 +3245,7 @@ class TestShortColumns(HelpTestCase):
32443245
but we don't want any exceptions thrown in such cases. Only ugly representation.
32453246
'''
32463247
def setUp(self):
3247-
env = support.EnvironmentVarGuard()
3248+
env = os_helper.EnvironmentVarGuard()
32483249
env.set("COLUMNS", '15')
32493250
self.addCleanup(env.__exit__)
32503251

Lib/test/test_asyncgen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import types
33
import unittest
44

5-
from test.support import import_module
5+
from test.support.import_helper import import_module
66
asyncio = import_module("asyncio")
77

88

Lib/test/test_clinic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Licensed to the PSF under a contributor agreement.
44

55
from test import support, test_tools
6+
from test.support import os_helper
67
from unittest import TestCase
78
import collections
89
import inspect
@@ -797,7 +798,7 @@ def test_external(self):
797798
source = support.findfile('clinic.test')
798799
with open(source, 'r', encoding='utf-8') as f:
799800
original = f.read()
800-
with support.temp_dir() as testdir:
801+
with os_helper.temp_dir() as testdir:
801802
testfile = os.path.join(testdir, 'clinic.test.c')
802803
with open(testfile, 'w', encoding='utf-8') as f:
803804
f.write(original)

Lib/test/test_codecs.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from unittest import mock
99

1010
from test import support
11+
from test.support import os_helper
12+
from test.support import warnings_helper
1113

1214
try:
1315
import _testcapi
@@ -709,11 +711,11 @@ def test_bug691291(self):
709711
s1 = 'Hello\r\nworld\r\n'
710712

711713
s = s1.encode(self.encoding)
712-
self.addCleanup(support.unlink, support.TESTFN)
713-
with open(support.TESTFN, 'wb') as fp:
714+
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
715+
with open(os_helper.TESTFN, 'wb') as fp:
714716
fp.write(s)
715-
with support.check_warnings(('', DeprecationWarning)):
716-
reader = codecs.open(support.TESTFN, 'U', encoding=self.encoding)
717+
with warnings_helper.check_warnings(('', DeprecationWarning)):
718+
reader = codecs.open(os_helper.TESTFN, 'U', encoding=self.encoding)
717719
with reader:
718720
self.assertEqual(reader.read(), s1)
719721

@@ -1697,10 +1699,10 @@ def test_all(self):
16971699
getattr(codecs, api)
16981700

16991701
def test_open(self):
1700-
self.addCleanup(support.unlink, support.TESTFN)
1702+
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
17011703
for mode in ('w', 'r', 'r+', 'w+', 'a', 'a+'):
17021704
with self.subTest(mode), \
1703-
codecs.open(support.TESTFN, mode, 'ascii') as file:
1705+
codecs.open(os_helper.TESTFN, mode, 'ascii') as file:
17041706
self.assertIsInstance(file, codecs.StreamReaderWriter)
17051707

17061708
def test_undefined(self):
@@ -1718,7 +1720,7 @@ def test_file_closes_if_lookup_error_raised(self):
17181720
mock_open = mock.mock_open()
17191721
with mock.patch('builtins.open', mock_open) as file:
17201722
with self.assertRaises(LookupError):
1721-
codecs.open(support.TESTFN, 'wt', 'invalid-encoding')
1723+
codecs.open(os_helper.TESTFN, 'wt', 'invalid-encoding')
17221724

17231725
file().close.assert_called()
17241726

@@ -2516,10 +2518,10 @@ def test_seek0(self):
25162518
"utf-32",
25172519
"utf-32-le",
25182520
"utf-32-be")
2519-
self.addCleanup(support.unlink, support.TESTFN)
2521+
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
25202522
for encoding in tests:
25212523
# Check if the BOM is written only once
2522-
with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
2524+
with codecs.open(os_helper.TESTFN, 'w+', encoding=encoding) as f:
25232525
f.write(data)
25242526
f.write(data)
25252527
f.seek(0)
@@ -2528,7 +2530,7 @@ def test_seek0(self):
25282530
self.assertEqual(f.read(), data * 2)
25292531

25302532
# Check that the BOM is written after a seek(0)
2531-
with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
2533+
with codecs.open(os_helper.TESTFN, 'w+', encoding=encoding) as f:
25322534
f.write(data[0])
25332535
self.assertNotEqual(f.tell(), 0)
25342536
f.seek(0)
@@ -2537,7 +2539,7 @@ def test_seek0(self):
25372539
self.assertEqual(f.read(), data)
25382540

25392541
# (StreamWriter) Check that the BOM is written after a seek(0)
2540-
with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
2542+
with codecs.open(os_helper.TESTFN, 'w+', encoding=encoding) as f:
25412543
f.writer.write(data[0])
25422544
self.assertNotEqual(f.writer.tell(), 0)
25432545
f.writer.seek(0)
@@ -2547,7 +2549,7 @@ def test_seek0(self):
25472549

25482550
# Check that the BOM is not written after a seek() at a position
25492551
# different than the start
2550-
with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
2552+
with codecs.open(os_helper.TESTFN, 'w+', encoding=encoding) as f:
25512553
f.write(data)
25522554
f.seek(f.tell())
25532555
f.write(data)
@@ -2556,7 +2558,7 @@ def test_seek0(self):
25562558

25572559
# (StreamWriter) Check that the BOM is not written after a seek()
25582560
# at a position different than the start
2559-
with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
2561+
with codecs.open(os_helper.TESTFN, 'w+', encoding=encoding) as f:
25602562
f.writer.write(data)
25612563
f.writer.seek(f.writer.tell())
25622564
f.writer.write(data)

Lib/test/test_doctest.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"""
44

55
from test import support
6+
from test.support import import_helper
7+
from test.support import os_helper
68
import doctest
79
import functools
810
import os
@@ -441,7 +443,7 @@ def basics(): r"""
441443
>>> tests = finder.find(sample_func)
442444
443445
>>> print(tests) # doctest: +ELLIPSIS
444-
[<DocTest sample_func from ...:25 (1 example)>]
446+
[<DocTest sample_func from ...:27 (1 example)>]
445447
446448
The exact name depends on how test_doctest was invoked, so allow for
447449
leading path components.
@@ -705,7 +707,7 @@ def test_empty_namespace_package(self):
705707
try:
706708
mod = importlib.import_module(pkg_name)
707709
finally:
708-
support.forget(pkg_name)
710+
import_helper.forget(pkg_name)
709711
sys.path.pop()
710712

711713
include_empty_finder = doctest.DocTestFinder(exclude_empty=False)
@@ -2758,7 +2760,7 @@ def test_lineendings(): r"""
27582760
>>> dn = tempfile.mkdtemp()
27592761
>>> pkg = os.path.join(dn, "doctest_testpkg")
27602762
>>> os.mkdir(pkg)
2761-
>>> support.create_empty_file(os.path.join(pkg, "__init__.py"))
2763+
>>> os_helper.create_empty_file(os.path.join(pkg, "__init__.py"))
27622764
>>> fn = os.path.join(pkg, "doctest_testfile.txt")
27632765
>>> with open(fn, 'wb') as f:
27642766
... f.write(
@@ -2840,7 +2842,8 @@ def test_CLI(): r"""
28402842
simple tests and no errors. We'll run both the unadorned doctest command, and
28412843
the verbose version, and then check the output:
28422844
2843-
>>> from test.support import script_helper, temp_dir
2845+
>>> from test.support import script_helper
2846+
>>> from test.support.os_helper import temp_dir
28442847
>>> with temp_dir() as tmpdir:
28452848
... fn = os.path.join(tmpdir, 'myfile.doc')
28462849
... with open(fn, 'w') as f:
@@ -2891,7 +2894,8 @@ def test_CLI(): r"""
28912894
file ends in '.py', its handling of python module files (as opposed to straight
28922895
text files).
28932896
2894-
>>> from test.support import script_helper, temp_dir
2897+
>>> from test.support import script_helper
2898+
>>> from test.support.os_helper import temp_dir
28952899
>>> with temp_dir() as tmpdir:
28962900
... fn = os.path.join(tmpdir, 'myfile.doc')
28972901
... with open(fn, 'w') as f:
@@ -3109,7 +3113,7 @@ def test_main():
31093113

31103114

31113115
def test_coverage(coverdir):
3112-
trace = support.import_module('trace')
3116+
trace = import_helper.import_module('trace')
31133117
tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,],
31143118
trace=0, count=1)
31153119
tracer.run('test_main()')

Lib/test/test_exceptions.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
import weakref
99
import errno
1010

11-
from test.support import (TESTFN, captured_stderr, check_impl_detail,
12-
check_warnings, cpython_only, gc_collect,
13-
no_tracing, unlink, import_module, script_helper,
11+
from test.support import (captured_stderr, check_impl_detail,
12+
cpython_only, gc_collect,
13+
no_tracing, script_helper,
1414
SuppressCrashReport)
15+
from test.support.import_helper import import_module
16+
from test.support.os_helper import TESTFN, unlink
17+
from test.support.warnings_helper import check_warnings
1518
from test import support
1619

1720

Lib/test/test_ftplib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from test import support
2222
from test.support import threading_helper
2323
from test.support import socket_helper
24+
from test.support import warnings_helper
2425
from test.support.socket_helper import HOST, HOSTv6
2526

2627
TIMEOUT = support.LOOPBACK_TIMEOUT
@@ -623,7 +624,7 @@ def test_storlines(self):
623624

624625
f = io.StringIO(RETR_DATA.replace('\r\n', '\n'))
625626
# storlines() expects a binary file, not a text file
626-
with support.check_warnings(('', BytesWarning), quiet=True):
627+
with warnings_helper.check_warnings(('', BytesWarning), quiet=True):
627628
self.assertRaises(TypeError, self.client.storlines, 'stor foo', f)
628629

629630
def test_nlst(self):

Lib/test/test_gc.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import unittest
22
import unittest.mock
33
from test.support import (verbose, refcount_test, run_unittest,
4-
cpython_only, temp_dir, TESTFN, unlink,
5-
import_module)
4+
cpython_only)
5+
from test.support.import_helper import import_module
6+
from test.support.os_helper import temp_dir, TESTFN, unlink
67
from test.support.script_helper import assert_python_ok, make_script
78
from test.support import threading_helper
89

0 commit comments

Comments
 (0)