Properly redirect invocations (#2070)

* Properly redirect invocations

* Don't convert with-env yet, as there's a random test failure
This commit is contained in:
Jonathan Turner 2020-06-28 09:04:57 +12:00 committed by GitHub
parent 8b3964f518
commit dffc9c9b1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 4 deletions

View File

@ -61,16 +61,18 @@ async fn do_(
registry: &CommandRegistry, registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let registry = registry.clone(); let registry = registry.clone();
let is_last = raw_args.call_info.args.is_last;
let mut context = Context::from_raw(&raw_args, &registry); let mut context = Context::from_raw(&raw_args, &registry);
let scope = raw_args.call_info.scope.clone(); let scope = raw_args.call_info.scope.clone();
let ( let (
DoArgs { DoArgs {
ignore_errors, ignore_errors,
block, mut block,
}, },
input, input,
) = raw_args.process(&registry).await?; ) = raw_args.process(&registry).await?;
block.set_is_last(!is_last);
let result = run_block( let result = run_block(
&block, &block,

View File

@ -40,7 +40,9 @@ impl WholeStreamCommand for AliasCommand {
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let call_info = args.call_info.clone(); let call_info = args.call_info.clone();
let registry = registry.clone(); let registry = registry.clone();
let block = self.block.clone(); let mut block = self.block.clone();
block.set_is_last(!call_info.args.is_last);
let alias_command = self.clone(); let alias_command = self.clone();
let mut context = Context::from_args(&args, &registry); let mut context = Context::from_args(&args, &registry);
let input = args.input; let input = args.input;

View File

@ -196,6 +196,9 @@ async fn evaluate_invocation(
let input = InputStream::empty(); let input = InputStream::empty();
let mut block = block.clone();
block.set_is_last(true);
let result = run_block(&block, &mut context, input, it, vars, env).await?; let result = run_block(&block, &mut context, input, it, vars, env).await?;
let output = result.into_vec().await; let output = result.into_vec().await;

View File

@ -200,6 +200,28 @@ impl Block {
commands.expand_it_usage(); commands.expand_it_usage();
} }
} }
pub fn set_is_last(&mut self, is_last: bool) {
if let Some(pipeline) = self.block.last_mut() {
if let Some(command) = pipeline.list.last_mut() {
if let ClassifiedCommand::Internal(internal) = command {
internal.args.is_last = is_last;
}
}
}
}
pub fn get_is_last(&mut self) -> Option<bool> {
if let Some(pipeline) = self.block.last_mut() {
if let Some(command) = pipeline.list.last_mut() {
if let ClassifiedCommand::Internal(internal) = command {
return Some(internal.args.is_last);
}
}
}
None
}
} }
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Hash, Deserialize, Serialize)] #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Hash, Deserialize, Serialize)]

View File

@ -84,13 +84,25 @@ fn it_expansion_of_invocation() {
assert_eq!(actual.out, "4"); assert_eq!(actual.out, "4");
} }
#[test]
fn invocation_properly_redirects() {
let actual = nu!(
cwd: ".",
r#"
echo $(nu --testbin cococo "hello") | str collect
"#
);
assert_eq!(actual.out, "hello");
}
#[test] #[test]
fn argument_invocation() { fn argument_invocation() {
let actual = nu!( let actual = nu!(
cwd: ".", cwd: ".",
r#" r#"
echo "foo" | echo $(echo $it) echo "foo" | echo $(echo $it)
"# "#
); );
assert_eq!(actual.out, "foo"); assert_eq!(actual.out, "foo");