Fix each while behavior when printing. (#6897)

Fixes #6895

Warning: `Iterator::map_while` does not return a `FusedIterator`!
Depending on the consuming adaptor or code (e.g. for loop) the iteration
may be stopped but this is not guaranteed.

Adds a test example but Examples handle iterators like
`FusedIterator` and thus don't catch the regression

Cleanup the message on another test
This commit is contained in:
Stefan Holderbach 2022-10-27 23:45:45 +02:00 committed by GitHub
parent f281cd5aa3
commit 834522d002
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -36,24 +36,42 @@ impl Command for EachWhile {
fn examples(&self) -> Vec<Example> {
let stream_test_1 = vec![
Value::Int {
val: 1,
val: 2,
span: Span::test_data(),
},
Value::Int {
val: 2,
val: 4,
span: Span::test_data(),
},
];
let stream_test_2 = vec![
Value::String {
val: "Output: 1".into(),
span: Span::test_data(),
},
Value::String {
val: "Output: 2".into(),
span: Span::test_data(),
},
];
vec![
Example {
example: "[1 2 3] | each while { |it| if $it < 3 { $it } else { null } }",
description: "Multiplies elements in list",
example: "[1 2 3] | each while { |it| if $it < 3 { $it * 2 } else { null } }",
description: "Multiplies elements below three by two",
result: Some(Value::List {
vals: stream_test_1,
span: Span::test_data(),
}),
},
Example {
example: r#"[1 2 stop 3 4] | each while { |it| if $it == 'stop' { null } else { $"Output: ($it)" } }"#,
description: "Output elements till reaching 'stop'",
result: Some(Value::List {
vals: stream_test_2,
span: Span::test_data(),
}),
},
Example {
example: r#"[1 2 3] | each while -n { |it| if $it.item < 2 { $"value ($it.item) at ($it.index)!"} else { null } }"#,
description: "Iterate over each element, print the matching value and its index",
@ -140,6 +158,7 @@ impl Command for EachWhile {
Err(_) => None,
}
})
.fuse()
.into_pipeline_data(ctrlc)),
PipelineData::ExternalStream { stdout: None, .. } => Ok(PipelineData::new(call.head)),
PipelineData::ExternalStream {
@ -198,6 +217,7 @@ impl Command for EachWhile {
Err(_) => None,
}
})
.fuse()
.into_pipeline_data(ctrlc)),
PipelineData::Value(x, ..) => {
if let Some(var) = block.signature.get_positional(0) {