Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let with pipeline #9589

Merged
merged 4 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/nu-cli/tests/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ fn external_completer_trailing_space() {

#[test]
fn external_completer_no_trailing_space() {
let block = "let external_completer = {|spans| $spans}";
let block = "{|spans| $spans}";
let input = "gh alias".to_string();

let suggestions = run_external_completion(block, &input);
Expand All @@ -154,7 +154,7 @@ fn external_completer_no_trailing_space() {

#[test]
fn external_completer_pass_flags() {
let block = "let external_completer = {|spans| $spans}";
let block = "{|spans| $spans}";
let input = "gh api --".to_string();

let suggestions = run_external_completion(block, &input);
Expand Down
23 changes: 9 additions & 14 deletions crates/nu-cmd-lang/src/core_commands/let_.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use nu_engine::eval_expression_with_input;
use nu_engine::eval_block;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type};
Expand Down Expand Up @@ -54,28 +54,23 @@ impl Command for Let {
.as_var()
.expect("internal error: missing variable");

let keyword_expr = call
let block_id = call
.positional_nth(1)
.expect("checked through parser")
.as_keyword()
.expect("internal error: missing keyword");
.as_block()
.expect("internal error: missing right hand side");

let (rhs, external_failed) = eval_expression_with_input(
let block = engine_state.get_block(block_id);
let pipeline_data = eval_block(
engine_state,
stack,
keyword_expr,
block,
input,
call.redirect_stdout,
call.redirect_stderr,
)?;
if external_failed {
// rhs must be a PipelineData::ExternalStream and it's failed
// return the failed stream (with a non-zero exit code) so the engine knows to stop running
Ok(rhs)
} else {
stack.add_var(var_id, rhs.into_value(call.head));
Ok(PipelineData::empty())
}
stack.add_var(var_id, pipeline_data.into_value(call.head));
Ok(PipelineData::empty())
}

fn examples(&self) -> Vec<Example> {
Expand Down
27 changes: 27 additions & 0 deletions crates/nu-command/tests/commands/let_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,35 @@ fn let_doesnt_mutate() {
assert!(actual.err.contains("immutable"));
}

#[test]
fn let_takes_pipeline() {
let actual = nu!(
cwd: ".", pipeline(
r#"
let x = "hello world" | str length; print $x
"#
));

assert_eq!(actual.out, "11");
}

#[test]
fn let_pipeline_allows_in() {
let actual = nu!(
cwd: ".", pipeline(
r#"
def foo [] { let x = $in | str length; print ($x + 10) }; "hello world" | foo
"#
));

assert_eq!(actual.out, "21");
}

#[ignore]
#[test]
fn let_with_external_failed() {
// FIXME: this test hasn't run successfully for a long time. We should
// bring it back to life at some point.
let actual = nu!(
cwd: ".",
pipeline(r#"let x = nu --testbin outcome_err "aa"; echo fail"#)
Expand Down
8 changes: 1 addition & 7 deletions crates/nu-parser/src/lite_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,11 @@ pub enum LiteElement {
},
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct LitePipeline {
pub commands: Vec<LiteElement>,
}

impl Default for LitePipeline {
fn default() -> Self {
Self::new()
}
}

impl LitePipeline {
pub fn new() -> Self {
Self { commands: vec![] }
Expand Down