]> granicus.if.org Git - python/commitdiff
Issue #28115: Added tests for CLI of the zipfile module.
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 23 Oct 2016 10:12:39 +0000 (13:12 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sun, 23 Oct 2016 10:12:39 +0000 (13:12 +0300)
1  2 
Lib/test/test_zipfile.py

index 5d7b91fcd267504bd3032804b94f156fc31808bb,6c6bf8d5b1881f95acf7abf140be6f3ad72585fa..fb3bbe45173ec22dda2eeafa126975a2bb2a71ae
@@@ -2027,19 -1984,202 +2028,84 @@@ class TestWithDirectory(unittest.TestCa
              unlink(TESTFN)
  
  
 -class AbstractUniversalNewlineTests:
 -    @classmethod
 -    def setUpClass(cls):
 -        cls.line_gen = [bytes("Test of zipfile line %d." % i, "ascii")
 -                        for i in range(FIXEDTEST_SIZE)]
 -        cls.seps = (b'\r', b'\r\n', b'\n')
 -        cls.arcdata = {}
 -        for n, s in enumerate(cls.seps):
 -            cls.arcdata[s] = s.join(cls.line_gen) + s
 -
 -    def setUp(self):
 -        self.arcfiles = {}
 -        for n, s in enumerate(self.seps):
 -            self.arcfiles[s] = '%s-%d' % (TESTFN, n)
 -            with open(self.arcfiles[s], "wb") as f:
 -                f.write(self.arcdata[s])
 -
 -    def make_test_archive(self, f, compression):
 -        # Create the ZIP archive
 -        with zipfile.ZipFile(f, "w", compression) as zipfp:
 -            for fn in self.arcfiles.values():
 -                zipfp.write(fn, fn)
 -
 -    def read_test(self, f, compression):
 -        self.make_test_archive(f, compression)
 -
 -        # Read the ZIP archive
 -        with zipfile.ZipFile(f, "r") as zipfp:
 -            for sep, fn in self.arcfiles.items():
 -                with openU(zipfp, fn) as fp:
 -                    zipdata = fp.read()
 -                self.assertEqual(self.arcdata[sep], zipdata)
 -
 -    def test_read(self):
 -        for f in get_files(self):
 -            self.read_test(f, self.compression)
 -
 -    def readline_read_test(self, f, compression):
 -        self.make_test_archive(f, compression)
 +class ZipInfoTests(unittest.TestCase):
 +    def test_from_file(self):
 +        zi = zipfile.ZipInfo.from_file(__file__)
 +        self.assertEqual(posixpath.basename(zi.filename), 'test_zipfile.py')
 +        self.assertFalse(zi.is_dir())
  
 -        # Read the ZIP archive
 -        with zipfile.ZipFile(f, "r") as zipfp:
 -            for sep, fn in self.arcfiles.items():
 -                with openU(zipfp, fn) as zipopen:
 -                    data = b''
 -                    while True:
 -                        read = zipopen.readline()
 -                        if not read:
 -                            break
 -                        data += read
 -
 -                        read = zipopen.read(5)
 -                        if not read:
 -                            break
 -                        data += read
 -
 -            self.assertEqual(data, self.arcdata[b'\n'])
 -
 -    def test_readline_read(self):
 -        for f in get_files(self):
 -            self.readline_read_test(f, self.compression)
 -
 -    def readline_test(self, f, compression):
 -        self.make_test_archive(f, compression)
 -
 -        # Read the ZIP archive
 -        with zipfile.ZipFile(f, "r") as zipfp:
 -            for sep, fn in self.arcfiles.items():
 -                with openU(zipfp, fn) as zipopen:
 -                    for line in self.line_gen:
 -                        linedata = zipopen.readline()
 -                        self.assertEqual(linedata, line + b'\n')
 -
 -    def test_readline(self):
 -        for f in get_files(self):
 -            self.readline_test(f, self.compression)
 -
 -    def readlines_test(self, f, compression):
 -        self.make_test_archive(f, compression)
 -
 -        # Read the ZIP archive
 -        with zipfile.ZipFile(f, "r") as zipfp:
 -            for sep, fn in self.arcfiles.items():
 -                with openU(zipfp, fn) as fp:
 -                    ziplines = fp.readlines()
 -                for line, zipline in zip(self.line_gen, ziplines):
 -                    self.assertEqual(zipline, line + b'\n')
 -
 -    def test_readlines(self):
 -        for f in get_files(self):
 -            self.readlines_test(f, self.compression)
 -
 -    def iterlines_test(self, f, compression):
 -        self.make_test_archive(f, compression)
 -
 -        # Read the ZIP archive
 -        with zipfile.ZipFile(f, "r") as zipfp:
 -            for sep, fn in self.arcfiles.items():
 -                with openU(zipfp, fn) as fp:
 -                    for line, zipline in zip(self.line_gen, fp):
 -                        self.assertEqual(zipline, line + b'\n')
 -
 -    def test_iterlines(self):
 -        for f in get_files(self):
 -            self.iterlines_test(f, self.compression)
 -
 -    def tearDown(self):
 -        for sep, fn in self.arcfiles.items():
 -            unlink(fn)
 -        unlink(TESTFN)
 -        unlink(TESTFN2)
 -
 -
 -class StoredUniversalNewlineTests(AbstractUniversalNewlineTests,
 -                                  unittest.TestCase):
 -    compression = zipfile.ZIP_STORED
 -
 -@requires_zlib
 -class DeflateUniversalNewlineTests(AbstractUniversalNewlineTests,
 -                                   unittest.TestCase):
 -    compression = zipfile.ZIP_DEFLATED
 -
 -@requires_bz2
 -class Bzip2UniversalNewlineTests(AbstractUniversalNewlineTests,
 -                                 unittest.TestCase):
 -    compression = zipfile.ZIP_BZIP2
 -
 -@requires_lzma
 -class LzmaUniversalNewlineTests(AbstractUniversalNewlineTests,
 -                                unittest.TestCase):
 -    compression = zipfile.ZIP_LZMA
 +    def test_from_dir(self):
 +        dirpath = os.path.dirname(os.path.abspath(__file__))
 +        zi = zipfile.ZipInfo.from_file(dirpath, 'stdlib_tests')
 +        self.assertEqual(zi.filename, 'stdlib_tests/')
 +        self.assertTrue(zi.is_dir())
 +        self.assertEqual(zi.compress_type, zipfile.ZIP_STORED)
 +        self.assertEqual(zi.file_size, 0)
  
 -                    if zi.filename.endswith('/'):
+ class CommandLineTest(unittest.TestCase):
+     def zipfilecmd(self, *args, **kwargs):
+         rc, out, err = script_helper.assert_python_ok('-m', 'zipfile', *args,
+                                                       **kwargs)
+         return out.replace(os.linesep.encode(), b'\n')
+     def zipfilecmd_failure(self, *args):
+         return script_helper.assert_python_failure('-m', 'zipfile', *args)
+     def test_test_command(self):
+         zip_name = findfile('zipdir.zip')
+         out = self.zipfilecmd('-t', zip_name)
+         self.assertEqual(out.rstrip(), b'Done testing')
+         zip_name = findfile('testtar.tar')
+         rc, out, err = self.zipfilecmd_failure('-t', zip_name)
+         self.assertEqual(out, b'')
+     def test_list_command(self):
+         zip_name = findfile('zipdir.zip')
+         t = io.StringIO()
+         with zipfile.ZipFile(zip_name, 'r') as tf:
+             tf.printdir(t)
+         expected = t.getvalue().encode('ascii', 'backslashreplace')
+         out = self.zipfilecmd('-l', zip_name,
+                               PYTHONIOENCODING='ascii:backslashreplace')
+         self.assertEqual(out, expected)
+     def test_create_command(self):
+         self.addCleanup(unlink, TESTFN)
+         with open(TESTFN, 'w') as f:
+             f.write('test 1')
+         os.mkdir(TESTFNDIR)
+         self.addCleanup(rmtree, TESTFNDIR)
+         with open(os.path.join(TESTFNDIR, 'file.txt'), 'w') as f:
+             f.write('test 2')
+         files = [TESTFN, TESTFNDIR]
+         namelist = [TESTFN, TESTFNDIR + '/', TESTFNDIR + '/file.txt']
+         try:
+             out = self.zipfilecmd('-c', TESTFN2, *files)
+             self.assertEqual(out, b'')
+             with zipfile.ZipFile(TESTFN2) as zf:
+                 self.assertEqual(zf.namelist(), namelist)
+                 self.assertEqual(zf.read(namelist[0]), b'test 1')
+                 self.assertEqual(zf.read(namelist[2]), b'test 2')
+         finally:
+             unlink(TESTFN2)
+     def test_extract_command(self):
+         zip_name = findfile('zipdir.zip')
+         with temp_dir() as extdir:
+             out = self.zipfilecmd('-e', zip_name, extdir)
+             self.assertEqual(out, b'')
+             with zipfile.ZipFile(zip_name) as zf:
+                 for zi in zf.infolist():
+                     path = os.path.join(extdir,
+                                 zi.filename.replace('/', os.sep))
++                    if zi.is_dir():
+                         self.assertTrue(os.path.isdir(path))
+                     else:
+                         self.assertTrue(os.path.isfile(path))
+                         with open(path, 'rb') as f:
+                             self.assertEqual(f.read(), zf.read(zi))
  if __name__ == "__main__":
      unittest.main()