Remove required positional arguments from run-external and exec (#14765)

# Description
This PR removes the required positional argument from `run-external` and
`exec` in favor of the rest arguments, meaning lists of external
commands can be spread directly into `run-external` and `exec`. This
does have the drawback of making calling `run-external` and `exec` with
no arguments a run-time error rather than a parse error, but I don't
imagine that is an issue.

Before (for both `run-external` and `exec`):
```nushell
run-external
# => Error: nu::parser::missing_positional
# => 
# =>   × Missing required positional argument.
# =>    ╭─[entry #9:1:13]
# =>  1 │ run-external
# =>    ╰────
# =>   help: Usage: run-external <command> ...(args) . Use `--help` for more
# =>         information.

let command = ["cat" "hello.txt"]
run-external ...$command
# => Error: nu::parser::missing_positional
# => 
# =>   × Missing required positional argument.
# =>    ╭─[entry #11:1:14]
# =>  1 │ run-external ...$command
# =>    ·              ▲
# =>    ·              ╰── missing command
# =>    ╰────
# =>   help: Usage: run-external <command> ...(args) . Use `--help` for more
# =>         information.
run-external ($command | first) ...($command | skip 1)
# => hello world!
```

After (for both `run-external` and `exec`):
```nushell
run-external
# => Error: nu:🐚:missing_parameter
# => 
# =>   × Missing parameter: no command given.
# =>    ╭─[entry #2:1:1]
# =>  1 │ run-external
# =>    · ──────┬─────
# =>    ·       ╰── missing parameter: no command given
# =>    ╰────
# => 

let command = ["cat" "hello.txt"]
run-external ...$command
# => hello world!
```



# User-Facing Changes
Lists can now be spread directly into `run-external` and `exec`:

```nushell
let command = [cat hello.txt]
run-external ...$command
# => hello world!
``` 

# Tests + Formatting
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting
N/A
This commit is contained in:
132ikl 2025-01-15 17:10:28 -05:00 committed by GitHub
parent 46566296c0
commit 06938659d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 246 additions and 160 deletions

12
Cargo.lock generated
View File

@ -3844,6 +3844,7 @@ dependencies = [
"rmp", "rmp",
"roxmltree", "roxmltree",
"rstest", "rstest",
"rstest_reuse",
"rusqlite", "rusqlite",
"scopeguard", "scopeguard",
"serde", "serde",
@ -6309,6 +6310,17 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "rstest_reuse"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3a8fb4672e840a587a66fc577a5491375df51ddb88f2a2c2a792598c326fe14"
dependencies = [
"quote",
"rand",
"syn 2.0.90",
]
[[package]] [[package]]
name = "rusqlite" name = "rusqlite"
version = "0.31.0" version = "0.31.0"

View File

@ -145,6 +145,7 @@ rmp = "0.8"
rmp-serde = "1.3" rmp-serde = "1.3"
roxmltree = "0.20" roxmltree = "0.20"
rstest = { version = "0.23", default-features = false } rstest = { version = "0.23", default-features = false }
rstest_reuse = "0.7"
rusqlite = "0.31" rusqlite = "0.31"
rust-embed = "8.5.0" rust-embed = "8.5.0"
scopeguard = { version = "1.2.0" } scopeguard = { version = "1.2.0" }

View File

@ -196,6 +196,7 @@ mockito = { workspace = true, default-features = false }
quickcheck = { workspace = true } quickcheck = { workspace = true }
quickcheck_macros = { workspace = true } quickcheck_macros = { workspace = true }
rstest = { workspace = true, default-features = false } rstest = { workspace = true, default-features = false }
rstest_reuse = { workspace = true }
pretty_assertions = { workspace = true } pretty_assertions = { workspace = true }
tempfile = { workspace = true } tempfile = { workspace = true }
rand_chacha = { workspace = true } rand_chacha = { workspace = true }

View File

@ -1,3 +1,5 @@
use std::borrow::Cow;
use nu_engine::{command_prelude::*, env_to_strings}; use nu_engine::{command_prelude::*, env_to_strings};
#[derive(Clone)] #[derive(Clone)]
@ -11,7 +13,11 @@ impl Command for Exec {
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("exec") Signature::build("exec")
.input_output_types(vec![(Type::Nothing, Type::Any)]) .input_output_types(vec![(Type::Nothing, Type::Any)])
.required("command", SyntaxShape::String, "The command to execute.") .rest(
"command",
SyntaxShape::OneOf(vec![SyntaxShape::GlobPattern, SyntaxShape::Any]),
"External command to run, with arguments.",
)
.allows_unknown_args() .allows_unknown_args()
.category(Category::System) .category(Category::System)
} }
@ -33,15 +39,29 @@ On Windows based systems, Nushell will wait for the command to finish and then e
_input: PipelineData, _input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let cwd = engine_state.cwd(Some(stack))?; let cwd = engine_state.cwd(Some(stack))?;
let rest = call.rest::<Value>(engine_state, stack, 0)?;
let name_args = rest.split_first();
let Some((name, call_args)) = name_args else {
return Err(ShellError::MissingParameter {
param_name: "no command given".into(),
span: call.head,
});
};
let name_str: Cow<str> = match &name {
Value::Glob { val, .. } => Cow::Borrowed(val),
Value::String { val, .. } => Cow::Borrowed(val),
_ => Cow::Owned(name.clone().coerce_into_string()?),
};
// Find the absolute path to the executable. If the command is not // Find the absolute path to the executable. If the command is not
// found, display a helpful error message. // found, display a helpful error message.
let name: Spanned<String> = call.req(engine_state, stack, 0)?;
let executable = { let executable = {
let paths = nu_engine::env::path_str(engine_state, stack, call.head)?; let paths = nu_engine::env::path_str(engine_state, stack, call.head)?;
let Some(executable) = crate::which(&name.item, &paths, cwd.as_ref()) else { let Some(executable) = crate::which(name_str.as_ref(), &paths, cwd.as_ref()) else {
return Err(crate::command_not_found( return Err(crate::command_not_found(
&name.item, &name_str,
call.head, call.head,
engine_state, engine_state,
stack, stack,
@ -73,7 +93,7 @@ On Windows based systems, Nushell will wait for the command to finish and then e
} }
// Configure args. // Configure args.
let args = crate::eval_arguments_from_call(engine_state, stack, call)?; let args = crate::eval_external_arguments(engine_state, stack, call_args.to_vec())?;
command.args(args.into_iter().map(|s| s.item)); command.args(args.into_iter().map(|s| s.item));
// Execute the child process, replacing/terminating the current process // Execute the child process, replacing/terminating the current process

View File

@ -33,7 +33,7 @@ pub use nu_check::NuCheck;
pub use ps::Ps; pub use ps::Ps;
#[cfg(windows)] #[cfg(windows)]
pub use registry_query::RegistryQuery; pub use registry_query::RegistryQuery;
pub use run_external::{command_not_found, eval_arguments_from_call, which, External}; pub use run_external::{command_not_found, eval_external_arguments, which, External};
pub use sys::*; pub use sys::*;
pub use uname::UName; pub use uname::UName;
pub use which_::Which; pub use which_::Which;

View File

@ -34,15 +34,10 @@ impl Command for External {
fn signature(&self) -> nu_protocol::Signature { fn signature(&self) -> nu_protocol::Signature {
Signature::build(self.name()) Signature::build(self.name())
.input_output_types(vec![(Type::Any, Type::Any)]) .input_output_types(vec![(Type::Any, Type::Any)])
.required(
"command",
SyntaxShape::OneOf(vec![SyntaxShape::GlobPattern, SyntaxShape::String]),
"External command to run.",
)
.rest( .rest(
"args", "command",
SyntaxShape::OneOf(vec![SyntaxShape::GlobPattern, SyntaxShape::Any]), SyntaxShape::OneOf(vec![SyntaxShape::GlobPattern, SyntaxShape::Any]),
"Arguments for external command.", "External command to run, with arguments.",
) )
.category(Category::System) .category(Category::System)
} }
@ -55,7 +50,15 @@ impl Command for External {
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let cwd = engine_state.cwd(Some(stack))?; let cwd = engine_state.cwd(Some(stack))?;
let name: Value = call.req(engine_state, stack, 0)?; let rest = call.rest::<Value>(engine_state, stack, 0)?;
let name_args = rest.split_first();
let Some((name, call_args)) = name_args else {
return Err(ShellError::MissingParameter {
param_name: "no command given".into(),
span: call.head,
});
};
let name_str: Cow<str> = match &name { let name_str: Cow<str> = match &name {
Value::Glob { val, .. } => Cow::Borrowed(val), Value::Glob { val, .. } => Cow::Borrowed(val),
@ -151,7 +154,7 @@ impl Command for External {
command.envs(envs); command.envs(envs);
// Configure args. // Configure args.
let args = eval_arguments_from_call(engine_state, stack, call)?; let args = eval_external_arguments(engine_state, stack, call_args.to_vec())?;
#[cfg(windows)] #[cfg(windows)]
if is_cmd_internal_command(&name_str) || potential_nuscript_in_windows { if is_cmd_internal_command(&name_str) || potential_nuscript_in_windows {
// The /D flag disables execution of AutoRun commands from registry. // The /D flag disables execution of AutoRun commands from registry.
@ -290,14 +293,13 @@ impl Command for External {
} }
} }
/// Evaluate all arguments from a call, performing expansions when necessary. /// Evaluate all arguments, performing expansions when necessary.
pub fn eval_arguments_from_call( pub fn eval_external_arguments(
engine_state: &EngineState, engine_state: &EngineState,
stack: &mut Stack, stack: &mut Stack,
call: &Call, call_args: Vec<Value>,
) -> Result<Vec<Spanned<OsString>>, ShellError> { ) -> Result<Vec<Spanned<OsString>>, ShellError> {
let cwd = engine_state.cwd(Some(stack))?; let cwd = engine_state.cwd(Some(stack))?;
let call_args = call.rest::<Value>(engine_state, stack, 1)?;
let mut args: Vec<Spanned<OsString>> = Vec::with_capacity(call_args.len()); let mut args: Vec<Spanned<OsString>> = Vec::with_capacity(call_args.len());
for arg in call_args { for arg in call_args {

View File

@ -1,23 +1,34 @@
use nu_test_support::fs::Stub::EmptyFile; use nu_test_support::fs::Stub::EmptyFile;
use nu_test_support::nu;
use nu_test_support::playground::Playground; use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline}; use rstest::rstest;
use rstest_reuse::*;
#[test] // Template for run-external test to ensure tests work when calling
fn better_empty_redirection() { // the binary directly, using the caret operator, and when using
// the run-external command
#[template]
#[rstest]
#[case("")]
#[case("^")]
#[case("run-external ")]
fn run_external_prefixes(#[case] prefix: &str) {}
#[apply(run_external_prefixes)]
fn better_empty_redirection(prefix: &str) {
let actual = nu!( let actual = nu!(
cwd: "tests/fixtures/formats", pipeline( cwd: "tests/fixtures/formats",
" "ls | each {{ |it| {}nu `--testbin` cococo $it.name }} | ignore",
ls | each { |it| nu --testbin cococo $it.name } | ignore prefix
" );
));
eprintln!("out: {}", actual.out); eprintln!("out: {}", actual.out);
assert!(!actual.out.contains('2')); assert!(!actual.out.contains('2'));
} }
#[test] #[apply(run_external_prefixes)]
fn explicit_glob() { fn explicit_glob(prefix: &str) {
Playground::setup("external with explicit glob", |dirs, sandbox| { Playground::setup("external with explicit glob", |dirs, sandbox| {
sandbox.with_files(&[ sandbox.with_files(&[
EmptyFile("D&D_volume_1.txt"), EmptyFile("D&D_volume_1.txt"),
@ -26,19 +37,20 @@ fn explicit_glob() {
]); ]);
let actual = nu!( let actual = nu!(
cwd: dirs.test(), pipeline( cwd: dirs.test(),
r#" r#"
^nu --testbin cococo ('*.txt' | into glob) {}nu `--testbin` cococo ('*.txt' | into glob)
"# "#,
)); prefix
);
assert!(actual.out.contains("D&D_volume_1.txt")); assert!(actual.out.contains("D&D_volume_1.txt"));
assert!(actual.out.contains("D&D_volume_2.txt")); assert!(actual.out.contains("D&D_volume_2.txt"));
}) })
} }
#[test] #[apply(run_external_prefixes)]
fn bare_word_expand_path_glob() { fn bare_word_expand_path_glob(prefix: &str) {
Playground::setup("bare word should do the expansion", |dirs, sandbox| { Playground::setup("bare word should do the expansion", |dirs, sandbox| {
sandbox.with_files(&[ sandbox.with_files(&[
EmptyFile("D&D_volume_1.txt"), EmptyFile("D&D_volume_1.txt"),
@ -47,19 +59,20 @@ fn bare_word_expand_path_glob() {
]); ]);
let actual = nu!( let actual = nu!(
cwd: dirs.test(), pipeline( cwd: dirs.test(),
" "
^nu --testbin cococo *.txt {}nu `--testbin` cococo *.txt
" ",
)); prefix
);
assert!(actual.out.contains("D&D_volume_1.txt")); assert!(actual.out.contains("D&D_volume_1.txt"));
assert!(actual.out.contains("D&D_volume_2.txt")); assert!(actual.out.contains("D&D_volume_2.txt"));
}) })
} }
#[test] #[apply(run_external_prefixes)]
fn backtick_expand_path_glob() { fn backtick_expand_path_glob(prefix: &str) {
Playground::setup("backtick should do the expansion", |dirs, sandbox| { Playground::setup("backtick should do the expansion", |dirs, sandbox| {
sandbox.with_files(&[ sandbox.with_files(&[
EmptyFile("D&D_volume_1.txt"), EmptyFile("D&D_volume_1.txt"),
@ -68,19 +81,20 @@ fn backtick_expand_path_glob() {
]); ]);
let actual = nu!( let actual = nu!(
cwd: dirs.test(), pipeline( cwd: dirs.test(),
r#" r#"
^nu --testbin cococo `*.txt` {}nu `--testbin` cococo `*.txt`
"# "#,
)); prefix
);
assert!(actual.out.contains("D&D_volume_1.txt")); assert!(actual.out.contains("D&D_volume_1.txt"));
assert!(actual.out.contains("D&D_volume_2.txt")); assert!(actual.out.contains("D&D_volume_2.txt"));
}) })
} }
#[test] #[apply(run_external_prefixes)]
fn single_quote_does_not_expand_path_glob() { fn single_quote_does_not_expand_path_glob(prefix: &str) {
Playground::setup("single quote do not run the expansion", |dirs, sandbox| { Playground::setup("single quote do not run the expansion", |dirs, sandbox| {
sandbox.with_files(&[ sandbox.with_files(&[
EmptyFile("D&D_volume_1.txt"), EmptyFile("D&D_volume_1.txt"),
@ -89,18 +103,19 @@ fn single_quote_does_not_expand_path_glob() {
]); ]);
let actual = nu!( let actual = nu!(
cwd: dirs.test(), pipeline( cwd: dirs.test(),
r#" r#"
^nu --testbin cococo '*.txt' {}nu `--testbin` cococo '*.txt'
"# "#,
)); prefix
);
assert_eq!(actual.out, "*.txt"); assert_eq!(actual.out, "*.txt");
}) })
} }
#[test] #[apply(run_external_prefixes)]
fn double_quote_does_not_expand_path_glob() { fn double_quote_does_not_expand_path_glob(prefix: &str) {
Playground::setup("double quote do not run the expansion", |dirs, sandbox| { Playground::setup("double quote do not run the expansion", |dirs, sandbox| {
sandbox.with_files(&[ sandbox.with_files(&[
EmptyFile("D&D_volume_1.txt"), EmptyFile("D&D_volume_1.txt"),
@ -109,44 +124,50 @@ fn double_quote_does_not_expand_path_glob() {
]); ]);
let actual = nu!( let actual = nu!(
cwd: dirs.test(), pipeline( cwd: dirs.test(),
r#" r#"
^nu --testbin cococo "*.txt" {}nu `--testbin` cococo "*.txt"
"# "#,
)); prefix
);
assert_eq!(actual.out, "*.txt"); assert_eq!(actual.out, "*.txt");
}) })
} }
#[test] #[apply(run_external_prefixes)]
fn failed_command_with_semicolon_will_not_execute_following_cmds() { fn failed_command_with_semicolon_will_not_execute_following_cmds(prefix: &str) {
Playground::setup("external failed command with semicolon", |dirs, _| { Playground::setup("external failed command with semicolon", |dirs, _| {
let actual = nu!( let actual = nu!(
cwd: dirs.test(), pipeline( cwd: dirs.test(),
" "
nu --testbin fail; echo done {}nu `--testbin` fail; echo done
" ",
)); prefix
);
assert!(!actual.out.contains("done")); assert!(!actual.out.contains("done"));
}) })
} }
#[test] #[apply(run_external_prefixes)]
fn external_args_with_quoted() { fn external_args_with_quoted(prefix: &str) {
Playground::setup("external failed command with semicolon", |dirs, _| { Playground::setup("external failed command with semicolon", |dirs, _| {
let actual = nu!( let actual = nu!(
cwd: dirs.test(), pipeline( cwd: dirs.test(),
r#" r#"
nu --testbin cococo "foo=bar 'hi'" {}nu `--testbin` cococo "foo=bar 'hi'"
"# "#,
)); prefix
);
assert_eq!(actual.out, "foo=bar 'hi'"); assert_eq!(actual.out, "foo=bar 'hi'");
}) })
} }
// don't use template for this once since echo with no prefix is an internal command
// and arguments flags are treated as arguments to run-external
// (but wrapping them in quotes defeats the point of test)
#[cfg(not(windows))] #[cfg(not(windows))]
#[test] #[test]
fn external_arg_with_option_like_embedded_quotes() { fn external_arg_with_option_like_embedded_quotes() {
@ -155,58 +176,67 @@ fn external_arg_with_option_like_embedded_quotes() {
"external arg with option like embedded quotes", "external arg with option like embedded quotes",
|dirs, _| { |dirs, _| {
let actual = nu!( let actual = nu!(
cwd: dirs.test(), pipeline( cwd: dirs.test(),
r#" r#"
^echo --foo='bar' -foo='bar' ^echo --foo='bar' -foo='bar'
"# "#,
)); );
assert_eq!(actual.out, "--foo=bar -foo=bar"); assert_eq!(actual.out, "--foo=bar -foo=bar");
}, },
) )
} }
#[test] // FIXME: parser complains about invalid characters after single quote
fn external_arg_with_non_option_like_embedded_quotes() { #[rstest]
#[case("")]
#[case("^")]
fn external_arg_with_non_option_like_embedded_quotes(#[case] prefix: &str) {
Playground::setup( Playground::setup(
"external arg with non option like embedded quotes", "external arg with non option like embedded quotes",
|dirs, _| { |dirs, _| {
let actual = nu!( let actual = nu!(
cwd: dirs.test(), pipeline( cwd: dirs.test(),
r#" r#"
^nu --testbin cococo foo='bar' 'foo'=bar {}nu `--testbin` cococo foo='bar' 'foo'=bar
"# "#,
)); prefix
);
assert_eq!(actual.out, "foo=bar foo=bar"); assert_eq!(actual.out, "foo=bar foo=bar");
}, },
) )
} }
#[test] // FIXME: parser bug prevents expressions from appearing within GlobPattern substrings
fn external_arg_with_string_interpolation() { #[rstest]
#[case("")]
#[case("^")]
fn external_arg_with_string_interpolation(#[case] prefix: &str) {
Playground::setup("external arg with string interpolation", |dirs, _| { Playground::setup("external arg with string interpolation", |dirs, _| {
let actual = nu!( let actual = nu!(
cwd: dirs.test(), pipeline( cwd: dirs.test(),
r#" r#"
^nu --testbin cococo foo=(2 + 2) $"foo=(2 + 2)" foo=$"(2 + 2)" {}nu `--testbin` cococo foo=(2 + 2) $"foo=(2 + 2)" foo=$"(2 + 2)"
"# "#,
)); prefix
);
assert_eq!(actual.out, "foo=4 foo=4 foo=4"); assert_eq!(actual.out, "foo=4 foo=4 foo=4");
}) })
} }
#[test] #[apply(run_external_prefixes)]
fn external_arg_with_variable_name() { fn external_arg_with_variable_name(prefix: &str) {
Playground::setup("external failed command with semicolon", |dirs, _| { Playground::setup("external failed command with semicolon", |dirs, _| {
let actual = nu!( let actual = nu!(
cwd: dirs.test(), pipeline( cwd: dirs.test(),
r#" r#"
let dump_command = "PGPASSWORD='db_secret' pg_dump -Fc -h 'db.host' -p '$db.port' -U postgres -d 'db_name' > '/tmp/dump_name'"; let dump_command = "PGPASSWORD='db_secret' pg_dump -Fc -h 'db.host' -p '$db.port' -U postgres -d 'db_name' > '/tmp/dump_name'";
nu --testbin nonu $dump_command {}nu `--testbin` nonu $dump_command
"# "#,
)); prefix
);
assert_eq!( assert_eq!(
actual.out, actual.out,
@ -215,25 +245,29 @@ fn external_arg_with_variable_name() {
}) })
} }
#[test] #[apply(run_external_prefixes)]
fn external_command_escape_args() { fn external_command_escape_args(prefix: &str) {
Playground::setup("external failed command with semicolon", |dirs, _| { Playground::setup("external failed command with semicolon", |dirs, _| {
let actual = nu!( let actual = nu!(
cwd: dirs.test(), pipeline( cwd: dirs.test(),
r#" r#"
nu --testbin cococo "\"abcd" {}nu `--testbin` cococo "\"abcd"
"# "#,
)); prefix
);
assert_eq!(actual.out, r#""abcd"#); assert_eq!(actual.out, r#""abcd"#);
}) })
} }
#[test] #[apply(run_external_prefixes)]
fn external_command_ndots_args() { fn external_command_ndots_args(prefix: &str) {
let actual = nu!(r#" let actual = nu!(
nu --testbin cococo foo/. foo/.. foo/... foo/./bar foo/../bar foo/.../bar ./bar ../bar .../bar r#"
"#); {}nu `--testbin` cococo foo/. foo/.. foo/... foo/./bar foo/../bar foo/.../bar ./bar ../bar .../bar
"#,
prefix
);
assert_eq!( assert_eq!(
actual.out, actual.out,
@ -247,23 +281,29 @@ fn external_command_ndots_args() {
); );
} }
#[test] #[apply(run_external_prefixes)]
fn external_command_ndots_leading_dot_slash() { fn external_command_ndots_leading_dot_slash(prefix: &str) {
// Don't expand ndots with a leading `./` // Don't expand ndots with a leading `./`
let actual = nu!(r#" let actual = nu!(
nu --testbin cococo ./... ./.... r#"
"#); {}nu `--testbin` cococo ./... ./....
"#,
prefix
);
assert_eq!(actual.out, "./... ./...."); assert_eq!(actual.out, "./... ./....");
} }
#[test] #[apply(run_external_prefixes)]
fn external_command_url_args() { fn external_command_url_args(prefix: &str) {
// If ndots is not handled correctly, we can lose the double forward slashes that are needed // If ndots is not handled correctly, we can lose the double forward slashes that are needed
// here // here
let actual = nu!(r#" let actual = nu!(
nu --testbin cococo http://example.com http://example.com/.../foo //foo r#"
"#); {}nu `--testbin` cococo http://example.com http://example.com/.../foo //foo
"#,
prefix
);
assert_eq!( assert_eq!(
actual.out, actual.out,
@ -271,12 +311,12 @@ fn external_command_url_args() {
); );
} }
#[test] #[apply(run_external_prefixes)]
#[cfg_attr( #[cfg_attr(
not(target_os = "linux"), not(target_os = "linux"),
ignore = "only runs on Linux, where controlling the HOME var is reliable" ignore = "only runs on Linux, where controlling the HOME var is reliable"
)] )]
fn external_command_expand_tilde() { fn external_command_expand_tilde(prefix: &str) {
Playground::setup("external command expand tilde", |dirs, _| { Playground::setup("external command expand tilde", |dirs, _| {
// Make a copy of the nu executable that we can use // Make a copy of the nu executable that we can use
let mut src = std::fs::File::open(nu_test_support::fs::binaries().join("nu")) let mut src = std::fs::File::open(nu_test_support::fs::binaries().join("nu"))
@ -302,22 +342,27 @@ fn external_command_expand_tilde() {
("HOME".to_string(), dirs.test().to_string_lossy().into_owned()), ("HOME".to_string(), dirs.test().to_string_lossy().into_owned()),
], ],
r#" r#"
^~/test_nu --testbin cococo hello {}~/test_nu `--testbin` cococo hello
"# "#,
prefix
); );
assert_eq!(actual.out, "hello"); assert_eq!(actual.out, "hello");
}) })
} }
#[test] // FIXME: parser bug prevents expressions from appearing within GlobPattern substrings
fn external_arg_expand_tilde() { #[rstest]
#[case("")]
#[case("^")]
fn external_arg_expand_tilde(#[case] prefix: &str) {
Playground::setup("external arg expand tilde", |dirs, _| { Playground::setup("external arg expand tilde", |dirs, _| {
let actual = nu!( let actual = nu!(
cwd: dirs.test(), pipeline( cwd: dirs.test(),
r#" r#"
^nu --testbin cococo ~/foo ~/(2 + 2) {}nu `--testbin` cococo ~/foo ~/(2 + 2)
"# "#,
)); prefix
);
let home = dirs::home_dir().expect("failed to find home dir"); let home = dirs::home_dir().expect("failed to find home dir");
@ -332,40 +377,43 @@ fn external_arg_expand_tilde() {
}) })
} }
#[test] #[apply(run_external_prefixes)]
fn external_command_not_expand_tilde_with_quotes() { fn external_command_not_expand_tilde_with_quotes(prefix: &str) {
Playground::setup( Playground::setup(
"external command not expand tilde with quotes", "external command not expand tilde with quotes",
|dirs, _| { |dirs, _| {
let actual = nu!(cwd: dirs.test(), pipeline(r#"nu --testbin nonu "~""#)); let actual = nu!(cwd: dirs.test(), r#"{}nu `--testbin` nonu "~""#, prefix);
assert_eq!(actual.out, r#"~"#); assert_eq!(actual.out, r#"~"#);
}, },
) )
} }
#[test] #[apply(run_external_prefixes)]
fn external_command_expand_tilde_with_back_quotes() { fn external_command_expand_tilde_with_back_quotes(prefix: &str) {
Playground::setup( Playground::setup(
"external command not expand tilde with quotes", "external command not expand tilde with quotes",
|dirs, _| { |dirs, _| {
let actual = nu!(cwd: dirs.test(), pipeline(r#"nu --testbin nonu `~`"#)); let actual = nu!(cwd: dirs.test(), r#"{}nu `--testbin` nonu `~`"#, prefix);
assert!(!actual.out.contains('~')); assert!(!actual.out.contains('~'));
}, },
) )
} }
#[test] #[apply(run_external_prefixes)]
fn external_command_receives_raw_binary_data() { fn external_command_receives_raw_binary_data(prefix: &str) {
Playground::setup("external command receives raw binary data", |dirs, _| { Playground::setup("external command receives raw binary data", |dirs, _| {
let actual = let actual = nu!(
nu!(cwd: dirs.test(), pipeline("0x[deadbeef] | nu --testbin input_bytes_length")); cwd: dirs.test(),
"0x[deadbeef] | {}nu `--testbin` input_bytes_length",
prefix
);
assert_eq!(actual.out, r#"4"#); assert_eq!(actual.out, r#"4"#);
}) })
} }
#[cfg(windows)] #[cfg(windows)]
#[test] #[apply(run_external_prefixes)]
fn can_run_cmd_files() { fn can_run_cmd_files(prefix: &str) {
use nu_test_support::fs::Stub::FileWithContent; use nu_test_support::fs::Stub::FileWithContent;
Playground::setup("run a Windows cmd file", |dirs, sandbox| { Playground::setup("run a Windows cmd file", |dirs, sandbox| {
sandbox.with_files(&[FileWithContent( sandbox.with_files(&[FileWithContent(
@ -376,14 +424,14 @@ fn can_run_cmd_files() {
"#, "#,
)]); )]);
let actual = nu!(cwd: dirs.test(), pipeline("foo.cmd")); let actual = nu!(cwd: dirs.test(), "{}foo.cmd", prefix);
assert!(actual.out.contains("Hello World")); assert!(actual.out.contains("Hello World"));
}); });
} }
#[cfg(windows)] #[cfg(windows)]
#[test] #[apply(run_external_prefixes)]
fn can_run_batch_files() { fn can_run_batch_files(prefix: &str) {
use nu_test_support::fs::Stub::FileWithContent; use nu_test_support::fs::Stub::FileWithContent;
Playground::setup("run a Windows batch file", |dirs, sandbox| { Playground::setup("run a Windows batch file", |dirs, sandbox| {
sandbox.with_files(&[FileWithContent( sandbox.with_files(&[FileWithContent(
@ -394,14 +442,14 @@ fn can_run_batch_files() {
"#, "#,
)]); )]);
let actual = nu!(cwd: dirs.test(), pipeline("foo.bat")); let actual = nu!(cwd: dirs.test(), "{}foo.bat", prefix);
assert!(actual.out.contains("Hello World")); assert!(actual.out.contains("Hello World"));
}); });
} }
#[cfg(windows)] #[cfg(windows)]
#[test] #[apply(run_external_prefixes)]
fn can_run_batch_files_without_cmd_extension() { fn can_run_batch_files_without_cmd_extension(prefix: &str) {
use nu_test_support::fs::Stub::FileWithContent; use nu_test_support::fs::Stub::FileWithContent;
Playground::setup( Playground::setup(
"run a Windows cmd file without specifying the extension", "run a Windows cmd file without specifying the extension",
@ -414,15 +462,15 @@ fn can_run_batch_files_without_cmd_extension() {
"#, "#,
)]); )]);
let actual = nu!(cwd: dirs.test(), pipeline("foo")); let actual = nu!(cwd: dirs.test(), "{}foo", prefix);
assert!(actual.out.contains("Hello World")); assert!(actual.out.contains("Hello World"));
}, },
); );
} }
#[cfg(windows)] #[cfg(windows)]
#[test] #[apply(run_external_prefixes)]
fn can_run_batch_files_without_bat_extension() { fn can_run_batch_files_without_bat_extension(prefix: &str) {
use nu_test_support::fs::Stub::FileWithContent; use nu_test_support::fs::Stub::FileWithContent;
Playground::setup( Playground::setup(
"run a Windows batch file without specifying the extension", "run a Windows batch file without specifying the extension",
@ -435,34 +483,36 @@ fn can_run_batch_files_without_bat_extension() {
"#, "#,
)]); )]);
let actual = nu!(cwd: dirs.test(), pipeline("foo")); let actual = nu!(cwd: dirs.test(), "{}foo", prefix);
assert!(actual.out.contains("Hello World")); assert!(actual.out.contains("Hello World"));
}, },
); );
} }
#[test] #[apply(run_external_prefixes)]
fn quotes_trimmed_when_shelling_out() { fn quotes_trimmed_when_shelling_out(prefix: &str) {
// regression test for a bug where we weren't trimming quotes around string args before shelling out to cmd.exe // regression test for a bug where we weren't trimming quotes around string args before shelling out to cmd.exe
let actual = nu!(pipeline( let actual = nu!(
r#" r#"
nu --testbin cococo "foo" {}nu `--testbin` cococo "foo"
"# "#,
)); prefix
);
assert_eq!(actual.out, "foo"); assert_eq!(actual.out, "foo");
} }
#[cfg(not(windows))] #[cfg(not(windows))]
#[test] #[apply(run_external_prefixes)]
fn redirect_combine() { fn redirect_combine(prefix: &str) {
Playground::setup("redirect_combine", |dirs, _| { Playground::setup("redirect_combine", |dirs, _| {
let actual = nu!( let actual = nu!(
cwd: dirs.test(), pipeline( cwd: dirs.test(),
r#" r#"
run-external sh ...[-c 'echo Foo; echo >&2 Bar'] o+e>| print {}sh ...[-c 'echo Foo; echo >&2 Bar'] o+e>| print
"# "#,
)); prefix
);
// Lines are collapsed in the nu! macro // Lines are collapsed in the nu! macro
assert_eq!(actual.out, "FooBar"); assert_eq!(actual.out, "FooBar");
@ -470,8 +520,8 @@ fn redirect_combine() {
} }
#[cfg(windows)] #[cfg(windows)]
#[test] #[apply(run_external_prefixes)]
fn can_run_ps1_files() { fn can_run_ps1_files(prefix: &str) {
use nu_test_support::fs::Stub::FileWithContent; use nu_test_support::fs::Stub::FileWithContent;
Playground::setup("run_a_windows_ps_file", |dirs, sandbox| { Playground::setup("run_a_windows_ps_file", |dirs, sandbox| {
sandbox.with_files(&[FileWithContent( sandbox.with_files(&[FileWithContent(
@ -481,7 +531,7 @@ fn can_run_ps1_files() {
"#, "#,
)]); )]);
let actual = nu!(cwd: dirs.test(), pipeline("foo.ps1")); let actual = nu!(cwd: dirs.test(), "{}foo.ps1", prefix);
assert!(actual.out.contains("Hello World")); assert!(actual.out.contains("Hello World"));
}); });
} }