Skip to content

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

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 13 commits into from
Jul 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
4 changes: 2 additions & 2 deletions Lib/distutils/tests/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import unittest
import sysconfig
from copy import deepcopy
import test.support
from test.support import os_helper

from distutils import log
from distutils.log import DEBUG, INFO, WARN, ERROR, FATAL
Expand Down Expand Up @@ -64,7 +64,7 @@ def tearDown(self):
super().tearDown()
while self.tempdirs:
tmpdir = self.tempdirs.pop()
test.support.rmtree(tmpdir)
os_helper.rmtree(tmpdir)

def mkdtemp(self):
"""Create a temporary directory that will be cleaned up.
Expand Down
3 changes: 2 additions & 1 deletion Lib/distutils/tests/test_build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import unittest
from test import support
from test.support import os_helper
from test.support.script_helper import assert_python_ok

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

Expand Down
13 changes: 6 additions & 7 deletions Lib/distutils/tests/test_filelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from distutils.filelist import glob_to_re, translate_pattern, FileList
from distutils import filelist

import test.support
from test.support import os_helper
from test.support import captured_stdout, run_unittest
from distutils.tests import support
Expand Down Expand Up @@ -298,7 +297,7 @@ def test_process_template(self):
class FindAllTestCase(unittest.TestCase):
@os_helper.skip_unless_symlink
def test_missing_symlink(self):
with test.support.temp_cwd():
with os_helper.temp_cwd():
os.symlink('foo', 'bar')
self.assertEqual(filelist.findall(), [])

Expand All @@ -308,13 +307,13 @@ def test_basic_discovery(self):
'.' as the parameter, the dot should be omitted from
the results.
"""
with test.support.temp_cwd():
with os_helper.temp_cwd():
os.mkdir('foo')
file1 = os.path.join('foo', 'file1.txt')
test.support.create_empty_file(file1)
os_helper.create_empty_file(file1)
os.mkdir('bar')
file2 = os.path.join('bar', 'file2.txt')
test.support.create_empty_file(file2)
os_helper.create_empty_file(file2)
expected = [file2, file1]
self.assertEqual(sorted(filelist.findall()), expected)

