Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_zipfile.TestsWithSourceFile Class Reference

List of all members.

Public Member Functions

def setUp
def make_test_archive
def zip_test
def test_stored
def zip_open_test
def test_open_stored
def test_open_via_zip_info
def zip_random_open_test
def test_random_open_stored
def test_univeral_readaheads
def zip_readline_read_test
def zip_readline_test
def zip_readlines_test
def zip_iterlines_test
def test_readline_read_stored
def test_readline_stored
def test_readlines_stored
def test_iterlines_stored
def test_deflated
def test_open_deflated
def test_random_open_deflated
def test_readline_read_deflated
def test_readline_deflated
def test_readlines_deflated
def test_iterlines_deflated
def test_low_compression
def test_absolute_arcnames
def test_append_to_zip_file
def test_append_to_non_zip_file
def test_ignores_newline_at_end
def test_ignores_stuff_appended_past_comments
def test_write_default_name
def test_per_file_compression
def test_write_to_readonly
def test_extract
def test_extract_all
def test_writestr_compression
def zip_test_writestr_permissions
def test_writestr_permissions
def test_writestr_extended_local_header_issue1202
def test_close
def test_close_on_exception
def test_unicode_filenames
def tearDown

Public Attributes

 line_gen
 data

Detailed Description

Definition at line 35 of file test_zipfile.py.


Member Function Documentation

def test.test_zipfile.TestsWithSourceFile.make_test_archive (   self,
  f,
  compression 
)

Definition at line 46 of file test_zipfile.py.

00046 
00047     def make_test_archive(self, f, compression):
00048         # Create the ZIP archive
00049         with zipfile.ZipFile(f, "w", compression) as zipfp:
00050             zipfp.write(TESTFN, "another.name")
00051             zipfp.write(TESTFN, TESTFN)
00052             zipfp.writestr("strfile", self.data)

Here is the caller graph for this function:

Definition at line 36 of file test_zipfile.py.

00036 
00037     def setUp(self):
00038         self.line_gen = (bytes("Zipfile test line %d. random float: %f" %
00039                                (i, random()), "ascii")
00040                          for i in range(FIXEDTEST_SIZE))
00041         self.data = b'\n'.join(self.line_gen) + b'\n'
00042 
00043         # Make a source file with some lines
00044         with open(TESTFN, "wb") as fp:
00045             fp.write(self.data)

Here is the caller graph for this function:

Definition at line 518 of file test_zipfile.py.

00518 
00519     def tearDown(self):
00520         unlink(TESTFN)
00521         unlink(TESTFN2)
00522 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 322 of file test_zipfile.py.

00322 
00323     def test_absolute_arcnames(self):
00324         with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
00325             zipfp.write(TESTFN, "/absolute")
00326 
00327         with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
00328             self.assertEqual(zipfp.namelist(), ["absolute"])

Here is the call graph for this function:

Test appending to an existing file that is not a zipfile.

Definition at line 338 of file test_zipfile.py.

00338 
00339     def test_append_to_non_zip_file(self):
00340         """Test appending to an existing file that is not a zipfile."""
00341         # NOTE: this test fails if len(d) < 22 because of the first
00342         # line "fpin.seek(-22, 2)" in _EndRecData
00343         data = b'I am not a ZipFile!'*10
00344         with open(TESTFN2, 'wb') as f:
00345             f.write(data)
00346 
00347         with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
00348             zipfp.write(TESTFN, TESTFN)
00349 
00350         with open(TESTFN2, 'rb') as f:
00351             f.seek(len(data))
00352             with zipfile.ZipFile(f, "r") as zipfp:
00353                 self.assertEqual(zipfp.namelist(), [TESTFN])

Here is the call graph for this function:

Test appending to an existing zipfile.

Definition at line 329 of file test_zipfile.py.

00329 
00330     def test_append_to_zip_file(self):
00331         """Test appending to an existing zipfile."""
00332         with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
00333             zipfp.write(TESTFN, TESTFN)
00334 
00335         with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
00336             zipfp.writestr("strfile", self.data)
00337             self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])

Here is the call graph for this function:

Check that the zipfile is closed after the 'with' block.

