]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40275: Use new test.support helper submodules in tests (GH-21743)
authorHai Shi <shihai1992@gmail.com>
Thu, 6 Aug 2020 11:51:29 +0000 (19:51 +0800)
committerGitHub <noreply@github.com>
Thu, 6 Aug 2020 11:51:29 +0000 (13:51 +0200)
20 files changed:
Lib/test/test_dbm_gnu.py
Lib/test/test_faulthandler.py
Lib/test/test_grp.py
Lib/test/test_http_cookiejar.py
Lib/test/test_httpservers.py
Lib/test/test_import/__init__.py
Lib/test/test_ntpath.py
Lib/test/test_site.py
Lib/test/test_socketserver.py
Lib/test/test_statistics.py
Lib/test/test_tabnanny.py
Lib/test/test_tcl.py
Lib/test/test_tools/test_md5sum.py
Lib/test/test_turtle.py
Lib/test/test_urllib2net.py
Lib/test/test_urllibnet.py
Lib/test/test_webbrowser.py
Lib/test/test_winsound.py
Lib/tkinter/test/test_tkinter/test_images.py
Lib/tkinter/test/test_tkinter/test_loadtk.py

index 078bf0e9b927016f5f715d6f6522442726539bb5..017d0ffa658bd620604b1b06e4a1c2b7847ab0b2 100644 (file)
@@ -3,7 +3,7 @@ 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
+from test.support.os_helper import TESTFN, TESTFN_NONASCII, unlink
 
 
 filename = TESTFN
index c64afe88c25d6c0c7453fa939128f3be5216fc41..80c1f7d90adf0fd07a74776b1a8d7911e697a13d 100644 (file)
@@ -7,6 +7,7 @@ import subprocess
 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
@@ -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):
index 0993f091f59560dd24fe4ac4027df0195a613e62..c7ec03ec0e4388431267d206de89cad6260f91a7 100644 (file)
@@ -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):
 
index 2d7077af6da39e58afca19bc6292246fd42ca567..99d038fa15c1e35459eb731579cdd1f4af8da553 100644 (file)
@@ -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
@@ -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))
 
@@ -353,7 +355,7 @@ class FileCookieJarTests(unittest.TestCase):
 
     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)
@@ -368,7 +370,7 @@ class FileCookieJarTests(unittest.TestCase):
 
     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:
@@ -475,7 +477,7 @@ class CookieTests(unittest.TestCase):
     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/')
@@ -599,7 +601,7 @@ class CookieTests(unittest.TestCase):
         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/")
@@ -1713,7 +1715,7 @@ class LWPCookieTests(unittest.TestCase):
         self.assertEqual(len(c), 6)
 
         # save and restore
-        filename = test.support.TESTFN
+        filename = os_helper.TESTFN
 
         try:
             c.save(filename, ignore_discard=True)
@@ -1753,7 +1755,7 @@ class LWPCookieTests(unittest.TestCase):
         # 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))
index 0c871afca37bdc02dd0a212450bf399bac351cc6..a7e1719ab60ee759233642d35784187c88b73c16 100644 (file)
@@ -67,7 +67,7 @@ class TestServerThread(threading.Thread):
 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()
@@ -621,7 +621,7 @@ class CGIHTTPServerTestCase(BaseTestCase):
         # 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:
index f4a83d2e7a13a179cc4a641fe7001e319863eba0..6fd3983a20f68e7ed717b7175d67865de4464e0b 100644 (file)
@@ -18,7 +18,6 @@ import time
 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 (
@@ -480,7 +479,7 @@ class ImportTests(unittest.TestCase):
             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)
 
index 69c44710f0b5adaeb35ca9a669dac0252544ed51..0d84ff8bce2c07a3f19c653c5ed0d4e5ec5b9aa4 100644 (file)
@@ -285,7 +285,7 @@ class TestNtpath(NtpathTestCase):
     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")
@@ -427,9 +427,9 @@ class TestNtpath(NtpathTestCase):
         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)
index 5901939725e185328c12043ecf01f89181eda53f..97b5c5de95bbc2425cc15c02008be3aa4f01b90f 100644 (file)
@@ -7,6 +7,7 @@ executing have not been removed.
 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
