if zlib -> skipUnless(zlib) and minor cleanups

This commit is contained in:
Ezio Melotti 2009-07-04 14:58:27 +00:00
parent 1844b0d748
commit e7a0cc2aa8
1 changed files with 49 additions and 37 deletions

View File

@ -4,11 +4,17 @@ try:
except ImportError: except ImportError:
zlib = None zlib = None
import zipfile, os, unittest, sys, shutil, struct import os
import sys
import shutil
import struct
import zipfile
import unittest
from StringIO import StringIO from StringIO import StringIO
from tempfile import TemporaryFile from tempfile import TemporaryFile
from random import randint, random from random import randint, random
from unittest import skipUnless
import test.test_support as support import test.test_support as support
from test.test_support import TESTFN, run_unittest, findfile from test.test_support import TESTFN, run_unittest, findfile
@ -217,43 +223,49 @@ class TestsWithSourceFile(unittest.TestCase):
for f in (TESTFN2, TemporaryFile(), StringIO()): for f in (TESTFN2, TemporaryFile(), StringIO()):
self.zipIterlinesTest(f, zipfile.ZIP_STORED) self.zipIterlinesTest(f, zipfile.ZIP_STORED)
if zlib: @skipUnless(zlib, "requires zlib")
def testDeflated(self): def testDeflated(self):
for f in (TESTFN2, TemporaryFile(), StringIO()): for f in (TESTFN2, TemporaryFile(), StringIO()):
self.zipTest(f, zipfile.ZIP_DEFLATED) self.zipTest(f, zipfile.ZIP_DEFLATED)
def testOpenDeflated(self): @skipUnless(zlib, "requires zlib")
for f in (TESTFN2, TemporaryFile(), StringIO()): def testOpenDeflated(self):
self.zipOpenTest(f, zipfile.ZIP_DEFLATED) for f in (TESTFN2, TemporaryFile(), StringIO()):
self.zipOpenTest(f, zipfile.ZIP_DEFLATED)
def testRandomOpenDeflated(self): @skipUnless(zlib, "requires zlib")
for f in (TESTFN2, TemporaryFile(), StringIO()): def testRandomOpenDeflated(self):
self.zipRandomOpenTest(f, zipfile.ZIP_DEFLATED) for f in (TESTFN2, TemporaryFile(), StringIO()):
self.zipRandomOpenTest(f, zipfile.ZIP_DEFLATED)
def testReadlineDeflated(self): @skipUnless(zlib, "requires zlib")
for f in (TESTFN2, TemporaryFile(), StringIO()): def testReadlineDeflated(self):
self.zipReadlineTest(f, zipfile.ZIP_DEFLATED) for f in (TESTFN2, TemporaryFile(), StringIO()):
self.zipReadlineTest(f, zipfile.ZIP_DEFLATED)
def testReadlinesDeflated(self): @skipUnless(zlib, "requires zlib")
for f in (TESTFN2, TemporaryFile(), StringIO()): def testReadlinesDeflated(self):
self.zipReadlinesTest(f, zipfile.ZIP_DEFLATED) for f in (TESTFN2, TemporaryFile(), StringIO()):
self.zipReadlinesTest(f, zipfile.ZIP_DEFLATED)
def testIterlinesDeflated(self): @skipUnless(zlib, "requires zlib")
for f in (TESTFN2, TemporaryFile(), StringIO()): def testIterlinesDeflated(self):
self.zipIterlinesTest(f, zipfile.ZIP_DEFLATED) for f in (TESTFN2, TemporaryFile(), StringIO()):
self.zipIterlinesTest(f, zipfile.ZIP_DEFLATED)
def testLowCompression(self): @skipUnless(zlib, "requires zlib")
# Checks for cases where compressed data is larger than original def testLowCompression(self):
# Create the ZIP archive # Checks for cases where compressed data is larger than original
zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) # Create the ZIP archive
zipfp.writestr("strfile", '12') zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
zipfp.close() zipfp.writestr("strfile", '12')
zipfp.close()
# Get an open object for strfile # Get an open object for strfile
zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED)
openobj = zipfp.open("strfile") openobj = zipfp.open("strfile")
self.assertEqual(openobj.read(1), '1') self.assertEqual(openobj.read(1), '1')
self.assertEqual(openobj.read(1), '2') self.assertEqual(openobj.read(1), '2')
def testAbsoluteArcnames(self): def testAbsoluteArcnames(self):
zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
@ -387,8 +399,8 @@ class TestsWithSourceFile(unittest.TestCase):
self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED) self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
def tearDown(self): def tearDown(self):
os.remove(TESTFN) support.unlink(TESTFN)
os.remove(TESTFN2) support.unlink(TESTFN2)
class TestZip64InSmallFiles(unittest.TestCase): class TestZip64InSmallFiles(unittest.TestCase):
# These tests test the ZIP64 functionality without using large files, # These tests test the ZIP64 functionality without using large files,
@ -510,8 +522,8 @@ class TestZip64InSmallFiles(unittest.TestCase):
def tearDown(self): def tearDown(self):
zipfile.ZIP64_LIMIT = self._limit zipfile.ZIP64_LIMIT = self._limit
os.remove(TESTFN) support.unlink(TESTFN)
os.remove(TESTFN2) support.unlink(TESTFN2)
class PyZipFileTests(unittest.TestCase): class PyZipFileTests(unittest.TestCase):
def testWritePyfile(self): def testWritePyfile(self):
@ -1010,7 +1022,7 @@ class TestsWithMultipleOpens(unittest.TestCase):
zipf.close() zipf.close()
def tearDown(self): def tearDown(self):
os.remove(TESTFN2) support.unlink(TESTFN2)
class TestWithDirectory(unittest.TestCase): class TestWithDirectory(unittest.TestCase):
def setUp(self): def setUp(self):
@ -1037,7 +1049,7 @@ class TestWithDirectory(unittest.TestCase):
def tearDown(self): def tearDown(self):
shutil.rmtree(TESTFN2) shutil.rmtree(TESTFN2)
if os.path.exists(TESTFN): if os.path.exists(TESTFN):
os.remove(TESTFN) support.unlink(TESTFN)
class UniversalNewlineTests(unittest.TestCase): class UniversalNewlineTests(unittest.TestCase):