Add Multifile test mode and fix some test function errors.
This commit is contained in:
parent
c5cf15a987
commit
fe25ac8e03
|
@ -1,5 +1,5 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Copyright (C) 2010-2012 Bastian Kleineidam
|
# Copyright (C) 2010-2013 Bastian Kleineidam
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU General Public License as published by
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
@ -36,9 +36,13 @@ class Content:
|
||||||
Recursive = 'recursive'
|
Recursive = 'recursive'
|
||||||
|
|
||||||
# Singlefile archives for extraction have a text file t.txt
|
# Singlefile archives for extraction have a text file t.txt
|
||||||
# Recursive archives for creation have a text file `foo .txt'
|
# Singlefile archives for creation have a text file `foo .txt'
|
||||||
Singlefile = 'singlefile'
|
Singlefile = 'singlefile'
|
||||||
|
|
||||||
|
# Multifile archives for extraction have two text files: t.txt and t2.txt
|
||||||
|
# Multifile archives for creation have two text files: foo .txt and foo2 .txt
|
||||||
|
Multifile = 'multifile'
|
||||||
|
|
||||||
|
|
||||||
class ArchiveTest (unittest.TestCase):
|
class ArchiveTest (unittest.TestCase):
|
||||||
"""Helper class for archive tests, handling one commandline program."""
|
"""Helper class for archive tests, handling one commandline program."""
|
||||||
|
@ -52,14 +56,7 @@ class ArchiveTest (unittest.TestCase):
|
||||||
self.archive_list(filename)
|
self.archive_list(filename)
|
||||||
if not kwargs.get('skip_test'):
|
if not kwargs.get('skip_test'):
|
||||||
self.archive_test(filename)
|
self.archive_test(filename)
|
||||||
if kwargs.get('singlefile'):
|
self.archive_extract(filename, check=kwargs.get('check', Content.Recursive))
|
||||||
check_default = Content.Singlefile
|
|
||||||
else:
|
|
||||||
check_default = Content.Recursive
|
|
||||||
check = kwargs.get('check', check_default)
|
|
||||||
if 'check' in kwargs:
|
|
||||||
del kwargs['check']
|
|
||||||
self.archive_extract(filename, check=check)
|
|
||||||
if not kwargs.get('skip_create'):
|
if not kwargs.get('skip_create'):
|
||||||
self.archive_create(filename, **kwargs)
|
self.archive_create(filename, **kwargs)
|
||||||
|
|
||||||
|
@ -76,17 +73,15 @@ class ArchiveTest (unittest.TestCase):
|
||||||
# create a temporary directory for extraction
|
# create a temporary directory for extraction
|
||||||
tmpdir = patoolib.util.tmpdir(dir=basedir)
|
tmpdir = patoolib.util.tmpdir(dir=basedir)
|
||||||
try:
|
try:
|
||||||
olddir = os.getcwd()
|
olddir = patoolib.util.chdir(tmpdir)
|
||||||
except OSError:
|
try:
|
||||||
olddir = None
|
output = patoolib._handle_archive(archive, 'extract', program=self.program, verbose=verbose)
|
||||||
os.chdir(tmpdir)
|
if check:
|
||||||
try:
|
self.check_extracted_archive(archive, output, check)
|
||||||
output = patoolib._handle_archive(archive, 'extract', program=self.program, verbose=verbose)
|
finally:
|
||||||
if check:
|
if olddir:
|
||||||
self.check_extracted_archive(archive, output, check)
|
os.chdir(olddir)
|
||||||
finally:
|
finally:
|
||||||
if olddir:
|
|
||||||
os.chdir(olddir)
|
|
||||||
shutil.rmtree(tmpdir)
|
shutil.rmtree(tmpdir)
|
||||||
|
|
||||||
def check_extracted_archive (self, archive, output, check):
|
def check_extracted_archive (self, archive, output, check):
|
||||||
|
@ -98,9 +93,14 @@ class ArchiveTest (unittest.TestCase):
|
||||||
self.check_textfile(txtfile, 't.txt')
|
self.check_textfile(txtfile, 't.txt')
|
||||||
elif check == Content.Singlefile:
|
elif check == Content.Singlefile:
|
||||||
# a non-existing directory to ensure files do not exist in it
|
# a non-existing directory to ensure files do not exist in it
|
||||||
ned = get_nonexisting_directory()
|
ned = get_nonexisting_directory(os.getcwd())
|
||||||
expected_output = os.path.basename(patoolib.util.get_single_outfile(ned, archive))
|
expected_output = os.path.basename(patoolib.util.get_single_outfile(ned, archive))
|
||||||
self.check_textfile(output, expected_output)
|
self.check_textfile(output, expected_output)
|
||||||
|
elif check == Content.Multifile:
|
||||||
|
txtfile = os.path.join(output, 't.txt')
|
||||||
|
self.check_textfile(txtfile, 't.txt')
|
||||||
|
txtfile2 = os.path.join(output, 't2.txt')
|
||||||
|
self.check_textfile(txtfile2, 't2.txt')
|
||||||
|
|
||||||
def check_directory (self, dirname, expectedname):
|
def check_directory (self, dirname, expectedname):
|
||||||
self.assertTrue(os.path.isdir(dirname), dirname)
|
self.assertTrue(os.path.isdir(dirname), dirname)
|
||||||
|
@ -123,35 +123,42 @@ class ArchiveTest (unittest.TestCase):
|
||||||
patoolib._handle_archive(archive, 'test', program=self.program)
|
patoolib._handle_archive(archive, 'test', program=self.program)
|
||||||
patoolib._handle_archive(archive, 'test', program=self.program, verbose=True)
|
patoolib._handle_archive(archive, 'test', program=self.program, verbose=True)
|
||||||
|
|
||||||
def archive_create (self, archive, srcfile=None, singlefile=False):
|
def archive_create (self, archive, srcfiles=None, check=Content.Recursive):
|
||||||
"""Test archive creation."""
|
"""Test archive creation."""
|
||||||
# determine filename which is added to the archive
|
if srcfiles is None:
|
||||||
if srcfile is None:
|
if check == Content.Recursive:
|
||||||
if singlefile:
|
srcfiles = ('t',)
|
||||||
srcfile = 't.txt'
|
elif check == Content.Singlefile:
|
||||||
|
srcfiles = ('t.txt',)
|
||||||
|
elif check == Content.Multifile:
|
||||||
|
srcfiles = ('t.txt', 't2.txt',)
|
||||||
else:
|
else:
|
||||||
srcfile = 't'
|
raise ValueError('invalid check value %r' % check)
|
||||||
os.chdir(datadir)
|
olddir = patoolib.util.chdir(datadir)
|
||||||
# The format and compression arguments are needed for creating
|
try:
|
||||||
# archives with unusual file extensions.
|
# The format and compression arguments are needed for creating
|
||||||
self._archive_create(archive, srcfile, program=self.program)
|
# archives with unusual file extensions.
|
||||||
# create again in verbose mode
|
self._archive_create(archive, srcfiles, program=self.program)
|
||||||
self._archive_create(archive, srcfile, program=self.program,
|
# create again in verbose mode
|
||||||
verbose=True)
|
self._archive_create(archive, srcfiles, program=self.program, verbose=True)
|
||||||
|
finally:
|
||||||
|
if olddir:
|
||||||
|
os.chdir(olddir)
|
||||||
|
|
||||||
def _archive_create (self, archive, srcfile, **kwargs):
|
def _archive_create (self, archive, srcfiles, **kwargs):
|
||||||
"""Create archive from filename."""
|
"""Create archive from filename."""
|
||||||
self.assertFalse(os.path.isabs(srcfile))
|
for srcfile in srcfiles:
|
||||||
self.assertTrue(os.path.exists(srcfile))
|
self.assertFalse(os.path.isabs(srcfile))
|
||||||
|
self.assertTrue(os.path.exists(srcfile))
|
||||||
# create a temporary directory for creation
|
# create a temporary directory for creation
|
||||||
tmpdir = patoolib.util.tmpdir(dir=basedir)
|
tmpdir = patoolib.util.tmpdir(dir=basedir)
|
||||||
archive = os.path.join(tmpdir, archive)
|
|
||||||
self.assertTrue(os.path.isabs(archive), "archive path is not absolute: %r" % archive)
|
|
||||||
try:
|
try:
|
||||||
patoolib._handle_archive(archive, 'create', srcfile, **kwargs)
|
archive = os.path.join(tmpdir, archive)
|
||||||
|
self.assertTrue(os.path.isabs(archive), "archive path is not absolute: %r" % archive)
|
||||||
|
patoolib._handle_archive(archive, 'create', *srcfiles, **kwargs)
|
||||||
self.assertTrue(os.path.isfile(archive))
|
self.assertTrue(os.path.isfile(archive))
|
||||||
self.check_created_archive_with_test(archive)
|
self.check_created_archive_with_test(archive)
|
||||||
self.check_created_archive_with_diff(archive, srcfile)
|
self.check_created_archive_with_diff(archive, srcfiles)
|
||||||
finally:
|
finally:
|
||||||
shutil.rmtree(tmpdir)
|
shutil.rmtree(tmpdir)
|
||||||
|
|
||||||
|
@ -176,7 +183,7 @@ class ArchiveTest (unittest.TestCase):
|
||||||
return
|
return
|
||||||
patoolib._handle_archive(archive, command, program=program)
|
patoolib._handle_archive(archive, command, program=program)
|
||||||
|
|
||||||
def check_created_archive_with_diff(self, archive, srcfile):
|
def check_created_archive_with_diff(self, archive, srcfiles):
|
||||||
"""Extract created archive again and compare the contents."""
|
"""Extract created archive again and compare the contents."""
|
||||||
# diff srcfile and output
|
# diff srcfile and output
|
||||||
diff = patoolib.util.find_program("diff")
|
diff = patoolib.util.find_program("diff")
|
||||||
|
@ -194,30 +201,36 @@ class ArchiveTest (unittest.TestCase):
|
||||||
program = 'unshar'
|
program = 'unshar'
|
||||||
tmpdir = patoolib.util.tmpdir(dir=basedir)
|
tmpdir = patoolib.util.tmpdir(dir=basedir)
|
||||||
try:
|
try:
|
||||||
olddir = os.getcwd()
|
olddir = patoolib.util.chdir(tmpdir)
|
||||||
except OSError:
|
try:
|
||||||
olddir = None
|
output = patoolib._handle_archive(archive, 'extract', program=program)
|
||||||
os.chdir(tmpdir)
|
if len(srcfiles) == 1:
|
||||||
try:
|
source = os.path.join(datadir, srcfiles[0])
|
||||||
output = patoolib._handle_archive(archive, 'extract', program=program)
|
res = patoolib.util.run([diff, "-urN", source, output])
|
||||||
res = patoolib.util.run([diff, "-urN", srcfile, output])
|
self.assertEqual(res, 0)
|
||||||
self.assertEqual(res, 0)
|
else:
|
||||||
|
for srcfile in srcfiles:
|
||||||
|
source = os.path.join(datadir, srcfile)
|
||||||
|
target = os.path.join(output, srcfile)
|
||||||
|
res = patoolib.util.run([diff, "-urN", source, target])
|
||||||
|
self.assertEqual(res, 0)
|
||||||
|
finally:
|
||||||
|
if olddir:
|
||||||
|
os.chdir(olddir)
|
||||||
finally:
|
finally:
|
||||||
if olddir:
|
|
||||||
os.chdir(olddir)
|
|
||||||
shutil.rmtree(tmpdir)
|
shutil.rmtree(tmpdir)
|
||||||
|
|
||||||
|
|
||||||
def get_filecontent(filename):
|
def get_filecontent(filename):
|
||||||
fo = open(filename)
|
with open(filename) as fo:
|
||||||
try:
|
|
||||||
return fo.read()
|
return fo.read()
|
||||||
finally:
|
|
||||||
fo.close()
|
|
||||||
|
|
||||||
|
|
||||||
def get_nonexisting_directory():
|
def get_nonexisting_directory(basedir):
|
||||||
d = os.path.join(os.getcwd(), "foo")
|
d = os.path.join(basedir, "foo")
|
||||||
while os.path.exists(d):
|
while os.path.exists(d):
|
||||||
d += 'a'
|
d += 'a'
|
||||||
|
if len(d) > 100:
|
||||||
|
# wtf
|
||||||
|
raise ValueError('could not find non-existing directory at %r' % basedir)
|
||||||
return d
|
return d
|
||||||
|
|
Loading…
Reference in New Issue