Merged revisions 73931 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r73931 | ezio.melotti | 2009-07-10 23:25:56 +0300 (Fri, 10 Jul 2009) | 1 line

  more cleanups and if zlib -> skipUnless(zlib)
........
This commit is contained in:
Ezio Melotti 2009-07-11 18:28:48 +00:00
parent 5e840cfd01
commit 76430242e7
1 changed files with 93 additions and 90 deletions

View File

@ -16,8 +16,7 @@ from tempfile import TemporaryFile
from random import randint, random
from unittest import skipUnless
from test import support
from test.support import TESTFN, run_unittest, findfile
from test.support import TESTFN, run_unittest, findfile, unlink
TESTFN2 = TESTFN + "2"
TESTFNDIR = TESTFN + "d"
@ -28,6 +27,7 @@ SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
class TestsWithSourceFile(unittest.TestCase):
def setUp(self):
self.line_gen = (bytes("Zipfile test line %d. random float: %f" %
@ -60,7 +60,6 @@ class TestsWithSourceFile(unittest.TestCase):
# Print the ZIP directory
fp = io.StringIO()
zipfp.printdir(file=fp)
directory = fp.getvalue()
lines = directory.splitlines()
self.assertEquals(len(lines), 4) # Number of files + header
@ -101,7 +100,7 @@ class TestsWithSourceFile(unittest.TestCase):
zipfp.testzip()
zipfp.close()
def testStored(self):
def test_Stored(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipTest(f, zipfile.ZIP_STORED)
@ -130,11 +129,11 @@ class TestsWithSourceFile(unittest.TestCase):
self.assertEqual(b''.join(zipdata2), self.data)
zipfp.close()
def testOpenStored(self):
def test_OpenStored(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipOpenTest(f, zipfile.ZIP_STORED)
def testOpenViaZipInfo(self):
def test_OpenViaZipInfo(self):
# Create the ZIP archive
zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
zipfp.writestr("name", "foo")
@ -169,7 +168,7 @@ class TestsWithSourceFile(unittest.TestCase):
self.assertEqual(b''.join(zipdata1), self.data)
zipfp.close()
def testRandomOpenStored(self):
def test_RandomOpenStored(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipRandomOpenTest(f, zipfile.ZIP_STORED)
@ -206,51 +205,51 @@ class TestsWithSourceFile(unittest.TestCase):
zipfp.close()
def testReadlineStored(self):
def test_ReadlineStored(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipReadlineTest(f, zipfile.ZIP_STORED)
def testReadlinesStored(self):
def test_ReadlinesStored(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipReadlinesTest(f, zipfile.ZIP_STORED)
def testIterlinesStored(self):
def test_IterlinesStored(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipIterlinesTest(f, zipfile.ZIP_STORED)
@skipUnless(zlib, "requires zlib")
def testDeflated(self):
def test_Deflated(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipTest(f, zipfile.ZIP_DEFLATED)
@skipUnless(zlib, "requires zlib")
def testOpenDeflated(self):
def test_OpenDeflated(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipOpenTest(f, zipfile.ZIP_DEFLATED)
@skipUnless(zlib, "requires zlib")
def testRandomOpenDeflated(self):
def test_RandomOpenDeflated(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipRandomOpenTest(f, zipfile.ZIP_DEFLATED)
@skipUnless(zlib, "requires zlib")
def testReadlineDeflated(self):
def test_ReadlineDeflated(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipReadlineTest(f, zipfile.ZIP_DEFLATED)
@skipUnless(zlib, "requires zlib")
def testReadlinesDeflated(self):
def test_ReadlinesDeflated(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipReadlinesTest(f, zipfile.ZIP_DEFLATED)
@skipUnless(zlib, "requires zlib")
def testIterlinesDeflated(self):
def test_IterlinesDeflated(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipIterlinesTest(f, zipfile.ZIP_DEFLATED)
@skipUnless(zlib, "requires zlib")
def testLowCompression(self):
def test_LowCompression(self):
# Checks for cases where compressed data is larger than original
# Create the ZIP archive
zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
@ -263,8 +262,7 @@ class TestsWithSourceFile(unittest.TestCase):
self.assertEqual(openobj.read(1), b'1')
self.assertEqual(openobj.read(1), b'2')
def testAbsoluteArcnames(self):
def test_AbsoluteArcnames(self):
zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
zipfp.write(TESTFN, "/absolute")
zipfp.close()
@ -273,7 +271,7 @@ class TestsWithSourceFile(unittest.TestCase):
self.assertEqual(zipfp.namelist(), ["absolute"])
zipfp.close()
def testAppendToZipFile(self):
def test_AppendToZipFile(self):
# Test appending to an existing zipfile
zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
zipfp.write(TESTFN, TESTFN)
@ -283,7 +281,7 @@ class TestsWithSourceFile(unittest.TestCase):
self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
zipfp.close()
def testAppendToNonZipFile(self):
def test_AppendToNonZipFile(self):
# Test appending to an existing file that is not a zipfile
# NOTE: this test fails if len(d) < 22 because of the first
# line "fpin.seek(-22, 2)" in _EndRecData
@ -330,7 +328,7 @@ class TestsWithSourceFile(unittest.TestCase):
self.assertRaises(RuntimeError, zipf.write, TESTFN)
zipf.close()
def testExtract(self):
def test_Extract(self):
zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
for fpath, fdata in SMALL_TEST_DATA:
zipfp.writestr(fpath, fdata)
@ -359,7 +357,7 @@ class TestsWithSourceFile(unittest.TestCase):
# remove the test file subdirectories
shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
def testExtractAll(self):
def test_ExtractAll(self):
zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
for fpath, fdata in SMALL_TEST_DATA:
zipfp.writestr(fpath, fdata)
@ -391,7 +389,7 @@ class TestsWithSourceFile(unittest.TestCase):
zinfo = zipfp.getinfo('strfile')
self.assertEqual(zinfo.external_attr, 0o600 << 16)
def test_writestr_permissions(self):
def test_WritestrPermissions(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
@ -404,8 +402,9 @@ class TestsWithSourceFile(unittest.TestCase):
orig_zip.close()
def tearDown(self):
support.unlink(TESTFN)
support.unlink(TESTFN2)
unlink(TESTFN)
unlink(TESTFN2)
class TestZip64InSmallFiles(unittest.TestCase):
# These tests test the ZIP64 functionality without using large files,
@ -436,7 +435,7 @@ class TestZip64InSmallFiles(unittest.TestCase):
zipfp.writestr, "another.name", self.data)
zipfp.close()
def testLargeFileException(self):
def test_LargeFileException(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.largeFileExceptionTest(f, zipfile.ZIP_STORED)
self.largeFileExceptionTest2(f, zipfile.ZIP_STORED)
@ -498,20 +497,18 @@ class TestZip64InSmallFiles(unittest.TestCase):
# Check that testzip doesn't raise an exception
zipfp.testzip()
zipfp.close()
def testStored(self):
def test_Stored(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipTest(f, zipfile.ZIP_STORED)
@skipUnless(zlib, "requires zlib")
def test_Deflated(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipTest(f, zipfile.ZIP_DEFLATED)
if zlib:
def testDeflated(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipTest(f, zipfile.ZIP_DEFLATED)
def testAbsoluteArcnames(self):
def test_AbsoluteArcnames(self):
zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, allowZip64=True)
zipfp.write(TESTFN, "/absolute")
zipfp.close()
@ -522,11 +519,12 @@ class TestZip64InSmallFiles(unittest.TestCase):
def tearDown(self):
zipfile.ZIP64_LIMIT = self._limit
support.unlink(TESTFN)
support.unlink(TESTFN2)
unlink(TESTFN)
unlink(TESTFN2)
class PyZipFileTests(unittest.TestCase):
def testWritePyfile(self):
def test_WritePyfile(self):
zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
fn = __file__
if fn.endswith('.pyc') or fn.endswith('.pyo'):
@ -539,7 +537,6 @@ class PyZipFileTests(unittest.TestCase):
self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
zipfp.close()
zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
fn = __file__
if fn.endswith('.pyc') or fn.endswith('.pyo'):
@ -552,7 +549,7 @@ class PyZipFileTests(unittest.TestCase):
self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
zipfp.close()
def testWritePythonPackage(self):
def test_WritePythonPackage(self):
import email
packagedir = os.path.dirname(email.__file__)
@ -564,7 +561,7 @@ class PyZipFileTests(unittest.TestCase):
self.assertTrue('email/__init__.pyo' in names or 'email/__init__.pyc' in names)
self.assertTrue('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names)
def testWritePythonDirectory(self):
def test_WritePythonDirectory(self):
os.mkdir(TESTFN2)
try:
fp = open(os.path.join(TESTFN2, "mod1.py"), "w")
@ -590,7 +587,7 @@ class PyZipFileTests(unittest.TestCase):
finally:
shutil.rmtree(TESTFN2)
def testWriteNonPyfile(self):
def test_WriteNonPyfile(self):
zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
open(TESTFN, 'w').write('most definitely not a python file')
self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
@ -598,7 +595,7 @@ class PyZipFileTests(unittest.TestCase):
class OtherTests(unittest.TestCase):
def testUnicodeFilenames(self):
def test_UnicodeFilenames(self):
zf = zipfile.ZipFile(TESTFN, "w")
zf.writestr("foo.txt", "Test for unicode filename")
zf.writestr("\xf6.txt", "Test for unicode filename")
@ -608,7 +605,7 @@ class OtherTests(unittest.TestCase):
self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
zf.close()
def testCreateNonExistentFileForAppend(self):
def test_CreateNonExistentFileForAppend(self):
if os.path.exists(TESTFN):
os.unlink(TESTFN)
@ -628,7 +625,7 @@ class OtherTests(unittest.TestCase):
self.assertEqual(zf.read(filename), content)
zf.close()
def testCloseErroneousFile(self):
def test_CloseErroneousFile(self):
# This test checks that the ZipFile constructor closes the file object
# it opens if there's an error in the file. If it doesn't, the traceback
# holds a reference to the ZipFile object and, indirectly, the file object.
@ -643,7 +640,7 @@ class OtherTests(unittest.TestCase):
except zipfile.BadZipfile:
pass
def testIsZipErroneousFile(self):
def test_IsZipErroneousFile(self):
# This test checks that the is_zipfile function correctly identifies
# a file that is not a zip file
@ -665,7 +662,7 @@ class OtherTests(unittest.TestCase):
chk = zipfile.is_zipfile(fp)
self.assertTrue(not chk)
def testIsZipValidFile(self):
def test_IsZipValidFile(self):
# This test checks that the is_zipfile function correctly identifies
# a file that is a zip file
@ -690,7 +687,7 @@ class OtherTests(unittest.TestCase):
chk = zipfile.is_zipfile(fp)
self.assertTrue(chk)
def testNonExistentFileRaisesIOError(self):
def test_NonExistentFileRaisesIOError(self):
# make sure we don't raise an AttributeError when a partially-constructed
# ZipFile instance is finalized; this tests for regression on SF tracker
# bug #403871.
@ -704,7 +701,7 @@ class OtherTests(unittest.TestCase):
# quickly.
self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
def testClosedZipRaisesRuntimeError(self):
def test_ClosedZipRaisesRuntimeError(self):
# Verify that testzip() doesn't swallow inappropriate exceptions.
data = io.BytesIO()
zipf = zipfile.ZipFile(data, mode="w")
@ -773,7 +770,7 @@ class OtherTests(unittest.TestCase):
self.assertEqual(zipfile.sizeEndCentDir64, 56)
self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
def testComments(self):
def test_Comments(self):
# This test checks that comments on the archive are handled properly
# check default comment is empty
@ -816,8 +813,9 @@ class OtherTests(unittest.TestCase):
zipfr.close()
def tearDown(self):
support.unlink(TESTFN)
support.unlink(TESTFN2)
unlink(TESTFN)
unlink(TESTFN2)
class DecryptionTests(unittest.TestCase):
# This test checks that ZIP decryption works. Since the library does not
@ -861,19 +859,19 @@ class DecryptionTests(unittest.TestCase):
self.zip2.close()
os.unlink(TESTFN2)
def testNoPassword(self):
def test_NoPassword(self):
# Reading the encrypted file without password
# must generate a RunTime exception
self.assertRaises(RuntimeError, self.zip.read, "test.txt")
self.assertRaises(RuntimeError, self.zip2.read, "zero")
def testBadPassword(self):
def test_BadPassword(self):
self.zip.setpassword(b"perl")
self.assertRaises(RuntimeError, self.zip.read, "test.txt")
self.zip2.setpassword(b"perl")
self.assertRaises(RuntimeError, self.zip2.read, "zero")
def testGoodPassword(self):
def test_GoodPassword(self):
self.zip.setpassword(b"python")
self.assertEquals(self.zip.read("test.txt"), self.plain)
self.zip2.setpassword(b"12345")
@ -892,8 +890,8 @@ class TestsWithRandomBinaryFiles(unittest.TestCase):
fp.close()
def tearDown(self):
support.unlink(TESTFN)
support.unlink(TESTFN2)
unlink(TESTFN)
unlink(TESTFN2)
def makeTestArchive(self, f, compression):
# Create the ZIP archive
@ -913,7 +911,7 @@ class TestsWithRandomBinaryFiles(unittest.TestCase):
self.assertEqual(zipfp.read("another.name"), self.data)
zipfp.close()
def testStored(self):
def test_Stored(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipTest(f, zipfile.ZIP_STORED)
@ -947,7 +945,7 @@ class TestsWithRandomBinaryFiles(unittest.TestCase):
self.assertEqual(testdata1, self.data)
zipfp.close()
def testOpenStored(self):
def test_OpenStored(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipOpenTest(f, zipfile.ZIP_STORED)
@ -969,10 +967,11 @@ class TestsWithRandomBinaryFiles(unittest.TestCase):
self.assertEqual(testdata, self.data)
zipfp.close()
def testRandomOpenStored(self):
def test_RandomOpenStored(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zipRandomOpenTest(f, zipfile.ZIP_STORED)
class TestsWithMultipleOpens(unittest.TestCase):
def setUp(self):
# Create the ZIP archive
@ -981,7 +980,7 @@ class TestsWithMultipleOpens(unittest.TestCase):
zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
zipfp.close()
def testSameFile(self):
def test_SameFile(self):
# Verify that (when the ZipFile is in control of creating file objects)
# multiple open() calls can be made without interfering with each other.
zipf = zipfile.ZipFile(TESTFN2, mode="r")
@ -994,7 +993,7 @@ class TestsWithMultipleOpens(unittest.TestCase):
self.assertEqual(data1, data2)
zipf.close()
def testDifferentFile(self):
def test_DifferentFile(self):
# Verify that (when the ZipFile is in control of creating file objects)
# multiple open() calls can be made without interfering with each other.
zipf = zipfile.ZipFile(TESTFN2, mode="r")
@ -1008,7 +1007,7 @@ class TestsWithMultipleOpens(unittest.TestCase):
self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
zipf.close()
def testInterleaved(self):
def test_Interleaved(self):
# Verify that (when the ZipFile is in control of creating file objects)
# multiple open() calls can be made without interfering with each other.
zipf = zipfile.ZipFile(TESTFN2, mode="r")
@ -1023,25 +1022,26 @@ class TestsWithMultipleOpens(unittest.TestCase):
zipf.close()
def tearDown(self):
support.unlink(TESTFN2)
unlink(TESTFN2)
class TestWithDirectory(unittest.TestCase):
def setUp(self):
os.mkdir(TESTFN2)
def testExtractDir(self):
def test_ExtractDir(self):
zipf = zipfile.ZipFile(findfile("zipdir.zip"))
zipf.extractall(TESTFN2)
self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
def test_bug_6050(self):
def test_Bug6050(self):
# Extraction should succeed if directories already exist
os.mkdir(os.path.join(TESTFN2, "a"))
self.testExtractDir()
self.test_ExtractDir()
def testStoreDir(self):
def test_StoreDir(self):
os.mkdir(os.path.join(TESTFN2, "x"))
zipf = zipfile.ZipFile(TESTFN, "w")
zipf.write(os.path.join(TESTFN2, "x"), "x")
@ -1050,7 +1050,7 @@ class TestWithDirectory(unittest.TestCase):
def tearDown(self):
shutil.rmtree(TESTFN2)
if os.path.exists(TESTFN):
support.unlink(TESTFN)
unlink(TESTFN)
class UniversalNewlineTests(unittest.TestCase):
@ -1123,51 +1123,54 @@ class UniversalNewlineTests(unittest.TestCase):
zipfp.close()
def testReadStored(self):
def test_ReadStored(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.readTest(f, zipfile.ZIP_STORED)
def testReadlineStored(self):
def test_ReadlineStored(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.readlineTest(f, zipfile.ZIP_STORED)
def testReadlinesStored(self):
def test_ReadlinesStored(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.readlinesTest(f, zipfile.ZIP_STORED)
def testIterlinesStored(self):
def test_IterlinesStored(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.iterlinesTest(f, zipfile.ZIP_STORED)
if zlib:
def testReadDeflated(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.readTest(f, zipfile.ZIP_DEFLATED)
@skipUnless(zlib, "requires zlib")
def test_ReadDeflated(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.readTest(f, zipfile.ZIP_DEFLATED)
def testReadlineDeflated(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.readlineTest(f, zipfile.ZIP_DEFLATED)
@skipUnless(zlib, "requires zlib")
def test_ReadlineDeflated(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.readlineTest(f, zipfile.ZIP_DEFLATED)
def testReadlinesDeflated(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.readlinesTest(f, zipfile.ZIP_DEFLATED)
@skipUnless(zlib, "requires zlib")
def test_ReadlinesDeflated(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.readlinesTest(f, zipfile.ZIP_DEFLATED)
def testIterlinesDeflated(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.iterlinesTest(f, zipfile.ZIP_DEFLATED)
@skipUnless(zlib, "requires zlib")
def test_IterlinesDeflated(self):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.iterlinesTest(f, zipfile.ZIP_DEFLATED)
def tearDown(self):
for sep, fn in self.arcfiles.items():
os.remove(fn)
support.unlink(TESTFN)
support.unlink(TESTFN2)
unlink(TESTFN)
unlink(TESTFN2)
def test_main():
run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
TestWithDirectory,
UniversalNewlineTests, TestsWithRandomBinaryFiles)
TestWithDirectory, UniversalNewlineTests,
TestsWithRandomBinaryFiles)
if __name__ == "__main__":
test_main()