From 6b4634b293abf66cb5471d9e1096c557719fb835 Mon Sep 17 00:00:00 2001 From: k-brk <25877802+k-brk@users.noreply.github.com> Date: Wed, 22 Jul 2020 23:41:34 +0200 Subject: [PATCH] Convert table of primitives to positional arguments for external cmd (#2232) * Convert table of primitives to positional arguments for external cmd * Multiple file test, fix for cococo --- .../src/commands/classified/external.rs | 26 ++++++++++++++-- crates/nu-cli/src/utils/test_bins.rs | 7 +---- crates/nu-cli/tests/commands/with_env.rs | 2 +- tests/shell/pipeline/commands/external.rs | 31 +++++++++++++++++++ 4 files changed, 56 insertions(+), 10 deletions(-) diff --git a/crates/nu-cli/src/commands/classified/external.rs b/crates/nu-cli/src/commands/classified/external.rs index 8aae1165d8..fedda6309f 100644 --- a/crates/nu-cli/src/commands/classified/external.rs +++ b/crates/nu-cli/src/commands/classified/external.rs @@ -62,9 +62,29 @@ async fn run_with_stdin( } // Do the cleanup that we need to do on any argument going out: - let trimmed_value_string = value.as_string()?.trim_end_matches('\n').to_string(); - - command_args.push(trimmed_value_string); + match &value.value { + UntaggedValue::Table(table) => { + for t in table { + match &t.value { + UntaggedValue::Primitive(_) => { + command_args + .push(t.convert_to_string().trim_end_matches('\n').to_string()); + } + _ => { + return Err(ShellError::labeled_error( + "Could not convert to positional arguments", + "could not convert to positional arguments", + value.tag(), + )); + } + } + } + } + _ => { + let trimmed_value_string = value.as_string()?.trim_end_matches('\n').to_string(); + command_args.push(trimmed_value_string); + } + } } let process_args = command_args diff --git a/crates/nu-cli/src/utils/test_bins.rs b/crates/nu-cli/src/utils/test_bins.rs index eb8a5c098c..78b727dfd7 100644 --- a/crates/nu-cli/src/utils/test_bins.rs +++ b/crates/nu-cli/src/utils/test_bins.rs @@ -7,12 +7,7 @@ pub fn cococo() { // Write back out all the arguments passed // if given at least 1 instead of chickens // speaking co co co. - let mut arguments = args.iter(); - arguments.next(); - - for arg in arguments { - println!("{}", &arg); - } + println!("{}", &args[1..].join(" ")); } else { println!("cococo"); } diff --git a/crates/nu-cli/tests/commands/with_env.rs b/crates/nu-cli/tests/commands/with_env.rs index 15b4f70d40..084652d2e2 100644 --- a/crates/nu-cli/tests/commands/with_env.rs +++ b/crates/nu-cli/tests/commands/with_env.rs @@ -27,7 +27,7 @@ fn shorthand_doesnt_reorder_arguments() { "FOO=BARRRR nu --testbin cococo first second" ); - assert_eq!(actual.out, "firstsecond"); + assert_eq!(actual.out, "first second"); } #[test] diff --git a/tests/shell/pipeline/commands/external.rs b/tests/shell/pipeline/commands/external.rs index d82009291b..2b7e7de8df 100644 --- a/tests/shell/pipeline/commands/external.rs +++ b/tests/shell/pipeline/commands/external.rs @@ -271,3 +271,34 @@ mod tilde_expansion { assert_eq!(actual.out, "1~1"); } } + +mod external_command_arguments { + use super::nu; + use nu_test_support::fs::Stub::EmptyFile; + use nu_test_support::{pipeline, playground::Playground}; + #[test] + fn expands_table_of_primitives_to_positional_arguments() { + Playground::setup( + "expands_table_of_primitives_to_positional_arguments", + |dirs, sandbox| { + sandbox.with_files(vec![ + EmptyFile("jonathan_likes_cake.txt"), + EmptyFile("andres_likes_arepas.txt"), + EmptyFile("ferris_not_here.txt"), + ]); + + let actual = nu!( + cwd: dirs.test(), pipeline( + r#" + nu --testbin cococo $(ls | get name) + "# + )); + + assert_eq!( + actual.out, + "andres_likes_arepas.txt ferris_not_here.txt jonathan_likes_cake.txt" + ); + }, + ) + } +}