Improve tests and labeling in FilesystemShell (#1305)

Additional `ls` command tests and better FilesystemShell error and label messages.
This commit is contained in:
Jason Gedge 2020-02-01 03:34:34 -05:00 committed by GitHub
parent 9474fa1ea5
commit dcdfa2a866
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 96 additions and 126 deletions

View file

@ -129,7 +129,7 @@ impl Shell for FilesystemShell {
if paths.peek().is_none() { if paths.peek().is_none() {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Invalid File or Pattern", "Invalid File or Pattern",
"Invalid File or Pattern", "invalid file or pattern",
&p_tag, &p_tag,
)); ));
} }
@ -180,7 +180,7 @@ impl Shell for FilesystemShell {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Can not change to directory", "Can not change to directory",
"is not a directory", "is not a directory",
v.tag(), &v.tag,
)); ));
} }
@ -190,7 +190,7 @@ impl Shell for FilesystemShell {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Cannot change to directory", "Cannot change to directory",
"directory not found", "directory not found",
v.tag(), &v.tag,
)) ))
} }
} }
@ -229,8 +229,8 @@ impl Shell for FilesystemShell {
Ok(files) => files.collect(), Ok(files) => files.collect(),
Err(_) => { Err(_) => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Invalid pattern.", "Invalid pattern",
"Invalid pattern.", "invalid pattern",
src.tag, src.tag,
)) ))
} }
@ -288,7 +288,7 @@ impl Shell for FilesystemShell {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
e.to_string(), e.to_string(),
e.to_string(), e.to_string(),
name_tag, dst.tag,
)); ));
} }
Ok(o) => o, Ok(o) => o,
@ -316,6 +316,7 @@ impl Shell for FilesystemShell {
let sources = sources.paths_applying_with(strategy)?; let sources = sources.paths_applying_with(strategy)?;
let dst_tag = dst.tag;
for (ref src, ref dst) in sources { for (ref src, ref dst) in sources {
if src.is_dir() && !dst.exists() { if src.is_dir() && !dst.exists() {
match std::fs::create_dir_all(dst) { match std::fs::create_dir_all(dst) {
@ -323,7 +324,7 @@ impl Shell for FilesystemShell {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
e.to_string(), e.to_string(),
e.to_string(), e.to_string(),
name_tag, dst_tag,
)); ));
} }
Ok(o) => o, Ok(o) => o,
@ -349,8 +350,8 @@ impl Shell for FilesystemShell {
None => { None => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Copy aborted. Not a valid path", "Copy aborted. Not a valid path",
"Copy aborted. Not a valid path", "not a valid path",
name_tag, dst.tag,
)) ))
} }
} }
@ -360,7 +361,7 @@ impl Shell for FilesystemShell {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
e.to_string(), e.to_string(),
e.to_string(), e.to_string(),
name_tag, dst.tag,
)); ));
} }
Ok(o) => o, Ok(o) => o,
@ -388,6 +389,7 @@ impl Shell for FilesystemShell {
let sources = sources.paths_applying_with(strategy)?; let sources = sources.paths_applying_with(strategy)?;
let dst_tag = dst.tag;
for (ref src, ref dst) in sources { for (ref src, ref dst) in sources {
if src.is_dir() && !dst.exists() { if src.is_dir() && !dst.exists() {
match std::fs::create_dir_all(dst) { match std::fs::create_dir_all(dst) {
@ -395,7 +397,7 @@ impl Shell for FilesystemShell {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
e.to_string(), e.to_string(),
e.to_string(), e.to_string(),
name_tag, dst_tag,
)); ));
} }
Ok(o) => o, Ok(o) => o,
@ -425,7 +427,7 @@ impl Shell for FilesystemShell {
}) { }) {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Copy aborted (directories found). Recursive copying in patterns not supported yet (try copying the directory directly)", "Copy aborted (directories found). Recursive copying in patterns not supported yet (try copying the directory directly)",
"Copy aborted (directories found). Recursive copying in patterns not supported yet (try copying the directory directly)", "recursive copying in patterns not supported",
src.tag, src.tag,
)); ));
} }
@ -439,8 +441,8 @@ impl Shell for FilesystemShell {
None => { None => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Copy aborted. Not a valid path", "Copy aborted. Not a valid path",
"Copy aborted. Not a valid path", "not a valid path",
name_tag, dst.tag,
)) ))
} }
} }
@ -466,8 +468,8 @@ impl Shell for FilesystemShell {
None => { None => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Copy aborted. Not a valid destination", "Copy aborted. Not a valid destination",
"Copy aborted. Not a valid destination", "not a valid destination",
name_tag, dst.tag,
)) ))
} }
} }
@ -475,8 +477,8 @@ impl Shell for FilesystemShell {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
format!("Copy aborted. (Does {:?} exist?)", destination_file_name), format!("Copy aborted. (Does {:?} exist?)", destination_file_name),
format!("Copy aborted. (Does {:?} exist?)", destination_file_name), format!("copy aborted (does {:?} exist?)", destination_file_name),
dst.tag(), dst.tag,
)); ));
} }
@ -538,7 +540,7 @@ impl Shell for FilesystemShell {
Err(_) => { Err(_) => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Invalid pattern.", "Invalid pattern.",
"Invalid pattern.", "invalid pattern",
src.tag, src.tag,
)) ))
} }
@ -550,8 +552,8 @@ impl Shell for FilesystemShell {
None => { None => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Rename aborted. Not a valid destination", "Rename aborted. Not a valid destination",
"Rename aborted. Not a valid destination", "not a valid destination",
dst.tag(), dst.tag,
)) ))
} }
} }
@ -564,8 +566,8 @@ impl Shell for FilesystemShell {
None => { None => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Rename aborted. Not a valid entry name", "Rename aborted. Not a valid entry name",
"Rename aborted. Not a valid entry name", "not a valid entry name",
name_tag, src.tag,
)) ))
} }
}; };
@ -576,8 +578,8 @@ impl Shell for FilesystemShell {
Err(e) => { Err(e) => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
format!("Rename aborted. {:}", e.to_string()), format!("Rename aborted. {:}", e.to_string()),
format!("Rename aborted. {:}", e.to_string()), e.to_string(),
name_tag, dst.tag,
)) ))
} }
}; };
@ -597,12 +599,7 @@ impl Shell for FilesystemShell {
destination_file_name, destination_file_name,
e.to_string(), e.to_string(),
), ),
format!(
"Rename {:?} to {:?} aborted. {:}",
entry_file_name,
destination_file_name,
e.to_string(), e.to_string(),
),
name_tag, name_tag,
)); ));
} }
@ -620,12 +617,7 @@ impl Shell for FilesystemShell {
destination_file_name, destination_file_name,
e.to_string(), e.to_string(),
), ),
format!(
"Rename {:?} to {:?} aborted. {:}",
entry_file_name,
destination_file_name,
e.to_string(), e.to_string(),
),
name_tag, name_tag,
)); ));
} }
@ -638,12 +630,7 @@ impl Shell for FilesystemShell {
destination_file_name, destination_file_name,
e.to_string(), e.to_string(),
), ),
format!(
"Rename {:?} to {:?} aborted. {:}",
entry_file_name,
destination_file_name,
e.to_string(), e.to_string(),
),
name_tag, name_tag,
)); ));
} }
@ -663,12 +650,7 @@ impl Shell for FilesystemShell {
destination_file_name, destination_file_name,
e.to_string(), e.to_string(),
), ),
format!(
"Rename {:?} to {:?} aborted. {:}",
entry_file_name,
destination_file_name,
e.to_string(), e.to_string(),
),
name_tag, name_tag,
)); ));
} }
@ -685,12 +667,7 @@ impl Shell for FilesystemShell {
destination_file_name, destination_file_name,
e.to_string(), e.to_string(),
), ),
format!(
"Rename {:?} to {:?} aborted. {:}",
entry_file_name,
destination_file_name,
e.to_string(), e.to_string(),
),
name_tag, name_tag,
)); ));
} }
@ -737,12 +714,7 @@ impl Shell for FilesystemShell {
destination_file_name, destination_file_name,
e.to_string(), e.to_string(),
), ),
format!(
"Rename {:?} to {:?} aborted. {:}",
entry_file_name,
destination_file_name,
e.to_string(), e.to_string(),
),
name_tag, name_tag,
)); ));
} }
@ -761,12 +733,7 @@ impl Shell for FilesystemShell {
destination_file_name, destination_file_name,
e.to_string(), e.to_string(),
), ),
format!(
"Rename {:?} to {:?} aborted. {:}",
src,
destination_file_name,
e.to_string(), e.to_string(),
),
name_tag, name_tag,
)); ));
} }
@ -779,12 +746,7 @@ impl Shell for FilesystemShell {
destination_file_name, destination_file_name,
e.to_string(), e.to_string(),
), ),
format!(
"Rename {:?} to {:?} aborted. {:}",
entry_file_name,
destination_file_name,
e.to_string(), e.to_string(),
),
name_tag, name_tag,
)); ));
} }
@ -802,12 +764,7 @@ impl Shell for FilesystemShell {
destination_file_name, destination_file_name,
e.to_string(), e.to_string(),
), ),
format!(
"Rename {:?} to {:?} aborted. {:}",
entry_file_name,
destination_file_name,
e.to_string(), e.to_string(),
),
name_tag, name_tag,
)); ));
} }
@ -824,7 +781,7 @@ impl Shell for FilesystemShell {
if !sources.iter().all(is_file) { if !sources.iter().all(is_file) {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Rename aborted (directories found). Renaming in patterns not supported yet (try moving the directory directly)", "Rename aborted (directories found). Renaming in patterns not supported yet (try moving the directory directly)",
"Rename aborted (directories found). Renaming in patterns not supported yet (try moving the directory directly)", "renaming in patterns not supported yet (try moving the directory directly)",
src.tag, src.tag,
)); ));
} }
@ -836,8 +793,8 @@ impl Shell for FilesystemShell {
None => { None => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Rename aborted. Not a valid entry name", "Rename aborted. Not a valid entry name",
"Rename aborted. Not a valid entry name", "not a valid entry name",
name_tag, src.tag,
)) ))
} }
}; };
@ -857,12 +814,7 @@ impl Shell for FilesystemShell {
destination_file_name, destination_file_name,
e.to_string(), e.to_string(),
), ),
format!(
"Rename {:?} to {:?} aborted. {:}",
entry_file_name,
destination_file_name,
e.to_string(), e.to_string(),
),
name_tag, name_tag,
)); ));
} }
@ -880,12 +832,7 @@ impl Shell for FilesystemShell {
destination_file_name, destination_file_name,
e.to_string(), e.to_string(),
), ),
format!(
"Rename {:?} to {:?} aborted. {:}",
entry_file_name,
destination_file_name,
e.to_string(), e.to_string(),
),
name_tag, name_tag,
)); ));
} }
@ -898,12 +845,7 @@ impl Shell for FilesystemShell {
destination_file_name, destination_file_name,
e.to_string(), e.to_string(),
), ),
format!(
"Remove {:?} to {:?} aborted. {:}",
entry_file_name,
destination_file_name,
e.to_string(), e.to_string(),
),
name_tag, name_tag,
)); ));
} }
@ -917,8 +859,8 @@ impl Shell for FilesystemShell {
} else { } else {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
format!("Rename aborted. (Does {:?} exist?)", destination_file_name), format!("Rename aborted. (Does {:?} exist?)", destination_file_name),
format!("Rename aborted. (Does {:?} exist?)", destination_file_name), format!("rename aborted (does {:?} exist?)", destination_file_name),
dst.tag(), dst.tag,
)); ));
} }
@ -940,8 +882,8 @@ impl Shell for FilesystemShell {
if target.item.to_str() == Some(".") || target.item.to_str() == Some("..") { if target.item.to_str() == Some(".") || target.item.to_str() == Some("..") {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Remove aborted. \".\" or \"..\" may not be removed.", "Remove aborted. \".\" or \"..\" may not be removed.",
"Remove aborted. \".\" or \"..\" may not be removed.", "\".\" or \"..\" may not be removed",
target.tag(), target.tag,
)); ));
} }
@ -955,8 +897,8 @@ impl Shell for FilesystemShell {
if files.is_empty() { if files.is_empty() {
Err(ShellError::labeled_error( Err(ShellError::labeled_error(
"Remove aborted. Not a valid path", "Remove aborted. Not a valid path",
"Remove aborted. Not a valid path", "not a valid path",
&name_tag, target.tag,
)) ))
} else { } else {
let stream = async_stream! { let stream = async_stream! {
@ -978,11 +920,11 @@ impl Shell for FilesystemShell {
"Could not delete {:}", "Could not delete {:}",
f.to_string_lossy() f.to_string_lossy()
); );
let label = format!("Error {:?}", e); let label = format!("{:?}", e);
yield Err(ShellError::labeled_error( yield Err(ShellError::labeled_error(
msg, msg,
label, label,
&name_tag, &target.tag,
)) ))
}, },
Ok(()) => { Ok(()) => {
@ -1002,11 +944,10 @@ impl Shell for FilesystemShell {
"Could not delete {:}", "Could not delete {:}",
f.to_string_lossy() f.to_string_lossy()
); );
let label = format!("Error {:}", e.to_string());
yield Err(ShellError::labeled_error( yield Err(ShellError::labeled_error(
msg, msg,
label, e.to_string(),
&name_tag, &target.tag,
)) ))
}, },
Ok(()) => { Ok(()) => {
@ -1023,30 +964,27 @@ impl Shell for FilesystemShell {
"Cannot remove {:}. try --recursive", "Cannot remove {:}. try --recursive",
f.to_string_lossy() f.to_string_lossy()
); );
let label = format!("Cannot remove non-empty directory");
yield Err(ShellError::labeled_error( yield Err(ShellError::labeled_error(
msg, msg,
label, "cannot remove non-empty directory",
&name_tag, &target.tag,
)) ))
} else { } else {
let msg = format!("Invalid file: {:}", f.to_string_lossy()); let msg = format!("Invalid file: {:}", f.to_string_lossy());
let label = format!("Invalid file");
yield Err(ShellError::labeled_error( yield Err(ShellError::labeled_error(
msg, msg,
label, "invalid file",
&name_tag, &target.tag,
)) ))
} }
} }
} }
Err(e) => { Err(e) => {
let msg = format!("Could not remove {:}", path.to_string_lossy()); let msg = format!("Could not remove {:}", path.to_string_lossy());
let label = format!("Error {:}", e.to_string());
yield Err(ShellError::labeled_error( yield Err(ShellError::labeled_error(
msg, msg,
label, e.to_string(),
&name_tag, &target.tag,
)) ))
}, },
} }
@ -1057,7 +995,7 @@ impl Shell for FilesystemShell {
} }
Err(e) => Err(ShellError::labeled_error( Err(e) => Err(ShellError::labeled_error(
format!("Remove aborted. {:}", e.to_string()), format!("Remove aborted. {:}", e.to_string()),
format!("Remove aborted. {:}", e.to_string()), e.to_string(),
&name_tag, &name_tag,
)), )),
} }

