cpython: 900c47c98711 (original) (raw)
--- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -12,7 +12,8 @@ import unittest from tempfile import TemporaryFile from random import randint, random, getrandbits -from test.support import (TESTFN, findfile, unlink, rmtree, +from test.support import script_helper +from test.support import (TESTFN, findfile, unlink, rmtree, temp_dir, requires_zlib, requires_bz2, requires_lzma, captured_stdout, check_warnings) @@ -2115,5 +2116,70 @@ class LzmaUniversalNewlineTests(Abstract unittest.TestCase): compression = zipfile.ZIP_LZMA + +class CommandLineTest(unittest.TestCase): +
- def zipfilecmd(self, *args, **kwargs):
rc, out, err = script_helper.assert_python_ok('-m', 'zipfile', *args,[](#l1.21)
**kwargs)[](#l1.22)
return out.replace(os.linesep.encode(), b'\n')[](#l1.23)
- def zipfilecmd_failure(self, *args):
return script_helper.assert_python_failure('-m', 'zipfile', *args)[](#l1.26)
- def test_test_command(self):
zip_name = findfile('zipdir.zip')[](#l1.29)
out = self.zipfilecmd('-t', zip_name)[](#l1.30)
self.assertEqual(out.rstrip(), b'Done testing')[](#l1.31)
zip_name = findfile('testtar.tar')[](#l1.32)
rc, out, err = self.zipfilecmd_failure('-t', zip_name)[](#l1.33)
self.assertEqual(out, b'')[](#l1.34)
- def test_list_command(self):
zip_name = findfile('zipdir.zip')[](#l1.37)
t = io.StringIO()[](#l1.38)
with zipfile.ZipFile(zip_name, 'r') as tf:[](#l1.39)
tf.printdir(t)[](#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)
with temp_dir() as extdir:[](#l1.68)
out = self.zipfilecmd('-e', zip_name, extdir)[](#l1.69)
self.assertEqual(out, b'')[](#l1.70)
with zipfile.ZipFile(zip_name) as zf:[](#l1.71)
for zi in zf.infolist():[](#l1.72)
path = os.path.join(extdir,[](#l1.73)
zi.filename.replace('/', os.sep))[](#l1.74)
if zi.filename.endswith('/'):[](#l1.75)
self.assertTrue(os.path.isdir(path))[](#l1.76)
else:[](#l1.77)
self.assertTrue(os.path.isfile(path))[](#l1.78)
with open(path, 'rb') as f:[](#l1.79)
self.assertEqual(f.read(), zf.read(zi))[](#l1.80)