Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit deb0162

Browse files
authored
bpo-40275: Use new test.support helper submodules in tests (GH-21317)
1 parent b4a9263 commit deb0162

18 files changed

Lines changed: 128 additions & 110 deletions

Lib/distutils/tests/support.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import unittest
77
import sysconfig
88
from copy import deepcopy
9-
import test.support
9+
from test.support import os_helper
1010

1111
from distutils import log
1212
from distutils.log import DEBUG, INFO, WARN, ERROR, FATAL
@@ -64,7 +64,7 @@ def tearDown(self):
6464
super().tearDown()
6565
while self.tempdirs:
6666
tmpdir = self.tempdirs.pop()
67-
test.support.rmtree(tmpdir)
67+
os_helper.rmtree(tmpdir)
6868

6969
def mkdtemp(self):
7070
"""Create a temporary directory that will be cleaned up.

Lib/distutils/tests/test_build_ext.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import unittest
1717
from test import support
18+
from test.support import os_helper
1819
from test.support.script_helper import assert_python_ok
1920

2021
# http://bugs.python.org/issue4373
@@ -38,7 +39,7 @@ def setUp(self):
3839
# bpo-30132: On Windows, a .pdb file may be created in the current
3940
# working directory. Create a temporary working directory to cleanup
4041
# everything at the end of the test.
41-
change_cwd = support.change_cwd(self.tmp_dir)
42+
change_cwd = os_helper.change_cwd(self.tmp_dir)
4243
change_cwd.__enter__()
4344
self.addCleanup(change_cwd.__exit__, None, None, None)
4445

Lib/distutils/tests/test_filelist.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from distutils.filelist import glob_to_re, translate_pattern, FileList
99
from distutils import filelist
1010

11-
import test.support
1211
from test.support import os_helper
1312
from test.support import captured_stdout, run_unittest
1413
from distutils.tests import support
@@ -298,7 +297,7 @@ def test_process_template(self):
298297
class FindAllTestCase(unittest.TestCase):
299298
@os_helper.skip_unless_symlink
300299
def test_missing_symlink(self):
301-
with test.support.temp_cwd():
300+
with os_helper.temp_cwd():
302301
os.symlink('foo', 'bar')
303302
self.assertEqual(filelist.findall(), [])
304303

@@ -308,13 +307,13 @@ def test_basic_discovery(self):
308307
'.' as the parameter, the dot should be omitted from
309308
the results.
310309
"""
311-
with test.support.temp_cwd():
310+
with os_helper.temp_cwd():
312311
os.mkdir('foo')
313312
file1 = os.path.join('foo', 'file1.txt')
314-
test.support.create_empty_file(file1)
313+
os_helper.create_empty_file(file1)
315314
os.mkdir('bar')
316315
file2 = os.path.join('bar', 'file2.txt')
317-
test.support.create_empty_file(file2)
316+
os_helper.create_empty_file(file2)
318317
expected = [file2, file1]
319318
self.assertEqual(sorted(filelist.findall()), expected)
320319

@@ -323,9 +322,9 @@ def test_non_local_discovery(self):
323322
When findall is called with another path, the full
324323
path name should be returned.
325324
"""
326-
with test.support.temp_dir() as temp_dir:
325+
with os_helper.temp_dir() as temp_dir:
327326
file1 = os.path.join(temp_dir, 'file1.txt')
328-
test.support.create_empty_file(file1)
327+
os_helper.create_empty_file(file1)
329328
expected = [file1]
330329
self.assertEqual(filelist.findall(temp_dir), expected)
331330

Lib/distutils/tests/test_spawn.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55
import unittest.mock
66
from test.support import run_unittest, unix_shell
7-
from test import support as test_support
7+
from test.support import os_helper
88

99
from distutils.spawn import find_executable
1010
from distutils.spawn import spawn
@@ -44,9 +44,9 @@ def test_spawn(self):
4444
spawn([exe]) # should work without any error
4545

4646
def test_find_executable(self):
47-
with test_support.temp_dir() as tmp_dir:
47+
with os_helper.temp_dir() as tmp_dir:
4848
# use TESTFN to get a pseudo-unique filename
49-
program_noeext = test_support.TESTFN
49+
program_noeext = os_helper.TESTFN
5050
# Give the temporary program an ".exe" suffix for all.
5151
# It's needed on Windows and not harmful on other platforms.
5252
program = program_noeext + ".exe"
@@ -66,7 +66,7 @@ def test_find_executable(self):
6666
self.assertEqual(rv, filename)
6767

6868
# test find in the current directory
69-
with test_support.change_cwd(tmp_dir):
69+
with os_helper.change_cwd(tmp_dir):
7070
rv = find_executable(program)
7171
self.assertEqual(rv, program)
7272

@@ -76,7 +76,7 @@ def test_find_executable(self):
7676
self.assertIsNone(rv)
7777

7878
# PATH='': no match, except in the current directory
79-
with test_support.EnvironmentVarGuard() as env:
79+
with os_helper.EnvironmentVarGuard() as env:
8080
env['PATH'] = ''
8181
with unittest.mock.patch('distutils.spawn.os.confstr',
8282
return_value=tmp_dir, create=True), \
@@ -86,12 +86,12 @@ def test_find_executable(self):
8686
self.assertIsNone(rv)
8787

8888
# look in current directory
89-
with test_support.change_cwd(tmp_dir):
89+
with os_helper.change_cwd(tmp_dir):
9090
rv = find_executable(program)
9191
self.assertEqual(rv, program)
9292

9393
# PATH=':': explicitly looks in the current directory
94-
with test_support.EnvironmentVarGuard() as env:
94+
with os_helper.EnvironmentVarGuard() as env:
9595
env['PATH'] = os.pathsep
9696
with unittest.mock.patch('distutils.spawn.os.confstr',
9797
return_value='', create=True), \
@@ -100,12 +100,12 @@ def test_find_executable(self):
100100
self.assertIsNone(rv)
101101

102102
# look in current directory
103-
with test_support.change_cwd(tmp_dir):
103+
with os_helper.change_cwd(tmp_dir):
104104
rv = find_executable(program)
105105
self.assertEqual(rv, program)
106106

107107
# missing PATH: test os.confstr("CS_PATH") and os.defpath
108-
with test_support.EnvironmentVarGuard() as env:
108+
with os_helper.EnvironmentVarGuard() as env:
109109
env.pop('PATH', None)
110110

111111
# without confstr

Lib/test/test_dbm_gnu.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from test import support
2-
gdbm = support.import_module("dbm.gnu") #skip if not supported
2+
from test.support import import_helper
3+
gdbm = import_helper.import_module("dbm.gnu") #skip if not supported
34
import unittest
45
import os
56
from test.support import TESTFN, TESTFN_NONASCII, unlink

Lib/test/test_eof.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import sys
44
from test import support
5+
from test.support import os_helper
56
from test.support import script_helper
67
import unittest
78

@@ -48,7 +49,7 @@ def test_line_continuation_EOF(self):
4849
@unittest.skipIf(not sys.executable, "sys.executable required")
4950
def test_line_continuation_EOF_from_file_bpo2180(self):
5051
"""Ensure tok_nextc() does not add too many ending newlines."""
51-
with support.temp_dir() as temp_dir:
52+
with os_helper.temp_dir() as temp_dir:
5253
file_name = script_helper.make_script(temp_dir, 'foo', '\\')
5354
rc, out, err = script_helper.assert_python_failure(file_name)
5455
self.assertIn(b'unexpected EOF while parsing', err)

Lib/test/test_hashlib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import unittest
1919
import warnings
2020
from test import support
21-
from test.support import _4G, bigmemtest, import_fresh_module
21+
from test.support import _4G, bigmemtest
22+
from test.support.import_helper import import_fresh_module
2223
from test.support import threading_helper
2324
from http.client import HTTPException
2425

Lib/test/test_idle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import unittest
2-
from test.support import import_module
2+
from test.support.import_helper import import_module
33

44
# Skip test_idle if _tkinter wasn't built, if tkinter is missing,
55
# if tcl/tk is not the 8.5+ needed for ttk widgets,

Lib/test/test_imaplib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
run_with_tz, run_with_locale, cpython_only)
1515
from test.support import hashlib_helper
1616
from test.support import threading_helper
17+
from test.support import warnings_helper
1718
import unittest
1819
from unittest import mock
1920
from datetime import datetime, timezone, timedelta
@@ -575,7 +576,7 @@ def test_ssl_verified(self):
575576
# to CPython stdlib
576577
@cpython_only
577578
def test_certfile_arg_warn(self):
578-
with support.check_warnings(('', DeprecationWarning)):
579+
with warnings_helper.check_warnings(('', DeprecationWarning)):
579580
with mock.patch.object(self.imap_class, 'open'):
580581
with mock.patch.object(self.imap_class, '_connect'):
581582
self.imap_class('localhost', 143, certfile=CERTFILE)

Lib/test/test_marshal.py

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from test import support
2+
from test.support import os_helper
23
import array
34
import io
45
import marshal
@@ -17,13 +18,13 @@ def helper(self, sample, *extra):
1718
new = marshal.loads(marshal.dumps(sample, *extra))
1819
self.assertEqual(sample, new)
1920
try:
20-
with open(support.TESTFN, "wb") as f:
21+
with open(os_helper.TESTFN, "wb") as f:
2122
marshal.dump(sample, f, *extra)
22-
with open(support.TESTFN, "rb") as f:
23+
with open(os_helper.TESTFN, "rb") as f:
2324
new = marshal.load(f)
2425
self.assertEqual(sample, new)
2526
finally:
26-
support.unlink(support.TESTFN)
27+
os_helper.unlink(os_helper.TESTFN)
2728

2829
class IntTestCase(unittest.TestCase, HelperMixin):
2930
def test_ints(self):
@@ -281,20 +282,20 @@ def test_multiple_dumps_and_loads(self):
281282
ilen = len(interleaved)
282283
positions = []
283284
try:
284-
with open(support.TESTFN, 'wb') as f:
285+
with open(os_helper.TESTFN, 'wb') as f:
285286
for d in data:
286287
marshal.dump(d, f)
287288
if ilen:
288289
f.write(interleaved)
289290
positions.append(f.tell())
290-
with open(support.TESTFN, 'rb') as f:
291+
with open(os_helper.TESTFN, 'rb') as f:
291292
for i, d in enumerate(data):
292293
self.assertEqual(d, marshal.load(f))
293294
if ilen:
294295
f.read(ilen)
295296
self.assertEqual(positions[i], f.tell())
296297
finally:
297-
support.unlink(support.TESTFN)
298+
os_helper.unlink(os_helper.TESTFN)
298299

299300
def test_loads_reject_unicode_strings(self):
300301
# Issue #14177: marshal.loads() should not accept unicode strings
@@ -516,81 +517,81 @@ class CAPI_TestCase(unittest.TestCase, HelperMixin):
516517

517518
def test_write_long_to_file(self):
518519
for v in range(marshal.version + 1):
519-
_testcapi.pymarshal_write_long_to_file(0x12345678, support.TESTFN, v)
520-
with open(support.TESTFN, 'rb') as f:
520+
_testcapi.pymarshal_write_long_to_file(0x12345678, os_helper.TESTFN, v)
521+
with open(os_helper.TESTFN, 'rb') as f:
521522
data = f.read()
522-
support.unlink(support.TESTFN)
523+
os_helper.unlink(os_helper.TESTFN)
523524
self.assertEqual(data, b'\x78\x56\x34\x12')
524525

525526
def test_write_object_to_file(self):
526527
obj = ('\u20ac', b'abc', 123, 45.6, 7+8j, 'long line '*1000)
527528
for v in range(marshal.version + 1):
528-
_testcapi.pymarshal_write_object_to_file(obj, support.TESTFN, v)
529-
with open(support.TESTFN, 'rb') as f:
529+
_testcapi.pymarshal_write_object_to_file(obj, os_helper.TESTFN, v)
530+
with open(os_helper.TESTFN, 'rb') as f:
530531
data = f.read()
531-
support.unlink(support.TESTFN)
532+
os_helper.unlink(os_helper.TESTFN)
532533
self.assertEqual(marshal.loads(data), obj)
533534

534535
def test_read_short_from_file(self):
535-
with open(support.TESTFN, 'wb') as f:
536+
with open(os_helper.TESTFN, 'wb') as f:
536537
f.write(b'\x34\x12xxxx')
537-
r, p = _testcapi.pymarshal_read_short_from_file(support.TESTFN)
538-
support.unlink(support.TESTFN)
538+
r, p = _testcapi.pymarshal_read_short_from_file(os_helper.TESTFN)
539+
os_helper.unlink(os_helper.TESTFN)
539540
self.assertEqual(r, 0x1234)
540541
self.assertEqual(p, 2)
541542

542-
with open(support.TESTFN, 'wb') as f:
543+
with open(os_helper.TESTFN, 'wb') as f:
543544
f.write(b'\x12')
544545
with self.assertRaises(EOFError):
545-
_testcapi.pymarshal_read_short_from_file(support.TESTFN)
546-
support.unlink(support.TESTFN)
546+
_testcapi.pymarshal_read_short_from_file(os_helper.TESTFN)
547+
os_helper.unlink(os_helper.TESTFN)
547548

548549
def test_read_long_from_file(self):
549-
with open(support.TESTFN, 'wb') as f:
550+
with open(os_helper.TESTFN, 'wb') as f:
550551
f.write(b'\x78\x56\x34\x12xxxx')
551-
r, p = _testcapi.pymarshal_read_long_from_file(support.TESTFN)
552-
support.unlink(support.TESTFN)
552+
r, p = _testcapi.pymarshal_read_long_from_file(os_helper.TESTFN)
553+
os_helper.unlink(os_helper.TESTFN)
553554
self.assertEqual(r, 0x12345678)
554555
self.assertEqual(p, 4)
555556

556-
with open(support.TESTFN, 'wb') as f:
557+
with open(os_helper.TESTFN, 'wb') as f:
557558
f.write(b'\x56\x34\x12')
558559
with self.assertRaises(EOFError):
559-
_testcapi.pymarshal_read_long_from_file(support.TESTFN)
560-
support.unlink(support.TESTFN)
560+
_testcapi.pymarshal_read_long_from_file(os_helper.TESTFN)
561+
os_helper.unlink(os_helper.TESTFN)
561562

562563
def test_read_last_object_from_file(self):
563564
obj = ('\u20ac', b'abc', 123, 45.6, 7+8j)
564565
for v in range(marshal.version + 1):
565566
data = marshal.dumps(obj, v)
566-
with open(support.TESTFN, 'wb') as f:
567+
with open(os_helper.TESTFN, 'wb') as f:
567568
f.write(data + b'xxxx')
568-
r, p = _testcapi.pymarshal_read_last_object_from_file(support.TESTFN)
569-
support.unlink(support.TESTFN)
569+
r, p = _testcapi.pymarshal_read_last_object_from_file(os_helper.TESTFN)
570+
os_helper.unlink(os_helper.TESTFN)
570571
self.assertEqual(r, obj)
571572

572-
with open(support.TESTFN, 'wb') as f:
573+
with open(os_helper.TESTFN, 'wb') as f:
573574
f.write(data[:1])
574575
with self.assertRaises(EOFError):
575-
_testcapi.pymarshal_read_last_object_from_file(support.TESTFN)
576-
support.unlink(support.TESTFN)
576+
_testcapi.pymarshal_read_last_object_from_file(os_helper.TESTFN)
577+
os_helper.unlink(os_helper.TESTFN)
577578

578579
def test_read_object_from_file(self):
579580
obj = ('\u20ac', b'abc', 123, 45.6, 7+8j)
580581
for v in range(marshal.version + 1):
581582
data = marshal.dumps(obj, v)
582-
with open(support.TESTFN, 'wb') as f:
583+
with open(os_helper.TESTFN, 'wb') as f:
583584
f.write(data + b'xxxx')
584-
r, p = _testcapi.pymarshal_read_object_from_file(support.TESTFN)
585-
support.unlink(support.TESTFN)
585+
r, p = _testcapi.pymarshal_read_object_from_file(os_helper.TESTFN)
586+
os_helper.unlink(os_helper.TESTFN)
586587
self.assertEqual(r, obj)
587588
self.assertEqual(p, len(data))
588589

589-
with open(support.TESTFN, 'wb') as f:
590+
with open(os_helper.TESTFN, 'wb') as f:
590591
f.write(data[:1])
591592
with self.assertRaises(EOFError):
592-
_testcapi.pymarshal_read_object_from_file(support.TESTFN)
593-
support.unlink(support.TESTFN)
593+
_testcapi.pymarshal_read_object_from_file(os_helper.TESTFN)
594+
os_helper.unlink(os_helper.TESTFN)
594595

595596

596597
if __name__ == "__main__":

0 commit comments

Comments
 (0)