Skip to content

Commit 8bfef03

Browse files
committed
fix: address failing tests, add tests for patched Path
1 parent dd29031 commit 8bfef03

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

nipype/pipeline/engine/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def write_node_report(node, result=None, is_mapnode=False):
123123

124124
cwd = node.output_dir()
125125
report_file = Path(cwd) / '_report' / 'report.rst'
126-
report_file.parent.mkdir(exist_ok=True)
126+
report_file.parent.mkdir(exist_ok=True, parents=True)
127127

128128
lines = [
129129
write_rst_header('Node: %s' % get_print_name(node), level=0),

nipype/utils/filemanip.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ def __init__(self, path):
6060
try:
6161
Path('/invented/file/path').resolve(strict=True)
6262
except TypeError:
63+
from tempfile import gettempdir
64+
6365
def _patch_resolve(self, strict=False):
6466
"""Add the argument strict to signature in Python>3,<3.6."""
6567
resolved = Path().old_resolve() / self
@@ -70,6 +72,24 @@ def _patch_resolve(self, strict=False):
7072

7173
Path.old_resolve = Path.resolve
7274
Path.resolve = _patch_resolve
75+
76+
if not hasattr(Path, 'write_text'):
77+
def _write_text(self, text):
78+
with open(str(self), 'w') as f:
79+
f.write(text)
80+
Path.write_text = _write_text
81+
82+
try:
83+
(Path(gettempdir()) / 'exist_ok_test').mkdir(exist_ok=True)
84+
except TypeError:
85+
def _mkdir(self, mode=0o777, parents=False, exist_ok=False):
86+
if not exist_ok and self.exists():
87+
raise FileExistsError(str(self))
88+
if not parents and not Path(str(self.parents)).exists():
89+
raise FileNotFoundError(str(self.parents))
90+
os.makedirs(str(self), mode=mode, exist_ok=exist_ok)
91+
Path.mkdir = _mkdir
92+
7393
except FileNotFoundError:
7494
pass
7595
except OSError:

nipype/utils/tests/test_filemanip.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,3 +596,21 @@ def test_pickle(tmp_path, save_versioning):
596596
savepkl(pickle_fname, testobj, versioning=save_versioning)
597597
outobj = loadpkl(pickle_fname)
598598
assert outobj == testobj
599+
600+
601+
def test_Path(tmpdir):
602+
tmp_path = Path(tmpdir.strpath)
603+
604+
assert hasattr(tmp_path, 'write_text')
605+
606+
(tmp_path / 'textfile').write_text('some text')
607+
608+
with pytest.raises(OSError):
609+
(tmp_path / 'no' / 'parents').mkdir(parents=False)
610+
611+
(tmp_path / 'no' / 'parents').mkdir(parents=True)
612+
613+
with pytest.raises(OSError):
614+
(tmp_path / 'no' / 'parents').mkdir(parents=False)
615+
616+
(tmp_path / 'no' / 'parents').mkdir(parents=True, exist_ok=True)

0 commit comments

Comments
 (0)