2010-02-21 11:14:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-04-08 16:54:37 +00:00
|
|
|
# Copyright (C) 2010-2012 Bastian Kleineidam
|
2010-02-21 11:14:57 +00:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import unittest
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import nose
|
|
|
|
import patoolib
|
|
|
|
|
2012-05-24 21:09:37 +00:00
|
|
|
# All text files have '42' as content.
|
|
|
|
TextFileContent = '42'
|
|
|
|
|
2012-05-25 19:22:40 +00:00
|
|
|
class Content:
|
2012-05-24 21:09:37 +00:00
|
|
|
"""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'
|
|
|
|
|
|
|
|
|
2010-02-21 11:14:57 +00:00
|
|
|
basedir = os.path.dirname(__file__)
|
|
|
|
datadir = os.path.join(basedir, 'data')
|
|
|
|
|
|
|
|
class ArchiveTest (unittest.TestCase):
|
|
|
|
"""Helper class for achive tests."""
|
|
|
|
|
2010-02-22 20:30:47 +00:00
|
|
|
def __init__ (self, *args):
|
2010-03-03 19:13:03 +00:00
|
|
|
"""Initialize this archive test."""
|
2010-02-22 20:30:47 +00:00
|
|
|
super(ArchiveTest, self).__init__(*args)
|
|
|
|
# set program to use for archive handling
|
|
|
|
self.program = None
|
2010-02-21 11:14:57 +00:00
|
|
|
|
2010-03-03 19:13:03 +00:00
|
|
|
def archive_commands (self, filename, **kwargs):
|
|
|
|
"""Run archive commands list, test, extract and create.
|
|
|
|
All keyword arguments are delegated to the create test function."""
|
2010-02-22 20:30:47 +00:00
|
|
|
self.archive_list(filename)
|
|
|
|
self.archive_test(filename)
|
2012-05-24 21:09:37 +00:00
|
|
|
if kwargs.get('singlefile'):
|
2012-05-25 19:22:40 +00:00
|
|
|
check_default = Content.Singlefile
|
2012-05-24 21:09:37 +00:00
|
|
|
else:
|
2012-05-25 19:22:40 +00:00
|
|
|
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'):
|
|
|
|
self.archive_create(filename, **kwargs)
|
|
|
|
|
|
|
|
def archive_extract (self, filename, check=Content.Recursive):
|
2010-03-03 19:13:03 +00:00
|
|
|
"""Test archive extraction."""
|
2010-02-21 11:14:57 +00:00
|
|
|
archive = os.path.join(datadir, filename)
|
2012-05-24 21:09:37 +00:00
|
|
|
self.assertTrue(os.path.isabs(archive), "archive path is not absolute: %r" % archive)
|
2012-05-25 19:22:40 +00:00
|
|
|
self._archive_extract(archive, check)
|
2012-05-24 21:09:37 +00:00
|
|
|
# archive name relative to tmpdir
|
|
|
|
relarchive = os.path.join("..", archive[len(basedir)+1:])
|
2012-05-25 19:22:40 +00:00
|
|
|
self._archive_extract(relarchive, check, verbose=True)
|
2012-05-24 21:09:37 +00:00
|
|
|
|
2012-05-25 19:22:40 +00:00
|
|
|
def _archive_extract (self, archive, check, verbose=False):
|
2010-02-21 11:14:57 +00:00
|
|
|
# create a temporary directory for extraction
|
|
|
|
tmpdir = patoolib.util.tmpdir(dir=basedir)
|
2012-04-06 10:58:15 +00:00
|
|
|
try:
|
|
|
|
olddir = os.getcwd()
|
|
|
|
except OSError:
|
|
|
|
olddir = None
|
2010-02-21 11:14:57 +00:00
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
2012-05-24 21:09:37 +00:00
|
|
|
output = patoolib._handle_archive(archive, 'extract', program=self.program, verbose=verbose)
|
2012-05-25 19:22:40 +00:00
|
|
|
if check:
|
|
|
|
self.check_extracted_archive(archive, output, check)
|
2010-02-21 11:14:57 +00:00
|
|
|
finally:
|
2012-04-06 10:58:15 +00:00
|
|
|
if olddir:
|
|
|
|
os.chdir(olddir)
|
2010-02-21 11:14:57 +00:00
|
|
|
shutil.rmtree(tmpdir)
|
|
|
|
|
2012-05-25 19:22:40 +00:00
|
|
|
def check_extracted_archive (self, archive, output, check):
|
|
|
|
if check == Content.Recursive:
|
2012-05-24 21:09:37 +00:00
|
|
|
# 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')
|
2012-05-25 19:22:40 +00:00
|
|
|
elif check == Content.Singlefile:
|
2012-05-25 13:01:18 +00:00
|
|
|
# a non-existing directory to ensure files do not exist in it
|
|
|
|
ned = get_nonexisting_directory()
|
|
|
|
expected_output = os.path.basename(patoolib.util.get_single_outfile(ned, archive))
|
|
|
|
self.check_textfile(output, expected_output)
|
2012-05-24 21:09:37 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2010-02-22 20:30:47 +00:00
|
|
|
def archive_list (self, filename):
|
2010-03-03 19:13:03 +00:00
|
|
|
"""Test archive listing."""
|
2010-02-21 11:14:57 +00:00
|
|
|
archive = os.path.join(datadir, filename)
|
2010-02-22 20:30:47 +00:00
|
|
|
patoolib._handle_archive(archive, 'list', program=self.program)
|
|
|
|
patoolib._handle_archive(archive, 'list', program=self.program, verbose=True)
|
2010-02-21 12:40:42 +00:00
|
|
|
|
2010-02-22 20:30:47 +00:00
|
|
|
def archive_test (self, filename):
|
2010-03-03 19:13:03 +00:00
|
|
|
"""Test archive testing."""
|
2010-02-21 12:40:42 +00:00
|
|
|
archive = os.path.join(datadir, filename)
|
2010-02-22 20:30:47 +00:00
|
|
|
patoolib._handle_archive(archive, 'test', program=self.program)
|
|
|
|
patoolib._handle_archive(archive, 'test', program=self.program, verbose=True)
|
2010-02-21 11:14:57 +00:00
|
|
|
|
2012-05-25 19:22:40 +00:00
|
|
|
def archive_create (self, archive, srcfile=None, singlefile=False):
|
2010-03-03 19:13:03 +00:00
|
|
|
"""Test archive creation."""
|
2012-05-17 09:31:00 +00:00
|
|
|
# determine filename which is added to the archive
|
|
|
|
if srcfile is None:
|
2012-05-17 09:27:03 +00:00
|
|
|
if singlefile:
|
2012-05-25 18:12:22 +00:00
|
|
|
srcfile = 't.txt'
|
2012-05-17 09:27:03 +00:00
|
|
|
else:
|
2012-05-25 18:12:22 +00:00
|
|
|
srcfile = 't'
|
|
|
|
os.chdir(datadir)
|
2012-05-17 09:28:05 +00:00
|
|
|
# The format and compression arguments are needed for creating
|
2010-03-03 19:13:03 +00:00
|
|
|
# archives with unusual file extensions.
|
2012-05-25 19:22:40 +00:00
|
|
|
self._archive_create(archive, srcfile, program=self.program)
|
2012-05-17 09:27:03 +00:00
|
|
|
# create again in verbose mode
|
2012-05-25 19:22:40 +00:00
|
|
|
self._archive_create(archive, srcfile, program=self.program,
|
|
|
|
verbose=True)
|
2010-03-06 13:01:02 +00:00
|
|
|
|
2012-05-25 19:22:40 +00:00
|
|
|
def _archive_create (self, archive, srcfile, **kwargs):
|
2010-03-06 13:01:02 +00:00
|
|
|
"""Create archive from filename."""
|
2012-05-25 18:12:22 +00:00
|
|
|
self.assertFalse(os.path.isabs(srcfile))
|
|
|
|
self.assertTrue(os.path.exists(srcfile))
|
2010-03-06 13:01:02 +00:00
|
|
|
# create a temporary directory for creation
|
|
|
|
tmpdir = patoolib.util.tmpdir(dir=basedir)
|
2012-05-25 18:12:22 +00:00
|
|
|
archive = os.path.join(tmpdir, archive)
|
|
|
|
self.assertTrue(os.path.isabs(archive), "archive path is not absolute: %r" % archive)
|
2010-02-21 14:48:52 +00:00
|
|
|
try:
|
2012-05-25 18:12:22 +00:00
|
|
|
patoolib._handle_archive(archive, 'create', srcfile, **kwargs)
|
2010-03-06 18:21:20 +00:00
|
|
|
self.assertTrue(os.path.isfile(archive))
|
2012-05-25 18:12:22 +00:00
|
|
|
self.check_created_archive_with_test(archive)
|
|
|
|
self.check_created_archive_with_diff(archive, srcfile)
|
2010-02-21 14:48:52 +00:00
|
|
|
finally:
|
|
|
|
shutil.rmtree(tmpdir)
|
|
|
|
|
2012-05-25 18:12:22 +00:00
|
|
|
def check_created_archive_with_test(self, archive):
|
|
|
|
command = 'test'
|
|
|
|
program = self.program
|
|
|
|
# special case for programs that cannot test what they create
|
|
|
|
if self.program in ('compress', 'py_gzip'):
|
|
|
|
program = 'gzip'
|
|
|
|
elif self.program == 'py_bz2':
|
|
|
|
program = 'bzip2'
|
|
|
|
elif self.program == 'zip':
|
|
|
|
program = 'unzip'
|
|
|
|
elif self.program in ('rzip', 'shorten'):
|
|
|
|
program = 'py_echo'
|
|
|
|
command = 'list'
|
|
|
|
elif self.program == 'lcab':
|
|
|
|
program = 'cabextract'
|
2012-05-25 20:07:22 +00:00
|
|
|
elif self.program == 'shar':
|
|
|
|
return
|
2012-05-25 18:12:22 +00:00
|
|
|
patoolib._handle_archive(archive, command, program=program)
|
|
|
|
|
|
|
|
def check_created_archive_with_diff(self, archive, srcfile):
|
|
|
|
"""Extract created archive again and compare the contents."""
|
|
|
|
# diff srcfile and output
|
|
|
|
diff = patoolib.util.find_program("diff")
|
|
|
|
if not diff:
|
|
|
|
return
|
|
|
|
program = self.program
|
|
|
|
# special case for programs that cannot extract what they create
|
2012-05-25 19:22:40 +00:00
|
|
|
if self.program == 'compress':
|
|
|
|
program = 'gzip'
|
|
|
|
elif self.program == 'zip':
|
|
|
|
program = 'unzip'
|
|
|
|
elif self.program == 'lcab':
|
2012-05-25 18:12:22 +00:00
|
|
|
program = 'cabextract'
|
2012-05-25 20:07:22 +00:00
|
|
|
elif self.program == 'shar':
|
|
|
|
program = 'unshar'
|
2012-05-25 18:12:22 +00:00
|
|
|
tmpdir = patoolib.util.tmpdir(dir=basedir)
|
|
|
|
try:
|
|
|
|
olddir = os.getcwd()
|
|
|
|
except OSError:
|
|
|
|
olddir = None
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
|
|
|
output = patoolib._handle_archive(archive, 'extract', program=program)
|
|
|
|
res = patoolib.util.run([diff, "-urN", srcfile, output])
|
|
|
|
self.assertEqual(res, 0)
|
|
|
|
finally:
|
|
|
|
if olddir:
|
|
|
|
os.chdir(olddir)
|
|
|
|
shutil.rmtree(tmpdir)
|
2012-08-04 11:39:29 +00:00
|
|
|
|
2010-02-21 11:14:57 +00:00
|
|
|
|
2012-05-24 21:09:37 +00:00
|
|
|
def get_filecontent(filename):
|
|
|
|
fo = open(filename)
|
|
|
|
try:
|
|
|
|
return fo.read()
|
|
|
|
finally:
|
|
|
|
fo.close()
|
|
|
|
|
|
|
|
|
2010-03-08 21:04:27 +00:00
|
|
|
def needs_os (name):
|
|
|
|
"""Decorator skipping test if given program is not available."""
|
|
|
|
def check_prog (f):
|
|
|
|
def newfunc (*args, **kwargs):
|
|
|
|
if os.name != name:
|
|
|
|
raise nose.SkipTest("operating system %s not found" % name)
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
newfunc.func_name = f.func_name
|
|
|
|
return newfunc
|
|
|
|
return check_prog
|
|
|
|
|
|
|
|
|
2010-02-22 20:30:47 +00:00
|
|
|
def needs_program (program):
|
|
|
|
"""Decorator skipping test if given program is not available."""
|
2010-02-21 11:14:57 +00:00
|
|
|
def check_prog (f):
|
|
|
|
def newfunc (*args, **kwargs):
|
2010-03-01 15:10:17 +00:00
|
|
|
if not patoolib.util.find_program(program):
|
2010-02-22 20:30:47 +00:00
|
|
|
raise nose.SkipTest("program `%s' not available" % program)
|
2010-02-21 11:14:57 +00:00
|
|
|
return f(*args, **kwargs)
|
|
|
|
newfunc.func_name = f.func_name
|
|
|
|
return newfunc
|
|
|
|
return check_prog
|
2010-02-22 20:55:29 +00:00
|
|
|
|
|
|
|
|
2010-03-11 17:33:58 +00:00
|
|
|
def needs_one_program (programs):
|
|
|
|
"""Decorator skipping test if not one of given programs are available."""
|
|
|
|
def check_prog (f):
|
|
|
|
def newfunc (*args, **kwargs):
|
|
|
|
for program in programs:
|
|
|
|
if patoolib.util.find_program(program):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise nose.SkipTest("None of programs %s available" % programs)
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
newfunc.func_name = f.func_name
|
|
|
|
return newfunc
|
|
|
|
return check_prog
|
|
|
|
|
|
|
|
|
2010-02-22 20:55:29 +00:00
|
|
|
def needs_codec (program, codec):
|
|
|
|
"""Decorator skipping test if given program codec is not available."""
|
|
|
|
def check_prog (f):
|
|
|
|
def newfunc (*args, **kwargs):
|
2010-03-01 15:10:17 +00:00
|
|
|
if not patoolib.util.find_program(program):
|
2010-02-23 06:32:13 +00:00
|
|
|
raise nose.SkipTest("program `%s' not available" % program)
|
2010-03-01 15:18:36 +00:00
|
|
|
if not has_codec(program, codec):
|
2010-02-22 20:55:29 +00:00
|
|
|
raise nose.SkipTest("codec `%s' for program `%s' not available" % (codec, program))
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
newfunc.func_name = f.func_name
|
|
|
|
return newfunc
|
|
|
|
return check_prog
|
|
|
|
|
|
|
|
|
2010-03-01 15:18:36 +00:00
|
|
|
def has_codec (program, codec):
|
2010-03-03 19:13:03 +00:00
|
|
|
"""Test if program supports given codec."""
|
2010-02-22 20:55:29 +00:00
|
|
|
if program == '7z' and codec == 'rar':
|
2010-03-01 15:05:32 +00:00
|
|
|
return patoolib.util.p7zip_supports_rar()
|
2012-05-23 03:19:14 +00:00
|
|
|
if patoolib.program_supports_compression(program, codec):
|
|
|
|
return True
|
|
|
|
return patoolib.util.find_program(codec)
|
2012-05-25 13:01:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_nonexisting_directory():
|
|
|
|
d = os.path.join(os.getcwd(), "foo")
|
|
|
|
while os.path.exists(d):
|
|
|
|
d += 'a'
|
|
|
|
return d
|