Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_extract_tar_absolute_path(self):
non_result = '/home/li/Desktop/absolute_folder'
assert not os.path.exists(non_result)
test_dir = self.get_temp_dir()
test_file = self.get_test_loc('archive/tar/tar_absolute.tar')
archive.extract_tar(test_file, test_dir)
assert not os.path.exists(non_result)
result = os.path.join(test_dir, 'home/li/Desktop/absolute_folder/absolute_file')
assert os.path.exists(result)
def test_extract_targz_from_apache_should_not_return_errors(self):
# from http://archive.apache.org/dist/commons/logging/source/commons-logging-1.1.2-src.tar.gz
# failed with ReadError('not a bzip2 file',)
test_file = self.get_test_loc('archive/tgz/commons-logging-1.1.2-src.tar.gz')
test_dir = self.get_temp_dir()
extractor = archive.get_extractor(test_file)
assert archive.extract_tar == extractor
result = archive.extract_tar(test_file, test_dir)
assert [] == result
assert os.listdir(test_dir)
def test_extract_targz_with_unicode_path_should_extract_without_error(self):
test_file = self.get_test_loc('archive/tgz/tgz_unicode.tgz')
test_dir = self.get_temp_dir()
extractor = archive.get_extractor(test_file)
assert archive.extract_tar == extractor
result = archive.extract_tar(test_file, test_dir)
assert [] == result
assert os.listdir(test_dir)
def test_extract_tar_with_absolute_path2(self):
assert not os.path.exists('/tmp/subdir')
test_file = self.get_test_loc('archive/tar/absolute_path.tar')
test_dir = self.get_temp_dir()
archive.extract_tar(test_file, test_dir)
assert not os.path.exists('/tmp/subdir')
result = os.path.join(test_dir, 'tmp/subdir/a.txt')
assert os.path.exists(result)
def test_extract_tar_bz2_basic_bz(self):
test_file = self.get_test_loc('archive/tbz/tarred_bzipped.bz')
test_dir = self.get_temp_dir()
archive.extract_tar(test_file, test_dir)
result = os.path.join(test_dir, 'e/a/b.txt')
assert os.path.exists(result)
def test_extract_targz_with_mixed_case_and_symlink(self):
test_file = self.get_test_loc('archive/tgz/mixed_case_and_symlink.tgz')
test_dir = self.get_temp_dir()
result = archive.extract_tar(test_file, test_dir)
assert [] == result
import json
exp_file = self.get_test_loc('archive/tgz/mixed_case_and_symlink.tgz.expected')
with codecs.open(exp_file, encoding='utf-8') as ef:
expected_files = json.load(ef)
check_files(test_dir, map(str, expected_files))
def test_extract_tar_bz2_absolute_path(self):
assert not os.path.exists('/tmp/subdir')
test_dir = self.get_temp_dir()
test_file = self.get_test_loc('archive/tbz/absolute_path.tar.bz2')
archive.extract_tar(test_file, test_dir)
assert not os.path.exists('/tmp/subdir')
result = os.path.join(test_dir, 'tmp/subdir/a.txt')
assert os.path.exists(result)
def check_get_extractors(self, test_file, expected, kinds=()):
from extractcode import archive
test_loc = self.get_test_loc(test_file)
if kinds:
extractors = archive.get_extractors(test_loc, kinds)
else:
extractors = archive.get_extractors(test_loc)
# import typecode
# ft = 'TODO' or typecode.contenttype.get_type(test_loc).filetype_file
# mt = 'TODO' or typecode.contenttype.get_type(test_loc).mimetype_file
fe = fileutils.file_extension(test_loc).lower()
em = ', '.join(e.__module__ + '.' + e.__name__ for e in extractors)
msg = ('%(expected)r == %(extractors)r for %(test_file)s\n'
'with fe:%(fe)r, em:%(em)s' % locals())
assert expected == extractors, msg
def test_uncompress_corrupted_archive_with_zlib(self):
from extractcode import archive
import zlib
test_dir = self.get_test_loc('extract/corrupted/a.tar.gz', copy=True)
target_dir = self.get_temp_dir()
try:
list(archive.uncompress_gzip(test_dir, target_dir))
raise Exception('no error raised')
except zlib.error as e:
assert str(e).startswith('Error -3 while decompressing')
def test_get_extractors(self):
test_data = [
('archive/zip/basic.zip', [archive.extract_zip]),
('archive/rar/basic.rar', [archive.extract_rar]),
('archive/deb/adduser_3.112ubuntu1_all.deb', [archive.extract_ar]),
('archive/cpio/elfinfo-1.0-1.fc9.src.cpio', [archive.extract_cpio]),
('archive/rpm/elfinfo-1.0-1.fc9.src.rpm', [archive.extract_rpm, archive.extract_cpio]),
('archive/gzip/file_4.26-1.diff.gz', [archive.uncompress_gzip]),
('archive/ar/liby.a', [archive.extract_ar]),
('archive/bz2/single_file_not_tarred.bz2', [archive.uncompress_bzip2]),
('archive/tar/tarred.tar', [archive.extract_tar]),
('archive/tbz/tarred_bzipped.bz', [archive.uncompress_bzip2]),
('archive/tbz/tarred_bzipped.tar.bz2', [archive.extract_tar]),
('archive/tbz/tarred_bzipped.tbz', [archive.extract_tar]),
('archive/tgz/tarred_gzipped.gz', [archive.uncompress_gzip]),
('archive/tgz/tarred_gzipped.tar.gz', [archive.extract_tar]),
('archive/tgz/tarred_gzipped.tgz', [archive.extract_tar]),
('archive/7z/z.7z', [archive.extract_7z]),
('archive/Z/tr2tex.Z', [archive.extract_Z, ]),