@@ -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])
index 5db8cec567afb58f05462848811b2519aab59243..7cdd115a951539958cd9517622b05d0a7f583295 100644 (file)
@@ -15,6 +15,7 @@ import socketserver
 
 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
 
@@ -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)
@@ -329,7 +330,7 @@ class ErrorHandlerTest(unittest.TestCase):
         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)
 
@@ -347,7 +348,7 @@ class BaseErrorTestServer(socketserver.TCPServer):
         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):
@@ -356,7 +357,7 @@ class BaseErrorTestServer(socketserver.TCPServer):
 
 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')
 
index bf415dda557e60c7d8efb0853375b868213e87f4..997110732a17657e106717621d41907407516646 100644 (file)
@@ -15,10 +15,10 @@ import random
 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.
@@ -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):
index 95840d6ac0c5f4c49e8be7e07da564c433a2b615..4dfbd2985d5b5a82c7ac614c77c91be4035c9bb4 100644 (file)
@@ -12,7 +12,8 @@ import tokenize
 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 = {
index db982dac8d6533f87ae25a1f0206e2e8eab24062..cd2a30e533ae032a50cd3f3ceb6e54abdda88b72 100644 (file)
@@ -6,6 +6,7 @@ import os
 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')
@@ -192,26 +193,26 @@ class TclTest(unittest.TestCase):
 
     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')
 
@@ -243,7 +244,7 @@ class TclTest(unittest.TestCase):
         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)'])
index 321bc4bb3628261fe0474ae6d29d836b246cfcff..bfc1f287fff6e122246d568c7c31c05e1f4989d0 100644 (file)
@@ -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
 
@@ -15,8 +15,8 @@ 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'
@@ -24,7 +24,7 @@ class MD5SumTests(unittest.TestCase):
 
     @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)
index 39b3d96fb43bbb564a6eaf4d50947f837bf07338..46ff4a3aac709a721537f8fb24e861ac0da3fcfc 100644 (file)
@@ -2,6 +2,7 @@ import pickle
 import unittest
 from test import support
 from test.support import import_helper
+from test.support import os_helper
 
 
 turtle = import_helper.import_module('turtle')
@@ -52,10 +53,10 @@ visible = False
 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):
 
index cb74685715d35b987073a6d5425317171ce41963..c1d55ee8b29b34fcef94367280428f944a0e58f6 100644 (file)
@@ -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
 
@@ -148,7 +149,7 @@ class OtherNetworkTests(unittest.TestCase):
         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')
index 28680aa6b2405d795cecdd3b2bcf6d6c2764d78f..773101ce41f6021d0f735bebbd15515342b8cdc7 100644 (file)
@@ -1,5 +1,6 @@
 import unittest
 from test import support
+from test.support import os_helper
 from test.support import socket_helper
 
 import contextlib
@@ -162,7 +163,7 @@ class urlretrieveNetworkTests(unittest.TestCase):
             try:
                 yield file_location, info
             finally:
-                support.unlink(file_location)
+                os_helper.unlink(file_location)
 
     def test_basic(self):
         # Test basic functionality.
@@ -176,8 +177,8 @@ class urlretrieveNetworkTests(unittest.TestCase):
     def test_specified_path(self):
         # Make sure that specifying the location of the file to write to works.
         with self.urlretrieve(self.logo,
-                              support.TESTFN) as (file_location, info):
-            self.assertEqual(file_location, support.TESTFN)
+                              os_helper.TESTFN) as (file_location, info):
+            self.assertEqual(file_location, os_helper.TESTFN)
             self.assertTrue(os.path.exists(file_location))
             with open(file_location, 'rb') as f:
                 self.assertTrue(f.read(), "reading from temporary file failed")
index 6ceb49069f65649baad01dfcb78a29ad56cfa318..673cc995d3f5a406a3fac86ef41be0106370c47b 100644 (file)
@@ -6,6 +6,7 @@ import subprocess
 from unittest import mock
 from test import support
 from test.support import import_helper
+from test.support import os_helper
 
 
 URL = 'http://www.example.com'
@@ -305,7 +306,7 @@ class ImportTest(unittest.TestCase):
             browser = webbrowser.get().name
         except (webbrowser.Error, AttributeError) as err:
             self.skipTest(str(err))
