Commit 76430242 authored by Ezio Melotti's avatar Ezio Melotti

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