2019-12-17 19:54:39 +01:00
|
|
|
use nu_test_support::fs::{files_exist_at, Stub::EmptyFile};
|
2020-05-07 13:03:43 +02:00
|
|
|
use nu_test_support::nu;
|
2019-12-17 19:54:39 +01:00
|
|
|
use nu_test_support::playground::Playground;
|
`open`, `rm`, `umv`, `cp`, `rm` and `du`: Don't globs if inputs are variables or string interpolation (#11886)
# Description
This is a follow up to
https://github.com/nushell/nushell/pull/11621#issuecomment-1937484322
Also Fixes: #11838
## About the code change
It applys the same logic when we pass variables to external commands:
https://github.com/nushell/nushell/blob/0487e9ffcbc57c2d5feca606e10c3f8221ff5e00/crates/nu-command/src/system/run_external.rs#L162-L170
That is: if user input dynamic things(like variables, sub-expression, or
string interpolation), it returns a quoted `NuPath`, then user input
won't be globbed
# User-Facing Changes
Given two input files: `a*c.txt`, `abc.txt`
* `let f = "a*c.txt"; rm $f` will remove one file: `a*c.txt`.
~* `let f = "a*c.txt"; rm --glob $f` will remove `a*c.txt` and
`abc.txt`~
* `let f: glob = "a*c.txt"; rm $f` will remove `a*c.txt` and `abc.txt`
## Rules about globbing with *variable*
Given two files: `a*c.txt`, `abc.txt`
| Cmd Type | example | Result |
| ----- | ------------------ | ------ |
| builtin | let f = "a*c.txt"; rm $f | remove `a*c.txt` |
| builtin | let f: glob = "a*c.txt"; rm $f | remove `a*c.txt` and
`abc.txt`
| builtin | let f = "a*c.txt"; rm ($f \| into glob) | remove `a*c.txt`
and `abc.txt`
| custom | def crm [f: glob] { rm $f }; let f = "a*c.txt"; crm $f |
remove `a*c.txt` and `abc.txt`
| custom | def crm [f: glob] { rm ($f \| into string) }; let f =
"a*c.txt"; crm $f | remove `a*c.txt`
| custom | def crm [f: string] { rm $f }; let f = "a*c.txt"; crm $f |
remove `a*c.txt`
| custom | def crm [f: string] { rm $f }; let f = "a*c.txt"; crm ($f \|
into glob) | remove `a*c.txt` and `abc.txt`
In general, if a variable is annotated with `glob` type, nushell will
expand glob pattern. Or else, we need to use `into | glob` to expand
glob pattern
# Tests + Formatting
Done
# After Submitting
I think `str glob-escape` command will be no-longer required. We can
remove it.
2024-02-23 02:17:09 +01:00
|
|
|
use rstest::rstest;
|
2023-06-18 10:00:12 +02:00
|
|
|
use std::fs;
|
2023-09-12 08:06:56 +02:00
|
|
|
use std::path::Path;
|
2019-08-20 13:20:48 +02:00
|
|
|
|
2019-08-28 19:48:52 +02:00
|
|
|
#[test]
|
2019-12-15 17:15:06 +01:00
|
|
|
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]
|
2019-12-15 17:15:06 +01:00
|
|
|
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(),
|
Unify glob behavior on `open`, `rm`, `cp-old`, `mv`, `umv`, `cp` and `du` commands (#11621)
# Description
This pr is a follow up to
[#11569](https://github.com/nushell/nushell/pull/11569#issuecomment-1902279587)
> Revert the logic in https://github.com/nushell/nushell/pull/10694 and
apply the logic in this pr to mv, cp, rv will require a larger change, I
need to think how to achieve the bahavior
And sorry @bobhy for reverting some of your changes.
This pr is going to unify glob behavior on the given commands:
* open
* rm
* cp-old
* mv
* umv
* cp
* du
So they have the same behavior to `ls`, which is:
If given parameter is quoted by single quote(`'`) or double quote(`"`),
don't auto-expand the glob pattern. If not quoted, auto-expand the glob
pattern.
Fixes: #9558 Fixes: #10211 Fixes: #9310 Fixes: #10364
# TODO
But there is one thing remains: if we give a variable to the command, it
will always auto-expand the glob pattern, e.g:
```nushell
let path = "a[123]b"
rm $path
```
I don't think it's expected. But I also think user might want to
auto-expand the glob pattern in variables.
So I'll introduce a new command called `glob escape`, then if user
doesn't want to auto-expand the glob pattern, he can just do this: `rm
($path | glob escape)`
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
# Tests + Formatting
Done
# 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.
-->
## NOTE
This pr changes the semantic of `GlobPattern`, before this pr, it will
`expand path` after evaluated, this makes `nu_engine::glob_from` have no
chance to glob things right if a path contains glob pattern.
e.g: [#9310
](https://github.com/nushell/nushell/issues/9310#issuecomment-1886824030)
#10211
I think changing the semantic is fine, because it makes glob works if
path contains something like '*'.
It maybe a breaking change if a custom command's argument are annotated
by `: glob`.
2024-01-26 14:57:35 +01:00
|
|
|
r#"rm src/*/*/*.rs"#
|
2019-08-29 08:31:56 +02:00
|
|
|
);
|
2019-08-28 19:48:52 +02:00
|
|
|
|
2019-12-15 17:15:06 +01: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]
|
2019-12-15 17:15:06 +01:00
|
|
|
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(),
|
2020-02-12 03:24:31 +01:00
|
|
|
"rm -r src/*"
|
2019-08-29 08:31:56 +02:00
|
|
|
);
|
2019-08-28 19:48:52 +02:00
|
|
|
|
2019-12-15 17:15:06 +01:00
|
|
|
assert!(!files_exist_at(
|
2019-08-28 19:48:52 +02:00
|
|
|
vec!["src/parser/parse", "src/parser/hir"],
|
|
|
|
dirs.test()
|
|
|
|
));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-12-15 17:15:06 +01:00
|
|
|
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]
|
2019-12-15 17:15:06 +01:00
|
|
|
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"),
|
2023-03-15 06:54:55 +01:00
|
|
|
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]
|
2019-12-15 17:15:06 +01:00
|
|
|
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")]);
|
2020-05-07 13:03:43 +02:00
|
|
|
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());
|
2020-05-07 13:03:43 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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(),
|
2023-06-30 21:57:51 +02:00
|
|
|
"$env.HOME = myhome ; rm -rf ~"
|
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]
|
2019-12-15 17:15:06 +01:00
|
|
|
fn errors_if_attempting_to_delete_single_dot_as_argument() {
|
2019-08-29 02:32:42 +02:00
|
|
|
Playground::setup("rm_test_7", |dirs, _| {
|
2020-05-07 13:03:43 +02:00
|
|
|
let actual = nu!(
|
2019-08-29 08:31:56 +02:00
|
|
|
cwd: dirs.root(),
|
|
|
|
"rm ."
|
|
|
|
);
|
2019-08-29 02:32:42 +02:00
|
|
|
|
2020-05-07 13:03:43 +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]
|
2019-12-15 17:15:06 +01:00
|
|
|
fn errors_if_attempting_to_delete_two_dot_as_argument() {
|
2019-08-29 02:32:42 +02:00
|
|
|
Playground::setup("rm_test_8", |dirs, _| {
|
2020-05-07 13:03:43 +02:00
|
|
|
let actual = nu!(
|
2019-08-29 08:31:56 +02:00
|
|
|
cwd: dirs.root(),
|
|
|
|
"rm .."
|
|
|
|
);
|
2019-08-29 02:32:42 +02:00
|
|
|
|
2022-03-25 21:14:48 +01:00
|
|
|
assert!(actual.err.contains("cannot"));
|
2019-08-29 02:32:42 +02:00
|
|
|
})
|
2019-08-28 19:48:52 +02:00
|
|
|
}
|
2020-03-25 21:19:01 +01: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"),
|
2023-03-15 06:54:55 +01:00
|
|
|
EmptyFile("jttxt"),
|
2020-03-25 21:19:01 +01:00
|
|
|
EmptyFile("andres.txt"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-03-15 06:54:55 +01:00
|
|
|
"rm yehuda.txt jttxt andres.txt"
|
2020-03-25 21:19:01 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
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"),
|
2023-03-15 06:54:55 +01:00
|
|
|
EmptyFile("jt.txt"),
|
2020-03-25 21:19:01 +01:00
|
|
|
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| {
|
2023-03-15 06:54:55 +01:00
|
|
|
sandbox.with_files(vec![EmptyFile("yehuda.txt"), EmptyFile("jt.toml")]);
|
2020-03-25 21:19:01 +01:00
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"rm *.txt yehuda* *.toml"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Playground::glob_vec(&format!("{}/*", dirs.test().display())),
|
|
|
|
Vec::<std::path::PathBuf>::new()
|
|
|
|
);
|
2020-05-07 13:03:43 +02:00
|
|
|
assert!(!actual.out.contains("error"))
|
2020-03-25 21:19:01 +01:00
|
|
|
})
|
|
|
|
}
|
2020-04-06 13:28:56 +02:00
|
|
|
|
|
|
|
#[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"),
|
2023-03-15 06:54:55 +01:00
|
|
|
EmptyFile("jt.txt"),
|
2020-04-06 13:28:56 +02:00
|
|
|
EmptyFile("kevin.txt"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
sandbox.within("foo").mkdir("bar");
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test().join("foo/bar"),
|
|
|
|
"rm .../*.txt"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(!files_exist_at(
|
2023-03-15 06:54:55 +01:00
|
|
|
vec!["yehuda.txt", "jttxt", "kevin.txt"],
|
2020-04-06 13:28:56 +02:00
|
|
|
dirs.test()
|
|
|
|
));
|
|
|
|
})
|
|
|
|
}
|
2020-09-22 23:11:31 +02:00
|
|
|
|
|
|
|
#[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"));
|
|
|
|
})
|
|
|
|
}
|
2021-05-30 05:36:36 +02:00
|
|
|
|
|
|
|
#[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(),
|
2023-07-21 17:32:37 +02:00
|
|
|
"rm .*"
|
2021-05-30 05:36:36 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert!(files_exist_at(vec!["foo"], dirs.test()));
|
|
|
|
assert!(!files_exist_at(vec![".bar"], dirs.test()));
|
|
|
|
})
|
|
|
|
}
|
2021-10-28 14:05:07 +02:00
|
|
|
|
|
|
|
#[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());
|
|
|
|
})
|
|
|
|
}
|
2022-08-04 13:59:20 +02:00
|
|
|
|
|
|
|
#[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(),
|
2023-03-17 20:58:13 +01:00
|
|
|
"ls | find test | get name | rm $in.0; ls | is-empty",
|
2022-08-04 13:59:20 +02:00
|
|
|
);
|
2023-03-17 20:58:13 +01:00
|
|
|
assert_eq!(actual.out, "true");
|
2022-08-04 13:59:20 +02:00
|
|
|
});
|
|
|
|
}
|
2023-06-18 10:00:12 +02:00
|
|
|
|
2023-07-20 20:16:03 +02:00
|
|
|
#[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());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-02 14:27:03 +01:00
|
|
|
#[test]
|
|
|
|
fn removes_symlink_pointing_to_directory() {
|
|
|
|
Playground::setup("rm_symlink_to_directory", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("test").symlink("test", "test_link");
|
|
|
|
|
|
|
|
nu!(cwd: sandbox.cwd(), "rm test_link");
|
|
|
|
|
|
|
|
assert!(!dirs.test().join("test_link").exists());
|
|
|
|
// The pointed directory should not be deleted.
|
|
|
|
assert!(dirs.test().join("test").exists());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-17 00:30:15 +01:00
|
|
|
#[test]
|
|
|
|
fn removes_file_after_cd() {
|
|
|
|
Playground::setup("rm_after_cd", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![EmptyFile("delete.txt")]);
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.root(),
|
|
|
|
"let file = 'delete.txt'; cd rm_after_cd; rm $file",
|
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("delete.txt");
|
|
|
|
assert!(!path.exists());
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-18 10:00:12 +02:00
|
|
|
struct Cleanup<'a> {
|
2023-09-12 08:06:56 +02:00
|
|
|
dir_to_clean: &'a Path,
|
2023-06-18 10:00:12 +02:00
|
|
|
}
|
|
|
|
|
2023-09-12 08:06:56 +02:00
|
|
|
fn set_dir_read_only(directory: &Path, read_only: bool) {
|
2023-06-18 10:00:12 +02:00
|
|
|
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 {
|
2023-06-20 10:17:33 +02:00
|
|
|
dir_to_clean: test_dir,
|
2023-06-18 10:00:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
Unify glob behavior on `open`, `rm`, `cp-old`, `mv`, `umv`, `cp` and `du` commands (#11621)
# Description
This pr is a follow up to
[#11569](https://github.com/nushell/nushell/pull/11569#issuecomment-1902279587)
> Revert the logic in https://github.com/nushell/nushell/pull/10694 and
apply the logic in this pr to mv, cp, rv will require a larger change, I
need to think how to achieve the bahavior
And sorry @bobhy for reverting some of your changes.
This pr is going to unify glob behavior on the given commands:
* open
* rm
* cp-old
* mv
* umv
* cp
* du
So they have the same behavior to `ls`, which is:
If given parameter is quoted by single quote(`'`) or double quote(`"`),
don't auto-expand the glob pattern. If not quoted, auto-expand the glob
pattern.
Fixes: #9558 Fixes: #10211 Fixes: #9310 Fixes: #10364
# TODO
But there is one thing remains: if we give a variable to the command, it
will always auto-expand the glob pattern, e.g:
```nushell
let path = "a[123]b"
rm $path
```
I don't think it's expected. But I also think user might want to
auto-expand the glob pattern in variables.
So I'll introduce a new command called `glob escape`, then if user
doesn't want to auto-expand the glob pattern, he can just do this: `rm
($path | glob escape)`
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
# Tests + Formatting
Done
# 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.
-->
## NOTE
This pr changes the semantic of `GlobPattern`, before this pr, it will
`expand path` after evaluated, this makes `nu_engine::glob_from` have no
chance to glob things right if a path contains glob pattern.
e.g: [#9310
](https://github.com/nushell/nushell/issues/9310#issuecomment-1886824030)
#10211
I think changing the semantic is fine, because it makes glob works if
path contains something like '*'.
It maybe a breaking change if a custom command's argument are annotated
by `: glob`.
2024-01-26 14:57:35 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rm_files_inside_glob_metachars_dir() {
|
|
|
|
Playground::setup("rm_files_inside_glob_metachars_dir", |dirs, sandbox| {
|
|
|
|
let sub_dir = "test[]";
|
|
|
|
sandbox
|
|
|
|
.within(sub_dir)
|
|
|
|
.with_files(vec![EmptyFile("test_file.txt")]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test().join(sub_dir),
|
|
|
|
"rm test_file.txt",
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(actual.err.is_empty());
|
|
|
|
assert!(!files_exist_at(
|
|
|
|
vec!["test_file.txt"],
|
|
|
|
dirs.test().join(sub_dir)
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
2024-01-28 16:01:19 +01:00
|
|
|
|
`open`, `rm`, `umv`, `cp`, `rm` and `du`: Don't globs if inputs are variables or string interpolation (#11886)
# Description
This is a follow up to
https://github.com/nushell/nushell/pull/11621#issuecomment-1937484322
Also Fixes: #11838
## About the code change
It applys the same logic when we pass variables to external commands:
https://github.com/nushell/nushell/blob/0487e9ffcbc57c2d5feca606e10c3f8221ff5e00/crates/nu-command/src/system/run_external.rs#L162-L170
That is: if user input dynamic things(like variables, sub-expression, or
string interpolation), it returns a quoted `NuPath`, then user input
won't be globbed
# User-Facing Changes
Given two input files: `a*c.txt`, `abc.txt`
* `let f = "a*c.txt"; rm $f` will remove one file: `a*c.txt`.
~* `let f = "a*c.txt"; rm --glob $f` will remove `a*c.txt` and
`abc.txt`~
* `let f: glob = "a*c.txt"; rm $f` will remove `a*c.txt` and `abc.txt`
## Rules about globbing with *variable*
Given two files: `a*c.txt`, `abc.txt`
| Cmd Type | example | Result |
| ----- | ------------------ | ------ |
| builtin | let f = "a*c.txt"; rm $f | remove `a*c.txt` |
| builtin | let f: glob = "a*c.txt"; rm $f | remove `a*c.txt` and
`abc.txt`
| builtin | let f = "a*c.txt"; rm ($f \| into glob) | remove `a*c.txt`
and `abc.txt`
| custom | def crm [f: glob] { rm $f }; let f = "a*c.txt"; crm $f |
remove `a*c.txt` and `abc.txt`
| custom | def crm [f: glob] { rm ($f \| into string) }; let f =
"a*c.txt"; crm $f | remove `a*c.txt`
| custom | def crm [f: string] { rm $f }; let f = "a*c.txt"; crm $f |
remove `a*c.txt`
| custom | def crm [f: string] { rm $f }; let f = "a*c.txt"; crm ($f \|
into glob) | remove `a*c.txt` and `abc.txt`
In general, if a variable is annotated with `glob` type, nushell will
expand glob pattern. Or else, we need to use `into | glob` to expand
glob pattern
# Tests + Formatting
Done
# After Submitting
I think `str glob-escape` command will be no-longer required. We can
remove it.
2024-02-23 02:17:09 +01:00
|
|
|
#[rstest]
|
|
|
|
#[case("a]c")]
|
|
|
|
#[case("a[c")]
|
|
|
|
#[case("a[bc]d")]
|
|
|
|
#[case("a][c")]
|
|
|
|
fn rm_files_with_glob_metachars(#[case] src_name: &str) {
|
|
|
|
Playground::setup("rm_files_with_glob_metachars", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![EmptyFile(src_name)]);
|
|
|
|
|
|
|
|
let src = dirs.test().join(src_name);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"rm '{}'",
|
|
|
|
src.display(),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(actual.err.is_empty());
|
|
|
|
assert!(!src.exists());
|
|
|
|
|
|
|
|
// test with variables
|
|
|
|
sandbox.with_files(vec![EmptyFile(src_name)]);
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"let f = '{}'; rm $f",
|
|
|
|
src.display(),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(actual.err.is_empty());
|
|
|
|
assert!(!src.exists());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
#[rstest]
|
|
|
|
#[case("a]?c")]
|
|
|
|
#[case("a*.?c")]
|
|
|
|
// windows doesn't allow filename with `*`.
|
|
|
|
fn rm_files_with_glob_metachars_nw(#[case] src_name: &str) {
|
|
|
|
rm_files_with_glob_metachars(src_name);
|
|
|
|
}
|
|
|
|
|
2024-01-28 16:01:19 +01:00
|
|
|
#[test]
|
|
|
|
fn force_rm_suppress_error() {
|
|
|
|
Playground::setup("force_rm_suppress_error", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![EmptyFile("test_file.txt")]);
|
|
|
|
|
|
|
|
// the second rm should suppress error.
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"rm test_file.txt; rm -f test_file.txt",
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(actual.err.is_empty());
|
|
|
|
});
|
|
|
|
}
|