Skip to content

Commit e423b7c

Browse files
author
Thierry RAMORASOAVINA
committed
Fix a border case on LocalFileSystem.copy_from_local where the target path is relative
Without this fix the library would try and fail to create a parent directory named ''
1 parent 18effc2 commit e423b7c

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import khiops
2424
import khiops.core as kh
25+
import khiops.core.internals.filesystems as fs
2526
from khiops.core import KhiopsRuntimeError
2627
from khiops.core.internals.io import KhiopsOutputWriter
2728
from khiops.core.internals.runner import KhiopsLocalRunner, KhiopsRunner
@@ -3186,5 +3187,40 @@ def test_raise_exception_on_error_case_without_a_message(self):
31863187
self.assertEqual(output_msg, expected_msg)
31873188

31883189

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

0 commit comments

Comments
 (0)