Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | # We can test part of the module without zlib. |
2 | try: | |
3 | import zlib | |
4 | except ImportError: | |
5 | zlib = None | |
6 | ||
7 | import zipfile, os, unittest | |
8 | ||
9 | from StringIO import StringIO | |
10 | from tempfile import TemporaryFile | |
11 | ||
12 | from test.test_support import TESTFN, run_unittest | |
13 | ||
14 | TESTFN2 = TESTFN + "2" | |
15 | ||
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) | |
20 | ||
21 | # Make a source file with some lines | |
22 | fp = open(TESTFN, "wb") | |
23 | fp.write(self.data) | |
24 | fp.close() | |
25 | ||
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() | |
32 | ||
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() | |
38 | ||
39 | def testStored(self): | |
40 | for f in (TESTFN2, TemporaryFile(), StringIO()): | |
41 | self.zipTest(f, zipfile.ZIP_STORED) | |
42 | ||
43 | if zlib: | |
44 | def testDeflated(self): | |
45 | for f in (TESTFN2, TemporaryFile(), StringIO()): | |
46 | self.zipTest(f, zipfile.ZIP_DEFLATED) | |
47 | ||
48 | def tearDown(self): | |
49 | os.remove(TESTFN) | |
50 | os.remove(TESTFN2) | |
51 | ||
52 | class OtherTests(unittest.TestCase): | |
53 | def testCloseErroneousFile(self): | |
54 | # This test checks that the ZipFile constructor closes the file object | |
55 | # it opens if there's an error in the file. If it doesn't, the traceback | |
56 | # holds a reference to the ZipFile object and, indirectly, the file object. | |
57 | # On Windows, this causes the os.unlink() call to fail because the | |
58 | # underlying file is still open. This is SF bug #412214. | |
59 | # | |
60 | fp = open(TESTFN, "w") | |
61 | fp.write("this is not a legal zip file\n") | |
62 | fp.close() | |
63 | try: | |
64 | zf = zipfile.ZipFile(TESTFN) | |
65 | except zipfile.BadZipfile: | |
66 | os.unlink(TESTFN) | |
67 | ||
68 | def testNonExistentFileRaisesIOError(self): | |
69 | # make sure we don't raise an AttributeError when a partially-constructed | |
70 | # ZipFile instance is finalized; this tests for regression on SF tracker | |
71 | # bug #403871. | |
72 | ||
73 | # The bug we're testing for caused an AttributeError to be raised | |
74 | # when a ZipFile instance was created for a file that did not | |
75 | # exist; the .fp member was not initialized but was needed by the | |
76 | # __del__() method. Since the AttributeError is in the __del__(), | |
77 | # it is ignored, but the user should be sufficiently annoyed by | |
78 | # the message on the output that regression will be noticed | |
79 | # quickly. | |
80 | self.assertRaises(IOError, zipfile.ZipFile, TESTFN) | |
81 | ||
82 | def testClosedZipRaisesRuntimeError(self): | |
83 | # Verify that testzip() doesn't swallow inappropriate exceptions. | |
84 | data = StringIO() | |
85 | zipf = zipfile.ZipFile(data, mode="w") | |
86 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") | |
87 | zipf.close() | |
88 | ||
89 | # This is correct; calling .read on a closed ZipFile should throw | |
90 | # a RuntimeError, and so should calling .testzip. An earlier | |
91 | # version of .testzip would swallow this exception (and any other) | |
92 | # and report that the first file in the archive was corrupt. | |
93 | self.assertRaises(RuntimeError, zipf.testzip) | |
94 | ||
95 | def test_main(): | |
96 | run_unittest(TestsWithSourceFile, OtherTests) | |
97 | ||
98 | if __name__ == "__main__": | |
99 | test_main() |