Skip to content

Commit

Permalink
dandi move: Fix resolution of nonexistent dests when in a subdirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Jul 15, 2022
1 parent e4dc875 commit de0c82d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
11 changes: 6 additions & 5 deletions dandi/move.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,17 @@ def calculate_moves(self, *srcs: str, dest: str, existing: str) -> list[Movement
Given a sequence of input source paths and a destination path, return a
sorted list of all assets that will be moved/renamed
"""
destpath, dest_is_dir = self.resolve(dest)
destobj: File | Folder | None
try:
destobj = self.get_path(dest, is_src=False)
except NotFoundError:
if dest.endswith("/") or len(srcs) > 1:
destobj = Folder(dest, [])
if dest_is_dir or len(srcs) > 1:
destobj = Folder(destpath, [])
elif len(srcs) == 1:
destobj = None
else:
destobj = File(AssetPath(dest))
destobj = File(destpath)
if isinstance(destobj, File) and len(srcs) > 1:
raise ValueError(
"Cannot take multiple source paths when destination is a file"
Expand All @@ -234,9 +235,9 @@ def calculate_moves(self, *srcs: str, dest: str, existing: str) -> list[Movement
for s in map(self.get_path, srcs):
if destobj is None:
if isinstance(s, File):
destobj = File(AssetPath(dest))
destobj = File(destpath)
else:
destobj = Folder(dest, [])
destobj = Folder(destpath, [])
if isinstance(s, File):
if isinstance(destobj, File):
pdest = AssetPath(destobj.path)
Expand Down
22 changes: 22 additions & 0 deletions dandi/tests/test_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,28 @@ def test_move_from_subdir(
)


@pytest.mark.parametrize("work_on", ["local", "remote", "both"])
def test_move_in_subdir(
monkeypatch: pytest.MonkeyPatch, moving_dandiset: SampleDandiset, work_on: str
) -> None:
starting_assets = list(moving_dandiset.dandiset.get_assets())
monkeypatch.chdir(moving_dandiset.dspath / "subdir1")
monkeypatch.setenv("DANDI_API_KEY", moving_dandiset.api.api_key)
move(
"apple.txt",
dest="macintosh.txt",
work_on=work_on,
dandi_instance=moving_dandiset.api.instance_id,
devel_debug=True,
)
check_assets(
moving_dandiset,
starting_assets,
work_on,
{"subdir1/apple.txt": "subdir1/macintosh.txt"},
)


@pytest.mark.parametrize("work_on", ["local", "remote", "both"])
def test_move_from_subdir_abspaths(
monkeypatch: pytest.MonkeyPatch, moving_dandiset: SampleDandiset, work_on: str
Expand Down

0 comments on commit de0c82d

Please sign in to comment.