kiwi-el8/test/unit/shell_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

34 lines
985 B
Python

from unittest.mock import patch
from kiwi.system.shell import Shell
from kiwi.defaults import Defaults
class TestShell:
def test_quote(self):
assert Shell.quote(r'aa\!') == 'aa\\\\\\!'
@patch('kiwi.path.Path.which')
def test_quote_key_value_file(self, mock_which):
mock_which.side_effect = ['cp', 'bash']
assert Shell.quote_key_value_file('../data/key_value') == [
"foo='bar'",
"bar='xxx'",
"name='bob'",
"strange='$a_foo'"
]
@patch('kiwi.system.shell.Command.run')
def test_run_common_function(self, mock_command):
Shell.run_common_function('foo', ['"param1"', '"param2"'])
command_string = ' '.join(
[
'source', Defaults.project_file('config/functions.sh') + ';',
'foo', '"param1"', '"param2"'
]
)
mock_command.assert_called_once_with(
['bash', '-c', command_string]
)