Skip to content

Commit e40799b

Browse files
authored
Merge pull request #2865 from OSInside/handle_pathlib_mkdir_errors_better
Catch potential exceptions from pathlib.Path.mkdir
2 parents 672a538 + 9a8818a commit e40799b

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

kiwi/path.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ def create(path: str) -> None:
108108
:param string path: path name
109109
"""
110110
log.debug("Creating directory %s", path)
111-
pathlib.Path(path).mkdir(parents=True, exist_ok=True)
111+
try:
112+
pathlib.Path(path).mkdir(parents=True, exist_ok=True)
113+
except Exception as issue:
114+
raise KiwiFileAccessError(
115+
f'Cannot create directory: {path}: {issue}'
116+
)
112117

113118
@staticmethod
114119
def wipe(path: str) -> None:

test/unit/path_test.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
2-
import pathlib
3-
from unittest.mock import patch, call
2+
from unittest.mock import (
3+
patch, call, Mock
4+
)
45
from pytest import (
56
raises, fixture
67
)
@@ -24,10 +25,17 @@ def test_sort_by_hierarchy(self):
2425
)
2526
assert ordered == ['usr', 'etc', 'usr/bin', 'usr/lib']
2627

27-
def test_create(self, tmp_path: pathlib.Path):
28-
dest = tmp_path / "foo" / "bar" / "baz"
29-
Path.create(str(dest))
30-
assert pathlib.Path(dest).exists()
28+
@patch('pathlib.Path')
29+
def test_create(self, mock_Path):
30+
path = Mock()
31+
mock_Path.return_value = path
32+
Path.create('foo')
33+
path.mkdir.assert_called_once_with(
34+
parents=True, exist_ok=True
35+
)
36+
mock_Path.return_value = Exception
37+
with raises(KiwiFileAccessError):
38+
Path.create('foo')
3139

3240
@patch('kiwi.command.os.path.exists')
3341
@patch('kiwi.command.Command.run')

0 commit comments

Comments
 (0)