From 271fda7c913b2adb76974b90522ae65e9655b4f6 Mon Sep 17 00:00:00 2001 From: nibon7 Date: Wed, 10 Aug 2022 19:51:11 +0800 Subject: [PATCH] Return error when moving a source directory to a target directory which contains a subdirectory with the same name as the source (#6284) Fixes #6275 Signed-off-by: nibon7 --- crates/nu-command/src/filesystem/mv.rs | 15 +++++++++++++++ crates/nu-command/tests/commands/move_/mv.rs | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/crates/nu-command/src/filesystem/mv.rs b/crates/nu-command/src/filesystem/mv.rs index c31db01f9e..b0ef89d699 100644 --- a/crates/nu-command/src/filesystem/mv.rs +++ b/crates/nu-command/src/filesystem/mv.rs @@ -120,6 +120,21 @@ impl Command for Mv { )); } + if source.is_dir() && destination.is_dir() { + if let Some(name) = source.file_name() { + let dst = destination.join(name); + if dst.is_dir() { + return Err(ShellError::GenericError( + format!("Can't move {:?} to {:?}", source, dst), + "Directory not empty".into(), + Some(spanned_destination.span), + None, + Vec::new(), + )); + } + } + } + let some_if_source_is_destination = sources .iter() .find(|f| matches!(f, Ok(f) if destination.starts_with(f))); diff --git a/crates/nu-command/tests/commands/move_/mv.rs b/crates/nu-command/tests/commands/move_/mv.rs index c08ccbd472..6764b9056f 100644 --- a/crates/nu-command/tests/commands/move_/mv.rs +++ b/crates/nu-command/tests/commands/move_/mv.rs @@ -375,3 +375,21 @@ fn mv_ignores_ansi() { assert_eq!(actual.out, "success.txt"); }) } + +#[test] +fn mv_directory_with_same_name() { + Playground::setup("mv_test_directory_with_same_name", |_dirs, sandbox| { + sandbox.mkdir("testdir"); + sandbox.mkdir("testdir/testdir"); + + let cwd = sandbox.cwd().join("testdir"); + let actual = nu!( + cwd, + r#" + mv testdir .. + "# + ); + + assert!(actual.err.contains("Directory not empty")); + }) +}