Skip to content

Commit 46c46c3

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 6cc07ac commit 46c46c3

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-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: 23 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,27 @@ 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_for_a_relative_target_path(self):
3197+
"""Test fs.copy_from_local for the specific case
3198+
where the target file has a relative path
3199+
"""
3200+
tmp_file_fd, tmp_file_path = tempfile.mkstemp(
3201+
prefix="khiops-python-unit-test", dir="/tmp/"
3202+
)
3203+
os.close(tmp_file_fd)
3204+
os.chdir("/tmp") # folder location of the target file
3205+
fs.copy_from_local("khiops-python-unit-test-target1.txt", tmp_file_path)
3206+
fs.copy_from_local("./khiops-python-unit-test-target2.txt", tmp_file_path)
3207+
3208+
def tearDown(self):
3209+
os.chdir(self.current_dir) # restore the current directory
3210+
3211+
31893212
if __name__ == "__main__":
31903213
unittest.main()

0 commit comments

Comments
 (0)