| 1 |
from unittest.mock import ( |
| 2 |
Mock, patch, call |
| 3 |
) |
| 4 |
|
| 5 |
from kiwi.utils.dir_files import DirFiles |
| 6 |
|
| 7 |
|
| 8 |
class TestDirFiles: |
| 9 |
@patch('kiwi.utils.dir_files.Path') |
| 10 |
def setup(self, mock_Path): |
| 11 |
self.dir_manager = DirFiles('box_dir') |
| 12 |
mock_Path.wipe.assert_called_once_with('box_dir.tmp') |
| 13 |
|
| 14 |
@patch('kiwi.utils.dir_files.Path') |
| 15 |
def setup_method(self, cls, mock_Path): |
| 16 |
self.setup() |
| 17 |
|
| 18 |
@patch('kiwi.utils.dir_files.NamedTemporaryFile') |
| 19 |
def test_register(self, mock_NamedTemporaryFile): |
| 20 |
tmpfile = Mock() |
| 21 |
tmpfile.name = 'tmp_a' |
| 22 |
mock_NamedTemporaryFile.return_value = tmpfile |
| 23 |
self.dir_manager.register('/some/path/to/file_a') |
| 24 |
assert self.dir_manager.collection == { |
| 25 |
'file_a': 'tmp_a' |
| 26 |
} |
| 27 |
|
| 28 |
@patch('kiwi.utils.dir_files.NamedTemporaryFile') |
| 29 |
def test_deregister(self, mock_NamedTemporaryFile): |
| 30 |
tmpfile = Mock() |
| 31 |
tmpfile.name = 'tmp_a' |
| 32 |
mock_NamedTemporaryFile.return_value = tmpfile |
| 33 |
self.dir_manager.register('/some/path/to/file_a') |
| 34 |
assert self.dir_manager.collection == { |
| 35 |
'file_a': 'tmp_a' |
| 36 |
} |
| 37 |
self.dir_manager.deregister('/some/path/to/some/not/present') |
| 38 |
assert self.dir_manager.collection == { |
| 39 |
'file_a': 'tmp_a' |
| 40 |
} |
| 41 |
self.dir_manager.deregister('/some/path/to/file_a') |
| 42 |
assert self.dir_manager.collection == {} |
| 43 |
|
| 44 |
@patch('kiwi.utils.dir_files.Path') |
| 45 |
@patch('kiwi.utils.dir_files.Command.run') |
| 46 |
def test_commit(self, mock_Command_run, mock_Path): |
| 47 |
self.dir_manager.collection = { |
| 48 |
'file_a': 'tmp_a' |
| 49 |
} |
| 50 |
self.dir_manager.commit() |
| 51 |
mock_Path.create.assert_called_once_with('box_dir.tmp') |
| 52 |
assert mock_Command_run.call_args_list == [ |
| 53 |
call( |
| 54 |
['bash', '-c', 'cp -a box_dir/* box_dir.tmp'], |
| 55 |
raise_on_error=False |
| 56 |
), |
| 57 |
call(['mv', 'tmp_a', 'box_dir.tmp/file_a']), |
| 58 |
call(['mv', 'box_dir', 'box_dir.wipe']), |
| 59 |
call(['mv', 'box_dir.tmp', 'box_dir']), |
| 60 |
call(['rm', '-rf', 'box_dir.wipe']) |
| 61 |
] |