Do not panic when failing to decode lines from external stdout (#1364)

This commit is contained in:
Jason Gedge 2020-02-10 10:37:48 -05:00 committed by GitHub
parent dc50e61f26
commit a29d52158e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 8 deletions

View file

@ -195,6 +195,11 @@ name = "cococo"
path = "crates/nu-test-support/src/bins/cococo.rs" path = "crates/nu-test-support/src/bins/cococo.rs"
required-features = ["test-bins"] required-features = ["test-bins"]
[[bin]]
name = "nonu"
path = "crates/nu-test-support/src/bins/nonu.rs"
required-features = ["test-bins"]
# Core plugins that ship with `cargo install nu` by default # Core plugins that ship with `cargo install nu` by default
# Currently, Cargo limits us to installing only one binary # Currently, Cargo limits us to installing only one binary
# unless we use [[bin]], so we use this as a workaround # unless we use [[bin]], so we use this as a workaround

View file

@ -0,0 +1,3 @@
fn main() {
std::env::args().skip(1).for_each(|arg| print!("{}", arg));
}

View file

@ -285,19 +285,27 @@ fn spawn(
}; };
let file = futures::io::AllowStdIo::new(stdout); let file = futures::io::AllowStdIo::new(stdout);
let mut stream = FramedRead::new(file, LinesCodec).map(|line| { let mut stream = FramedRead::new(file, LinesCodec);
while let Some(line) = stream.next().await {
if let Ok(line) = line { if let Ok(line) = line {
Value { yield Ok(Value {
value: UntaggedValue::Primitive(Primitive::Line(line)), value: UntaggedValue::Primitive(Primitive::Line(line)),
tag: name_tag.clone(), tag: name_tag.clone(),
} });
} else { } else {
panic!("Internal error: could not read lines of text from stdin") yield Ok(Value {
value: UntaggedValue::Error(
ShellError::labeled_error(
"Unable to read lines from stdout. This usually happens when the output does not end with a newline.",
"unable to read from stdout",
&name_tag,
)
),
tag: name_tag.clone(),
});
return;
} }
});
while let Some(item) = stream.next().await {
yield Ok(item)
} }
} }

View file

@ -78,6 +78,24 @@ mod it_evaluation {
} }
} }
mod stdin_evaluation {
use super::nu_error;
use nu_test_support::pipeline;
#[test]
fn does_not_panic_with_no_newline_in_stream() {
let stderr = nu_error!(
cwd: ".",
pipeline(r#"
nonu "where's the nuline?"
| count
"#
));
assert_eq!(stderr, "");
}
}
mod external_words { mod external_words {
use super::nu; use super::nu;