mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 10:36:00 +02: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:
0487e9ffcb/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.
This commit is contained in:
@ -1019,6 +1019,40 @@ fn copies_files_with_glob_metachars(#[case] src_name: &str) {
|
||||
});
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case("a]c")]
|
||||
#[case("a[c")]
|
||||
#[case("a[bc]d")]
|
||||
#[case("a][c")]
|
||||
fn copies_files_with_glob_metachars_when_input_are_variables(#[case] src_name: &str) {
|
||||
Playground::setup("ucp_test_35", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
src_name,
|
||||
"What is the sound of one hand clapping?",
|
||||
)]);
|
||||
|
||||
let src = dirs.test().join(src_name);
|
||||
|
||||
// -- open command doesn't like file name
|
||||
//// Get the hash of the file content to check integrity after copy.
|
||||
//let src_hash = get_file_hash(src.display());
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(),
|
||||
"let f = '{}'; cp $f {}",
|
||||
src.display(),
|
||||
TEST_HELLO_WORLD_DEST
|
||||
);
|
||||
|
||||
assert!(actual.err.is_empty());
|
||||
assert!(dirs.test().join(TEST_HELLO_WORLD_DEST).exists());
|
||||
|
||||
//// Get the hash of the copied file content to check against first_hash.
|
||||
//let after_cp_hash = get_file_hash(dirs.test().join(TEST_HELLO_WORLD_DEST).display());
|
||||
//assert_eq!(src_hash, after_cp_hash);
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
#[rstest]
|
||||
#[case(r#"'a]?c'"#)]
|
||||
@ -1026,6 +1060,7 @@ fn copies_files_with_glob_metachars(#[case] src_name: &str) {
|
||||
// windows doesn't allow filename with `*`.
|
||||
fn copies_files_with_glob_metachars_nw(#[case] src_name: &str) {
|
||||
copies_files_with_glob_metachars(src_name);
|
||||
copies_files_with_glob_metachars_when_input_are_variables(src_name);
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
|
Reference in New Issue
Block a user