mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 14:40:06 +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:
@ -62,6 +62,15 @@ fn du_files_with_glob_metachars(#[case] src_name: &str) {
|
||||
);
|
||||
|
||||
assert!(actual.err.is_empty());
|
||||
|
||||
// also test for variables.
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(),
|
||||
"let f = '{}'; du -d 1 $f",
|
||||
src.display(),
|
||||
);
|
||||
|
||||
assert!(actual.err.is_empty());
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -493,7 +493,7 @@ fn lists_with_directory_flag() {
|
||||
r#"
|
||||
cd dir_empty;
|
||||
['.' '././.' '..' '../dir_files' '../dir_files/*']
|
||||
| each { |it| ls --directory $it }
|
||||
| each { |it| ls --directory ($it | into glob) }
|
||||
| flatten
|
||||
| get name
|
||||
| to text
|
||||
|
@ -584,6 +584,32 @@ fn mv_files_with_glob_metachars(#[case] src_name: &str) {
|
||||
});
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case("a]c")]
|
||||
#[case("a[c")]
|
||||
#[case("a[bc]d")]
|
||||
#[case("a][c")]
|
||||
fn mv_files_with_glob_metachars_when_input_are_variables(#[case] src_name: &str) {
|
||||
Playground::setup("umv_test_18", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
src_name,
|
||||
"What is the sound of one hand clapping?",
|
||||
)]);
|
||||
|
||||
let src = dirs.test().join(src_name);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(),
|
||||
"let f = '{}'; umv $f {}",
|
||||
src.display(),
|
||||
"hello_world_dest"
|
||||
);
|
||||
|
||||
assert!(actual.err.is_empty());
|
||||
assert!(dirs.test().join("hello_world_dest").exists());
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
#[rstest]
|
||||
#[case("a]?c")]
|
||||
@ -591,6 +617,7 @@ fn mv_files_with_glob_metachars(#[case] src_name: &str) {
|
||||
// windows doesn't allow filename with `*`.
|
||||
fn mv_files_with_glob_metachars_nw(#[case] src_name: &str) {
|
||||
mv_files_with_glob_metachars(src_name);
|
||||
mv_files_with_glob_metachars_when_input_are_variables(src_name);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -357,6 +357,15 @@ fn open_files_with_glob_metachars(#[case] src_name: &str) {
|
||||
|
||||
assert!(actual.err.is_empty());
|
||||
assert!(actual.out.contains("hello"));
|
||||
|
||||
// also test for variables.
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(),
|
||||
"let f = '{}'; open $f",
|
||||
src.display(),
|
||||
);
|
||||
assert!(actual.err.is_empty());
|
||||
assert!(actual.out.contains("hello"));
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
use nu_test_support::fs::{files_exist_at, Stub::EmptyFile};
|
||||
use nu_test_support::nu;
|
||||
use nu_test_support::playground::Playground;
|
||||
use rstest::rstest;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
@ -481,6 +482,48 @@ fn rm_files_inside_glob_metachars_dir() {
|
||||
});
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn force_rm_suppress_error() {
|
||||
Playground::setup("force_rm_suppress_error", |dirs, sandbox| {
|
||||
|
@ -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