1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
# Copyright (C) 2010-2023 Bastian Kleineidam
#
# 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/>.
"""Basic archive test utility functions."""
import unittest
import os
import patoolib
from .. import basedir, datadir
# All text files have '42' as content.
TextFileContent = '42'
class Content:
"""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
# Singlefile archives for creation have a text file `foo .txt'
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):
"""Helper class for archive tests, handling one commandline program."""
# set program to use for archive handling in subclass
program = None
# set password for test with password
password = None
# default archive basename to check
filename = 't'
# verbosity levels in decreasing order, so that failing tests have maximum verbosity
verbosity_levels = (2, 1, 0, -1, -2)
def archive_commands(self, filename, **kwargs):
"""Run archive commands list, test, extract and create.
All keyword arguments are delegated to the create test function.
"""
self.archive_list(filename)
if not kwargs.get('skip_test'):
self.archive_test(filename)
self.archive_extract(filename, check=kwargs.get('check', Content.Recursive))
if not kwargs.get('skip_create'):
self.archive_create(filename, **kwargs)
def archive_extract(self, filename, check=Content.Recursive):
"""Test archive extraction."""
archive = os.path.join(datadir, filename)
self.assertTrue(
os.path.isabs(archive), f"archive path is not absolute: {archive!r}"
)
self._archive_extract(archive, check)
# archive name relative to tmpdir
relarchive = os.path.join("..", archive[len(basedir) + 1 :])
self._archive_extract(relarchive, check, verbosity=1)
def _archive_extract(self, archive, check, verbosity=0):
"""Extract an archive into a temporary directory and check its
contents.
"""
# create a temporary directory for extraction
tmpdir = patoolib.fileutil.tmpdir(dir=basedir, prefix="test_")
try:
olddir = patoolib.fileutil.chdir(tmpdir)
try:
output = patoolib.extract_archive(
archive,
program=self.program,
verbosity=verbosity,
interactive=False,
password=self.password,
)
if check:
self.check_extracted_archive(archive, output, check)
finally:
if olddir:
os.chdir(olddir)
finally:
patoolib.fileutil.rmtree(tmpdir)
def check_extracted_archive(self, archive, output, check):
"""Check contents of an extracted archive"""
if check == Content.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 check == Content.Singlefile:
# a non-existing directory to ensure files do not exist in it
expected_output = self.get_expected_singlefile_output(archive)
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 get_expected_singlefile_output(self, archive):
"""Return the expected output file name of a singlefile archive."""
if archive.endswith(".foo"):
return patoolib.fileutil.stripext(os.path.basename(archive))
return "t.txt"
def check_directory(self, dirname, expectedname):
"""Check that directory exists."""
self.assertTrue(os.path.isdir(dirname), dirname)
self.assertEqual(os.path.basename(dirname), expectedname)
def check_textfile(self, filename, expectedname):
"""Check that filename exists and has the default content."""
self.assertTrue(os.path.isfile(filename), repr(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)
for verbosity in self.verbosity_levels:
patoolib.list_archive(
archive,
program=self.program,
verbosity=verbosity,
interactive=False,
password=self.password,
)
def archive_test(self, filename):
"""Test archive testing."""
archive = os.path.join(datadir, filename)
for verbosity in self.verbosity_levels:
patoolib.test_archive(
archive,
program=self.program,
verbosity=verbosity,
interactive=False,
password=self.password,
)
def archive_create(self, archive, srcfiles=None, check=Content.Recursive):
"""Test archive creation."""
if srcfiles is None:
if check == Content.Recursive:
srcfiles = ('t',)
elif check == Content.Singlefile:
srcfiles = ('t.txt',)
elif check == Content.Multifile:
srcfiles = (
't.txt',
't2.txt',
)
else:
raise ValueError('invalid check value {check!r}')
olddir = patoolib.fileutil.chdir(datadir)
try:
# The format and compression arguments are needed for creating
# archives with unusual file extensions.
for verbosity in self.verbosity_levels:
self._archive_create(
archive, srcfiles, program=self.program, verbosity=verbosity
)
finally:
if olddir:
os.chdir(olddir)
def _archive_create(self, archive, srcfiles, program=None, verbosity=0):
"""Create archive from filename."""
for srcfile in srcfiles:
self.assertFalse(os.path.isabs(srcfile))
self.assertTrue(os.path.exists(srcfile))
# create a temporary directory for creation
tmpdir = patoolib.fileutil.tmpdir(dir=basedir)
try:
archive = os.path.join(tmpdir, archive)
self.assertTrue(
os.path.isabs(archive), f"archive path is not absolute: {archive!r}"
)
patoolib.create_archive(
archive,
srcfiles,
verbosity=verbosity,
interactive=False,
program=program,
password=self.password,
)
self.assertTrue(os.path.isfile(archive))
self.check_created_archive_with_test(archive)
self.check_created_archive_with_diff(archive, srcfiles)
finally:
patoolib.fileutil.rmtree(tmpdir)
def check_created_archive_with_test(self, archive):
"""Check given archive filename. Some archives need different
test programs than the programs who created the archives.
"""
command = patoolib.test_archive
program = self.program
# special case for programs that cannot test what they create
if self.program in ('compress', 'py_gzip', 'zopfli'):
program = 'gzip'
elif self.program == 'py_bz2':
program = 'bzip2'
elif self.program == 'py_lzma':
program = 'xz'
elif self.program == 'py_zstd':
program = 'zstd'
elif self.program == 'zip':
program = 'unzip'
elif self.program in ('rzip', 'shorten'):
program = 'py_echo'
command = patoolib.list_archive
elif self.program == 'lcab':
program = 'cabextract'
elif self.program == 'genisoimage':
program = '7z'
elif self.program == 'shar':
return
command(archive, program=program, password=self.password)
def check_created_archive_with_diff(self, archive, srcfiles):
"""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
if self.program in ('compress', 'zopfli'):
program = 'gzip'
elif self.program == 'zip':
program = 'unzip'
elif self.program == 'lcab':
program = 'cabextract'
elif self.program == 'shar':
program = 'unshar'
elif self.program == 'genisoimage':
program = '7z'
tmpdir = patoolib.fileutil.tmpdir(dir=basedir)
try:
olddir = patoolib.fileutil.chdir(tmpdir)
try:
output = patoolib.extract_archive(
archive, program=program, interactive=False, password=self.password
)
if len(srcfiles) == 1:
source = os.path.join(datadir, srcfiles[0])
patoolib.util.run_checked([diff, "-urN", source, output])
else:
for srcfile in srcfiles:
source = os.path.join(datadir, srcfile)
target = os.path.join(output, srcfile)
patoolib.util.run_checked([diff, "-urN", source, target])
finally:
if olddir:
os.chdir(olddir)
finally:
patoolib.fileutil.rmtree(tmpdir)
def get_filecontent(filename):
"""Get file data as text."""
with open(filename) as fo:
return fo.read()
def get_nonexisting_directory(basedir):
"""Note: this is _not_ intended to be used to create a directory."""
return patoolib.fileutil.get_single_outfile(basedir, "foo")
|