View file

@ -1,6 +1,6 @@
use nu_test_support::fs::Stub::EmptyFile; use nu_test_support::fs::Stub::EmptyFile;
use nu_test_support::playground::Playground; use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline}; use nu_test_support::{nu, nu_error, pipeline};
#[test] #[test]
fn lists_regular_files() { fn lists_regular_files() {
@ -99,3 +99,35 @@ fn lists_all_files_in_directories_from_stream() {
assert_eq!(actual, "4"); assert_eq!(actual, "4");
}) })
} }
#[test]
fn does_not_fail_if_glob_matches_empty_directory() {
Playground::setup("ls_test_5", |dirs, sandbox| {
sandbox.within("dir_a");
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
ls dir_a
| count
| echo $it
"#
));
assert_eq!(actual, "0");
})
}
#[test]
fn fails_when_glob_doesnt_match() {
Playground::setup("ls_test_5", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("root1.txt"), EmptyFile("root2.txt")]);
let actual = nu_error!(
cwd: dirs.test(),
"ls root3*"
);
assert!(actual.contains("invalid file or pattern"));
})
}

View file

@ -132,7 +132,7 @@ fn errors_if_attempting_to_delete_a_directory_with_content_without_recursive_fla
); );
assert!(dirs.test().exists()); assert!(dirs.test().exists());
assert!(actual.contains("Cannot remove non-empty directory")); assert!(actual.contains("cannot remove non-empty directory"));
}) })
} }