From 643b532537260d1adb7d50b80fe768996736ea52 Mon Sep 17 00:00:00 2001 From: George Tsomlektsis Date: Fri, 7 Feb 2020 19:40:48 +0200 Subject: [PATCH] Fixed mv not throwing error when the source path was invalid (#1351) * Fixed mv not throwing error when the source path was invalid * Fixed failing test * Fixed another lint error * Fix $PATH conflicts in .gitpod.Dockerfile (#1349) - Use the correct user for gitpod Dockerfile. - Remove unneeded packages (curl, rustc) from gitpod Dockerfile. * Added test to check for the error * Fixed linting error * Fixed mv not moving files on Windows. (#1342) Move files correctly in windows. * Fixed mv not throwing error when the source path was invalid * Fixed failing test * Fixed another lint error * Added test to check for the error * Fixed linting error * Changed error message * Typo and fixed test Co-authored-by: Sean Hellum --- src/shell/filesystem_shell.rs | 15 +++++++++++++++ tests/commands/mv.rs | 14 +++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/shell/filesystem_shell.rs b/src/shell/filesystem_shell.rs index d8903f86c0..de24807505 100644 --- a/src/shell/filesystem_shell.rs +++ b/src/shell/filesystem_shell.rs @@ -546,6 +546,13 @@ impl Shell for FilesystemShell { } }; + if sources.is_empty() { + return Err(ShellError::labeled_error( + "Invalid File or Pattern.", + "Invalid File or Pattern", + src.tag, + )); + } let destination_file_name = { match destination.file_name() { Some(name) => PathBuf::from(name), @@ -559,6 +566,14 @@ impl Shell for FilesystemShell { } }; + if sources.is_empty() { + return Err(ShellError::labeled_error( + "Move aborted. Not a valid destination", + "not a valid destination", + src.tag, + )); + } + if sources.len() == 1 { if let Ok(entry) = &sources[0] { let entry_file_name = match entry.file_name() { diff --git a/tests/commands/mv.rs b/tests/commands/mv.rs index a997bb68d5..d4fa014809 100644 --- a/tests/commands/mv.rs +++ b/tests/commands/mv.rs @@ -1,6 +1,6 @@ use nu_test_support::fs::{files_exist_at, Stub::EmptyFile}; -use nu_test_support::nu; use nu_test_support::playground::Playground; +use nu_test_support::{nu, nu_error}; #[test] fn moves_a_file() { @@ -218,3 +218,15 @@ fn moves_a_directory_with_files() { )); }) } + +#[test] +fn errors_if_source_doesnt_exist() { + Playground::setup("mv_test_10", |dirs, sandbox| { + sandbox.mkdir("test_folder"); + let actual = nu_error!( + cwd: dirs.root(), + "mv non-existing-file test_folder/" + ); + assert!(actual.contains("Invalid File or Pattern")); + }) +}