Skip to content

bpo-40275: Use new test.support helper submodules in tests #21743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2020
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
2 changes: 1 addition & 1 deletion Lib/test/test_dbm_gnu.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
gdbm = import_helper.import_module("dbm.gnu") #skip if not supported
import unittest
import os
from test.support import TESTFN, TESTFN_NONASCII, unlink
from test.support.os_helper import TESTFN, TESTFN_NONASCII, unlink


filename = TESTFN
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_faulthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys
import sysconfig
from test import support
from test.support import os_helper
from test.support import script_helper, is_android
import tempfile
import unittest
Expand Down Expand Up @@ -51,7 +52,7 @@ def temporary_filename():
try:
yield filename
finally:
support.unlink(filename)
os_helper.unlink(filename)

class FaultHandlerTests(unittest.TestCase):
def get_output(self, code, filename=None, fd=None):
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_grp.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Test script for the grp module."""

import unittest
from test import support
from test.support import import_helper

grp = support.import_module('grp')

grp = import_helper.import_module('grp')

class GroupDatabaseTestCase(unittest.TestCase):

Expand Down
18 changes: 10 additions & 8 deletions Lib/test/test_http_cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
import re
import test.support
from test.support import os_helper
from test.support import warnings_helper
import time
import unittest
import urllib.request
Expand Down Expand Up @@ -328,12 +330,12 @@ def _interact(cookiejar, url, set_cookie_hdrs, hdr_name):

class FileCookieJarTests(unittest.TestCase):
def test_constructor_with_str(self):
filename = test.support.TESTFN
filename = os_helper.TESTFN
c = LWPCookieJar(filename)
self.assertEqual(c.filename, filename)

def test_constructor_with_path_like(self):
filename = pathlib.Path(test.support.TESTFN)
filename = pathlib.Path(os_helper.TESTFN)
c = LWPCookieJar(filename)
self.assertEqual(c.filename, os.fspath(filename))

Expand All @@ -353,7 +355,7 @@ class A:

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

def test_bad_magic(self):
# OSErrors (eg. file doesn't exist) are allowed to propagate
filename = test.support.TESTFN
filename = os_helper.TESTFN
for cookiejar_class in LWPCookieJar, MozillaCookieJar:
c = cookiejar_class()
try:
Expand Down Expand Up @@ -475,7 +477,7 @@ def test_domain_return_ok(self):
def test_missing_value(self):
# missing = sign in Cookie: header is regarded by Mozilla as a missing
# name, and by http.cookiejar as a missing value
filename = test.support.TESTFN
filename = os_helper.TESTFN
c = MozillaCookieJar(filename)
interact_netscape(c, "http://www.acme.com/", 'eggs')
interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/')
Expand Down Expand Up @@ -599,7 +601,7 @@ def test_expires(self):
c = CookieJar()
future = time2netscape(time.time()+3600)

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

# save and restore
filename = test.support.TESTFN
filename = os_helper.TESTFN

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

filename = test.support.TESTFN
filename = os_helper.TESTFN

c = MozillaCookieJar(filename,
policy=DefaultCookiePolicy(rfc2965=True))
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_httpservers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def stop(self):
class BaseTestCase(unittest.TestCase):
def setUp(self):
self._threads = threading_helper.threading_setup()
os.environ = support.EnvironmentVarGuard()
os.environ = os_helper.EnvironmentVarGuard()
self.server_started = threading.Event()
self.thread = TestServerThread(self, self.request_handler)
self.thread.start()
Expand Down Expand Up @@ -621,7 +621,7 @@ def setUp(self):
# The shebang line should be pure ASCII: use symlink if possible.
# See issue #7668.
self._pythonexe_symlink = None
if support.can_symlink():
if os_helper.can_symlink():
self.pythonexe = os.path.join(self.parent_dir, 'python')
self._pythonexe_symlink = support.PythonSymlink(self.pythonexe).__enter__()
else:
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import unittest
from unittest import mock

import test.support
from test.support import os_helper
from test.support import (is_jython, swap_attr, swap_item, cpython_only)
from test.support.import_helper import (
Expand Down Expand Up @@ -480,7 +479,7 @@ def test_dll_dependency_import(self):
os.path.dirname(pydname),
"sqlite3{}.dll".format("_d" if "_d" in pydname else ""))

with test.support.temp_dir() as tmp:
with os_helper.temp_dir() as tmp:
tmp2 = os.path.join(tmp, "DLLs")
os.mkdir(tmp2)

Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def test_realpath_relative(self):
def test_realpath_broken_symlinks(self):
ABSTFN = ntpath.abspath(os_helper.TESTFN)
os.mkdir(ABSTFN)
self.addCleanup(support.rmtree, ABSTFN)
self.addCleanup(os_helper.rmtree, ABSTFN)

with support.change_cwd(ABSTFN):
os.mkdir("subdir")
Expand Down Expand Up @@ -427,9 +427,9 @@ def test_realpath_cwd(self):
ABSTFN = ntpath.abspath(os_helper.TESTFN)

os_helper.unlink(ABSTFN)
support.rmtree(ABSTFN)
os_helper.rmtree(ABSTFN)
os.mkdir(ABSTFN)
self.addCleanup(support.rmtree, ABSTFN)
self.addCleanup(os_helper.rmtree, ABSTFN)

test_dir_long = ntpath.join(ABSTFN, "MyVeryLongDirectoryName")
os.mkdir(test_dir_long)
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import unittest
import test.support
from test import support
from test.support import os_helper
from test.support import socket_helper
from test.support import captured_stderr
from test.support.os_helper import TESTFN, EnvironmentVarGuard, change_cwd
Expand Down Expand Up @@ -601,7 +602,7 @@ class _pthFileTests(unittest.TestCase):
def _create_underpth_exe(self, lines, exe_pth=True):
import _winapi
temp_dir = tempfile.mkdtemp()
self.addCleanup(test.support.rmtree, temp_dir)
self.addCleanup(os_helper.rmtree, temp_dir)
exe_file = os.path.join(temp_dir, os.path.split(sys.executable)[1])
dll_src_file = _winapi.GetModuleFileName(sys.dllhandle)
dll_file = os.path.join(temp_dir, os.path.split(dll_src_file)[1])
Expand Down
9 changes: 5 additions & 4 deletions Lib/test/test_socketserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import test.support
from test.support import reap_children, verbose
from test.support import os_helper
from test.support import socket_helper
from test.support import threading_helper

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

def tearDown(self):
test.support.unlink(test.support.TESTFN)
os_helper.unlink(os_helper.TESTFN)

def test_sync_handled(self):
BaseErrorTestServer(ValueError)
Expand Down Expand Up @@ -329,7 +330,7 @@ def test_forking_not_handled(self):
self.check_result(handled=False)

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

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

def handle_error(self, request, client_address):
with open(test.support.TESTFN, 'a') as log:
with open(os_helper.TESTFN, 'a') as log:
log.write('Error handled\n')

def wait_done(self):
Expand All @@ -356,7 +357,7 @@ def wait_done(self):

class BadHandler(socketserver.BaseRequestHandler):
def handle(self):
with open(test.support.TESTFN, 'a') as log:
with open(os_helper.TESTFN, 'a') as log:
log.write('Handler called\n')
raise self.server.exception('Test error')

Expand Down
8 changes: 5 additions & 3 deletions Lib/test/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import sys
import unittest
from test import support
from test.support import import_helper

from decimal import Decimal
from fractions import Fraction
from test import support


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

py_statistics = support.import_fresh_module('statistics', blocked=['_statistics'])
c_statistics = support.import_fresh_module('statistics', fresh=['_statistics'])
py_statistics = import_helper.import_fresh_module('statistics',
blocked=['_statistics'])
c_statistics = import_helper.import_fresh_module('statistics',
fresh=['_statistics'])


class TestModules(unittest.TestCase):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_tabnanny.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import tempfile
import textwrap
from test.support import (captured_stderr, captured_stdout, script_helper,
findfile, unlink)
findfile)
from test.support.os_helper import unlink


SOURCE_CODES = {
Expand Down
15 changes: 8 additions & 7 deletions Lib/test/test_tcl.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import warnings
from test import support
from test.support import import_helper
from test.support import os_helper

# Skip this test if the _tkinter module wasn't built.
_tkinter = import_helper.import_module('_tkinter')
Expand Down Expand Up @@ -192,26 +193,26 @@ def test_getboolean(self):

def testEvalFile(self):
tcl = self.interp
with open(support.TESTFN, 'w') as f:
self.addCleanup(support.unlink, support.TESTFN)
with open(os_helper.TESTFN, 'w') as f:
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
f.write("""set a 1
set b 2
set c [ expr $a + $b ]
""")
tcl.evalfile(support.TESTFN)
tcl.evalfile(os_helper.TESTFN)
self.assertEqual(tcl.eval('set a'),'1')
self.assertEqual(tcl.eval('set b'),'2')
self.assertEqual(tcl.eval('set c'),'3')

def test_evalfile_null_in_result(self):
tcl = self.interp
with open(support.TESTFN, 'w') as f:
self.addCleanup(support.unlink, support.TESTFN)
with open(os_helper.TESTFN, 'w') as f:
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
f.write("""
set a "a\0b"
set b "a\\0b"
""")
tcl.evalfile(support.TESTFN)
tcl.evalfile(os_helper.TESTFN)
self.assertEqual(tcl.eval('set a'), 'a\x00b')
self.assertEqual(tcl.eval('set b'), 'a\x00b')

Expand Down Expand Up @@ -243,7 +244,7 @@ def testLoadWithUNC(self):
if not os.path.exists(unc_name):
raise unittest.SkipTest('Cannot connect to UNC Path')

with support.EnvironmentVarGuard() as env:
with os_helper.EnvironmentVarGuard() as env:
env.unset("TCL_LIBRARY")
stdout = subprocess.check_output(
[unc_name, '-c', 'import tkinter; print(tkinter)'])
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_tools/test_md5sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import os
import unittest
from test import support
from test.support import os_helper
from test.support import hashlib_helper
from test.support.script_helper import assert_python_ok, assert_python_failure

Expand All @@ -15,16 +15,16 @@ class MD5SumTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.script = os.path.join(scriptsdir, 'md5sum.py')
os.mkdir(support.TESTFN)
cls.fodder = os.path.join(support.TESTFN, 'md5sum.fodder')
os.mkdir(os_helper.TESTFN)
cls.fodder = os.path.join(os_helper.TESTFN, 'md5sum.fodder')
with open(cls.fodder, 'wb') as f:
f.write(b'md5sum\r\ntest file\r\n')
cls.fodder_md5 = b'd38dae2eb1ab346a292ef6850f9e1a0d'
cls.fodder_textmode_md5 = b'a8b07894e2ca3f2a4c3094065fa6e0a5'

@classmethod
def tearDownClass(cls):
support.rmtree(support.TESTFN)
os_helper.rmtree(os_helper.TESTFN)

def test_noargs(self):
rc, out, err = assert_python_ok(self.script)
Expand Down
7 changes: 4 additions & 3 deletions Lib/test/test_turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
from test import support
from test.support import import_helper
from test.support import os_helper


turtle = import_helper.import_module('turtle')
Expand Down Expand Up @@ -52,10 +53,10 @@
class TurtleConfigTest(unittest.TestCase):

def get_cfg_file(self, cfg_str):
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(cfg_str)
return support.TESTFN
return os_helper.TESTFN

def test_config_dict(self):

Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_urllib2net.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import errno
import unittest
from test import support
from test.support import os_helper
from test.support import socket_helper
from test.test_urllib2 import sanepathname2url

Expand Down Expand Up @@ -148,7 +149,7 @@ def test_ftp(self):
self._test_urls(urls, self._extra_handlers())

def test_file(self):
TESTFN = support.TESTFN
TESTFN = os_helper.TESTFN
f = open(TESTFN, 'w')
try:
f.write('hi there\n')
Expand Down
Loading