blob: 0241348d865fb3eb7b0572d96abc363bb70e49c1 [file] [log] [blame]
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001# We can test part of the module without zlib.
Guido van Rossum368f04a2000-04-10 13:23:04 +00002try:
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00003 import zlib
4except ImportError:
5 zlib = None
Tim Petersa45cacf2004-08-20 03:47:14 +00006
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00007import zipfile, os, unittest
Tim Petersa19a1682001-03-29 04:36:09 +00008
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00009from StringIO import StringIO
10from tempfile import TemporaryFile
Tim Petersa19a1682001-03-29 04:36:09 +000011
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000012from test.test_support import TESTFN, run_unittest
Guido van Rossum368f04a2000-04-10 13:23:04 +000013
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000014TESTFN2 = TESTFN + "2"
Guido van Rossum368f04a2000-04-10 13:23:04 +000015
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000016class 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 Drake6e7e4852001-02-28 05:34:16 +000020
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000021 # Make a source file with some lines
22 fp = open(TESTFN, "wb")
23 fp.write(self.data)
24 fp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +000025
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000026 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 Peters7d3bad62001-04-04 18:56:49 +000032
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000033 # 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 Peters7d3bad62001-04-04 18:56:49 +000038
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000039 def testStored(self):
40 for f in (TESTFN2, TemporaryFile(), StringIO()):
41 self.zipTest(f, zipfile.ZIP_STORED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +000042
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000043 if zlib:
44 def testDeflated(self):
45 for f in (TESTFN2, TemporaryFile(), StringIO()):
46 self.zipTest(f, zipfile.ZIP_DEFLATED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +000047
Georg Brandl8f7c54e2006-02-20 08:40:38 +000048 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 Peters32cbc962006-02-20 21:42:18 +000056
Georg Brandl8f7c54e2006-02-20 08:40:38 +000057
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000058 def tearDown(self):
59 os.remove(TESTFN)
60 os.remove(TESTFN2)
61
62class 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
105def test_main():
106 run_unittest(TestsWithSourceFile, OtherTests)
107
108if __name__ == "__main__":
109 test_main()