mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 16:33:37 +01:00
f7d647ac3c
# 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.
85 lines
1.8 KiB
Rust
85 lines
1.8 KiB
Rust
use nu_test_support::fs::Stub::EmptyFile;
|
|
use nu_test_support::{nu, pipeline, playground::Playground};
|
|
use rstest::rstest;
|
|
|
|
#[test]
|
|
fn test_du_flag_min_size() {
|
|
let actual = nu!(
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
r#"
|
|
du -m -1
|
|
"#
|
|
));
|
|
assert!(actual
|
|
.err
|
|
.contains("Negative value passed when positive one is required"));
|
|
|
|
let actual = nu!(
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
r#"
|
|
du -m 1
|
|
"#
|
|
));
|
|
assert!(actual.err.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn test_du_flag_max_depth() {
|
|
let actual = nu!(
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
r#"
|
|
du -d -2
|
|
"#
|
|
));
|
|
assert!(actual
|
|
.err
|
|
.contains("Negative value passed when positive one is required"));
|
|
|
|
let actual = nu!(
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
r#"
|
|
du -d 2
|
|
"#
|
|
));
|
|
assert!(actual.err.is_empty());
|
|
}
|
|
|
|
#[rstest]
|
|
#[case("a]c")]
|
|
#[case("a[c")]
|
|
#[case("a[bc]d")]
|
|
#[case("a][c")]
|
|
fn du_files_with_glob_metachars(#[case] src_name: &str) {
|
|
Playground::setup("umv_test_16", |dirs, sandbox| {
|
|
sandbox.with_files(vec![EmptyFile(src_name)]);
|
|
|
|
let src = dirs.test().join(src_name);
|
|
|
|
let actual = nu!(
|
|
cwd: dirs.test(),
|
|
"du -d 1 '{}'",
|
|
src.display(),
|
|
);
|
|
|
|
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());
|
|
});
|
|
}
|
|
|
|
#[cfg(not(windows))]
|
|
#[rstest]
|
|
#[case("a]?c")]
|
|
#[case("a*.?c")]
|
|
// windows doesn't allow filename with `*`.
|
|
fn du_files_with_glob_metachars_nw(#[case] src_name: &str) {
|
|
du_files_with_glob_metachars(src_name);
|
|
}
|