Definition at line 485 of file test_zipfile.py.

00485 
00486     def test_close(self):
00487         """Check that the zipfile is closed after the 'with' block."""
00488         with zipfile.ZipFile(TESTFN2, "w") as zipfp:
00489             for fpath, fdata in SMALL_TEST_DATA:
00490                 zipfp.writestr(fpath, fdata)
00491                 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
00492         self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
00493 
00494         with zipfile.ZipFile(TESTFN2, "r") as zipfp:
00495             self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
00496         self.assertTrue(zipfp.fp is None, 'zipfp is not closed')

Here is the call graph for this function:

Check that the zipfile is closed if an exception is raised in the
'with' block.

Definition at line 497 of file test_zipfile.py.

00497 
00498     def test_close_on_exception(self):
00499         """Check that the zipfile is closed if an exception is raised in the
00500         'with' block."""
00501         with zipfile.ZipFile(TESTFN2, "w") as zipfp:
00502             for fpath, fdata in SMALL_TEST_DATA:
00503                 zipfp.writestr(fpath, fdata)
00504 
00505         try:
00506             with zipfile.ZipFile(TESTFN2, "r") as zipfp2:
00507                 raise zipfile.BadZipFile()
00508         except zipfile.BadZipFile:
00509             self.assertTrue(zipfp2.fp is None, 'zipfp is not closed')

Here is the call graph for this function:

Definition at line 273 of file test_zipfile.py.

00273 
00274     def test_deflated(self):
00275         for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
00276             self.zip_test(f, zipfile.ZIP_DEFLATED)
00277 

Here is the call graph for this function:

Definition at line 401 of file test_zipfile.py.

00401 
00402     def test_extract(self):
00403         with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
00404             for fpath, fdata in SMALL_TEST_DATA:
00405                 zipfp.writestr(fpath, fdata)
00406 
00407         with zipfile.ZipFile(TESTFN2, "r") as zipfp:
00408             for fpath, fdata in SMALL_TEST_DATA:
00409                 writtenfile = zipfp.extract(fpath)
00410 
00411                 # make sure it was written to the right place
00412                 if os.path.isabs(fpath):
00413                     correctfile = os.path.join(os.getcwd(), fpath[1:])
00414                 else:
00415                     correctfile = os.path.join(os.getcwd(), fpath)
00416                 correctfile = os.path.normpath(correctfile)
00417 
00418                 self.assertEqual(writtenfile, correctfile)
00419 
00420                 # make sure correct data is in correct file
00421                 with open(writtenfile, "rb") as f:
00422                     self.assertEqual(fdata.encode(), f.read())
00423 
00424                 os.remove(writtenfile)
00425 
00426         # remove the test file subdirectories
00427         shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))

Here is the call graph for this function:

Definition at line 428 of file test_zipfile.py.

00428 
00429     def test_extract_all(self):
00430         with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
00431             for fpath, fdata in SMALL_TEST_DATA:
00432                 zipfp.writestr(fpath, fdata)
00433 
00434         with zipfile.ZipFile(TESTFN2, "r") as zipfp:
00435             zipfp.extractall()
00436             for fpath, fdata in SMALL_TEST_DATA:
00437                 if os.path.isabs(fpath):
00438                     outfile = os.path.join(os.getcwd(), fpath[1:])
00439                 else:
00440                     outfile = os.path.join(os.getcwd(), fpath)
00441 
00442                 with open(outfile, "rb") as f:
00443                     self.assertEqual(fdata.encode(), f.read())
00444 
00445                 os.remove(outfile)
00446 
00447         # remove the test file subdirectories
00448         shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))

Here is the call graph for this function:

Definition at line 354 of file test_zipfile.py.

00354 
00355     def test_ignores_newline_at_end(self):
00356         with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
00357             zipfp.write(TESTFN, TESTFN)
00358         with open(TESTFN2, 'a') as f:
00359             f.write("\r\n\00\00\00")
00360         with zipfile.ZipFile(TESTFN2, "r") as zipfp:
00361             self.assertIsInstance(zipfp, zipfile.ZipFile)

Here is the call graph for this function:

