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

This commit is contained in:
Hai Shi 2020-08-04 00:41:24 +08:00 committed by GitHub
parent 488512bf49
commit a7f5d93bb6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 154 additions and 132 deletions

View file

@ -11,6 +11,7 @@ import unittest.mock
import tarfile
from test import support
from test.support import os_helper
from test.support import script_helper
# Check for our compression modules.
@ -30,7 +31,7 @@ except ImportError:
def sha256sum(data):
return sha256(data).hexdigest()
TEMPDIR = os.path.abspath(support.TESTFN) + "-tardir"
TEMPDIR = os.path.abspath(os_helper.TESTFN) + "-tardir"
tarextdir = TEMPDIR + '-extract-test'
tarname = support.findfile("testtar.tar")
gzipname = os.path.join(TEMPDIR, "testtar.tar.gz")
@ -575,21 +576,21 @@ class MiscReadTestBase(CommonReadTest):
@unittest.skipUnless(hasattr(os, "link"),
"Missing hardlink implementation")
@support.skip_unless_symlink
@os_helper.skip_unless_symlink
def test_extract_hardlink(self):
# Test hardlink extraction (e.g. bug #857297).
with tarfile.open(tarname, errorlevel=1, encoding="iso8859-1") as tar:
tar.extract("ustar/regtype", TEMPDIR)
self.addCleanup(support.unlink, os.path.join(TEMPDIR, "ustar/regtype"))
self.addCleanup(os_helper.unlink, os.path.join(TEMPDIR, "ustar/regtype"))
tar.extract("ustar/lnktype", TEMPDIR)
self.addCleanup(support.unlink, os.path.join(TEMPDIR, "ustar/lnktype"))
self.addCleanup(os_helper.unlink, os.path.join(TEMPDIR, "ustar/lnktype"))
with open(os.path.join(TEMPDIR, "ustar/lnktype"), "rb") as f:
data = f.read()
self.assertEqual(sha256sum(data), sha256_regtype)
tar.extract("ustar/symtype", TEMPDIR)
self.addCleanup(support.unlink, os.path.join(TEMPDIR, "ustar/symtype"))
self.addCleanup(os_helper.unlink, os.path.join(TEMPDIR, "ustar/symtype"))
with open(os.path.join(TEMPDIR, "ustar/symtype"), "rb") as f:
data = f.read()
self.assertEqual(sha256sum(data), sha256_regtype)
@ -622,7 +623,7 @@ class MiscReadTestBase(CommonReadTest):
self.assertEqual(tarinfo.mtime, file_mtime, errmsg)
finally:
tar.close()
support.rmtree(DIR)
os_helper.rmtree(DIR)
def test_extract_directory(self):
dirtype = "ustar/dirtype"
@ -637,11 +638,11 @@ class MiscReadTestBase(CommonReadTest):
if sys.platform != "win32":
self.assertEqual(os.stat(extracted).st_mode & 0o777, 0o755)
finally:
support.rmtree(DIR)
os_helper.rmtree(DIR)
def test_extractall_pathlike_name(self):
DIR = pathlib.Path(TEMPDIR) / "extractall"
with support.temp_dir(DIR), \
with os_helper.temp_dir(DIR), \
tarfile.open(tarname, encoding="iso8859-1") as tar:
directories = [t for t in tar if t.isdir()]
tar.extractall(DIR, directories)
@ -652,7 +653,7 @@ class MiscReadTestBase(CommonReadTest):
def test_extract_pathlike_name(self):
dirtype = "ustar/dirtype"
DIR = pathlib.Path(TEMPDIR) / "extractall"
with support.temp_dir(DIR), \
with os_helper.temp_dir(DIR), \
tarfile.open(tarname, encoding="iso8859-1") as tar:
tarinfo = tar.getmember(dirtype)
tar.extract(tarinfo, path=DIR)
@ -676,7 +677,7 @@ class MiscReadTestBase(CommonReadTest):
else:
self.fail("ReadError not raised")
finally:
support.unlink(empty)
os_helper.unlink(empty)
def test_parallel_iteration(self):
# Issue #16601: Restarting iteration over tarfile continued
@ -1026,7 +1027,7 @@ class GNUReadTest(LongnameTest, ReadTest, unittest.TestCase):
fobj.write(b'x' * 4096)
fobj.truncate()
s = os.stat(name)
support.unlink(name)
os_helper.unlink(name)
return (s.st_blocks * 512 < s.st_size)
else:
return False
@ -1171,7 +1172,7 @@ class WriteTest(WriteTestBase, unittest.TestCase):
finally:
tar.close()
finally:
support.rmdir(path)
os_helper.rmdir(path)
# mock the following:
# os.listdir: so we know that files are in the wrong order
@ -1193,9 +1194,9 @@ class WriteTest(WriteTestBase, unittest.TestCase):
finally:
tar.close()
finally:
support.unlink(os.path.join(path, "1"))
support.unlink(os.path.join(path, "2"))
support.rmdir(path)
os_helper.unlink(os.path.join(path, "1"))
os_helper.unlink(os.path.join(path, "2"))
os_helper.rmdir(path)
def test_gettarinfo_pathlike_name(self):
with tarfile.open(tmpname, self.mode) as tar:
@ -1229,10 +1230,10 @@ class WriteTest(WriteTestBase, unittest.TestCase):
finally:
tar.close()
finally:
support.unlink(target)
support.unlink(link)
os_helper.unlink(target)
os_helper.unlink(link)
@support.skip_unless_symlink
@os_helper.skip_unless_symlink
def test_symlink_size(self):
path = os.path.join(TEMPDIR, "symlink")
os.symlink("link_target", path)
@ -1244,7 +1245,7 @@ class WriteTest(WriteTestBase, unittest.TestCase):
finally:
tar.close()
finally:
support.unlink(path)
os_helper.unlink(path)
def test_add_self(self):
# Test for #1257255.
@ -1257,7 +1258,7 @@ class WriteTest(WriteTestBase, unittest.TestCase):
self.assertEqual(tar.getnames(), [],
"added the archive to itself")
with support.change_cwd(TEMPDIR):
with os_helper.change_cwd(TEMPDIR):
tar.add(dstname)
self.assertEqual(tar.getnames(), [],
"added the archive to itself")
@ -1270,7 +1271,7 @@ class WriteTest(WriteTestBase, unittest.TestCase):
try:
for name in ("foo", "bar", "baz"):
name = os.path.join(tempdir, name)
support.create_empty_file(name)
os_helper.create_empty_file(name)
def filter(tarinfo):
if os.path.basename(tarinfo.name) == "bar":
@ -1298,7 +1299,7 @@ class WriteTest(WriteTestBase, unittest.TestCase):
finally:
tar.close()
finally:
support.rmtree(tempdir)
os_helper.rmtree(tempdir)
# Guarantee that stored pathnames are not modified. Don't
# remove ./ or ../ or double slashes. Still make absolute
@ -1309,7 +1310,7 @@ class WriteTest(WriteTestBase, unittest.TestCase):
# and compare the stored name with the original.
foo = os.path.join(TEMPDIR, "foo")
if not dir:
support.create_empty_file(foo)
os_helper.create_empty_file(foo)
else:
os.mkdir(foo)
@ -1326,14 +1327,14 @@ class WriteTest(WriteTestBase, unittest.TestCase):
tar.close()
if not dir:
support.unlink(foo)
os_helper.unlink(foo)
else:
support.rmdir(foo)
os_helper.rmdir(foo)
self.assertEqual(t.name, cmp_path or path.replace(os.sep, "/"))
@support.skip_unless_symlink
@os_helper.skip_unless_symlink
def test_extractall_symlinks(self):
# Test if extractall works properly when tarfile contains symlinks
tempdir = os.path.join(TEMPDIR, "testsymlinks")
@ -1356,8 +1357,8 @@ class WriteTest(WriteTestBase, unittest.TestCase):
except OSError:
self.fail("extractall failed with symlinked files")
finally:
support.unlink(temparchive)
support.rmtree(tempdir)
os_helper.unlink(temparchive)
os_helper.rmtree(tempdir)
def test_pathnames(self):
self._test_pathname("foo")
@ -1385,7 +1386,7 @@ class WriteTest(WriteTestBase, unittest.TestCase):
def test_cwd(self):
# Test adding the current working directory.
with support.change_cwd(TEMPDIR):
with os_helper.change_cwd(TEMPDIR):
tar = tarfile.open(tmpname, self.mode)
try:
tar.add(".")
@ -1453,7 +1454,7 @@ class StreamWriteTest(WriteTestBase, unittest.TestCase):
# Test for issue #8464: Create files with correct
# permissions.
if os.path.exists(tmpname):
support.unlink(tmpname)
os_helper.unlink(tmpname)
original_umask = os.umask(0o022)
try:
@ -1599,7 +1600,7 @@ class DeviceHeaderTest(WriteTestBase, unittest.TestCase):
self.assertEqual(buf_blk[device_headers], b"0000000\0" * 2)
self.assertEqual(buf_reg[device_headers], b"\0" * 16)
finally:
support.rmtree(tempdir)
os_helper.rmtree(tempdir)
class CreateTest(WriteTestBase, unittest.TestCase):
@ -1609,7 +1610,7 @@ class CreateTest(WriteTestBase, unittest.TestCase):
file_path = os.path.join(TEMPDIR, "spameggs42")
def setUp(self):
support.unlink(tmpname)
os_helper.unlink(tmpname)
@classmethod
def setUpClass(cls):
@ -1618,7 +1619,7 @@ class CreateTest(WriteTestBase, unittest.TestCase):
@classmethod
def tearDownClass(cls):
support.unlink(cls.file_path)
os_helper.unlink(cls.file_path)
def test_create(self):
with tarfile.open(tmpname, self.mode) as tobj:
@ -1733,8 +1734,8 @@ class HardlinkTest(unittest.TestCase):
def tearDown(self):
self.tar.close()
support.unlink(self.foo)
support.unlink(self.bar)
os_helper.unlink(self.foo)
os_helper.unlink(self.bar)
def test_add_twice(self):
# The same name will be added as a REGTYPE every
@ -2043,7 +2044,7 @@ class AppendTestBase:
def setUp(self):
self.tarname = tmpname
if os.path.exists(self.tarname):
support.unlink(self.tarname)
os_helper.unlink(self.tarname)
def _create_testtar(self, mode="w:"):
with tarfile.open(tarname, encoding="iso8859-1") as src:
@ -2288,7 +2289,7 @@ class CommandLineTest(unittest.TestCase):
files = [support.findfile('tokenize_tests.txt'),
support.findfile('tokenize_tests-no-coding-cookie-'
'and-utf8-bom-sig-only.txt')]
self.addCleanup(support.unlink, tar_name)
self.addCleanup(os_helper.unlink, tar_name)
with tarfile.open(tar_name, 'w') as tf:
for tardata in files:
tf.add(tardata, arcname=os.path.basename(tardata))
@ -2334,7 +2335,7 @@ class CommandLineTest(unittest.TestCase):
self.assertEqual(out, b'')
self.assertEqual(rc, 1)
finally:
support.unlink(tmpname)
os_helper.unlink(tmpname)
def test_list_command(self):
for tar_name in testtarnames:
@ -2376,7 +2377,7 @@ class CommandLineTest(unittest.TestCase):
with tarfile.open(tmpname) as tar:
tar.getmembers()
finally:
support.unlink(tmpname)
os_helper.unlink(tmpname)
def test_create_command_verbose(self):
files = [support.findfile('tokenize_tests.txt'),
@ -2390,7 +2391,7 @@ class CommandLineTest(unittest.TestCase):
with tarfile.open(tmpname) as tar:
tar.getmembers()
finally:
support.unlink(tmpname)
os_helper.unlink(tmpname)
def test_create_command_dotless_filename(self):
files = [support.findfile('tokenize_tests.txt')]
@ -2400,7 +2401,7 @@ class CommandLineTest(unittest.TestCase):
with tarfile.open(dotlessname) as tar:
tar.getmembers()
finally:
support.unlink(dotlessname)
os_helper.unlink(dotlessname)
def test_create_command_dot_started_filename(self):
tar_name = os.path.join(TEMPDIR, ".testtar")
@ -2411,7 +2412,7 @@ class CommandLineTest(unittest.TestCase):
with tarfile.open(tar_name) as tar:
tar.getmembers()
finally:
support.unlink(tar_name)
os_helper.unlink(tar_name)
def test_create_command_compressed(self):
files = [support.findfile('tokenize_tests.txt'),
@ -2426,41 +2427,41 @@ class CommandLineTest(unittest.TestCase):
with filetype.taropen(tar_name) as tar:
tar.getmembers()
finally:
support.unlink(tar_name)
os_helper.unlink(tar_name)
def test_extract_command(self):
self.make_simple_tarfile(tmpname)
for opt in '-e', '--extract':
try:
with support.temp_cwd(tarextdir):
with os_helper.temp_cwd(tarextdir):
out = self.tarfilecmd(opt, tmpname)
self.assertEqual(out, b'')
finally:
support.rmtree(tarextdir)
os_helper.rmtree(tarextdir)
def test_extract_command_verbose(self):
self.make_simple_tarfile(tmpname)
for opt in '-v', '--verbose':
try:
with support.temp_cwd(tarextdir):
with os_helper.temp_cwd(tarextdir):
out = self.tarfilecmd(opt, '-e', tmpname,
PYTHONIOENCODING='utf-8')
self.assertIn(b' file is extracted.', out)
finally:
support.rmtree(tarextdir)
os_helper.rmtree(tarextdir)
def test_extract_command_different_directory(self):
self.make_simple_tarfile(tmpname)
try:
with support.temp_cwd(tarextdir):
with os_helper.temp_cwd(tarextdir):
out = self.tarfilecmd('-e', tmpname, 'spamdir')
self.assertEqual(out, b'')
finally:
support.rmtree(tarextdir)
os_helper.rmtree(tarextdir)
def test_extract_command_invalid_file(self):
zipname = support.findfile('zipdir.zip')
with support.temp_cwd(tarextdir):
with os_helper.temp_cwd(tarextdir):
rc, out, err = self.tarfilecmd_failure('-e', zipname)
self.assertIn(b' is not a tar archive.', err)
self.assertEqual(out, b'')
@ -2723,7 +2724,7 @@ class NumericOwnerTest(unittest.TestCase):
def setUpModule():
support.unlink(TEMPDIR)
os_helper.unlink(TEMPDIR)
os.makedirs(TEMPDIR)
global testtarnames
@ -2734,14 +2735,14 @@ def setUpModule():
# Create compressed tarfiles.
for c in GzipTest, Bz2Test, LzmaTest:
if c.open:
support.unlink(c.tarname)
os_helper.unlink(c.tarname)
testtarnames.append(c.tarname)
with c.open(c.tarname, "wb") as tar:
tar.write(data)
def tearDownModule():
if os.path.exists(TEMPDIR):
support.rmtree(TEMPDIR)
os_helper.rmtree(TEMPDIR)
if __name__ == "__main__":
unittest.main()