Fix and improve unit tests

This commit is contained in:
Wolfgang Maier
2019-07-21 10:48:15 +02:00
parent 2c20eeba3f
commit b66eeea526
2 changed files with 19 additions and 9 deletions
+3
View File
@@ -48,6 +48,9 @@ def get_fileobj_raw(filename, mode="r", compressed_formats=None):
elif 'zip' in compressed_formats and zipfile.is_zipfile(filename):
# Return fileobj for the first file in a zip file.
# 'b' is not allowed in the ZipFile mode argument
# since it always opens files in binary mode.
# For emulating text mode, we will be returning the binary fh in a
# TextIOWrapper.
zf_mode = mode.replace('b', '')
with zipfile.ZipFile(filename, zf_mode) as zh:
fh = zh.open(zh.namelist()[0], zf_mode)
+16 -9
View File
@@ -2,12 +2,18 @@ import shutil
import tempfile
import unittest
from galaxy.util.compression_utils import CompressedFile, get_fileobj_raw
import six
from galaxy.util.compression_utils import (
CompressedFile,
get_fileobj_raw
)
class CompressionUtilTestCase(unittest.TestCase):
def test_compression_safety(self):
self.assert_safety("test-data/unsafe.tar", False)
self.assert_safety("test-data/unsafe_relative_symlink.tar", False)
self.assert_safety("test-data/unsafe.zip", False)
self.assert_safety("test-data/4.bed.zip", True)
@@ -41,11 +47,12 @@ class CompressionUtilTestCase(unittest.TestCase):
shutil.rmtree(temp_dir, ignore_errors=True)
def assert_format_detected(self, path, expected_fmt, allowed_fmts=None):
fmt1 = get_fileobj_raw(path, "r", allowed_fmts)[0]
fmt2 = get_fileobj_raw(path, "rb", allowed_fmts)[0]
fmt3 = get_fileobj_raw(path, "rt", allowed_fmts)[0]
fmt4 = get_fileobj_raw(path, "U", allowed_fmts)[0]
self.assertEqual(fmt1, fmt2)
self.assertEqual(fmt1, fmt3)
self.assertEqual(fmt1, fmt4)
self.assertEqual(fmt1, expected_fmt)
for mode in ['r', 'rb', 'rt', 'U']:
if 'b' in mode:
expected_type = six.binary_type
else:
expected_type = six.text_type
fmt, fh = get_fileobj_raw(path, mode, allowed_fmts)
assert fmt == expected_fmt
if 'b' in mode or fmt is not None:
assert isinstance(fh.read(1), expected_type)