Skip to content

Commit 6aea158

Browse files
authored
Fix a border case on LocalFileSystem.copy_from_local where the target path is relative (#504)
Without this fix the library would try and fail to create a parent directory named ''
1 parent 18effc2 commit 6aea158

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

khiops/core/internals/filesystems.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ def remove(self):
477477

478478
def copy_from_local(self, local_path):
479479
directory = os.path.dirname(self.path)
480-
if not os.path.isdir(directory):
480+
if len(directory) > 0 and not os.path.isdir(directory):
481481
os.makedirs(directory)
482482
shutil.copy(local_path, self.path)
483483

tests/test_core.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import json
1111
import math
1212
import os
13+
import pathlib
1314
import shutil
1415
import tempfile
1516
import textwrap
@@ -22,6 +23,7 @@
2223

2324
import khiops
2425
import khiops.core as kh
26+
import khiops.core.internals.filesystems as fs
2527
from khiops.core import KhiopsRuntimeError
2628
from khiops.core.internals.io import KhiopsOutputWriter
2729
from khiops.core.internals.runner import KhiopsLocalRunner, KhiopsRunner
@@ -3186,5 +3188,40 @@ def test_raise_exception_on_error_case_without_a_message(self):
31863188
self.assertEqual(output_msg, expected_msg)
31873189

31883190

3191+
class LocalFileSystemTests(unittest.TestCase):
3192+
"""Test the methods of the `LocalFileSystem`"""
3193+
3194+
def setUp(self):
3195+
self.current_dir = os.getcwd() # save the current directory
3196+
3197+
def test_copy_from_local(self):
3198+
"""Ensure fs.copy_from_local behaves as expected"""
3199+
3200+
tmp_dir = "/tmp"
3201+
os.chdir(tmp_dir) # folder location of the target files
3202+
# target file names that will be created
3203+
target_file_name1 = "khiops-python-unit-test-target1.txt"
3204+
target_file_name2 = "khiops-python-unit-test-target2.txt"
3205+
with tempfile.NamedTemporaryFile(
3206+
prefix="khiops-python-unit-test-source", dir=tmp_dir
3207+
) as tmp_file_source:
3208+
try:
3209+
fs.copy_from_local(target_file_name1, tmp_file_source.name)
3210+
fs.copy_from_local(f"./{target_file_name2}", tmp_file_source.name)
3211+
fs.copy_from_local(
3212+
"./created_folder/khiops-python-unit-test-target3.txt",
3213+
tmp_file_source.name,
3214+
)
3215+
except FileNotFoundError as exc:
3216+
self.fail(f"'copy_from_local' failed unexpectedly : {str(exc)}")
3217+
finally:
3218+
pathlib.Path(target_file_name1).unlink(missing_ok=True)
3219+
pathlib.Path(target_file_name1).unlink(missing_ok=True)
3220+
shutil.rmtree("./created_folder/", ignore_errors=True)
3221+
3222+
def tearDown(self):
3223+
os.chdir(self.current_dir) # restore the current directory
3224+
3225+
31893226
if __name__ == "__main__":
31903227
unittest.main()

0 commit comments

Comments
 (0)