kiwi-el8/test/unit/utils/compress_test.py
Dan Čermák 48817a6441
Refactor Command class
Command.run() currently has a bit of a confusing behavior: if raise_on_error is
False and the executable is not found, then a weird CommandT is returned (return
code is -1 and stdout+stderr is None). This makes it possible to hanlde command
not found errors separately, but it makes that needlessly verbose. So instead,
let's just return None in *this* special case.

That in turn uncovered, that in most cases when we set `raise_on_error=True`, we
actually want an error if the command is not present but no error if the command
fails to execute (e.g. because it returns -1 if you run `$cmd --version`). Hence we
introduce the flag `raise_on_command_not_found`, which causes an exception to
be raised if the command is not found. This makes it independent of the
`raise_on_error` flag.

Additionally, we add a small optimization: if command starts with /, then we
assume it's a full path and we omit the call to which (and just check whether it
exists).

Co-authored-by: Marcus Schäfer <marcus.schaefer@gmail.com>
2024-02-19 16:33:24 +01:00

119 lines
3.9 KiB
Python

import logging
from unittest.mock import (
patch, Mock
)
from pytest import (
raises, fixture
)
from kiwi.utils.compress import Compress
from kiwi.exceptions import (
KiwiFileNotFound,
KiwiCompressionFormatUnknown
)
class TestCompress:
@fixture(autouse=True)
def inject_fixtures(self, caplog):
self._caplog = caplog
@patch('os.path.exists')
def setup(self, mock_exists):
mock_exists.return_value = True
self.compress = Compress('some-file', True)
@patch('os.path.exists')
def setup_method(self, cls, mock_exists):
self.setup()
def test_source_file_not_found(self):
with raises(KiwiFileNotFound):
Compress('some-file')
@patch('kiwi.command.Command.run')
def test_xz(self, mock_command):
assert self.compress.xz() == 'some-file.xz'
mock_command.assert_called_once_with(
[
'xz', '-f', '--threads=0', '--keep',
'some-file'
]
)
assert self.compress.compressed_filename == 'some-file.xz'
@patch('kiwi.command.Command.run')
def test_xz_with_custom_options(self, mock_command):
assert self.compress.xz(options=['foo', 'bar']) == 'some-file.xz'
mock_command.assert_called_once_with(
[
'xz', '-f', 'foo', 'bar', '--keep',
'some-file'
]
)
assert self.compress.compressed_filename == 'some-file.xz'
@patch('kiwi.command.Command.run')
def test_gzip(self, mock_command):
assert self.compress.gzip() == 'some-file.gz'
mock_command.assert_called_once_with(
['gzip', '-f', '-9', '--keep', 'some-file']
)
assert self.compress.compressed_filename == 'some-file.gz'
@patch('kiwi.command.Command.run')
@patch('kiwi.utils.compress.Temporary.new_file')
@patch('kiwi.utils.compress.Compress.get_format')
def test_uncompress(self, mock_format, mock_temp, mock_command):
mock_format.return_value = 'xz'
self.compress.uncompress()
mock_command.assert_called_once_with(
['xz', '-d', 'some-file']
)
assert self.compress.uncompressed_filename == 'some-file'
@patch('kiwi.command.Command.run')
@patch('kiwi.utils.compress.Temporary.new_file')
@patch('kiwi.utils.compress.Compress.get_format')
def test_uncompress_temporary(self, mock_format, mock_temp, mock_command):
tempfile = Mock()
tempfile.name = 'tempfile'
mock_temp.return_value = tempfile
mock_format.return_value = 'xz'
self.compress.uncompress(temporary=True)
mock_command.assert_called_once_with(
['bash', '-c', 'xz -c -d some-file > tempfile']
)
assert self.compress.uncompressed_filename == 'tempfile'
@patch('kiwi.utils.compress.Compress.get_format')
def test_uncompress_unknown_format(self, mock_format):
mock_format.return_value = None
with raises(KiwiCompressionFormatUnknown):
self.compress.uncompress()
@patch('kiwi.path.Path.which')
def test_get_format(self, mock_which):
mock_which.side_effect = ['xz']
xz = Compress('../data/xz_data.xz')
assert xz.get_format() == 'xz'
mock_which.side_effect = ['xz', 'gzip']
gzip = Compress('../data/gz_data.gz')
assert gzip.get_format() == 'gzip'
@patch('kiwi.command.Command.run')
def test_get_format_invalid_format(self, mock_run):
mock_run.side_effect = ValueError("nothing")
invalid = Compress("../data/gz_data.gz")
invalid.supported_zipper = ["mock_zip"]
with self._caplog.at_level(logging.DEBUG):
assert invalid.get_format() is None
mock_run.assert_called_once_with(
['mock_zip', '-l', '../data/gz_data.gz']
)
assert 'Error running "mock_zip -l ../data/gz_data.gz", got a'
' ValueError: nothing' in self._caplog.text