diff options
author | Brian Harring <ferringb@gmail.com> | 2022-12-24 13:14:53 -0800 |
---|---|---|
committer | Arthur Zamarin <arthurzam@gentoo.org> | 2022-12-25 19:49:11 +0200 |
commit | d6a7c2e44b4f497357f8569d423104232a58f384 (patch) | |
tree | 625ac52169356714a9f5e69e11f2b6cc2d72355a /tests/compression | |
parent | compression: prefer gtar over tar if available (diff) | |
download | snakeoil-d6a7c2e44b4f497357f8569d423104232a58f384.tar.gz snakeoil-d6a7c2e44b4f497357f8569d423104232a58f384.tar.bz2 snakeoil-d6a7c2e44b4f497357f8569d423104232a58f384.zip |
Reformat w/ black 22.12.0 for consistency.
Signed-off-by: Brian Harring <ferringb@gmail.com>
Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
Diffstat (limited to 'tests/compression')
-rw-r--r-- | tests/compression/__init__.py | 68 | ||||
-rw-r--r-- | tests/compression/test_bzip2.py | 40 | ||||
-rw-r--r-- | tests/compression/test_init.py | 77 | ||||
-rw-r--r-- | tests/compression/test_xz.py | 30 |
4 files changed, 117 insertions, 98 deletions
diff --git a/tests/compression/__init__.py b/tests/compression/__init__.py index 3b70dcba..0bf26d04 100644 --- a/tests/compression/__init__.py +++ b/tests/compression/__init__.py @@ -4,78 +4,100 @@ import pytest from snakeoil import compression from snakeoil.process import CommandNotFound, find_binary + def hide_binary(*binaries: str): def mock_find_binary(name): if name in binaries: raise CommandNotFound(name) return find_binary(name) - return patch('snakeoil.process.find_binary', side_effect=mock_find_binary) + return patch("snakeoil.process.find_binary", side_effect=mock_find_binary) class Base: - module: str = '' - decompressed_test_data: bytes = b'' - compressed_test_data: bytes = b'' + module: str = "" + decompressed_test_data: bytes = b"" + compressed_test_data: bytes = b"" def decompress(self, data: bytes) -> bytes: - raise NotImplementedError(self, 'decompress') + raise NotImplementedError(self, "decompress") - @pytest.mark.parametrize('parallelize', (True, False)) - @pytest.mark.parametrize('level', (1, 9)) + @pytest.mark.parametrize("parallelize", (True, False)) + @pytest.mark.parametrize("level", (1, 9)) def test_compress_data(self, level, parallelize): - compressed = compression.compress_data(self.module, self.decompressed_test_data, level=level, parallelize=parallelize) + compressed = compression.compress_data( + self.module, + self.decompressed_test_data, + level=level, + parallelize=parallelize, + ) assert compressed assert self.decompress(compressed) == self.decompressed_test_data - @pytest.mark.parametrize('parallelize', (True, False)) + @pytest.mark.parametrize("parallelize", (True, False)) def test_decompress_data(self, parallelize): - assert self.decompressed_test_data == compression.decompress_data(self.module, self.compressed_test_data, parallelize=parallelize) + assert self.decompressed_test_data == compression.decompress_data( + self.module, self.compressed_test_data, parallelize=parallelize + ) - @pytest.mark.parametrize('parallelize', (True, False)) - @pytest.mark.parametrize('level', (1, 9)) + @pytest.mark.parametrize("parallelize", (True, False)) + @pytest.mark.parametrize("level", (1, 9)) def test_compress_handle(self, tmp_path, level, parallelize): - path = tmp_path / f'test.{self.module}' + path = tmp_path / f"test.{self.module}" - stream = compression.compress_handle(self.module, str(path), level=level, parallelize=parallelize) + stream = compression.compress_handle( + self.module, str(path), level=level, parallelize=parallelize + ) stream.write(self.decompressed_test_data) stream.close() assert self.decompress(path.read_bytes()) == self.decompressed_test_data with path.open("wb") as file: - stream = compression.compress_handle(self.module, file, level=level, parallelize=parallelize) + stream = compression.compress_handle( + self.module, file, level=level, parallelize=parallelize + ) stream.write(self.decompressed_test_data) stream.close() assert self.decompress(path.read_bytes()) == self.decompressed_test_data with path.open("wb") as file: - stream = compression.compress_handle(self.module, file.fileno(), level=level, parallelize=parallelize) + stream = compression.compress_handle( + self.module, file.fileno(), level=level, parallelize=parallelize + ) stream.write(self.decompressed_test_data) stream.close() assert self.decompress(path.read_bytes()) == self.decompressed_test_data with pytest.raises(TypeError): - compression.compress_handle(self.module, b'', level=level, parallelize=parallelize) + compression.compress_handle( + self.module, b"", level=level, parallelize=parallelize + ) - @pytest.mark.parametrize('parallelize', (True, False)) + @pytest.mark.parametrize("parallelize", (True, False)) def test_decompress_handle(self, tmp_path, parallelize): - path = tmp_path / f'test.{self.module}' + path = tmp_path / f"test.{self.module}" path.write_bytes(self.compressed_test_data) - stream = compression.decompress_handle(self.module, str(path), parallelize=parallelize) + stream = compression.decompress_handle( + self.module, str(path), parallelize=parallelize + ) assert stream.read() == self.decompressed_test_data stream.close() with path.open("rb") as file: - stream = compression.decompress_handle(self.module, file, parallelize=parallelize) + stream = compression.decompress_handle( + self.module, file, parallelize=parallelize + ) assert stream.read() == self.decompressed_test_data stream.close() with path.open("rb") as file: - stream = compression.decompress_handle(self.module, file.fileno(), parallelize=parallelize) + stream = compression.decompress_handle( + self.module, file.fileno(), parallelize=parallelize + ) assert stream.read() == self.decompressed_test_data stream.close() with pytest.raises(TypeError): - compression.decompress_handle(self.module, b'', parallelize=parallelize) + compression.decompress_handle(self.module, b"", parallelize=parallelize) diff --git a/tests/compression/test_bzip2.py b/tests/compression/test_bzip2.py index f3093d09..9fdffd9a 100644 --- a/tests/compression/test_bzip2.py +++ b/tests/compression/test_bzip2.py @@ -10,28 +10,29 @@ from . import Base, hide_binary def test_no_native(): - with hide_imports('bz2'): + with hide_imports("bz2"): importlib.reload(_bzip2) assert not _bzip2.native def test_missing_bzip2_binary(): - with hide_binary('bzip2'): - with pytest.raises(CommandNotFound, match='bzip2'): + with hide_binary("bzip2"): + with pytest.raises(CommandNotFound, match="bzip2"): importlib.reload(_bzip2) def test_missing_lbzip2_binary(): - with hide_binary('lbzip2'): + with hide_binary("lbzip2"): importlib.reload(_bzip2) assert not _bzip2.parallelizable + class Bzip2Base(Base): - module = 'bzip2' - decompressed_test_data = b'Some text here\n' + module = "bzip2" + decompressed_test_data = b"Some text here\n" compressed_test_data = ( - b'BZh91AY&SY\x1bM\x00\x02\x00\x00\x01\xd3\x80\x00\x10@\x00\x08\x00\x02' + b"BZh91AY&SY\x1bM\x00\x02\x00\x00\x01\xd3\x80\x00\x10@\x00\x08\x00\x02" b'B\x94@ \x00"\r\x03\xd4\x0c \t!\x1b\xb7\x80u/\x17rE8P\x90\x1bM\x00\x02' ) @@ -40,37 +41,36 @@ class Bzip2Base(Base): class TestStdlib(Bzip2Base): - - @pytest.fixture(autouse=True, scope='class') + @pytest.fixture(autouse=True, scope="class") def _setup(self): try: - find_binary('bzip2') + find_binary("bzip2") except CommandNotFound: - pytest.skip('bzip2 binary not found') - with hide_binary('lbzip2'): + pytest.skip("bzip2 binary not found") + with hide_binary("lbzip2"): importlib.reload(_bzip2) yield class TestBzip2(Bzip2Base): - - @pytest.fixture(autouse=True, scope='class') + @pytest.fixture(autouse=True, scope="class") def _setup(self): - with hide_binary('lbzip2'): + with hide_binary("lbzip2"): importlib.reload(_bzip2) yield class TestLbzip2(Bzip2Base): - - @pytest.fixture(autouse=True, scope='class') + @pytest.fixture(autouse=True, scope="class") def _setup(self): try: - find_binary('lbzip2') + find_binary("lbzip2") except CommandNotFound: - pytest.skip('lbzip2 binary not found') + pytest.skip("lbzip2 binary not found") importlib.reload(_bzip2) def test_bad_level(self): with pytest.raises(ValueError, match='unknown option "-0"'): - _bzip2.compress_data(self.decompressed_test_data, level=90, parallelize=True) + _bzip2.compress_data( + self.decompressed_test_data, level=90, parallelize=True + ) diff --git a/tests/compression/test_init.py b/tests/compression/test_init.py index f3a40270..f1fe5bda 100644 --- a/tests/compression/test_init.py +++ b/tests/compression/test_init.py @@ -11,78 +11,77 @@ from . import hide_binary @pytest.mark.skipif(sys.platform == "darwin", reason="darwin fails with bzip2") class TestArComp: - - @pytest.fixture(scope='class') + @pytest.fixture(scope="class") def tar_file(self, tmp_path_factory): data = tmp_path_factory.mktemp("data") - (data / 'file1').write_text('Hello world') - (data / 'file2').write_text('Larry the Cow') - path = data / 'test 1.tar' - subprocess.run(['tar', 'cf', str(path), 'file1', 'file2'], cwd=data, check=True) - (data / 'file1').unlink() - (data / 'file2').unlink() + (data / "file1").write_text("Hello world") + (data / "file2").write_text("Larry the Cow") + path = data / "test 1.tar" + subprocess.run(["tar", "cf", str(path), "file1", "file2"], cwd=data, check=True) + (data / "file1").unlink() + (data / "file2").unlink() return str(path) - @pytest.fixture(scope='class') + @pytest.fixture(scope="class") def tar_bz2_file(self, tar_file): - subprocess.run(['bzip2', '-z', '-k', tar_file], check=True) + subprocess.run(["bzip2", "-z", "-k", tar_file], check=True) return tar_file + ".bz2" - @pytest.fixture(scope='class') + @pytest.fixture(scope="class") def tbz2_file(self, tar_bz2_file): - new_path = tar_bz2_file.replace('.tar.bz2', '.tbz2') + new_path = tar_bz2_file.replace(".tar.bz2", ".tbz2") shutil.copyfile(tar_bz2_file, new_path) return new_path - @pytest.fixture(scope='class') + @pytest.fixture(scope="class") def lzma_file(self, tmp_path_factory): - data = (tmp_path_factory.mktemp("data") / 'test 2.lzma') - with data.open('wb') as f: - subprocess.run(['lzma'], check=True, input=b'Hello world', stdout=f) + data = tmp_path_factory.mktemp("data") / "test 2.lzma" + with data.open("wb") as f: + subprocess.run(["lzma"], check=True, input=b"Hello world", stdout=f) return str(data) def test_unknown_extenstion(self, tmp_path): - file = tmp_path / 'test.file' - with pytest.raises(ArCompError, match='unknown compression file extension'): - ArComp(file, ext='.foo') + file = tmp_path / "test.file" + with pytest.raises(ArCompError, match="unknown compression file extension"): + ArComp(file, ext=".foo") def test_missing_tar(self, tmp_path, tar_file): - with hide_binary('tar'), chdir(tmp_path): - with pytest.raises(ArCompError, match='required binary not found'): - ArComp(tar_file, ext='.tar').unpack(dest=tmp_path) + with hide_binary("tar"), chdir(tmp_path): + with pytest.raises(ArCompError, match="required binary not found"): + ArComp(tar_file, ext=".tar").unpack(dest=tmp_path) def test_tar(self, tmp_path, tar_file): with chdir(tmp_path): - ArComp(tar_file, ext='.tar').unpack(dest=tmp_path) - assert (tmp_path / 'file1').read_text() == 'Hello world' - assert (tmp_path / 'file2').read_text() == 'Larry the Cow' + ArComp(tar_file, ext=".tar").unpack(dest=tmp_path) + assert (tmp_path / "file1").read_text() == "Hello world" + assert (tmp_path / "file2").read_text() == "Larry the Cow" def test_tar_bz2(self, tmp_path, tar_bz2_file): with chdir(tmp_path): - ArComp(tar_bz2_file, ext='.tar.bz2').unpack(dest=tmp_path) - assert (tmp_path / 'file1').read_text() == 'Hello world' - assert (tmp_path / 'file2').read_text() == 'Larry the Cow' + ArComp(tar_bz2_file, ext=".tar.bz2").unpack(dest=tmp_path) + assert (tmp_path / "file1").read_text() == "Hello world" + assert (tmp_path / "file2").read_text() == "Larry the Cow" def test_tbz2(self, tmp_path, tbz2_file): with chdir(tmp_path): - ArComp(tbz2_file, ext='.tbz2').unpack(dest=tmp_path) - assert (tmp_path / 'file1').read_text() == 'Hello world' - assert (tmp_path / 'file2').read_text() == 'Larry the Cow' + ArComp(tbz2_file, ext=".tbz2").unpack(dest=tmp_path) + assert (tmp_path / "file1").read_text() == "Hello world" + assert (tmp_path / "file2").read_text() == "Larry the Cow" def test_fallback_tbz2(self, tmp_path, tbz2_file): with hide_binary(*next(zip(*_TarBZ2.compress_binary[:-1]))): with chdir(tmp_path): - ArComp(tbz2_file, ext='.tbz2').unpack(dest=tmp_path) - assert (tmp_path / 'file1').read_text() == 'Hello world' - assert (tmp_path / 'file2').read_text() == 'Larry the Cow' + ArComp(tbz2_file, ext=".tbz2").unpack(dest=tmp_path) + assert (tmp_path / "file1").read_text() == "Hello world" + assert (tmp_path / "file2").read_text() == "Larry the Cow" def test_no_fallback_tbz2(self, tmp_path, tbz2_file): with hide_binary(*next(zip(*_TarBZ2.compress_binary))), chdir(tmp_path): - with pytest.raises(ArCompError, match='no compression binary'): - ArComp(tbz2_file, ext='.tbz2').unpack(dest=tmp_path) + with pytest.raises(ArCompError, match="no compression binary"): + ArComp(tbz2_file, ext=".tbz2").unpack(dest=tmp_path) def test_lzma(self, tmp_path, lzma_file): - dest = tmp_path / 'file' + dest = tmp_path / "file" with chdir(tmp_path): - ArComp(lzma_file, ext='.lzma').unpack(dest=dest) - assert (dest).read_bytes() == b'Hello world' + ArComp(lzma_file, ext=".lzma").unpack(dest=dest) + assert (dest).read_bytes() == b"Hello world" diff --git a/tests/compression/test_xz.py b/tests/compression/test_xz.py index f8417b30..0af7c645 100644 --- a/tests/compression/test_xz.py +++ b/tests/compression/test_xz.py @@ -10,26 +10,26 @@ from . import Base, hide_binary def test_no_native(): - with hide_imports('lzma'): + with hide_imports("lzma"): importlib.reload(_xz) assert not _xz.native def test_missing_xz_binary(): - with hide_binary('xz'): - with pytest.raises(CommandNotFound, match='xz'): + with hide_binary("xz"): + with pytest.raises(CommandNotFound, match="xz"): importlib.reload(_xz) class XzBase(Base): - module = 'xz' - decompressed_test_data = b'Some text here\n' * 2 + module = "xz" + decompressed_test_data = b"Some text here\n" * 2 compressed_test_data = ( - b'\xfd7zXZ\x00\x00\x04\xe6\xd6\xb4F\x04\xc0\x1e\x1e!\x01\x16\x00\x00\x00' - b'\x00\x00\x00\x00\x00\x00j\xf6\x947\xe0\x00\x1d\x00\x16]\x00)\x9b\xc9\xa6g' - b'Bw\x8c\xb3\x9eA\x9a\xbeT\xc9\xfa\xe3\x19\x8f(\x00\x00\x00\x00\x00\x96N' - b'\xa8\x8ed\xa2WH\x00\x01:\x1e1V \xff\x1f\xb6\xf3}\x01\x00\x00\x00\x00\x04YZ' + b"\xfd7zXZ\x00\x00\x04\xe6\xd6\xb4F\x04\xc0\x1e\x1e!\x01\x16\x00\x00\x00" + b"\x00\x00\x00\x00\x00\x00j\xf6\x947\xe0\x00\x1d\x00\x16]\x00)\x9b\xc9\xa6g" + b"Bw\x8c\xb3\x9eA\x9a\xbeT\xc9\xfa\xe3\x19\x8f(\x00\x00\x00\x00\x00\x96N" + b"\xa8\x8ed\xa2WH\x00\x01:\x1e1V \xff\x1f\xb6\xf3}\x01\x00\x00\x00\x00\x04YZ" ) def decompress(self, data: bytes) -> bytes: @@ -37,20 +37,18 @@ class XzBase(Base): class TestStdlib(XzBase): - - @pytest.fixture(autouse=True, scope='class') + @pytest.fixture(autouse=True, scope="class") def _setup(self): try: - find_binary('xz') + find_binary("xz") except CommandNotFound: - pytest.skip('xz binary not found') + pytest.skip("xz binary not found") importlib.reload(_xz) class TestXz(XzBase): - - @pytest.fixture(autouse=True, scope='class') + @pytest.fixture(autouse=True, scope="class") def _setup(self): - with hide_imports('lzma'): + with hide_imports("lzma"): importlib.reload(_xz) yield |