Package translate :: Package misc :: Module test_zipfileext

[hide private]
[frames] | no frames]

# Source Code for Module translate.misc.test_zipfileext

      1  import zlib # implied prerequisite
      2  import zipfile, os, StringIO, tempfile
      3  try:
      4      from test.test_support import TestFailed
      5  except ImportError:
      6 -    class TestFailed(Exception):
      7          pass
      8  from translate.misc import zipfileext
      9
     10  BrokenStringIO = StringIO.StringIO
     11 -class FixedStringIO(BrokenStringIO):
     12 -    def truncate(self, size=None):
     13          BrokenStringIO.truncate(self, size)
     14          self.len = len(self.buf)
     15
     16  StringIO.StringIO = FixedStringIO
     17
     18 -def zipTest(srcname, f, compression, srccontents):
     19      zip = zipfileext.ZipFileExt(f, "w", compression)   # Create the ZIP archive
     20      zip.write(srcname, "another"+os.extsep+"name")
     21      zip.write(srcname, srcname)
     22      zip.close()
     23
     24      zip = zipfileext.ZipFileExt(f, "r", compression)   # Read the ZIP archive
     25      readData2 = zip.read(srcname)
     26      readData1 = zip.read("another"+os.extsep+"name")
     27      zip.close()
     28
     29      if readData1 != srccontents or readData2 != srccontents:
     30          raise TestFailed, "Written data doesn't equal read data."
     31
     32 -def deleteTest(srcname, f, compression, srccontents):
     33      zip = zipfileext.ZipFileExt(f, "w", compression)   # Create the ZIP archive
     34      othername = "another"+os.extsep+"name"
     35      finalname = "adifferent"+os.extsep+"name"
     36      leftname, deletenames = othername, [srcname, finalname]
     37      zip.write(srcname, srcname)
     38      zip.write(srcname, othername)
     39      zip.write(srcname, finalname)
     40      zip.close()
     41
     42      zip = zipfileext.ZipFileExt(f, "a", compression)   # Modify the ZIP archive
     43      for deletename in deletenames:
     44          zip.delete(deletename)
     45      zip.close()
     46
     47      zip = zipfileext.ZipFileExt(f, "r", compression)   # Read the ZIP archive
     48      testfailed = zip.testzip()
     49      readData = zip.read(leftname)
     50      zip.close()
     51
     52      if testfailed:
     53          raise TestFailed, "zip file didn't pass test"
     54      if readData != srccontents:
     55          raise TestFailed, "Written data doesn't equal read data."
     56
     57 -class TestZipfile:
     58
     59 -    def setup_method(self, method):
     60          print repr(method), dir(method)
     61          self.srcname = self.__class__.__name__ + "_" + method.__name__ + os.extsep + "tmp"
     62          self.zipname = self.__class__.__name__ + "_" + method.__name__ + os.extsep + "zip"
     63          if os.path.exists(self.srcname):
     64              os.remove(self.srcname)
     65          if os.path.exists(self.zipname):
     66              os.remove(self.zipname)
     67
     68 -    def teardown_method(self, method):
     69          if os.path.exists(self.srcname):           # Remove temporary files
     70              os.unlink(self.srcname)
     71          if os.path.exists(self.zipname):
     72              os.unlink(self.zipname)
     73
     74 -    def test_consistent(self):
     75          fp = open(self.srcname, "wb")               # Make a source file with some lines
     76          for i in range(0, 1000):
     77              fp.write("Test of zipfile line %d.\n" % i)
     78          fp.close()
     79
     80          fp = open(self.srcname, "rb")
     81          writtenData = fp.read()
     82          fp.close()
     83
     84          for file in (self.zipname, tempfile.TemporaryFile(), StringIO.StringIO()):
     85              zipTest(self.srcname, file, zipfile.ZIP_STORED, writtenData)
     86
     87          for file in (self.zipname, tempfile.TemporaryFile(), StringIO.StringIO()):
     88              zipTest(self.srcname, file, zipfile.ZIP_DEFLATED, writtenData)
     89
     90 -    def test_delete(self):
     91          fp = open(self.srcname, "wb")               # Make a source file with some lines
     92          for i in range(0, 1000):
     93              fp.write("Test of zipfile line %d.\n" % i)
     94          fp.close()
     95
     96          fp = open(self.srcname, "rb")
     97          writtenData = fp.read()
     98          fp.close()
     99
    100          for file in (self.zipname, tempfile.TemporaryFile(), StringIO.StringIO()):
    101              deleteTest(self.srcname, file, zipfile.ZIP_STORED, writtenData)
    102
    103          for file in (self.zipname, tempfile.TemporaryFile(), StringIO.StringIO()):
    104              deleteTest(self.srcname, file, zipfile.ZIP_DEFLATED, writtenData)
    105
    106 -    def test_closes(self):
    107          # This test checks that the ZipFile constructor closes the file object
    108          # it opens if there's an error in the file.  If it doesn't, the traceback
    109          # holds a reference to the ZipFile object and, indirectly, the file object.
    110          # On Windows, this causes the os.unlink() call to fail because the
    111          # underlying file is still open.  This is SF bug #412214.
    112          #
    113          fp = open(self.srcname, "w")
    114          fp.write("this is not a legal zip file\n")
    115          fp.close()
    116          try:
    117              zf = zipfileext.ZipFileExt(self.srcname)
    118          except zipfile.BadZipfile:
    119              os.unlink(self.srcname)
    120
    121 -    def test_403871(self):
    122          # make sure we don't raise an AttributeError when a partially-constructed
    123          # ZipFile instance is finalized; this tests for regression on SF tracker
    124          # bug #403871.
    125          try:
    126              zipfileext.ZipFileExt(self.srcname)
    127          except IOError:
    128              # The bug we're testing for caused an AttributeError to be raised
    129              # when a ZipFile instance was created for a file that did not
    130              # exist; the .fp member was not initialized but was needed by the
    131              # __del__() method.  Since the AttributeError is in the __del__(),
    132              # it is ignored, but the user should be sufficiently annoyed by
    133              # the message on the output that regression will be noticed
    134              # quickly.
    135              pass
    136          else:
    137              raise TestFailed("expected creation of readable ZipFile without\n"
    138                               "  a file to raise an IOError.")
    139
    140 -    def test_closedthrow(self):
    141          # Verify that testzip() doesn't swallow inappropriate exceptions.
    142          data = StringIO.StringIO()
    143          zipf = zipfileext.ZipFileExt(data, mode="w")
    144          zipf.writestr("foo.txt", "O, for a Muse of Fire!")
    145          zipf.close()
    146          zipf = zipfileext.ZipFileExt(data, mode="r")
    147          zipf.close()
    148          try:
    149              zipf.testzip()
    150          except RuntimeError:
    151              # This is correct; calling .read on a closed ZipFile should throw
    152              # a RuntimeError, and so should calling .testzip.  An earlier
    153              # version of .testzip would swallow this exception (and any other)
    154              # and report that the first file in the archive was corrupt.
    155              pass
    156          else:
    157              raise TestFailed("expected calling .testzip on a closed ZipFile"
    158                               " to raise a RuntimeError")
    159          del data, zipf
    160