| 1 |
import os |
| 2 |
from unittest.mock import ( |
| 3 |
patch, call, Mock |
| 4 |
) |
| 5 |
from pytest import raises |
| 6 |
|
| 7 |
from kiwi.boxbuild.defaults import BoxBuildDefaults |
| 8 |
from kiwi.defaults import Defaults |
| 9 |
|
| 10 |
from kiwi.exceptions import KiwiBoxTargetPathError |
| 11 |
|
| 12 |
|
| 13 |
class TestBoxBuildDefaults: |
| 14 |
@patch('pathlib.Path.home') |
| 15 |
@patch('os.path.exists') |
| 16 |
def test_get_box_config_file(self, mock_os_path_exists, mock_Path_home): |
| 17 |
mock_os_path_exists.return_value = False |
| 18 |
mock_Path_home.return_value.joinpath.return_value.exists.return_value = \ |
| 19 |
False |
| 20 |
assert BoxBuildDefaults.get_box_config_file() == \ |
| 21 |
Defaults.project_file('config/kiwi_boxed_plugin.yml') |
| 22 |
|
| 23 |
@patch('os.path.exists', lambda f: True) |
| 24 |
@patch.dict(os.environ, KIWI_BOXED_PLUGIN_CFG='aarchderwelt.conf') |
| 25 |
def test_get_box_config_file_env(self): |
| 26 |
assert BoxBuildDefaults.get_box_config_file() == 'aarchderwelt.conf' |
| 27 |
|
| 28 |
@patch.dict(os.environ, {}, clear=True) |
| 29 |
@patch('os.path.abspath', lambda f: '/highway/to/hell.conf') |
| 30 |
@patch('os.path.exists', lambda f: True) |
| 31 |
def test_get_box_config_file_currdir(self): |
| 32 |
assert BoxBuildDefaults.get_box_config_file() == \ |
| 33 |
'/highway/to/hell.conf' |
| 34 |
|
| 35 |
@patch.dict(os.environ, {}, clear=True) |
| 36 |
@patch('pathlib.Path.home') |
| 37 |
@patch('os.path.exists') |
| 38 |
def test_get_box_config_file_local_kiwi( |
| 39 |
self, mock_os_path_exists, mock_Path_home |
| 40 |
): |
| 41 |
mock_os_path_exists.return_value = False |
| 42 |
config_path_home = Mock() |
| 43 |
config_path_home.exists.return_value = True |
| 44 |
config_path_home.as_posix.return_value = \ |
| 45 |
'/home/zoidberg/.config/kiwi/kiwi_boxed_plugin.yml' |
| 46 |
mock_Path_home.return_value.joinpath.return_value = config_path_home |
| 47 |
assert BoxBuildDefaults.get_box_config_file() == \ |
| 48 |
'/home/zoidberg/.config/kiwi/kiwi_boxed_plugin.yml' |
| 49 |
mock_Path_home.return_value.joinpath.assert_called_once_with( |
| 50 |
'.config/kiwi/kiwi_boxed_plugin.yml' |
| 51 |
) |
| 52 |
|
| 53 |
@patch.dict(os.environ, {}, clear=True) |
| 54 |
@patch('pathlib.Path.home') |
| 55 |
@patch('os.path.exists', lambda f: f == '/etc/kiwi_boxed_plugin.yml') |
| 56 |
def test_get_box_config_file_etc(self, mock_Path_home): |
| 57 |
mock_Path_home.return_value.joinpath.return_value.exists.return_value = \ |
| 58 |
False |
| 59 |
assert BoxBuildDefaults.get_box_config_file() == \ |
| 60 |
'/etc/kiwi_boxed_plugin.yml' |
| 61 |
|
| 62 |
@patch('os.path.isdir') |
| 63 |
def test_get_local_box_cache_dir(self, mock_os_path_isdir): |
| 64 |
mock_os_path_isdir.return_value = True |
| 65 |
with patch.dict(os.environ, {'KIWI_BOXED_CACHE_DIR': '/var/cache'}): |
| 66 |
assert BoxBuildDefaults.get_local_box_cache_dir() == '/var/cache' |
| 67 |
with patch.dict(os.environ, {'HOME': '/home/zoidberg'}, clear=True): |
| 68 |
assert BoxBuildDefaults.get_local_box_cache_dir() == \ |
| 69 |
'/home/zoidberg/.kiwi_boxes' |
| 70 |
|
| 71 |
@patch('pathlib.Path') |
| 72 |
def test_create_build_target_dir(self, mock_Path): |
| 73 |
BoxBuildDefaults.create_build_target_dir('some') |
| 74 |
assert mock_Path.call_args_list == [ |
| 75 |
call('some'), |
| 76 |
call('some/result.log'), |
| 77 |
] |
| 78 |
mock_Path.side_effect = Exception('some error') |
| 79 |
with raises(KiwiBoxTargetPathError): |
| 80 |
BoxBuildDefaults.create_build_target_dir('some') |