diff --git a/crates/nu-command/src/filters/each.rs b/crates/nu-command/src/filters/each.rs index 7f77917b85..412477a36f 100644 --- a/crates/nu-command/src/filters/each.rs +++ b/crates/nu-command/src/filters/each.rs @@ -90,6 +90,11 @@ with 'transpose' first."# Value::nothing(Span::test_data()), ])), }, + Example { + example: r#"$env.name? | each { $"hello ($in)" } | default "bye""#, + description: "Update value if not null, otherwise do nothing", + result: None, + }, ] } @@ -107,6 +112,7 @@ with 'transpose' first."# let metadata = input.metadata(); match input { PipelineData::Empty => Ok(PipelineData::empty()), + PipelineData::Value(Value::Nothing { .. }, ..) => Ok(input), PipelineData::Value(Value::Range { .. }, ..) | PipelineData::Value(Value::List { .. }, ..) | PipelineData::ListStream(..) => { diff --git a/crates/nu-command/tests/commands/each.rs b/crates/nu-command/tests/commands/each.rs index b3c04af0a4..b03ffe590c 100644 --- a/crates/nu-command/tests/commands/each.rs +++ b/crates/nu-command/tests/commands/each.rs @@ -63,3 +63,10 @@ fn errors_in_nested_each_show() { let actual = nu!("[[1,2]] | each {|x| $x | each {|y| error make {msg: \"oh noes\"} } }"); assert!(actual.err.contains("oh noes")) } + +#[test] +fn each_noop_on_single_null() { + let actual = nu!("null | each { \"test\" } | describe"); + + assert_eq!(actual.out, "nothing"); +} diff --git a/tests/repl/test_engine.rs b/tests/repl/test_engine.rs index 22d81f08cb..d29325eb80 100644 --- a/tests/repl/test_engine.rs +++ b/tests/repl/test_engine.rs @@ -1,4 +1,4 @@ -use crate::repl::tests::{TestResult, fail_test, run_test}; +use crate::repl::tests::{TestResult, fail_test, run_test, run_test_contains}; use rstest::rstest; #[test] @@ -87,7 +87,8 @@ fn in_used_in_range_to() -> TestResult { #[test] fn help_works_with_missing_requirements() -> TestResult { - run_test(r#"each --help | lines | length"#, "72") + fail_test(r#"each"#, "missing_positional")?; + run_test_contains(r#"each --help"#, "Usage") } #[rstest]