Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to copy file to dir #13

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion anypathlib/anypath.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,12 @@ def copy(self, target: Optional[AnyPathLikeType] = None, force_overwrite: bool =
if target is None:
valid_target = self.__get_local_cache_path()
else:
valid_target = AnyPath(target)
input_target = AnyPath(target)
# if source is a file and target is either an existing dir copy the file to the target dir
if self.is_file() and input_target.is_dir():
valid_target = input_target / self.name
else:
valid_target = input_target
if valid_target.is_local:
self.__get_local_path(target_path=Path(valid_target.base_path), force_overwrite=force_overwrite,
verbose=verbose)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_copy_file_to_dir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest

from anypathlib import PathType, AnyPath
from tests.tests_urls import PATH_TYPE_TO_HANDLER
from fixtures_anypath import temp_local_dir, temp_dir_with_files, clean_remote_dir


@pytest.mark.usefixtures("temp_dir_with_files", "temp_local_dir", "clean_remote_dir")
@pytest.mark.parametrize("path_type", [PathType.azure, PathType.s3, PathType.local])
def test_copy_file_to_dir(path_type: PathType, temp_dir_with_files, temp_local_dir, clean_remote_dir):
path_handler = PATH_TYPE_TO_HANDLER[path_type]
local_dir_path, local_dir_files = temp_dir_with_files

remote_dir = clean_remote_dir
path_handler.upload_directory(local_dir=local_dir_path, target_url=remote_dir, verbose=False)
source_files = AnyPath(remote_dir).iterdir()
remote_file = source_files[0]
local_downloaded_file_path = AnyPath(remote_file).copy(target=temp_local_dir, force_overwrite=True)
assert local_downloaded_file_path.exists()
assert local_downloaded_file_path.name == remote_file.name
assert local_downloaded_file_path.is_file()
assert local_downloaded_file_path.parent.base_path == AnyPath(temp_local_dir).base_path
Loading