Definition at line 362 of file test_zipfile.py.

00362 
00363     def test_ignores_stuff_appended_past_comments(self):
00364         with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
00365             zipfp.comment = b"this is a comment"
00366             zipfp.write(TESTFN, TESTFN)
00367         with open(TESTFN2, 'a') as f:
00368             f.write("abcdef\r\n")
00369         with zipfile.ZipFile(TESTFN2, "r") as zipfp:
00370             self.assertIsInstance(zipfp, zipfile.ZipFile)
00371             self.assertEqual(zipfp.comment, b"this is a comment")

Here is the call graph for this function:

Definition at line 305 of file test_zipfile.py.

00305 
00306     def test_iterlines_deflated(self):
00307         for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
00308             self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED)

Here is the call graph for this function:

Definition at line 268 of file test_zipfile.py.

00268 
00269     def test_iterlines_stored(self):
00270         for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
00271             self.zip_iterlines_test(f, zipfile.ZIP_STORED)

Here is the call graph for this function:

Check for cases where compressed data is larger than original.

Definition at line 310 of file test_zipfile.py.

00310 
00311     def test_low_compression(self):
00312         """Check for cases where compressed data is larger than original."""
00313         # Create the ZIP archive
00314         with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
00315             zipfp.writestr("strfile", '12')
00316 
00317         # Get an open object for strfile
00318         with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp:
00319             with zipfp.open("strfile") as openobj:
00320                 self.assertEqual(openobj.read(1), b'1')
00321                 self.assertEqual(openobj.read(1), b'2')

Here is the call graph for this function:

Definition at line 279 of file test_zipfile.py.

00279 
00280     def test_open_deflated(self):
00281         for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
00282             self.zip_open_test(f, zipfile.ZIP_DEFLATED)

Here is the call graph for this function:

Definition at line 137 of file test_zipfile.py.

00137 
00138     def test_open_stored(self):
00139         for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
00140             self.zip_open_test(f, zipfile.ZIP_STORED)

Here is the call graph for this function:

Definition at line 141 of file test_zipfile.py.

00141 
00142     def test_open_via_zip_info(self):
00143         # Create the ZIP archive
00144         with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
00145             zipfp.writestr("name", "foo")
00146             zipfp.writestr("name", "bar")
00147 
00148         with zipfile.ZipFile(TESTFN2, "r") as zipfp:
00149             infos = zipfp.infolist()
00150             data = b""
00151             for info in infos:
00152                 with zipfp.open(info) as zipopen:
00153                     data += zipopen.read()
00154             self.assertTrue(data == b"foobar" or data == b"barfoo")
00155             data = b""
00156             for info in infos:
00157                 data += zipfp.read(info)
00158             self.assertTrue(data == b"foobar" or data == b"barfoo")

Here is the call graph for this function:

Check that files within a Zip archive can have different
compression options.

Definition at line 381 of file test_zipfile.py.

00381 
00382     def test_per_file_compression(self):
00383         """Check that files within a Zip archive can have different
00384         compression options."""
00385         with zipfile.ZipFile(TESTFN2, "w") as zipfp:
00386             zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
00387             zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
00388             sinfo = zipfp.getinfo('storeme')
00389             dinfo = zipfp.getinfo('deflateme')
00390             self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
00391             self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)

Here is the call graph for this function:

Definition at line 284 of file test_zipfile.py.

00284 
00285     def test_random_open_deflated(self):
00286         for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
00287             self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)

Here is the call graph for this function:

Definition at line 176 of file test_zipfile.py.

00176 
00177     def test_random_open_stored(self):
00178         for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
00179             self.zip_random_open_test(f, zipfile.ZIP_STORED)

Here is the call graph for this function:

Definition at line 295 of file test_zipfile.py.

00295 
00296     def test_readline_deflated(self):
00297         for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
00298             self.zip_readline_test(f, zipfile.ZIP_DEFLATED)

Here is the call graph for this function:

Definition at line 289 of file test_zipfile.py.

00289 
00290     def test_readline_read_deflated(self):
00291         # Issue #7610: calls to readline() interleaved with calls to read().
00292         for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
00293             self.zip_readline_read_test(f, zipfile.ZIP_DEFLATED)

