Skip to content

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

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 2 commits into from
Aug 3, 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
5 changes: 3 additions & 2 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from io import StringIO

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

Expand Down Expand Up @@ -3244,7 +3245,7 @@ class TestShortColumns(HelpTestCase):
but we don't want any exceptions thrown in such cases. Only ugly representation.
'''
def setUp(self):
env = support.EnvironmentVarGuard()
env = os_helper.EnvironmentVarGuard()
env.set("COLUMNS", '15')
self.addCleanup(env.__exit__)

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import types
import unittest

from test.support import import_module
from test.support.import_helper import import_module
asyncio = import_module("asyncio")


Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed to the PSF under a contributor agreement.

from test import support, test_tools
from test.support import os_helper
from unittest import TestCase
import collections
import inspect
Expand Down Expand Up @@ -797,7 +798,7 @@ def test_external(self):
source = support.findfile('clinic.test')
with open(source, 'r', encoding='utf-8') as f:
original = f.read()
with support.temp_dir() as testdir:
with os_helper.temp_dir() as testdir:
testfile = os.path.join(testdir, 'clinic.test.c')
with open(testfile, 'w', encoding='utf-8') as f:
f.write(original)
Expand Down
28 changes: 15 additions & 13 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from unittest import mock

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

try:
import _testcapi
Expand Down Expand Up @@ -709,11 +711,11 @@ def test_bug691291(self):
s1 = 'Hello\r\nworld\r\n'

s = s1.encode(self.encoding)
self.addCleanup(support.unlink, support.TESTFN)
with open(support.TESTFN, 'wb') as fp:
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
with open(os_helper.TESTFN, 'wb') as fp:
fp.write(s)
with support.check_warnings(('', DeprecationWarning)):
reader = codecs.open(support.TESTFN, 'U', encoding=self.encoding)
with warnings_helper.check_warnings(('', DeprecationWarning)):
reader = codecs.open(os_helper.TESTFN, 'U', encoding=self.encoding)
with reader:
self.assertEqual(reader.read(), s1)

Expand Down Expand Up @@ -1697,10 +1699,10 @@ def test_all(self):
getattr(codecs, api)

def test_open(self):
self.addCleanup(support.unlink, support.TESTFN)
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
for mode in ('w', 'r', 'r+', 'w+', 'a', 'a+'):
with self.subTest(mode), \
codecs.open(support.TESTFN, mode, 'ascii') as file:
codecs.open(os_helper.TESTFN, mode, 'ascii') as file:
self.assertIsInstance(file, codecs.StreamReaderWriter)

def test_undefined(self):
Expand All @@ -1718,7 +1720,7 @@ def test_file_closes_if_lookup_error_raised(self):
mock_open = mock.mock_open()
with mock.patch('builtins.open', mock_open) as file:
with self.assertRaises(LookupError):
codecs.open(support.TESTFN, 'wt', 'invalid-encoding')
codecs.open(os_helper.TESTFN, 'wt', 'invalid-encoding')

file().close.assert_called()

Expand Down Expand Up @@ -2516,10 +2518,10 @@ def test_seek0(self):
"utf-32",
"utf-32-le",
"utf-32-be")
self.addCleanup(support.unlink, support.TESTFN)
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
for encoding in tests:
# Check if the BOM is written only once
with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
with codecs.open(os_helper.TESTFN, 'w+', encoding=encoding) as f:
f.write(data)
f.write(data)
f.seek(0)
Expand All @@ -2528,7 +2530,7 @@ def test_seek0(self):
self.assertEqual(f.read(), data * 2)

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

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

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

# (StreamWriter) Check that the BOM is not written after a seek()
# at a position different than the start
with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
with codecs.open(os_helper.TESTFN, 'w+', encoding=encoding) as f:
f.writer.write(data)
f.writer.seek(f.writer.tell())
f.writer.write(data)
Expand Down
16 changes: 10 additions & 6 deletions Lib/test/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""

from test import support
from test.support import import_helper
from test.support import os_helper
import doctest
import functools
import os
Expand Down Expand Up @@ -441,7 +443,7 @@ def basics(): r"""
>>> tests = finder.find(sample_func)

>>> print(tests) # doctest: +ELLIPSIS
[<DocTest sample_func from ...:25 (1 example)>]
[<DocTest sample_func from ...:27 (1 example)>]

The exact name depends on how test_doctest was invoked, so allow for
leading path components.
Expand Down Expand Up @@ -705,7 +707,7 @@ def test_empty_namespace_package(self):
try:
mod = importlib.import_module(pkg_name)
finally:
support.forget(pkg_name)
import_helper.forget(pkg_name)
sys.path.pop()

include_empty_finder = doctest.DocTestFinder(exclude_empty=False)
Expand Down Expand Up @@ -2758,7 +2760,7 @@ def test_lineendings(): r"""
>>> dn = tempfile.mkdtemp()
>>> pkg = os.path.join(dn, "doctest_testpkg")
>>> os.mkdir(pkg)
>>> support.create_empty_file(os.path.join(pkg, "__init__.py"))
>>> os_helper.create_empty_file(os.path.join(pkg, "__init__.py"))
>>> fn = os.path.join(pkg, "doctest_testfile.txt")
>>> with open(fn, 'wb') as f:
... f.write(
Expand Down Expand Up @@ -2840,7 +2842,8 @@ def test_CLI(): r"""
simple tests and no errors. We'll run both the unadorned doctest command, and
the verbose version, and then check the output:

>>> from test.support import script_helper, temp_dir
>>> from test.support import script_helper
>>> from test.support.os_helper import temp_dir
>>> with temp_dir() as tmpdir:
... fn = os.path.join(tmpdir, 'myfile.doc')
... with open(fn, 'w') as f:
Expand Down Expand Up @@ -2891,7 +2894,8 @@ def test_CLI(): r"""
file ends in '.py', its handling of python module files (as opposed to straight
text files).

>>> from test.support import script_helper, temp_dir
>>> from test.support import script_helper
>>> from test.support.os_helper import temp_dir
>>> with temp_dir() as tmpdir:
... fn = os.path.join(tmpdir, 'myfile.doc')
... with open(fn, 'w') as f:
Expand Down Expand Up @@ -3109,7 +3113,7 @@ def test_main():


def test_coverage(coverdir):
trace = support.import_module('trace')
trace = import_helper.import_module('trace')
tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,],
trace=0, count=1)
tracer.run('test_main()')
Expand Down
9 changes: 6 additions & 3 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import weakref
import errno

from test.support import (TESTFN, captured_stderr, check_impl_detail,
check_warnings, cpython_only, gc_collect,
no_tracing, unlink, import_module, script_helper,
from test.support import (captured_stderr, check_impl_detail,
cpython_only, gc_collect,
no_tracing, script_helper,
SuppressCrashReport)
from test.support.import_helper import import_module
from test.support.os_helper import TESTFN, unlink
from test.support.warnings_helper import check_warnings
from test import support


Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ftplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from test import support
from test.support import threading_helper
from test.support import socket_helper
from test.support import warnings_helper
from test.support.socket_helper import HOST, HOSTv6

TIMEOUT = support.LOOPBACK_TIMEOUT
Expand Down Expand Up @@ -623,7 +624,7 @@ def test_storlines(self):

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

def test_nlst(self):
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_gc.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import unittest
import unittest.mock
from test.support import (verbose, refcount_test, run_unittest,
cpython_only, temp_dir, TESTFN, unlink,
import_module)
cpython_only)
from test.support.import_helper import import_module
from test.support.os_helper import temp_dir, TESTFN, unlink
from test.support.script_helper import assert_python_ok, make_script
from test.support import threading_helper

Expand Down
Loading