Improved tests.

This commit is contained in:
Bastian Kleineidam 2012-05-24 23:09:37 +02:00
parent c0db69fe0c
commit df3863e177
51 changed files with 162 additions and 106 deletions

View File

@ -19,6 +19,27 @@ import shutil
import nose
import patoolib
# All text files have '42' as content.
TextFileContent = '42'
class ContentSet:
"""The test archives have one of several set of content files.
The different content file sets have each a constant defined
by this class.
"""
# Recursive archives for extraction have a text file in a directory:
# t/t.txt
# Recursive archives for creation have two text files in directories:
# foo dir/t.txt
# foo dir/bar/t.txt
Recursive = 'recursive'
# Singlefile archives for extraction have a text file t.txt
# Recursive archives for creation have a text file `foo .txt'
Singlefile = 'singlefile'
basedir = os.path.dirname(__file__)
datadir = os.path.join(basedir, 'data')
@ -36,12 +57,24 @@ class ArchiveTest (unittest.TestCase):
All keyword arguments are delegated to the create test function."""
self.archive_list(filename)
self.archive_test(filename)
self.archive_extract(filename)
if kwargs.get('singlefile'):
contents_default = ContentSet.Singlefile
else:
contents_default = ContentSet.Recursive
contents = kwargs.get('contents', contents_default)
self.archive_extract(filename, contents=contents)
self.archive_create(filename, **kwargs)
def archive_extract (self, filename):
def archive_extract (self, filename, contents=ContentSet.Recursive):
"""Test archive extraction."""
archive = os.path.join(datadir, filename)
self.assertTrue(os.path.isabs(archive), "archive path is not absolute: %r" % archive)
self._archive_extract(archive, contents)
# archive name relative to tmpdir
relarchive = os.path.join("..", archive[len(basedir)+1:])
self._archive_extract(relarchive, contents, verbose=True)
def _archive_extract (self, archive, contents, verbose=False):
# create a temporary directory for extraction
tmpdir = patoolib.util.tmpdir(dir=basedir)
try:
@ -49,16 +82,34 @@ class ArchiveTest (unittest.TestCase):
except OSError:
olddir = None
os.chdir(tmpdir)
# archive name relative to tmpdir
relarchive = os.path.join("..", archive[len(basedir)+1:])
try:
patoolib._handle_archive(archive, 'extract', program=self.program)
patoolib._handle_archive(relarchive, 'extract', program=self.program, verbose=True)
output = patoolib._handle_archive(archive, 'extract', program=self.program, verbose=verbose)
self.check_extracted_contents(archive, output, contents)
finally:
if olddir:
os.chdir(olddir)
shutil.rmtree(tmpdir)
def check_extracted_contents (self, archive, output, contents):
if contents == ContentSet.Recursive:
# outdir is the 't' directory of the archive
self.assertEqual(output, 't')
self.check_directory(output, 't')
txtfile = os.path.join(output, 't.txt')
self.check_textfile(txtfile, 't.txt')
elif contents == ContentSet.Singlefile:
txtfile = output
self.check_textfile(txtfile, 't.txt')
def check_directory (self, dirname, expectedname):
self.assertTrue(os.path.isdir(dirname), dirname)
self.assertEqual(os.path.basename(dirname), expectedname)
def check_textfile (self, filename, expectedname):
self.assertTrue(os.path.isfile(filename), filename)
self.assertEqual(os.path.basename(filename), expectedname)
self.assertEqual(get_filecontent(filename), TextFileContent)
def archive_list (self, filename):
"""Test archive listing."""
archive = os.path.join(datadir, filename)
@ -72,14 +123,16 @@ class ArchiveTest (unittest.TestCase):
patoolib._handle_archive(archive, 'test', program=self.program, verbose=True)
def archive_create (self, archive, srcfile=None, singlefile=False,
format=None, compression=None):
format=None, compression=None, contents=None):
"""Test archive creation."""
# determine filename which is added to the archive
if srcfile is None:
if singlefile:
srcfile = 'foo .txt'
contents = ContentSet.Singlefile
else:
srcfile = 'foo dir'
contents = ContentSet.Recursive
srcfile = os.path.join(datadir, srcfile)
# The format and compression arguments are needed for creating
# archives with unusual file extensions.
@ -92,6 +145,7 @@ class ArchiveTest (unittest.TestCase):
# create again in verbose mode
kwargs['verbose'] = True
self._archive_create(archive, srcfile, kwargs)
# XXX check content
def _archive_create (self, filename, topack, kwargs):
"""Create archive from filename."""
@ -121,6 +175,14 @@ class ArchiveTest (unittest.TestCase):
shutil.rmtree(tmpdir)
def get_filecontent(filename):
fo = open(filename)
try:
return fo.read()
finally:
fo.close()
def needs_os (name):
"""Decorator skipping test if given program is not available."""
def check_prog (f):

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,3 +1,3 @@
!<arch>
t.txt 1337743924 501 20 100640 4 `
foo
t.txt/ 1337890761 1000 1000 100640 2 `
42

View File

@ -1,3 +1,3 @@
!<arch>
t.txt 1337743924 501 20 100640 4 `
foo
t.txt/ 1337890761 1000 1000 100640 2 `
42

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
tests/data/t.txt.Z Normal file

Binary file not shown.

BIN
tests/data/t.txt.Z.foo Normal file

Binary file not shown.

BIN
tests/data/t.txt.bz2 Normal file

Binary file not shown.

BIN
tests/data/t.txt.bz2.foo Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
tests/data/t.txt.lzma Normal file

Binary file not shown.

BIN
tests/data/t.txt.lzma.foo Normal file

Binary file not shown.

BIN
tests/data/t.txt.xz Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -13,7 +13,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from tests import ArchiveTest, needs_os, needs_program, needs_codec
from . import ArchiveTest, needs_os, needs_program, needs_codec, ContentSet
class TestArchives (ArchiveTest):
@ -158,36 +158,36 @@ class TestArchives (ArchiveTest):
@needs_program('bzip2')
def test_bzip2 (self):
self.program = 'bzip2'
self.archive_extract('t .bz2')
self.archive_test('t .bz2')
self.archive_create('t .bz2', singlefile=True)
self.archive_extract('t.txt.bz2', contents=ContentSet.Singlefile)
self.archive_test('t.txt.bz2')
self.archive_create('t.txt.bz2', singlefile=True)
@needs_program('bzip2')
def test_py_bz2 (self):
self.program = 'py_bz2'
self.archive_extract('t .bz2')
self.archive_extract('t.txt.bz2', contents=ContentSet.Singlefile)
# bzip2 is used to test the created archive
self.archive_create('t .bz2', singlefile=True)
self.archive_create('t.txt.bz2', singlefile=True)
@needs_program('pbzip2')
def test_pbzip2 (self):
self.program = 'pbzip2'
self.archive_extract('t .bz2')
self.archive_test('t .bz2')
self.archive_create('t .bz2', singlefile=True)
self.archive_extract('t.txt.bz2', contents=ContentSet.Singlefile)
self.archive_test('t.txt.bz2')
self.archive_create('t.txt.bz2', singlefile=True)
@needs_program('lbzip2')
def test_lbzip2 (self):
self.program = 'lbzip2'
self.archive_extract('t .bz2')
self.archive_test('t .bz2')
self.archive_create('t .bz2', singlefile=True)
self.archive_extract('t.txt.bz2', contents=ContentSet.Singlefile)
self.archive_test('t.txt.bz2')
self.archive_create('t.txt.bz2', singlefile=True)
def test_py_echo (self):
self.program = 'py_echo'
self.archive_list('t .bz2')
self.archive_list('t.Z')
self.archive_list('t.lzma')
self.archive_list('t.txt.bz2')
self.archive_list('t.txt.Z')
self.archive_list('t.txt.lzma')
self.archive_list('t.txt.lz')
self.archive_list('t.txt.lrz')
self.archive_list('t.txt.rz')
@ -201,7 +201,7 @@ class TestArchives (ArchiveTest):
self.archive_extract('t.zip')
self.archive_list('t.zip')
self.archive_test('t.zip')
self.archive_extract('t.jar')
self.archive_extract('t.jar', contents=None)
self.archive_list('t.jar')
self.archive_test('t.jar')
@ -217,61 +217,58 @@ class TestArchives (ArchiveTest):
@needs_program('gzip')
def test_gzip (self):
self.program = 'gzip'
self.archive_commands('t.gz', singlefile=True)
self.archive_commands('t.txt.gz', singlefile=True)
self.archive_extract('t.Z')
self.archive_extract('t.txt.Z', contents=ContentSet.Singlefile)
@needs_program('gzip')
def test_py_gzip (self):
self.program = 'py_gzip'
self.archive_extract('t.gz')
self.archive_extract('t.txt.gz')
self.archive_extract('t.txt.gz', contents=ContentSet.Singlefile)
# gzip is used to test the created archive
self.archive_create('t.gz', singlefile=True)
self.archive_create('t.txt.gz', singlefile=True)
@needs_program('pigz')
def test_pigz (self):
self.program = 'pigz'
self.archive_commands('t.gz', singlefile=True)
self.archive_commands('t.txt.gz', singlefile=True)
@needs_program('uncompress.real')
def test_uncompress (self):
self.program = 'uncompress.real'
self.archive_extract('t.Z')
self.archive_extract('t.txt.Z', contents=ContentSet.Singlefile)
@needs_program('compress')
def test_compress (self):
self.program = 'compress'
self.archive_create('t.Z', singlefile=True)
self.archive_create('t.txt.Z', singlefile=True)
@needs_program('7z')
def test_p7zip (self):
self.program = '7z'
self.archive_commands('t .7z')
self.archive_commands('t.zip')
self.archive_list('t.gz')
self.archive_list('t .bz2')
self.archive_list('t.txt.gz')
self.archive_list('t.txt.bz2')
self.archive_list('t.jar')
self.archive_list('t.Z')
self.archive_list('t.txt.Z')
self.archive_list('t.cab')
self.archive_list('t.arj')
self.archive_list('t.cpio')
self.archive_list('t.rpm')
self.archive_list('t.deb')
self.archive_extract('t.gz')
self.archive_extract('t .bz2')
self.archive_extract('t.jar')
self.archive_extract('t.Z')
self.archive_extract('t.txt.gz', contents=ContentSet.Singlefile)
self.archive_extract('t.txt.bz2', contents=ContentSet.Singlefile)
self.archive_extract('t.jar', contents=None)
self.archive_extract('t.txt.Z', contents=ContentSet.Singlefile)
self.archive_extract('t.cab')
self.archive_extract('t.arj')
self.archive_extract('t.cpio')
self.archive_extract('t.rpm')
self.archive_extract('t.deb')
self.archive_test('t.gz')
self.archive_test('t .bz2')
self.archive_extract('t.rpm', contents=None)
self.archive_extract('t.deb', contents=None)
self.archive_test('t.txt.gz')
self.archive_test('t.txt.bz2')
self.archive_test('t.jar')
self.archive_test('t.Z')
self.archive_test('t.txt.Z')
self.archive_test('t.cab')
self.archive_test('t.arj')
self.archive_test('t.cpio')
@ -292,28 +289,28 @@ class TestArchives (ArchiveTest):
self.program = '7za'
self.archive_commands('t .7z')
self.archive_commands('t.zip')
self.archive_list('t.gz')
self.archive_list('t .bz2')
self.archive_list('t.txt.gz')
self.archive_list('t.txt.bz2')
self.archive_list('t.jar')
self.archive_list('t.Z')
self.archive_list('t.txt.Z')
self.archive_list('t.cab')
#self.archive_list('t.arj')
#self.archive_list('t.cpio')
self.archive_list('t.rpm')
#self.archive_list('t.deb')
self.archive_extract('t.gz')
self.archive_extract('t .bz2')
self.archive_extract('t.jar')
self.archive_extract('t.Z')
self.archive_extract('t.txt.gz', contents=ContentSet.Singlefile)
self.archive_extract('t.txt.bz2', contents=ContentSet.Singlefile)
self.archive_extract('t.jar', contents=None)
self.archive_extract('t.txt.Z', contents=ContentSet.Singlefile)
self.archive_extract('t.cab')
#self.archive_extract('t.arj')
#self.archive_extract('t.cpio')
#self.archive_extract('t.rpm')
#self.archive_extract('t.deb')
self.archive_test('t.gz')
self.archive_test('t .bz2')
#self.archive_extract('t.rpm', contents=None)
#self.archive_extract('t.deb', contents=None)
self.archive_test('t.txt.gz')
self.archive_test('t.txt.bz2')
self.archive_test('t.jar')
self.archive_test('t.Z')
self.archive_test('t.txt.Z')
self.archive_test('t.cab')
#self.archive_test('t.arj')
#self.archive_test('t.cpio')
@ -383,13 +380,13 @@ class TestArchives (ArchiveTest):
@needs_program('cpio')
def test_rpm_extract (self):
self.program = 'rpm2cpio'
self.archive_extract('t.rpm')
self.archive_extract('t.rpm', contents=None)
@needs_program('dpkg-deb')
def test_dpkg (self):
self.program = 'dpkg'
self.archive_list('t.deb')
self.archive_extract('t.deb')
self.archive_extract('t.deb', contents=None)
self.archive_test('t.deb')
@needs_program('lzop')
@ -400,36 +397,36 @@ class TestArchives (ArchiveTest):
@needs_program('lzma')
def test_lzma (self):
self.program = 'lzma'
self.archive_test('t.lzma')
self.archive_extract('t.lzma')
self.archive_create('t.lzma', singlefile=True)
self.archive_test('t.txt.lzma')
self.archive_extract('t.txt.lzma', contents=ContentSet.Singlefile)
self.archive_create('t.txt.lzma', singlefile=True)
@needs_program('lzip')
def test_lzip (self):
self.program = 'lzip'
self.archive_test('t.txt.lz')
self.archive_extract('t.txt.lz')
self.archive_extract('t.txt.lz', contents=ContentSet.Singlefile)
self.archive_create('t.txt.lz', singlefile=True)
@needs_program('clzip')
def test_clzip (self):
self.program = 'clzip'
self.archive_test('t.txt.lz')
self.archive_extract('t.txt.lz')
self.archive_extract('t.txt.lz', contents=ContentSet.Singlefile)
self.archive_create('t.txt.lz', singlefile=True)
@needs_program('plzip')
def test_plzip (self):
self.program = 'plzip'
self.archive_test('t.txt.lz')
self.archive_extract('t.txt.lz')
self.archive_extract('t.txt.lz', contents=ContentSet.Singlefile)
self.archive_create('t.txt.lz', singlefile=True)
@needs_program('pdlzip')
def test_pdlzip (self):
self.program = 'pdlzip'
self.archive_test('t.txt.lz')
self.archive_extract('t.txt.lz')
self.archive_extract('t.txt.lz', contents=ContentSet.Singlefile)
self.archive_create('t.txt.lz', singlefile=True)
@needs_program('unalz')
@ -442,7 +439,7 @@ class TestArchives (ArchiveTest):
@needs_program('xz')
def test_xz (self):
self.program = 'xz'
self.archive_commands('t .xz', singlefile=True)
self.archive_commands('t.txt.xz', singlefile=True)
@needs_program('lha')
def test_lha (self):
@ -495,19 +492,19 @@ class TestArchives (ArchiveTest):
@needs_program('mac')
def test_mac (self):
self.program = 'mac'
self.archive_extract('t.ape')
self.archive_extract('t.ape', contents=None)
self.archive_test('t.ape')
self.archive_create('t.ape', srcfile="t.wav")
@needs_program('shorten')
def test_shorten (self):
self.program = 'shorten'
self.archive_extract('t.shn')
self.archive_extract('t.shn', contents=None)
self.archive_create('t.shn', srcfile="t.wav")
@needs_program('flac')
def test_flac (self):
self.program = 'flac'
self.archive_extract('t.flac')
self.archive_extract('t.flac', contents=None)
self.archive_test('t.flac')
self.archive_create('t.flac', srcfile="t.wav")

View File

@ -212,7 +212,7 @@ class TestArchives (ArchiveTest):
def test_py_echo (self):
self.program = 'py_echo'
self.archive_list('t.bz2.foo')
self.archive_list('t.Z.foo')
self.archive_list('t.txt.Z.foo')
# file(1) does not recognize .lzma files
#self.archive_list('t.lzma.foo')
self.archive_list('t.txt.lz.foo')
@ -230,7 +230,7 @@ class TestArchives (ArchiveTest):
self.archive_extract('t.zip.foo')
self.archive_list('t.zip.foo')
self.archive_test('t.zip.foo')
self.archive_extract('t.jar.foo')
self.archive_extract('t.jar.foo', contents=None)
self.archive_list('t.jar.foo')
self.archive_test('t.jar.foo')
@ -249,31 +249,28 @@ class TestArchives (ArchiveTest):
@needs_program('gzip')
def test_gzip (self):
self.program = 'gzip'
self.archive_commands('t.gz.foo', format="gzip", singlefile=True)
self.archive_commands('t.txt.gz.foo', format="gzip", singlefile=True)
self.archive_extract('t.Z.foo')
self.archive_extract('t.txt.Z.foo')
@needs_program('file')
@needs_program('gzip')
def test_py_gzip (self):
self.program = 'py_gzip'
self.archive_extract('t.gz.foo')
self.archive_extract('t.txt.gz.foo')
# gzip is used to test the created archive
self.archive_create('t.gz.foo', format="gzip", singlefile=True)
self.archive_create('t.txt.gz.foo', format="gzip", singlefile=True)
@needs_program('file')
@needs_program('uncompress.real')
def test_uncompress (self):
self.program = 'uncompress.real'
self.archive_extract('t.Z.foo')
self.archive_extract('t.txt.Z.foo')
@needs_program('file')
@needs_program('compress')
def test_compress (self):
self.program = 'compress'
self.archive_create('t.Z.foo', format="compress", singlefile=True)
self.archive_create('t.txt.Z.foo', format="compress", singlefile=True)
@needs_program('file')
@needs_program('7z')
@ -281,28 +278,28 @@ class TestArchives (ArchiveTest):
self.program = '7z'
self.archive_commands('t.7z.foo', format="7z")
self.archive_commands('t.zip.foo', format="zip")
self.archive_list('t.gz.foo')
self.archive_list('t.bz2.foo')
self.archive_list('t.txt.gz.foo')
self.archive_list('t.txt.bz2.foo')
self.archive_list('t.jar.foo')
self.archive_list('t.Z.foo')
self.archive_list('t.txt.Z.foo')
self.archive_list('t.cab.foo')
self.archive_list('t.arj.foo')
self.archive_list('t.cpio.foo')
self.archive_list('t.rpm.foo')
self.archive_list('t.deb.foo')
self.archive_extract('t.gz.foo')
self.archive_extract('t.bz2.foo')
self.archive_extract('t.jar.foo')
self.archive_extract('t.Z.foo')
self.archive_extract('t.txt.gz.foo')
self.archive_extract('t.txt.bz2.foo')
self.archive_extract('t.jar.foo', contents=None)
self.archive_extract('t.txt.Z.foo')
self.archive_extract('t.cab.foo')
self.archive_extract('t.arj.foo')
self.archive_extract('t.cpio.foo')
self.archive_extract('t.rpm.foo')
self.archive_extract('t.deb.foo')
self.archive_test('t.gz.foo')
self.archive_test('t.bz2.foo')
self.archive_extract('t.rpm.foo', contents=None)
self.archive_extract('t.deb.foo', contents=None)
self.archive_test('t.txt.gz.foo')
self.archive_test('t.txt.bz2.foo')
self.archive_test('t.jar.foo')
self.archive_test('t.Z.foo')
self.archive_test('t.txt.Z.foo')
self.archive_test('t.cab.foo')
self.archive_test('t.arj.foo')
self.archive_test('t.cpio.foo')
@ -315,28 +312,28 @@ class TestArchives (ArchiveTest):
self.program = '7za'
self.archive_commands('t.7z.foo', format="7z")
self.archive_commands('t.zip.foo', format="zip")
self.archive_list('t.gz.foo')
self.archive_list('t.bz2.foo')
self.archive_list('t.txt.gz.foo')
self.archive_list('t.txt.bz2.foo')
self.archive_list('t.jar.foo')
self.archive_list('t.Z.foo')
self.archive_list('t.txt.Z.foo')
self.archive_list('t.cab.foo')
#self.archive_list('t.arj.foo')
#self.archive_list('t.cpio.foo')
self.archive_list('t.rpm.foo')
#self.archive_list('t.deb.foo')
self.archive_extract('t.gz.foo')
self.archive_extract('t.bz2.foo')
self.archive_extract('t.jar.foo')
self.archive_extract('t.Z.foo')
self.archive_extract('t.txt.gz.foo')
self.archive_extract('t.txt.bz2.foo')
self.archive_extract('t.jar.foo', contents=None)
self.archive_extract('t.txt.Z.foo')
self.archive_extract('t.cab.foo')
#self.archive_extract('t.arj.foo')
#self.archive_extract('t.cpio.foo')
#self.archive_extract('t.rpm.foo')
#self.archive_extract('t.deb.foo')
self.archive_test('t.gz.foo')
self.archive_test('t.bz2.foo')
#self.archive_extract('t.rpm.foo', contents=None)
#self.archive_extract('t.deb.foo', contents=None)
self.archive_test('t.txt.gz.foo')
self.archive_test('t.txt.bz2.foo')
self.archive_test('t.jar.foo')
self.archive_test('t.Z.foo')
self.archive_test('t.txt.Z.foo')
self.archive_test('t.cab.foo')
#self.archive_test('t.arj.foo')
#self.archive_test('t.cpio.foo')
@ -426,14 +423,14 @@ class TestArchives (ArchiveTest):
@needs_program('cpio')
def test_rpm_extract (self):
self.program = 'rpm2cpio'
self.archive_extract('t.rpm.foo')
self.archive_extract('t.rpm.foo', contents=None)
@needs_program('file')
@needs_program('dpkg-deb')
def test_dpkg (self):
self.program = 'dpkg'
self.archive_list('t.deb.foo')
self.archive_extract('t.deb.foo')
self.archive_extract('t.deb.foo', contents=None)
self.archive_test('t.deb.foo')
@needs_program('file')