Here is the call graph for this function:

Definition at line 255 of file test_zipfile.py.

00255 
00256     def test_readline_read_stored(self):
00257         # Issue #7610: calls to readline() interleaved with calls to read().
00258         for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
00259             self.zip_readline_read_test(f, zipfile.ZIP_STORED)

Here is the call graph for this function:

Definition at line 260 of file test_zipfile.py.

00260 
00261     def test_readline_stored(self):
00262         for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
00263             self.zip_readline_test(f, zipfile.ZIP_STORED)

Here is the call graph for this function:

Definition at line 300 of file test_zipfile.py.

00300 
00301     def test_readlines_deflated(self):
00302         for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
00303             self.zip_readlines_test(f, zipfile.ZIP_DEFLATED)

Here is the call graph for this function:

Definition at line 264 of file test_zipfile.py.

00264 
00265     def test_readlines_stored(self):
00266         for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
00267             self.zip_readlines_test(f, zipfile.ZIP_STORED)

Here is the call graph for this function:

Definition at line 107 of file test_zipfile.py.

00107 
00108     def test_stored(self):
00109         for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
00110             self.zip_test(f, zipfile.ZIP_STORED)

Here is the call graph for this function:

Definition at line 511 of file test_zipfile.py.

00511 
00512     def test_unicode_filenames(self):
00513         # bug #10801
00514         fname = findfile('zip_cp437_header.zip')
00515         with zipfile.ZipFile(fname) as zipfp:
00516             for name in zipfp.namelist():
00517                 zipfp.open(name).close()

Here is the call graph for this function:

Definition at line 180 of file test_zipfile.py.

00180 
00181     def test_univeral_readaheads(self):
00182         f = io.BytesIO()
00183 
00184         data = b'a\r\n' * 16 * 1024
00185         zipfp = zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED)
00186         zipfp.writestr(TESTFN, data)
00187         zipfp.close()
00188 
00189         data2 = b''
00190         zipfp = zipfile.ZipFile(f, 'r')
00191         with zipfp.open(TESTFN, 'rU') as zipopen:
00192             for line in zipopen:
00193                 data2 += line
00194         zipfp.close()
00195 
00196         self.assertEqual(data, data2.replace(b'\n', b'\r\n'))

Here is the call graph for this function:

Check that calling ZipFile.write without arcname specified
produces the expected result.

Definition at line 372 of file test_zipfile.py.

00372 
00373     def test_write_default_name(self):
00374         """Check that calling ZipFile.write without arcname specified
00375         produces the expected result."""
00376         with zipfile.ZipFile(TESTFN2, "w") as zipfp:
00377             zipfp.write(TESTFN)
00378             with open(TESTFN, "rb") as f:
00379                 self.assertEqual(zipfp.read(TESTFN), f.read())

Here is the call graph for this function:

Check that trying to call write() on a readonly ZipFile object
raises a RuntimeError.

Definition at line 392 of file test_zipfile.py.

00392 
00393     def test_write_to_readonly(self):
00394         """Check that trying to call write() on a readonly ZipFile object
00395         raises a RuntimeError."""
00396         with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
00397             zipfp.writestr("somefile.txt", "bogus")
00398 
00399         with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
00400             self.assertRaises(RuntimeError, zipfp.write, TESTFN)

Here is the call graph for this function:

Definition at line 449 of file test_zipfile.py.

00449 
00450     def test_writestr_compression(self):
00451         zipfp = zipfile.ZipFile(TESTFN2, "w")
00452         zipfp.writestr("a.txt", "hello world", compress_type=zipfile.ZIP_STORED)
00453         if zlib:
00454             zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_DEFLATED)
00455 
00456         info = zipfp.getinfo('a.txt')
00457         self.assertEqual(info.compress_type, zipfile.ZIP_STORED)
00458 
00459         if zlib:
00460             info = zipfp.getinfo('b.txt')
00461             self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED)
00462 

Here is the call graph for this function:

Definition at line 478 of file test_zipfile.py.

