Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 1 | # We can test part of the module without zlib. |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 2 | try: |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 3 | import zlib |
| 4 | except ImportError: |
| 5 | zlib = None |
Tim Peters | a45cacf | 2004-08-20 03:47:14 +0000 | [diff] [blame] | 6 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 7 | import zipfile, os, unittest |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 8 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 9 | from StringIO import StringIO |
| 10 | from tempfile import TemporaryFile |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 11 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 12 | from test.test_support import TESTFN, run_unittest |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 13 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 14 | TESTFN2 = TESTFN + "2" |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 15 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 16 | class TestsWithSourceFile(unittest.TestCase): |
| 17 | def setUp(self): |
| 18 | line_gen = ("Test of zipfile line %d." % i for i in range(0, 1000)) |
| 19 | self.data = '\n'.join(line_gen) |
Fred Drake | 6e7e485 | 2001-02-28 05:34:16 +0000 | [diff] [blame] | 20 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 21 | # Make a source file with some lines |
| 22 | fp = open(TESTFN, "wb") |
| 23 | fp.write(self.data) |
| 24 | fp.close() |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 25 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 26 | def zipTest(self, f, compression): |
| 27 | # Create the ZIP archive |
| 28 | zipfp = zipfile.ZipFile(f, "w", compression) |
| 29 | zipfp.write(TESTFN, "another"+os.extsep+"name") |
| 30 | zipfp.write(TESTFN, TESTFN) |
| 31 | zipfp.close() |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 32 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 33 | # Read the ZIP archive |
| 34 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 35 | self.assertEqual(zipfp.read(TESTFN), self.data) |
| 36 | self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data) |
| 37 | zipfp.close() |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 38 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 39 | def testStored(self): |
| 40 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
| 41 | self.zipTest(f, zipfile.ZIP_STORED) |
Raymond Hettinger | c0fac96 | 2003-06-27 22:25:03 +0000 | [diff] [blame] | 42 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 43 | if zlib: |
| 44 | def testDeflated(self): |
| 45 | for f in (TESTFN2, TemporaryFile(), StringIO()): |
| 46 | self.zipTest(f, zipfile.ZIP_DEFLATED) |
Raymond Hettinger | c0fac96 | 2003-06-27 22:25:03 +0000 | [diff] [blame] | 47 | |
Georg Brandl | 8f7c54e | 2006-02-20 08:40:38 +0000 | [diff] [blame] | 48 | def testAbsoluteArcnames(self): |
| 49 | zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) |
| 50 | zipfp.write(TESTFN, "/absolute") |
| 51 | zipfp.close() |
| 52 | |
| 53 | zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) |
| 54 | self.assertEqual(zipfp.namelist(), ["absolute"]) |
| 55 | zipfp.close() |
Tim Peters | 32cbc96 | 2006-02-20 21:42:18 +0000 | [diff] [blame] | 56 | |
Georg Brandl | 8f7c54e | 2006-02-20 08:40:38 +0000 | [diff] [blame] | 57 | |
Johannes Gijsbers | 3caf9c1 | 2004-08-19 15:11:50 +0000 | [diff] [blame] | 58 | def tearDown(self): |
| 59 | os.remove(TESTFN) |
| 60 | os.remove(TESTFN2) |
| 61 | |
| 62 | class OtherTests(unittest.TestCase): |
| 63 | def testCloseErroneousFile(self): |
| 64 | # This test checks that the ZipFile constructor closes the file object |
| 65 | # it opens if there's an error in the file. If it doesn't, the traceback |
| 66 | # holds a reference to the ZipFile object and, indirectly, the file object. |
| 67 | # On Windows, this causes the os.unlink() call to fail because the |
| 68 | # underlying file is still open. This is SF bug #412214. |
| 69 | # |
| 70 | fp = open(TESTFN, "w") |
| 71 | fp.write("this is not a legal zip file\n") |
| 72 | fp.close() |
| 73 | try: |
| 74 | zf = zipfile.ZipFile(TESTFN) |
| 75 | except zipfile.BadZipfile: |
| 76 | os.unlink(TESTFN) |
| 77 | |
| 78 | def testNonExistentFileRaisesIOError(self): |
| 79 | # make sure we don't raise an AttributeError when a partially-constructed |
| 80 | # ZipFile instance is finalized; this tests for regression on SF tracker |
| 81 | # bug #403871. |
| 82 | |
| 83 | # The bug we're testing for caused an AttributeError to be raised |
| 84 | # when a ZipFile instance was created for a file that did not |
| 85 | # exist; the .fp member was not initialized but was needed by the |
| 86 | # __del__() method. Since the AttributeError is in the __del__(), |
| 87 | # it is ignored, but the user should be sufficiently annoyed by |
| 88 | # the message on the output that regression will be noticed |
| 89 | # quickly. |
| 90 | self.assertRaises(IOError, zipfile.ZipFile, TESTFN) |
| 91 | |
| 92 | def testClosedZipRaisesRuntimeError(self): |
| 93 | # Verify that testzip() doesn't swallow inappropriate exceptions. |
| 94 | data = StringIO() |
| 95 | zipf = zipfile.ZipFile(data, mode="w") |
| 96 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 97 | zipf.close() |
| 98 | |
| 99 | # This is correct; calling .read on a closed ZipFile should throw |
| 100 | # a RuntimeError, and so should calling .testzip. An earlier |
| 101 | # version of .testzip would swallow this exception (and any other) |
| 102 | # and report that the first file in the archive was corrupt. |
| 103 | self.assertRaises(RuntimeError, zipf.testzip) |
| 104 | |
| 105 | def test_main(): |
| 106 | run_unittest(TestsWithSourceFile, OtherTests) |
| 107 | |
| 108 | if __name__ == "__main__": |
| 109 | test_main() |