Skip to content

Commit 4fd5b72

Browse files
committed
tools/mpremote: Support trailing slash on dest for non-recursive copy.
This fixes a regression in db59e55: prior to that commit `mpremote` supported trailing slashes on the destination of a normal (non-recursive) copy. Add back support for that, with the semantics that a trailing slash requires the destination to be an existing directory. Also add a test for this. Signed-off-by: Damien George <[email protected]>
1 parent 3b6024a commit 4fd5b72

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

tools/mpremote/mpremote/commands.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,15 @@ def _remote_path_basename(a):
129129

130130
def do_filesystem_cp(state, src, dest, multiple, check_hash=False):
131131
if dest.startswith(":"):
132-
dest_exists = state.transport.fs_exists(dest[1:])
133-
dest_isdir = dest_exists and state.transport.fs_isdir(dest[1:])
132+
dest_no_slash = dest.rstrip("/" + os.path.sep + (os.path.altsep or ""))
133+
dest_exists = state.transport.fs_exists(dest_no_slash[1:])
134+
dest_isdir = dest_exists and state.transport.fs_isdir(dest_no_slash[1:])
135+
136+
# A trailing / on dest forces it to be a directory.
137+
if dest != dest_no_slash:
138+
if not dest_isdir:
139+
raise CommandError("cp: destination is not a directory")
140+
dest = dest_no_slash
134141
else:
135142
dest_exists = os.path.exists(dest)
136143
dest_isdir = dest_exists and os.path.isdir(dest)

tools/mpremote/tests/test_filesystem.sh

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ echo -----
7171
$MPREMOTE resume cp -f "${TMP}/a.py" :aaa
7272
$MPREMOTE resume cat :aaa/a.py
7373

74+
# Test cp where the destination has a trailing /.
75+
echo -----
76+
$MPREMOTE resume cp "${TMP}/a.py" :aaa/
77+
$MPREMOTE resume cp "${TMP}/a.py" :aaa/a.py/ || echo "expect error"
78+
7479
echo -----
7580
$MPREMOTE resume rm :b.py c.py
7681
$MPREMOTE resume ls

tools/mpremote/tests/test_filesystem.sh.exp

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ cp ${TMP}/a.py :aaa
4242
print("Hello")
4343
print("World")
4444
-----
45+
cp ${TMP}/a.py :aaa/
46+
Up to date: aaa/a.py
47+
cp ${TMP}/a.py :aaa/a.py/
48+
mpremote: cp: destination is not a directory
49+
expect error
50+
-----
4551
rm :b.py
4652
rm :c.py
4753
ls :

0 commit comments

Comments
 (0)