00478 
00479     def test_writestr_extended_local_header_issue1202(self):
00480         with zipfile.ZipFile(TESTFN2, 'w') as orig_zip:
00481             for data in 'abcdefghijklmnop':
00482                 zinfo = zipfile.ZipInfo(data)
00483                 zinfo.flag_bits |= 0x08  # Include an extended local header.
00484                 orig_zip.writestr(zinfo, data)

Definition at line 474 of file test_zipfile.py.

00474 
00475     def test_writestr_permissions(self):
00476         for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
00477             self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)

Here is the call graph for this function:

def test.test_zipfile.TestsWithSourceFile.zip_iterlines_test (   self,
  f,
  compression 
)

Definition at line 244 of file test_zipfile.py.

00244 
00245     def zip_iterlines_test(self, f, compression):
00246         self.make_test_archive(f, compression)
00247 
00248         # Read the ZIP archive
00249         with zipfile.ZipFile(f, "r") as zipfp:
00250             with zipfp.open(TESTFN) as zipopen:
00251                 for line, zipline in zip(self.line_gen, zipopen):
00252                     self.assertEqual(zipline, line + '\n')
00253         if not isinstance(f, str):
00254             f.close()

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_zipfile.TestsWithSourceFile.zip_open_test (   self,
  f,
  compression 
)

Definition at line 111 of file test_zipfile.py.

00111 
00112     def zip_open_test(self, f, compression):
00113         self.make_test_archive(f, compression)
00114 
00115         # Read the ZIP archive
00116         with zipfile.ZipFile(f, "r", compression) as zipfp:
00117             zipdata1 = []
00118             with zipfp.open(TESTFN) as zipopen1:
00119                 while True:
00120                     read_data = zipopen1.read(256)
00121                     if not read_data:
00122                         break
00123                     zipdata1.append(read_data)
00124 
00125             zipdata2 = []
00126             with zipfp.open("another.name") as zipopen2:
00127                 while True:
00128                     read_data = zipopen2.read(256)
00129                     if not read_data:
00130                         break
00131                     zipdata2.append(read_data)
00132 
00133             self.assertEqual(b''.join(zipdata1), self.data)
00134             self.assertEqual(b''.join(zipdata2), self.data)
00135         if not isinstance(f, str):
00136             f.close()

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_zipfile.TestsWithSourceFile.zip_random_open_test (   self,
  f,
  compression 
)

Definition at line 159 of file test_zipfile.py.

00159 
00160     def zip_random_open_test(self, f, compression):
00161         self.make_test_archive(f, compression)
00162 
00163         # Read the ZIP archive
00164         with zipfile.ZipFile(f, "r", compression) as zipfp:
00165             zipdata1 = []
00166             with zipfp.open(TESTFN) as zipopen1:
00167                 while True:
00168                     read_data = zipopen1.read(randint(1, 1024))
00169                     if not read_data:
00170                         break
00171                     zipdata1.append(read_data)
00172 
00173             self.assertEqual(b''.join(zipdata1), self.data)
00174         if not isinstance(f, str):
00175             f.close()

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_zipfile.TestsWithSourceFile.zip_readline_read_test (   self,
  f,
  compression 
)

Definition at line 197 of file test_zipfile.py.

00197 
00198     def zip_readline_read_test(self, f, compression):
00199         self.make_test_archive(f, compression)
00200 
00201         # Read the ZIP archive
00202         zipfp = zipfile.ZipFile(f, "r")
00203         with zipfp.open(TESTFN) as zipopen:
00204             data = b''
00205             while True:
00206                 read = zipopen.readline()
00207                 if not read:
00208                     break
00209                 data += read
00210 
00211                 read = zipopen.read(100)
00212                 if not read:
00213                     break
00214                 data += read
00215 
00216         self.assertEqual(data, self.data)
00217         zipfp.close()
00218         if not isinstance(f, str):
00219             f.close()

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_zipfile.TestsWithSourceFile.zip_readline_test (   self,
  f,
  compression 
)

Definition at line 220 of file test_zipfile.py.