Expand All @@ -323,9 +322,9 @@ def test_non_local_discovery(self):
When findall is called with another path, the full
path name should be returned.
"""
with test.support.temp_dir() as temp_dir:
with os_helper.temp_dir() as temp_dir:
file1 = os.path.join(temp_dir, 'file1.txt')
test.support.create_empty_file(file1)
os_helper.create_empty_file(file1)
expected = [file1]
self.assertEqual(filelist.findall(temp_dir), expected)

Expand Down
18 changes: 9 additions & 9 deletions Lib/distutils/tests/test_spawn.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import unittest.mock
from test.support import run_unittest, unix_shell
from test import support as test_support
from test.support import os_helper

from distutils.spawn import find_executable
from distutils.spawn import spawn
Expand Down Expand Up @@ -44,9 +44,9 @@ def test_spawn(self):
spawn([exe]) # should work without any error

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

# test find in the current directory
with test_support.change_cwd(tmp_dir):
with os_helper.change_cwd(tmp_dir):
rv = find_executable(program)
self.assertEqual(rv, program)

Expand All @@ -76,7 +76,7 @@ def test_find_executable(self):
self.assertIsNone(rv)

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

# look in current directory
with test_support.change_cwd(tmp_dir):
with os_helper.change_cwd(tmp_dir):
rv = find_executable(program)
self.assertEqual(rv, program)

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

# look in current directory
with test_support.change_cwd(tmp_dir):
with os_helper.change_cwd(tmp_dir):
rv = find_executable(program)
self.assertEqual(rv, program)

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

# without confstr
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_dbm_gnu.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from test import support
gdbm = support.import_module("dbm.gnu") #skip if not supported
from test.support import import_helper
gdbm = import_helper.import_module("dbm.gnu") #skip if not supported
import unittest
import os
from test.support import TESTFN, TESTFN_NONASCII, unlink
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_eof.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import sys
from test import support
from test.support import os_helper
from test.support import script_helper
import unittest

Expand Down Expand Up @@ -48,7 +49,7 @@ def test_line_continuation_EOF(self):
@unittest.skipIf(not sys.executable, "sys.executable required")
def test_line_continuation_EOF_from_file_bpo2180(self):
"""Ensure tok_nextc() does not add too many ending newlines."""
with support.temp_dir() as temp_dir:
with os_helper.temp_dir() as temp_dir:
file_name = script_helper.make_script(temp_dir, 'foo', '\\')
rc, out, err = script_helper.assert_python_failure(file_name)
self.assertIn(b'unexpected EOF while parsing', err)
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import unittest
import warnings
from test import support
from test.support import _4G, bigmemtest, import_fresh_module
from test.support import _4G, bigmemtest
from test.support.import_helper import import_fresh_module
from test.support import threading_helper
from http.client import HTTPException

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_idle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from test.support import import_module
from test.support.import_helper import import_module

# Skip test_idle if _tkinter wasn't built, if tkinter is missing,
# if tcl/tk is not the 8.5+ needed for ttk widgets,
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_imaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
run_with_tz, run_with_locale, cpython_only)
from test.support import hashlib_helper
from test.support import threading_helper
from test.support import warnings_helper
import unittest
from unittest import mock
from datetime import datetime, timezone, timedelta
Expand Down Expand Up @@ -575,7 +576,7 @@ def test_ssl_verified(self):
# to CPython stdlib
@cpython_only
def test_certfile_arg_warn(self):
with support.check_warnings(('', DeprecationWarning)):
with warnings_helper.check_warnings(('', DeprecationWarning)):
with mock.patch.object(self.imap_class, 'open'):
with mock.patch.object(self.imap_class, '_connect'):
self.imap_class('localhost', 143, certfile=CERTFILE)
Expand Down
73 changes: 37 additions & 36 deletions Lib/test/test_marshal.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from test import support
from test.support import os_helper
import array
import io
import marshal
Expand All @@ -17,13 +18,13 @@ def helper(self, sample, *extra):
new = marshal.loads(marshal.dumps(sample, *extra))
self.assertEqual(sample, new)
try:
with open(support.TESTFN, "wb") as f:
with open(os_helper.TESTFN, "wb") as f:
marshal.dump(sample, f, *extra)
with open(support.TESTFN, "rb") as f:
with open(os_helper.TESTFN, "rb") as f:
new = marshal.load(f)
self.assertEqual(sample, new)
finally:
support.unlink(support.TESTFN)
os_helper.unlink(os_helper.TESTFN)

class IntTestCase(unittest.TestCase, HelperMixin):
def test_ints(self):
Expand Down Expand Up @@ -281,20 +282,20 @@ def test_multiple_dumps_and_loads(self):
ilen = len(interleaved)
positions = []
try:
with open(support.TESTFN, 'wb') as f:
with open(os_helper.TESTFN, 'wb') as f:
for d in data:
marshal.dump(d, f)
if ilen:
f.write(interleaved)
positions.append(f.tell())
with open(support.TESTFN, 'rb') as f:
with open(os_helper.TESTFN, 'rb') as f:
for i, d in enumerate(data):
self.assertEqual(d, marshal.load(f))
if ilen:
f.read(ilen)
self.assertEqual(positions[i], f.tell())
finally:
support.unlink(support.TESTFN)
os_helper.unlink(os_helper.TESTFN)

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

def test_write_long_to_file(self):
for v in range(marshal.version + 1):
_testcapi.pymarshal_write_long_to_file(0x12345678, support.TESTFN, v)
with open(support.TESTFN, 'rb') as f:
_testcapi.pymarshal_write_long_to_file(0x12345678, os_helper.TESTFN, v)
with open(os_helper.TESTFN, 'rb') as f:
data = f.read()
support.unlink(support.TESTFN)
os_helper.unlink(os_helper.TESTFN)
self.assertEqual(data, b'\x78\x56\x34\x12')

def test_write_object_to_file(self):
obj = ('\u20ac', b'abc', 123, 45.6, 7+8j, 'long line '*1000)
for v in range(marshal.version + 1):
_testcapi.pymarshal_write_object_to_file(obj, support.TESTFN, v)
with open(support.TESTFN, 'rb') as f:
_testcapi.pymarshal_write_object_to_file(obj, os_helper.TESTFN, v)
with open(os_helper.TESTFN, 'rb') as f:
data = f.read()
support.unlink(support.TESTFN)
os_helper.unlink(os_helper.TESTFN)
self.assertEqual(marshal.loads(data), obj)

def test_read_short_from_file(self):
with open(support.TESTFN, 'wb') as f:
with open(os_helper.TESTFN, 'wb') as f:
f.write(b'\x34\x12xxxx')
r, p = _testcapi.pymarshal_read_short_from_file(support.TESTFN)
support.unlink(support.TESTFN)
r, p = _testcapi.pymarshal_read_short_from_file(os_helper.TESTFN)
os_helper.unlink(os_helper.TESTFN)
self.assertEqual(r, 0x1234)
self.assertEqual(p, 2)

with open(support.TESTFN, 'wb') as f:
with open(os_helper.TESTFN, 'wb') as f:
f.write(b'\x12')
with self.assertRaises(EOFError):
_testcapi.pymarshal_read_short_from_file(support.TESTFN)
support.unlink(support.TESTFN)
_testcapi.pymarshal_read_short_from_file(os_helper.TESTFN)
os_helper.unlink(os_helper.TESTFN)

def test_read_long_from_file(self):
with open(support.TESTFN, 'wb') as f:
with open(os_helper.TESTFN, 'wb') as f:
f.write(b'\x78\x56\x34\x12xxxx')
r, p = _testcapi.pymarshal_read_long_from_file(support.TESTFN)
support.unlink(support.TESTFN)
r, p = _testcapi.pymarshal_read_long_from_file(os_helper.TESTFN)
os_helper.unlink(os_helper.TESTFN)
self.assertEqual(r, 0x12345678)
self.assertEqual(p, 4)

with open(support.TESTFN, 'wb') as f:
with open(os_helper.TESTFN, 'wb') as f:
f.write(b'\x56\x34\x12')
with self.assertRaises(EOFError):
_testcapi.pymarshal_read_long_from_file(support.TESTFN)
support.unlink(support.TESTFN)
_testcapi.pymarshal_read_long_from_file(os_helper.TESTFN)
os_helper.unlink(os_helper.TESTFN)

def test_read_last_object_from_file(self):
obj = ('\u20ac', b'abc', 123, 45.6, 7+8j)
for v in range(marshal.version + 1):
data = marshal.dumps(obj, v)
with open(support.TESTFN, 'wb') as f:
with open(os_helper.TESTFN, 'wb') as f:
f.write(data + b'xxxx')
r, p = _testcapi.pymarshal_read_last_object_from_file(support.TESTFN)
support.unlink(support.TESTFN)
r, p = _testcapi.pymarshal_read_last_object_from_file(os_helper.TESTFN)
os_helper.unlink(os_helper.TESTFN)
self.assertEqual(r, obj)

with open(support.TESTFN, 'wb') as f:
with open(os_helper.TESTFN, 'wb') as f:
f.write(data[:1])
with self.assertRaises(EOFError):
_testcapi.pymarshal_read_last_object_from_file(support.TESTFN)
support.unlink(support.TESTFN)
_testcapi.pymarshal_read_last_object_from_file(os_helper.TESTFN)
os_helper.unlink(os_helper.TESTFN)

def test_read_object_from_file(self):
obj = ('\u20ac', b'abc', 123, 45.6, 7+8j)
for v in range(marshal.version + 1):
data = marshal.dumps(obj, v)
with open(support.TESTFN, 'wb') as f:
with open(os_helper.TESTFN, 'wb') as f:
f.write(data + b'xxxx')
r, p = _testcapi.pymarshal_read_object_from_file(support.TESTFN)
support.unlink(support.TESTFN)
r, p = _testcapi.pymarshal_read_object_from_file(os_helper.TESTFN)
os_helper.unlink(os_helper.TESTFN)
self.assertEqual(r, obj)
self.assertEqual(p, len(data))

with open(support.TESTFN, 'wb') as f:
with open(os_helper.TESTFN, 'wb') as f:
f.write(data[:1])
with self.assertRaises(EOFError):
_testcapi.pymarshal_read_object_from_file(support.TESTFN)
support.unlink(support.TESTFN)
_testcapi.pymarshal_read_object_from_file(os_helper.TESTFN)
os_helper.unlink(os_helper.TESTFN)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_py_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_cache_path(self):
self.assertTrue(os.path.exists(self.cache_path))

def test_cwd(self):
with support.change_cwd(self.directory):
with os_helper.change_cwd(self.directory):
py_compile.compile(os.path.basename(self.source_path),
os.path.basename(self.pyc_path))
self.assertTrue(os.path.exists(self.pyc_path))
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_set.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 warnings_helper
import gc
import weakref
import operator
Expand Down Expand Up @@ -953,7 +954,7 @@ def test_repr(self):

class TestBasicOpsMixedStringBytes(TestBasicOps, unittest.TestCase):
def setUp(self):
self._warning_filters = support.check_warnings()
self._warning_filters = warnings_helper.check_warnings()
self._warning_filters.__enter__()
warnings.simplefilter('ignore', BytesWarning)
self.case = "string and bytes set"
Expand Down
Loading