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

434 lines
12 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::nu;
2019-12-17 19:54:39 +01:00
use nu_test_support::playground::Playground;
use std::fs;
use std::path::PathBuf;
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("jttxt"),
2019-09-11 16:36:50 +02:00
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")]);
let actual = nu!(
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.err.contains("cannot remove non-empty directory"));
2019-08-29 02:32:42 +02:00
})
2019-08-28 19:48:52 +02:00
}
check if is homedir first when rm (#9117) I don't want to rm my home again.. sadly.. # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> check if there is unique argument # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> user will not easily rm their home # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --> --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-05-25 16:45:50 +02:00
#[test]
fn errors_if_attempting_to_delete_home() {
Playground::setup("rm_test_8", |dirs, _| {
let actual = nu!(
cwd: dirs.root(),
remove let-env, focus on mutating $env (#9574) # Description For years, Nushell has used `let-env` to set a single environment variable. As our work on scoping continued, we refined what it meant for a variable to be in scope using `let` but never updated how `let-env` would work. Instead, `let-env` confusingly created mutations to the command's copy of `$env`. So, to help fix the mental model and point people to the right way of thinking about what changing the environment means, this PR removes `let-env` to encourage people to think of it as updating the command's environment variable via mutation. Before: ``` let-env FOO = "BAR" ``` Now: ``` $env.FOO = "BAR" ``` It's also a good reminder that the environment owned by the command is in the `$env` variable rather than global like it is in other shells. # User-Facing Changes BREAKING CHANGE BREAKING CHANGE This completely removes `let-env FOO = "BAR"` so that we can focus on `$env.FOO = "BAR"`. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After / Before Submitting integration scripts to update: - :heavy_check_mark: [starship](https://github.com/starship/starship/blob/master/src/init/starship.nu) - :heavy_check_mark: [virtualenv](https://github.com/pypa/virtualenv/blob/main/src/virtualenv/activation/nushell/activate.nu) - :heavy_check_mark: [atuin](https://github.com/ellie/atuin/blob/main/atuin/src/shell/atuin.nu) (PR: https://github.com/ellie/atuin/pull/1080) - :x: [zoxide](https://github.com/ajeetdsouza/zoxide/blob/main/templates/nushell.txt) (PR: https://github.com/ajeetdsouza/zoxide/pull/587) - :heavy_check_mark: [oh-my-posh](https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/src/shell/scripts/omp.nu) (pr: https://github.com/JanDeDobbeleer/oh-my-posh/pull/4011)
2023-06-30 21:57:51 +02:00
"$env.HOME = myhome ; rm -rf ~"
check if is homedir first when rm (#9117) I don't want to rm my home again.. sadly.. # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> check if there is unique argument # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> user will not easily rm their home # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --> --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-05-25 16:45:50 +02:00
);
assert!(actual.err.contains("please use -I or -i"));
})
}
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, _| {
let actual = nu!(
2019-08-29 08:31:56 +02:00
cwd: dirs.root(),
"rm ."
);
2019-08-29 02:32:42 +02:00
assert!(actual.err.contains("cannot remove any parent directory"));
2019-08-29 02:32:42 +02:00
})
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, _| {
let actual = nu!(
2019-08-29 08:31:56 +02:00
cwd: dirs.root(),
"rm .."
);
2019-08-29 02:32:42 +02:00
assert!(actual.err.contains("cannot"));
2019-08-29 02:32:42 +02:00
})
2019-08-28 19:48:52 +02:00
}
#[test]
fn removes_multiple_directories() {
Playground::setup("rm_test_9", |dirs, sandbox| {
sandbox
.within("src")
.with_files(vec![EmptyFile("a.rs"), EmptyFile("b.rs")])
.within("src/cli")
.with_files(vec![EmptyFile("c.rs"), EmptyFile("d.rs")])
.within("test")
.with_files(vec![EmptyFile("a_test.rs"), EmptyFile("b_test.rs")]);
nu!(
cwd: dirs.test(),
"rm src test --recursive"
);
assert_eq!(
Playground::glob_vec(&format!("{}/*", dirs.test().display())),
Vec::<std::path::PathBuf>::new()
);
})
}
#[test]
fn removes_multiple_files() {
Playground::setup("rm_test_10", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("yehuda.txt"),
EmptyFile("jttxt"),
EmptyFile("andres.txt"),
]);
nu!(
cwd: dirs.test(),
"rm yehuda.txt jttxt andres.txt"
);
assert_eq!(
Playground::glob_vec(&format!("{}/*", dirs.test().display())),
Vec::<std::path::PathBuf>::new()
);
})
}
#[test]
fn removes_multiple_files_with_asterisks() {
Playground::setup("rm_test_11", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("yehuda.txt"),
EmptyFile("jt.txt"),
EmptyFile("andres.toml"),
]);
nu!(
cwd: dirs.test(),
"rm *.txt *.toml"
);
assert_eq!(
Playground::glob_vec(&format!("{}/*", dirs.test().display())),
Vec::<std::path::PathBuf>::new()
);
})
}
#[test]
fn allows_doubly_specified_file() {
Playground::setup("rm_test_12", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("yehuda.txt"), EmptyFile("jt.toml")]);
let actual = nu!(
cwd: dirs.test(),
"rm *.txt yehuda* *.toml"
);
assert_eq!(
Playground::glob_vec(&format!("{}/*", dirs.test().display())),
Vec::<std::path::PathBuf>::new()
);
assert!(!actual.out.contains("error"))
})
}
#[test]
fn remove_files_from_two_parents_up_using_multiple_dots_and_glob() {
Playground::setup("rm_test_13", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("yehuda.txt"),
EmptyFile("jt.txt"),
EmptyFile("kevin.txt"),
]);
sandbox.within("foo").mkdir("bar");
nu!(
cwd: dirs.test().join("foo/bar"),
"rm .../*.txt"
);
assert!(!files_exist_at(
vec!["yehuda.txt", "jttxt", "kevin.txt"],
dirs.test()
));
})
}
#[test]
fn no_errors_if_attempting_to_delete_non_existent_file_with_f_flag() {
Playground::setup("rm_test_14", |dirs, _| {
let actual = nu!(
cwd: dirs.root(),
"rm -f non_existent_file.txt"
);
assert!(!actual.err.contains("no valid path"));
})
}
#[test]
fn rm_wildcard_keeps_dotfiles() {
Playground::setup("rm_test_15", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("foo"), EmptyFile(".bar")]);
nu!(
cwd: dirs.test(),
r#"rm *"#
);
assert!(!files_exist_at(vec!["foo"], dirs.test()));
assert!(files_exist_at(vec![".bar"], dirs.test()));
})
}
#[test]
fn rm_wildcard_leading_dot_deletes_dotfiles() {
Playground::setup("rm_test_16", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("foo"), EmptyFile(".bar")]);
nu!(
cwd: dirs.test(),
r#"rm .*"#
);
assert!(files_exist_at(vec!["foo"], dirs.test()));
assert!(!files_exist_at(vec![".bar"], dirs.test()));
})
}
#[test]
fn removes_files_with_case_sensitive_glob_matches_by_default() {
Playground::setup("glob_test", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("A0"), EmptyFile("a1")]);
nu!(
cwd: dirs.root(),
"rm glob_test/A*"
);
let deleted_path = dirs.test().join("A0");
let skipped_path = dirs.test().join("a1");
assert!(!deleted_path.exists());
assert!(skipped_path.exists());
})
}
#[test]
fn remove_ignores_ansi() {
Playground::setup("rm_test_ansi", |_dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("test.txt")]);
let actual = nu!(
cwd: sandbox.cwd(),
"ls | find test | get name | rm $in.0; ls | is-empty",
);
assert_eq!(actual.out, "true");
});
}
#[test]
fn removes_symlink() {
let symlink_target = "symlink_target";
let symlink = "symlink";
Playground::setup("rm_test_symlink", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile(symlink_target)]);
#[cfg(not(windows))]
std::os::unix::fs::symlink(dirs.test().join(symlink_target), dirs.test().join(symlink))
.unwrap();
#[cfg(windows)]
std::os::windows::fs::symlink_file(
dirs.test().join(symlink_target),
dirs.test().join(symlink),
)
.unwrap();
let _ = nu!(cwd: sandbox.cwd(), "rm symlink");
assert!(!dirs.test().join(symlink).exists());
});
}
struct Cleanup<'a> {
dir_to_clean: &'a PathBuf,
}
fn set_dir_read_only(directory: &PathBuf, read_only: bool) {
let mut permissions = fs::metadata(directory).unwrap().permissions();
permissions.set_readonly(read_only);
fs::set_permissions(directory, permissions).expect("failed to set directory permissions");
}
impl<'a> Drop for Cleanup<'a> {
/// Restores write permissions to the given directory so that the Playground can be successfully
/// cleaned up.
fn drop(&mut self) {
set_dir_read_only(self.dir_to_clean, false);
}
}
#[test]
// This test is only about verifying file names are included in rm error messages. It is easier
// to only have this work on non-windows systems (i.e., unix-like) than to try to get the
// permissions to work on all platforms.
#[cfg(not(windows))]
fn rm_prints_filenames_on_error() {
Playground::setup("rm_prints_filenames_on_error", |dirs, sandbox| {
let file_names = vec!["test1.txt", "test2.txt"];
let with_files = file_names
.iter()
.map(|file_name| EmptyFile(file_name))
.collect();
sandbox.with_files(with_files);
let test_dir = dirs.test();
set_dir_read_only(test_dir, true);
let _cleanup = Cleanup {
Apply nightly clippy fixes (#9482) <!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description This PR fixes the following nightly clippy warnings. ``` warning: you should consider adding a `Default` implementation for `HjsonFormatter<'a>` --> crates/nu-json/src/ser.rs:700:5 | 700 | / pub fn new() -> Self { 701 | | HjsonFormatter::with_indent(b" ") 702 | | } | |_____^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default = note: `#[warn(clippy::new_without_default)]` on by default help: try adding this | 698 + impl<'a> Default for HjsonFormatter<'a> { 699 + fn default() -> Self { 700 + Self::new() 701 + } 702 + } | warning: `nu-json` (lib) generated 1 warning warning: private item shadows public glob re-export --> crates/nu-command/src/strings/mod.rs:8:1 | 8 | mod str_; | ^^^^^^^^^ | note: the name `str_` in the type namespace is supposed to be publicly re-exported here --> crates/nu-command/src/strings/mod.rs:17:9 | 17 | pub use str_::*; | ^^^^^^^ note: but the private item here shadows it --> crates/nu-command/src/strings/mod.rs:8:1 | 8 | mod str_; | ^^^^^^^^^ = note: `#[warn(hidden_glob_reexports)]` on by default warning: incorrect NaN comparison, NaN cannot be directly compared to itself --> crates/nu-command/src/formats/to/nuon.rs:186:20 | 186 | && val != &f64::NAN | ^^^^^^^^^^^^^^^^ | = note: `#[warn(invalid_nan_comparisons)]` on by default help: use `f32::is_nan()` or `f64::is_nan()` instead | 186 - && val != &f64::NAN 186 + && !val.is_nan() | warning: `nu-command` (lib) generated 2 warnings (run `cargo clippy --fix --lib -p nu-command` to apply 1 suggestion) Compiling nu v0.81.1 (/data/source/nushell) warning: this expression creates a reference which is immediately dereferenced by the compiler --> crates/nu-command/tests/commands/rm.rs:392:27 | 392 | dir_to_clean: &test_dir, | ^^^^^^^^^ help: change this to: `test_dir` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default warning: `nu-command` (test "main") generated 1 warning (run `cargo clippy --fix --test "main"` to apply 1 suggestion) warning: `nu-command` (lib test) generated 2 warnings (2 duplicates) warning: `nu-json` (lib test) generated 1 warning (1 duplicate) Finished dev [unoptimized + debuginfo] target(s) in 3.89s ``` # User-Facing Changes N/A # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2023-06-20 10:17:33 +02:00
dir_to_clean: test_dir,
};
// This rm is expected to fail, and stderr output indicating so is also expected.
let actual = nu!(cwd: test_dir, "rm test*.txt");
assert!(files_exist_at(file_names.clone(), test_dir));
for file_name in file_names {
let path = test_dir.join(file_name);
let substr = format!("Could not delete {}", path.to_string_lossy());
assert!(
actual.err.contains(&substr),
"Matching: {}\n=== Command stderr:\n{}\n=== End stderr",
substr,
actual.err
);
}
});
}