Replaced my dumb way of calculating seconds to midnight with Tim Peters' much more...
[python.git] / Lib / test / test_zipfile.py
blob0241348d865fb3eb7b0572d96abc363bb70e49c1
1 # We can test part of the module without zlib.
2 try:
3 import zlib
4 except ImportError:
5 zlib = None
7 import zipfile, os, unittest
9 from StringIO import StringIO
10 from tempfile import TemporaryFile
12 from test.test_support import TESTFN, run_unittest
14 TESTFN2 = TESTFN + "2"
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)
21 # Make a source file with some lines
22 fp = open(TESTFN, "wb")
23 fp.write(self.data)
24 fp.close()
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()
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()
39 def testStored(self):
40 for f in (TESTFN2, TemporaryFile(), StringIO()):
41 self.zipTest(f, zipfile.ZIP_STORED)
43 if zlib:
44 def testDeflated(self):
45 for f in (TESTFN2, TemporaryFile(), StringIO()):
46 self.zipTest(f, zipfile.ZIP_DEFLATED)
48 def testAbsoluteArcnames(self):
49 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
50 zipfp.write(TESTFN, "/absolute")
51 zipfp.close()
53 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
54 self.assertEqual(zipfp.namelist(), ["absolute"])
55 zipfp.close()
58 def tearDown(self):
59 os.remove(TESTFN)
60 os.remove(TESTFN2)
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.
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)
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.
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)
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()
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)
105 def test_main():
106 run_unittest(TestsWithSourceFile, OtherTests)
108 if __name__ == "__main__":
109 test_main()