00220 
00221     def zip_readline_test(self, f, compression):
00222         self.make_test_archive(f, compression)
00223 
00224         # Read the ZIP archive
00225         with zipfile.ZipFile(f, "r") as zipfp:
00226             with zipfp.open(TESTFN) as zipopen:
00227                 for line in self.line_gen:
00228                     linedata = zipopen.readline()
00229                     self.assertEqual(linedata, line + '\n')
00230         if not isinstance(f, str):
00231             f.close()

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_zipfile.TestsWithSourceFile.zip_readlines_test (   self,
  f,
  compression 
)

Definition at line 232 of file test_zipfile.py.

00232 
00233     def zip_readlines_test(self, f, compression):
00234         self.make_test_archive(f, compression)
00235 
00236         # Read the ZIP archive
00237         with zipfile.ZipFile(f, "r") as zipfp:
00238             with zipfp.open(TESTFN) as zipopen:
00239                 ziplines = zipopen.readlines()
00240             for line, zipline in zip(self.line_gen, ziplines):
00241                 self.assertEqual(zipline, line + '\n')
00242         if not isinstance(f, str):
00243             f.close()

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_zipfile.TestsWithSourceFile.zip_test (   self,
  f,
  compression 
)

Definition at line 53 of file test_zipfile.py.

00053 
00054     def zip_test(self, f, compression):
00055         self.make_test_archive(f, compression)
00056 
00057         # Read the ZIP archive
00058         with zipfile.ZipFile(f, "r", compression) as zipfp:
00059             self.assertEqual(zipfp.read(TESTFN), self.data)
00060             self.assertEqual(zipfp.read("another.name"), self.data)
00061             self.assertEqual(zipfp.read("strfile"), self.data)
00062 
00063             # Print the ZIP directory
00064             fp = io.StringIO()
00065             zipfp.printdir(file=fp)
00066             directory = fp.getvalue()
00067             lines = directory.splitlines()
00068             self.assertEqual(len(lines), 4) # Number of files + header
00069 
00070             self.assertIn('File Name', lines[0])
00071             self.assertIn('Modified', lines[0])
00072             self.assertIn('Size', lines[0])
00073 
00074             fn, date, time_, size = lines[1].split()
00075             self.assertEqual(fn, 'another.name')
00076             self.assertTrue(time.strptime(date, '%Y-%m-%d'))
00077             self.assertTrue(time.strptime(time_, '%H:%M:%S'))
00078             self.assertEqual(size, str(len(self.data)))
00079 
00080             # Check the namelist
00081             names = zipfp.namelist()
00082             self.assertEqual(len(names), 3)
00083             self.assertIn(TESTFN, names)
00084             self.assertIn("another.name", names)
00085             self.assertIn("strfile", names)
00086 
00087             # Check infolist
00088             infos = zipfp.infolist()
00089             names = [i.filename for i in infos]
00090             self.assertEqual(len(names), 3)
00091             self.assertIn(TESTFN, names)
00092             self.assertIn("another.name", names)
00093             self.assertIn("strfile", names)
00094             for i in infos:
00095                 self.assertEqual(i.file_size, len(self.data))
00096 
00097             # check getinfo
00098             for nm in (TESTFN, "another.name", "strfile"):
00099                 info = zipfp.getinfo(nm)
00100                 self.assertEqual(info.filename, nm)
00101                 self.assertEqual(info.file_size, len(self.data))
00102 
00103             # Check that testzip doesn't raise an exception
00104             zipfp.testzip()
00105         if not isinstance(f, str):
00106             f.close()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 463 of file test_zipfile.py.

00463 
00464     def zip_test_writestr_permissions(self, f, compression):
00465         # Make sure that writestr creates files with mode 0600,
00466         # when it is passed a name rather than a ZipInfo instance.
00467 
00468         self.make_test_archive(f, compression)
00469         with zipfile.ZipFile(f, "r") as zipfp:
00470             zinfo = zipfp.getinfo('strfile')
00471             self.assertEqual(zinfo.external_attr, 0o600 << 16)
00472         if not isinstance(f, str):
00473             f.close()

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 40 of file test_zipfile.py.

Definition at line 37 of file test_zipfile.py.


The documentation for this class was generated from the following file: