bpo-40275: Use new test.support helper submodules in tests by shihai1991 · Pull Request #21314 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 8 additions & 5 deletions Lib/test/pickletester.py
20 changes: 12 additions & 8 deletions Lib/test/test_binhex.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@
"""
import unittest
from test import support
from test.support import import_helper
from test.support import os_helper
from test.support import warnings_helper

with support.check_warnings(('', DeprecationWarning)):
binhex = support.import_fresh_module('binhex')

with warnings_helper.check_warnings(('', DeprecationWarning)):
binhex = import_helper.import_fresh_module('binhex')


class BinHexTestCase(unittest.TestCase):

def setUp(self):
# binhex supports only file names encodable to Latin1
self.fname1 = support.TESTFN_ASCII + "1"
self.fname2 = support.TESTFN_ASCII + "2"
self.fname3 = support.TESTFN_ASCII + "very_long_filename__very_long_filename__very_long_filename__very_long_filename__"
self.fname1 = os_helper.TESTFN_ASCII + "1"
self.fname2 = os_helper.TESTFN_ASCII + "2"
self.fname3 = os_helper.TESTFN_ASCII + "very_long_filename__very_long_filename__very_long_filename__very_long_filename__"

def tearDown(self):
support.unlink(self.fname1)
support.unlink(self.fname2)
support.unlink(self.fname3)
os_helper.unlink(self.fname1)
os_helper.unlink(self.fname2)
os_helper.unlink(self.fname3)

DATA = b'Jack is my hero'

Expand Down
9 changes: 5 additions & 4 deletions Lib/test/test_bufio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from test import support
from test.support import os_helper

import io # C implementation.
import _pyio as pyio # Python implementation.
Expand All @@ -17,18 +18,18 @@ def try_one(self, s):
# .readline()s deliver what we wrote.

# Ensure we can open TESTFN for writing.
support.unlink(support.TESTFN)
os_helper.unlink(os_helper.TESTFN)

# Since C doesn't guarantee we can write/read arbitrary bytes in text
# files, use binary mode.
f = self.open(support.TESTFN, "wb")
f = self.open(os_helper.TESTFN, "wb")
try:
# write once with \n and once without
f.write(s)
f.write(b"\n")
f.write(s)
f.close()
f = open(support.TESTFN, "rb")
f = open(os_helper.TESTFN, "rb")
line = f.readline()
self.assertEqual(line, s + b"\n")
line = f.readline()
Expand All @@ -37,7 +38,7 @@ def try_one(self, s):
self.assertFalse(line) # Must be at EOF
f.close()
finally:
support.unlink(support.TESTFN)
os_helper.unlink(os_helper.TESTFN)

def drive_one(self, pattern):
for length in lengths:
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_capi.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import importlib.util
from test import support
from test.support import MISSING_C_DOCSTRINGS
from test.support import import_helper
from test.support import threading_helper
from test.support.script_helper import assert_python_failure, assert_python_ok
try:
Expand All @@ -25,7 +26,7 @@
_posixsubprocess = None

# Skip this test if the _testcapi module isn't available.
_testcapi = support.import_module('_testcapi')
_testcapi = import_helper.import_module('_testcapi')

import _testinternalcapi

Expand Down
11 changes: 6 additions & 5 deletions Lib/test/test_compileall.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
_have_multiprocessing = False

from test import support
from test.support import os_helper
from test.support import script_helper

from .test_py_compile import without_source_date_epoch
Expand Down Expand Up @@ -356,7 +357,7 @@ def test_multiple_optimization_levels(self):
except Exception:
pass

@support.skip_unless_symlink
@os_helper.skip_unless_symlink
def test_ignore_symlink_destination(self):
# Create folders for allowed files, symlinks and prohibited area
allowed_path = os.path.join(self.directory, "test", "dir", "allowed")
Expand Down Expand Up @@ -438,7 +439,7 @@ def setUpClass(cls):
sys_path_writable = False
break
finally:
support.unlink(str(path))
os_helper.unlink(str(path))
if directory_created:
directory.rmdir()
else:
Expand Down Expand Up @@ -477,7 +478,7 @@ def assertNotCompiled(self, fn):

def setUp(self):
self.directory = tempfile.mkdtemp()
self.addCleanup(support.rmtree, self.directory)
self.addCleanup(os_helper.rmtree, self.directory)
self.pkgdir = os.path.join(self.directory, 'foo')
os.mkdir(self.pkgdir)
self.pkgdir_cachedir = os.path.join(self.pkgdir, '__pycache__')
Expand Down Expand Up @@ -625,7 +626,7 @@ def test_recursion_limit(self):
self.assertCompiled(spamfn)
self.assertCompiled(eggfn)

@support.skip_unless_symlink
@os_helper.skip_unless_symlink
def test_symlink_loop(self):
# Currently, compileall ignores symlinks to directories.
# If that limitation is ever lifted, it should protect against
Expand Down Expand Up @@ -823,7 +824,7 @@ def test_multiple_optimization_levels(self):
except Exception:
pass

@support.skip_unless_symlink
@os_helper.skip_unless_symlink
def test_ignore_symlink_destination(self):
# Create folders for allowed files, symlinks and prohibited area
allowed_path = os.path.join(self.directory, "test", "dir", "allowed")
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from weakref import proxy
from functools import wraps

from test.support import (TESTFN, TESTFN_UNICODE, check_warnings, run_unittest,
make_bad_fd, cpython_only, swap_attr)
from test.support import (run_unittest, cpython_only, swap_attr)
from test.support.os_helper import (TESTFN, TESTFN_UNICODE, make_bad_fd)
from test.support.warnings_helper import check_warnings
from collections import UserList

import _io # C implementation of io
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_heapq.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import doctest

from test import support
from test.support import import_helper
from unittest import TestCase, skipUnless
from operator import itemgetter

py_heapq = support.import_fresh_module('heapq', blocked=['_heapq'])
c_heapq = support.import_fresh_module('heapq', fresh=['_heapq'])
py_heapq = import_helper.import_fresh_module('heapq', blocked=['_heapq'])
c_heapq = import_helper.import_fresh_module('heapq', fresh=['_heapq'])

# _heapq.nlargest/nsmallest are saved in heapq._nlargest/_smallest when
# _heapq is imported, so check them there
Expand Down
21 changes: 12 additions & 9 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
TestCase = unittest.TestCase

from test import support
from test.support import os_helper
from test.support import socket_helper
from test.support import warnings_helper


here = os.path.dirname(__file__)
# Self-signed cert file for 'localhost'
Expand Down Expand Up @@ -1766,14 +1769,14 @@ def test_local_bad_hostname(self):
with self.assertRaises(ssl.CertificateError):
h.request('GET', '/')
# Same with explicit check_hostname=True
with support.check_warnings(('', DeprecationWarning)):
with warnings_helper.check_warnings(('', DeprecationWarning)):
h = client.HTTPSConnection('localhost', server.port,
context=context, check_hostname=True)
with self.assertRaises(ssl.CertificateError):
h.request('GET', '/')
# With check_hostname=False, the mismatching is ignored
context.check_hostname = False
with support.check_warnings(('', DeprecationWarning)):
with warnings_helper.check_warnings(('', DeprecationWarning)):
h = client.HTTPSConnection('localhost', server.port,
context=context, check_hostname=False)
h.request('GET', '/nonexistent')
Expand All @@ -1792,7 +1795,7 @@ def test_local_bad_hostname(self):
h.close()
# Passing check_hostname to HTTPSConnection should override the
# context's setting.
with support.check_warnings(('', DeprecationWarning)):
with warnings_helper.check_warnings(('', DeprecationWarning)):
h = client.HTTPSConnection('localhost', server.port,
context=context, check_hostname=True)
with self.assertRaises(ssl.CertificateError):
Expand Down Expand Up @@ -1908,10 +1911,10 @@ def test_bytes_body(self):
self.assertEqual(b'body\xc1', f.read())

def test_text_file_body(self):
self.addCleanup(support.unlink, support.TESTFN)
with open(support.TESTFN, "w") as f:
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
with open(os_helper.TESTFN, "w") as f:
f.write("body")
with open(support.TESTFN) as f:
with open(os_helper.TESTFN) as f:
self.conn.request("PUT", "/url", f)
message, f = self.get_headers_and_fp()
self.assertEqual("text/plain", message.get_content_type())
Expand All @@ -1923,10 +1926,10 @@ def test_text_file_body(self):
self.assertEqual(b'4\r\nbody\r\n0\r\n\r\n', f.read())

def test_binary_file_body(self):
self.addCleanup(support.unlink, support.TESTFN)
with open(support.TESTFN, "wb") as f:
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
with open(os_helper.TESTFN, "wb") as f:
f.write(b"body\xc1")
with open(support.TESTFN, "rb") as f:
with open(os_helper.TESTFN, "rb") as f:
self.conn.request("PUT", "/url", f)
message, f = self.get_headers_and_fp()
self.assertEqual("text/plain", message.get_content_type())
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_importlib/util.py
Loading