bpo-40275: Use new test.support helper submodules in tests (GH-21448)

This commit is contained in:
Hai Shi 2020-08-04 00:49:18 +08:00 committed by GitHub
parent bb0424b122
commit 4660597b51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 185 additions and 154 deletions

View file

@ -3,7 +3,9 @@ import os
import sys
import unittest
import warnings
from test.support import TestFailed, FakePath
from test.support import os_helper
from test.support import TestFailed
from test.support.os_helper import FakePath
from test import support, test_genericpath
from tempfile import TemporaryFile
@ -254,34 +256,34 @@ class TestNtpath(NtpathTestCase):
tester("ntpath.realpath('\\'.join(['..'] * 50))",
ntpath.splitdrive(expected)[0] + '\\')
@support.skip_unless_symlink
@os_helper.skip_unless_symlink
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
def test_realpath_basic(self):
ABSTFN = ntpath.abspath(support.TESTFN)
ABSTFN = ntpath.abspath(os_helper.TESTFN)
open(ABSTFN, "wb").close()
self.addCleanup(support.unlink, ABSTFN)
self.addCleanup(support.unlink, ABSTFN + "1")
self.addCleanup(os_helper.unlink, ABSTFN)
self.addCleanup(os_helper.unlink, ABSTFN + "1")
os.symlink(ABSTFN, ABSTFN + "1")
self.assertPathEqual(ntpath.realpath(ABSTFN + "1"), ABSTFN)
self.assertPathEqual(ntpath.realpath(os.fsencode(ABSTFN + "1")),
os.fsencode(ABSTFN))
@support.skip_unless_symlink
@os_helper.skip_unless_symlink
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
def test_realpath_relative(self):
ABSTFN = ntpath.abspath(support.TESTFN)
ABSTFN = ntpath.abspath(os_helper.TESTFN)
open(ABSTFN, "wb").close()
self.addCleanup(support.unlink, ABSTFN)
self.addCleanup(support.unlink, ABSTFN + "1")
self.addCleanup(os_helper.unlink, ABSTFN)
self.addCleanup(os_helper.unlink, ABSTFN + "1")
os.symlink(ABSTFN, ntpath.relpath(ABSTFN + "1"))
self.assertPathEqual(ntpath.realpath(ABSTFN + "1"), ABSTFN)
@support.skip_unless_symlink
@os_helper.skip_unless_symlink
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
def test_realpath_broken_symlinks(self):
ABSTFN = ntpath.abspath(support.TESTFN)
ABSTFN = ntpath.abspath(os_helper.TESTFN)
os.mkdir(ABSTFN)
self.addCleanup(support.rmtree, ABSTFN)
@ -335,18 +337,18 @@ class TestNtpath(NtpathTestCase):
self.assertPathEqual(ntpath.realpath(b"broken5"),
os.fsencode(ABSTFN + r"\missing"))
@support.skip_unless_symlink
@os_helper.skip_unless_symlink
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
def test_realpath_symlink_loops(self):
# Symlink loops are non-deterministic as to which path is returned, but
# it will always be the fully resolved path of one member of the cycle
ABSTFN = ntpath.abspath(support.TESTFN)
self.addCleanup(support.unlink, ABSTFN)
self.addCleanup(support.unlink, ABSTFN + "1")
self.addCleanup(support.unlink, ABSTFN + "2")
self.addCleanup(support.unlink, ABSTFN + "y")
self.addCleanup(support.unlink, ABSTFN + "c")
self.addCleanup(support.unlink, ABSTFN + "a")
ABSTFN = ntpath.abspath(os_helper.TESTFN)
self.addCleanup(os_helper.unlink, ABSTFN)
self.addCleanup(os_helper.unlink, ABSTFN + "1")
self.addCleanup(os_helper.unlink, ABSTFN + "2")
self.addCleanup(os_helper.unlink, ABSTFN + "y")
self.addCleanup(os_helper.unlink, ABSTFN + "c")
self.addCleanup(os_helper.unlink, ABSTFN + "a")
os.symlink(ABSTFN, ABSTFN)
self.assertPathEqual(ntpath.realpath(ABSTFN), ABSTFN)
@ -381,14 +383,14 @@ class TestNtpath(NtpathTestCase):
# Test using relative path as well.
self.assertPathEqual(ntpath.realpath(ntpath.basename(ABSTFN)), ABSTFN)
@support.skip_unless_symlink
@os_helper.skip_unless_symlink
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
def test_realpath_symlink_prefix(self):
ABSTFN = ntpath.abspath(support.TESTFN)
self.addCleanup(support.unlink, ABSTFN + "3")
self.addCleanup(support.unlink, "\\\\?\\" + ABSTFN + "3.")
self.addCleanup(support.unlink, ABSTFN + "3link")
self.addCleanup(support.unlink, ABSTFN + "3.link")
ABSTFN = ntpath.abspath(os_helper.TESTFN)
self.addCleanup(os_helper.unlink, ABSTFN + "3")
self.addCleanup(os_helper.unlink, "\\\\?\\" + ABSTFN + "3.")
self.addCleanup(os_helper.unlink, ABSTFN + "3link")
self.addCleanup(os_helper.unlink, ABSTFN + "3.link")
with open(ABSTFN + "3", "wb") as f:
f.write(b'0')
@ -422,9 +424,9 @@ class TestNtpath(NtpathTestCase):
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
@unittest.skipUnless(HAVE_GETSHORTPATHNAME, 'need _getshortpathname')
def test_realpath_cwd(self):
ABSTFN = ntpath.abspath(support.TESTFN)
ABSTFN = ntpath.abspath(os_helper.TESTFN)
support.unlink(ABSTFN)
os_helper.unlink(ABSTFN)
support.rmtree(ABSTFN)
os.mkdir(ABSTFN)
self.addCleanup(support.rmtree, ABSTFN)
@ -449,7 +451,7 @@ class TestNtpath(NtpathTestCase):
self.assertPathEqual(test_file_long, ntpath.realpath("file.txt"))
def test_expandvars(self):
with support.EnvironmentVarGuard() as env:
with os_helper.EnvironmentVarGuard() as env:
env.clear()
env["foo"] = "bar"
env["{foo"] = "baz1"
@ -474,13 +476,13 @@ class TestNtpath(NtpathTestCase):
tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
tester('ntpath.expandvars("bar\'%foo%")', "bar\'%foo%")
@unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII')
@unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII')
def test_expandvars_nonascii(self):
def check(value, expected):
tester('ntpath.expandvars(%r)' % value, expected)
with support.EnvironmentVarGuard() as env:
with os_helper.EnvironmentVarGuard() as env:
env.clear()
nonascii = support.FS_NONASCII
nonascii = os_helper.FS_NONASCII
env['spam'] = nonascii
env[nonascii] = 'ham' + nonascii
check('$spam bar', '%s bar' % nonascii)
@ -497,7 +499,7 @@ class TestNtpath(NtpathTestCase):
def test_expanduser(self):
tester('ntpath.expanduser("test")', 'test')
with support.EnvironmentVarGuard() as env:
with os_helper.EnvironmentVarGuard() as env:
env.clear()
tester('ntpath.expanduser("~test")', '~test')
@ -533,7 +535,7 @@ class TestNtpath(NtpathTestCase):
@unittest.skipUnless(nt, "abspath requires 'nt' module")
def test_abspath(self):
tester('ntpath.abspath("C:\\")', "C:\\")
with support.temp_cwd(support.TESTFN) as cwd_dir: # bpo-31047
with os_helper.temp_cwd(os_helper.TESTFN) as cwd_dir: # bpo-31047
tester('ntpath.abspath("")', cwd_dir)
tester('ntpath.abspath(" ")', cwd_dir + "\\ ")
tester('ntpath.abspath("?")', cwd_dir + "\\?")
@ -545,7 +547,7 @@ class TestNtpath(NtpathTestCase):
tester('ntpath.relpath(ntpath.abspath("a"))', 'a')
tester('ntpath.relpath("a/b")', 'a\\b')
tester('ntpath.relpath("../a/b")', '..\\a\\b')
with support.temp_cwd(support.TESTFN) as cwd_dir:
with os_helper.temp_cwd(os_helper.TESTFN) as cwd_dir:
currentdir = ntpath.basename(cwd_dir)
tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a')
tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b')
@ -661,7 +663,7 @@ class TestNtpath(NtpathTestCase):
self.assertTrue(ntpath.ismount(b"\\\\.\\c:\\"))
self.assertTrue(ntpath.ismount(b"\\\\.\\C:\\"))
with support.temp_dir() as d:
with os_helper.temp_dir() as d:
self.assertFalse(ntpath.ismount(d))
if sys.platform == "win32":
@ -725,9 +727,9 @@ class PathLikeTests(NtpathTestCase):
path = ntpath
def setUp(self):
self.file_name = support.TESTFN
self.file_path = FakePath(support.TESTFN)
self.addCleanup(support.unlink, self.file_name)
self.file_name = os_helper.TESTFN
self.file_path = FakePath(os_helper.TESTFN)
self.addCleanup(os_helper.unlink, self.file_name)
with open(self.file_name, 'xb', 0) as file:
file.write(b"test_ntpath.PathLikeTests")