cpython: 042c923c5b67 (original) (raw)
--- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -17,8 +17,10 @@ from tempfile import TemporaryFile from random import randint, random, getrandbits from unittest import skipUnless +from test import script_helper from test.test_support import TESTFN, TESTFN_UNICODE, TESTFN_ENCODING, [](#l1.8)
run_unittest, findfile, unlink, rmtree, check_warnings[](#l1.9)
run_unittest, findfile, unlink, rmtree, \[](#l1.10)
check_warnings, captured_stdout[](#l1.11)
try: TESTFN_UNICODE.encode(TESTFN_ENCODING) except (UnicodeError, TypeError): @@ -1770,11 +1772,79 @@ class UniversalNewlineTests(unittest.Tes unlink(TESTFN2) +class CommandLineTest(unittest.TestCase): +
- def zipfilecmd(self, *args, **kwargs):
rc, out, err = script_helper.assert_python_ok('-m', 'zipfile', *args,[](#l1.22)
**kwargs)[](#l1.23)
return out.replace(os.linesep.encode(), b'\n')[](#l1.24)
- def zipfilecmd_failure(self, *args):
return script_helper.assert_python_failure('-m', 'zipfile', *args)[](#l1.27)
- def test_test_command(self):
zip_name = findfile('zipdir.zip')[](#l1.30)
out = self.zipfilecmd('-t', zip_name)[](#l1.31)
self.assertEqual(out.rstrip(), b'Done testing')[](#l1.32)
zip_name = findfile('testtar.tar')[](#l1.33)
rc, out, err = self.zipfilecmd_failure('-t', zip_name)[](#l1.34)
self.assertEqual(out, b'')[](#l1.35)
- def test_list_command(self):
zip_name = findfile('zipdir.zip')[](#l1.38)
with captured_stdout() as t, zipfile.ZipFile(zip_name, 'r') as tf:[](#l1.39)
tf.printdir()[](#l1.40)
expected = t.getvalue().encode('ascii', 'backslashreplace')[](#l1.41)
out = self.zipfilecmd('-l', zip_name,[](#l1.42)
PYTHONIOENCODING='ascii:backslashreplace')[](#l1.43)
self.assertEqual(out, expected)[](#l1.44)
- def test_create_command(self):
self.addCleanup(unlink, TESTFN)[](#l1.47)
with open(TESTFN, 'w') as f:[](#l1.48)
f.write('test 1')[](#l1.49)
os.mkdir(TESTFNDIR)[](#l1.50)
self.addCleanup(rmtree, TESTFNDIR)[](#l1.51)
with open(os.path.join(TESTFNDIR, 'file.txt'), 'w') as f:[](#l1.52)
f.write('test 2')[](#l1.53)
files = [TESTFN, TESTFNDIR][](#l1.54)
namelist = [TESTFN, TESTFNDIR + '/', TESTFNDIR + '/file.txt'][](#l1.55)
try:[](#l1.56)
out = self.zipfilecmd('-c', TESTFN2, *files)[](#l1.57)
self.assertEqual(out, b'')[](#l1.58)
with zipfile.ZipFile(TESTFN2) as zf:[](#l1.59)
self.assertEqual(zf.namelist(), namelist)[](#l1.60)
self.assertEqual(zf.read(namelist[0]), b'test 1')[](#l1.61)
self.assertEqual(zf.read(namelist[2]), b'test 2')[](#l1.62)
finally:[](#l1.63)
unlink(TESTFN2)[](#l1.64)
- def test_extract_command(self):
zip_name = findfile('zipdir.zip')[](#l1.67)
extdir = TESTFNDIR[](#l1.68)
os.mkdir(extdir)[](#l1.69)
try:[](#l1.70)
out = self.zipfilecmd('-e', zip_name, extdir)[](#l1.71)
self.assertEqual(out, b'')[](#l1.72)
with zipfile.ZipFile(zip_name) as zf:[](#l1.73)
for zi in zf.infolist():[](#l1.74)
path = os.path.join(extdir,[](#l1.75)
zi.filename.replace('/', os.sep))[](#l1.76)
if zi.filename.endswith('/'):[](#l1.77)
self.assertTrue(os.path.isdir(path))[](#l1.78)
else:[](#l1.79)
self.assertTrue(os.path.isfile(path))[](#l1.80)
with open(path, 'rb') as f:[](#l1.81)
self.assertEqual(f.read(), zf.read(zi))[](#l1.82)
finally:[](#l1.83)
rmtree(extdir)[](#l1.84)
+ def test_main(): run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests, PyZipFileTests, DecryptionTests, TestsWithMultipleOpens, TestWithDirectory, UniversalNewlineTests,
TestsWithRandomBinaryFiles)[](#l1.90)
TestsWithRandomBinaryFiles, CommandLineTest)[](#l1.91)