-        with support.EnvironmentVarGuard() as env:
+        with os_helper.EnvironmentVarGuard() as env:
             env["BROWSER"] = browser
             webbrowser = import_helper.import_fresh_module('webbrowser')
             webbrowser.get()
@@ -318,12 +319,12 @@ class ImportTest(unittest.TestCase):
         except (webbrowser.Error, AttributeError, IndexError) as err:
             self.skipTest(str(err))
 
-        with support.EnvironmentVarGuard() as env:
+        with os_helper.EnvironmentVarGuard() as env:
             env["BROWSER"] = least_preferred_browser
             webbrowser = import_helper.import_fresh_module('webbrowser')
             self.assertEqual(webbrowser.get().name, least_preferred_browser)
 
-        with support.EnvironmentVarGuard() as env:
+        with os_helper.EnvironmentVarGuard() as env:
             env["BROWSER"] = sys.executable
             webbrowser = import_helper.import_fresh_module('webbrowser')
             self.assertEqual(webbrowser.get().name, sys.executable)
index dca77cb45bfa915b4ed8d0064d5d3481f49ab912..3c3359b9004fd2feb102228dfd4904ccd55b5da7 100644 (file)
@@ -5,9 +5,11 @@ import time
 import unittest
 
 from test import support
+from test.support import import_helper
+
 
 support.requires('audio')
-winsound = support.import_module('winsound')
+winsound = import_helper.import_module('winsound')
 
 
 # Unless we actually have an ear in the room, we have no idea whether a sound
index 2805d35a1f5b1bffde880cd179572c043827e05b..6c6cb4e148573febd0b28a85045c818861206efb 100644 (file)
@@ -1,6 +1,7 @@
 import unittest
 import tkinter
 from test import support
+from test.support import os_helper
 from tkinter.test.support import AbstractTkTest, requires_tcl
 
 support.requires('gui')
@@ -296,12 +297,12 @@ class PhotoImageTest(AbstractTkTest, unittest.TestCase):
 
     def test_write(self):
         image = self.create()
-        self.addCleanup(support.unlink, support.TESTFN)
+        self.addCleanup(os_helper.unlink, os_helper.TESTFN)
 
-        image.write(support.TESTFN)
+        image.write(os_helper.TESTFN)
         image2 = tkinter.PhotoImage('::img::test2', master=self.root,
                                     format='ppm',
-                                    file=support.TESTFN)
+                                    file=os_helper.TESTFN)
         self.assertEqual(str(image2), '::img::test2')
         self.assertEqual(image2.type(), 'photo')
         self.assertEqual(image2.width(), 16)
@@ -309,10 +310,10 @@ class PhotoImageTest(AbstractTkTest, unittest.TestCase):
         self.assertEqual(image2.get(0, 0), image.get(0, 0))
         self.assertEqual(image2.get(15, 8), image.get(15, 8))
 
-        image.write(support.TESTFN, format='gif', from_coords=(4, 6, 6, 9))
+        image.write(os_helper.TESTFN, format='gif', from_coords=(4, 6, 6, 9))
         image3 = tkinter.PhotoImage('::img::test3', master=self.root,
                                     format='gif',
-                                    file=support.TESTFN)
+                                    file=os_helper.TESTFN)
         self.assertEqual(str(image3), '::img::test3')
         self.assertEqual(image3.type(), 'photo')
         self.assertEqual(image3.width(), 2)
index bab7bcd37ce31882f23cbc2437e90ee734706d2a..760ba721340829ec24bf17a43b83e2eafe81b44a 100644 (file)
@@ -2,6 +2,7 @@ import os
 import sys
 import unittest
 import test.support as test_support
+from test.support import os_helper
 from tkinter import Tcl, TclError
 
 test_support.requires('gui')
@@ -24,7 +25,7 @@ class TkLoadTest(unittest.TestCase):
             # XXX Maybe on tk older than 8.4.13 it would be possible,
             # see tkinter.h.
             return
-        with test_support.EnvironmentVarGuard() as env:
+        with os_helper.EnvironmentVarGuard() as env:
             if 'DISPLAY' in os.environ:
                 del env['DISPLAY']
                 # on some platforms, deleting environment variables