nushell/crates/nu-cli/tests/commands/rm.rs

162 lines
4.2 KiB
Rust
Raw Normal View History

2019-12-17 19:54:39 +01:00
use nu_test_support::fs::{files_exist_at, Stub::EmptyFile};
use nu_test_support::playground::Playground;
use nu_test_support::{nu, nu_error};
2019-08-28 19:48:52 +02:00
#[test]
fn removes_a_file() {
2019-08-29 02:32:42 +02:00
Playground::setup("rm_test_1", |dirs, sandbox| {
2019-09-11 16:36:50 +02:00
sandbox.with_files(vec![EmptyFile("i_will_be_deleted.txt")]);
2019-08-28 19:48:52 +02:00
2019-08-29 08:31:56 +02:00
nu!(
cwd: dirs.root(),
"rm rm_test_1/i_will_be_deleted.txt"
);
2019-08-28 19:48:52 +02:00
let path = dirs.test().join("i_will_be_deleted.txt");
2019-08-29 08:31:56 +02:00
assert!(!path.exists());
2019-08-28 19:48:52 +02:00
})
}
#[test]
fn removes_files_with_wildcard() {
2019-08-29 02:32:42 +02:00
Playground::setup("rm_test_2", |dirs, sandbox| {
sandbox
2019-08-28 19:48:52 +02:00
.within("src")
.with_files(vec![
EmptyFile("cli.rs"),
EmptyFile("lib.rs"),
2019-09-11 16:36:50 +02:00
EmptyFile("prelude.rs"),
2019-08-28 19:48:52 +02:00
])
.within("src/parser")
.with_files(vec![EmptyFile("parse.rs"), EmptyFile("parser.rs")])
.within("src/parser/parse")
.with_files(vec![EmptyFile("token_tree.rs")])
.within("src/parser/hir")
.with_files(vec![
EmptyFile("baseline_parse.rs"),
2019-09-11 16:36:50 +02:00
EmptyFile("baseline_parse_tokens.rs"),
]);
2019-08-28 19:48:52 +02:00
2019-08-29 08:31:56 +02:00
nu!(
cwd: dirs.test(),
r#"rm "src/*/*/*.rs""#
);
2019-08-28 19:48:52 +02:00
assert!(!files_exist_at(
2019-08-28 19:48:52 +02:00
vec![
"src/parser/parse/token_tree.rs",
"src/parser/hir/baseline_parse.rs",
"src/parser/hir/baseline_parse_tokens.rs"
],
dirs.test()
));
assert_eq!(
Playground::glob_vec(&format!("{}/src/*/*/*.rs", dirs.test().display())),
2019-08-29 02:32:42 +02:00
Vec::<std::path::PathBuf>::new()
2019-08-28 19:48:52 +02:00
);
})
}
#[test]
fn removes_deeply_nested_directories_with_wildcard_and_recursive_flag() {
2019-08-29 02:32:42 +02:00
Playground::setup("rm_test_3", |dirs, sandbox| {
sandbox
2019-08-28 19:48:52 +02:00
.within("src")
.with_files(vec![
EmptyFile("cli.rs"),
EmptyFile("lib.rs"),
2019-09-11 16:36:50 +02:00
EmptyFile("prelude.rs"),
2019-08-28 19:48:52 +02:00
])
.within("src/parser")
.with_files(vec![EmptyFile("parse.rs"), EmptyFile("parser.rs")])
.within("src/parser/parse")
.with_files(vec![EmptyFile("token_tree.rs")])
.within("src/parser/hir")
.with_files(vec![
EmptyFile("baseline_parse.rs"),
2019-09-11 16:36:50 +02:00
EmptyFile("baseline_parse_tokens.rs"),
]);
2019-08-28 19:48:52 +02:00
2019-08-29 08:31:56 +02:00
nu!(
cwd: dirs.test(),
"rm -r src/*"
2019-08-29 08:31:56 +02:00
);
2019-08-28 19:48:52 +02:00
assert!(!files_exist_at(
2019-08-28 19:48:52 +02:00
vec!["src/parser/parse", "src/parser/hir"],
dirs.test()
));
})
}
#[test]
fn removes_directory_contents_without_recursive_flag_if_empty() {
2019-08-29 02:32:42 +02:00
Playground::setup("rm_test_4", |dirs, _| {
2019-08-29 08:31:56 +02:00
nu!(
cwd: dirs.root(),
"rm rm_test_4"
);
2019-08-28 19:48:52 +02:00
2019-08-29 08:31:56 +02:00
assert!(!dirs.test().exists());
2019-08-28 19:48:52 +02:00
})
}
#[test]
fn removes_directory_contents_with_recursive_flag() {
2019-08-29 02:32:42 +02:00
Playground::setup("rm_test_5", |dirs, sandbox| {
2019-09-11 16:36:50 +02:00
sandbox.with_files(vec![
EmptyFile("yehuda.txt"),
EmptyFile("jonathan.txt"),
EmptyFile("andres.txt"),
2019-08-29 02:32:42 +02:00
]);
2019-08-29 08:31:56 +02:00
nu!(
cwd: dirs.root(),
"rm rm_test_5 --recursive"
);
2019-08-29 02:32:42 +02:00
2019-08-29 08:31:56 +02:00
assert!(!dirs.test().exists());
2019-08-29 02:32:42 +02:00
})
2019-08-28 19:48:52 +02:00
}
#[test]
fn errors_if_attempting_to_delete_a_directory_with_content_without_recursive_flag() {
2019-08-29 02:32:42 +02:00
Playground::setup("rm_test_6", |dirs, sandbox| {
2019-09-11 16:36:50 +02:00
sandbox.with_files(vec![EmptyFile("some_empty_file.txt")]);
2019-08-29 02:32:42 +02:00
let actual = nu_error!(
2019-08-29 08:31:56 +02:00
cwd: dirs.root(),
2019-08-29 02:32:42 +02:00
"rm rm_test_6"
);
2019-08-29 08:31:56 +02:00
assert!(dirs.test().exists());
assert!(actual.contains("cannot remove non-empty directory"));
2019-08-29 02:32:42 +02:00
})
2019-08-28 19:48:52 +02:00
}
#[test]
fn errors_if_attempting_to_delete_single_dot_as_argument() {
2019-08-29 02:32:42 +02:00
Playground::setup("rm_test_7", |dirs, _| {
2019-08-29 08:31:56 +02:00
let actual = nu_error!(
cwd: dirs.root(),
"rm ."
);
2019-08-29 02:32:42 +02:00
assert!(actual.contains("may not be removed"));
})
2019-08-28 19:48:52 +02:00
}
#[test]
fn errors_if_attempting_to_delete_two_dot_as_argument() {
2019-08-29 02:32:42 +02:00
Playground::setup("rm_test_8", |dirs, _| {
2019-08-29 08:31:56 +02:00
let actual = nu_error!(
cwd: dirs.root(),
"rm .."
);
2019-08-29 02:32:42 +02:00
assert!(actual.contains("may not be removed"));
})
2019-08-28 19:48:52 +02:00
}