Skip to content

Commit 72461cb

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 72461cb

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-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_helper_functions.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import pathlib
1111
import platform
1212
import stat
13+
import tempfile
1314
import unittest
1415

1516
import pandas as pd
1617

18+
import khiops.core.internals.filesystems as fs
1719
from khiops.core.dictionary import DictionaryDomain
1820
from khiops.core.helpers import _build_multi_table_dictionary_domain, visualize_report
1921
from khiops.sklearn import train_test_split_dataset
@@ -473,3 +475,20 @@ def test_visualize_report_fails_on_file_with_executable_permissions(self):
473475
7,4,16.66.64.13
474476
7,4,15.13.69.18
475477
""".lstrip()
478+
479+
480+
class LocalFileSystemTests(unittest.TestCase):
481+
482+
def setUp(self):
483+
self.current_dir = os.getcwd() # save the current directory
484+
485+
def test_copy_from_local_for_a_relative_target_path(self):
486+
"""Test fs.copy_from_local for the specific case where the target file has a relative path"""
487+
tmp_file_fd, tmp_file_path = tempfile.mkstemp(prefix='khiops-python-unit-test', dir='/tmp/')
488+
os.close(tmp_file_fd)
489+
os.chdir('/tmp') # folder location of the target file
490+
fs.copy_from_local('khiops-python-unit-test-target1.txt', tmp_file_path)
491+
fs.copy_from_local('./khiops-python-unit-test-target2.txt', tmp_file_path)
492+
493+
def tearDown(self):
494+
os.chdir(self.current_dir) # restore the current directory

0 commit comments

Comments
 (0)