Ensure end_filter plugin lifecycle stage gets called.

This commit is contained in:
Andrés N. Robalino 2020-05-29 03:10:15 -05:00
parent 360e8340d1
commit 48ee20782f
4 changed files with 74 additions and 34 deletions

View File

@ -194,7 +194,7 @@ pub fn filter_plugin(
}
}
// End of the stream
// post stream contents
{
let stdin = child.stdin.as_mut().expect("Failed to open stdin");
let stdout = child.stdout.as_mut().expect("Failed to open stdout");
@ -202,15 +202,23 @@ pub fn filter_plugin(
let mut reader = BufReader::new(stdout);
let request: JsonRpc<std::vec::Vec<Value>> = JsonRpc::new("end_filter", vec![]);
let request_raw = match serde_json::to_string(&request) {
Ok(req) => req,
Err(err) => {
yield Err(ShellError::unexpected(format!("{}", err)));
return;
}
};
let request_raw = serde_json::to_string(&request);
let _ = stdin.write(format!("{}\n", request_raw).as_bytes()); // TODO: Handle error
match request_raw {
Err(_) => {
yield Err(ShellError::labeled_error(
"Could not load json from plugin",
"could not load json from plugin",
&call_info.name_tag,
));
}
Ok(request_raw) => match stdin.write(format!("{}\n", request_raw).as_bytes()) {
Ok(_) => {}
Err(err) => {
yield Err(ShellError::unexpected(format!("{}", err)));
}
},
}
let mut input = String::new();
match reader.read_line(&mut input) {
@ -218,26 +226,7 @@ pub fn filter_plugin(
let response = serde_json::from_str::<NuResult>(&input);
match response {
Ok(NuResult::response { params }) => match params {
Ok(params) => {
let request: JsonRpc<std::vec::Vec<Value>> =
JsonRpc::new("quit", vec![]);
let request_raw = serde_json::to_string(&request);
match request_raw {
Ok(request_raw) => {
let _ = stdin.write(format!("{}\n", request_raw).as_bytes()); // TODO: Handle error
}
Err(e) => {
yield Err(ShellError::untagged_runtime_error(format!(
"Error while processing begin_filter response: {:?} {}",
e, input
)));
return;
}
}
//yield ReturnValue::Ok(params)
//yield ReturnSuccess::value(Value)
}
Ok(params) => for param in params { yield param },
Err(e) => {
yield ReturnValue::Err(e);
}
@ -252,14 +241,38 @@ pub fn filter_plugin(
}
Err(e) => {
yield Err(ShellError::untagged_runtime_error(format!(
"Error while reading end_filter: {:?}",
"Error while reading end_filter response: {:?}",
e
)));
}
};
let _ = child.wait();
}
}
// End of the stream
{
let stdin = child.stdin.as_mut().expect("Failed to open stdin");
let stdout = child.stdout.as_mut().expect("Failed to open stdout");
let mut reader = BufReader::new(stdout);
let request: JsonRpc<std::vec::Vec<Value>> = JsonRpc::new("quit", vec![]);
let request_raw = serde_json::to_string(&request);
match request_raw {
Ok(request_raw) => {
let _ = stdin.write(format!("{}\n", request_raw).as_bytes()); // TODO: Handle error
}
Err(e) => {
yield Err(ShellError::untagged_runtime_error(format!(
"Error while processing quit response: {:?}",
e
)));
return;
}
}
}
let _ = child.wait();
};
Ok(stream.to_output_stream())

View File

@ -1,5 +1,5 @@
# average
This command allows you to calculate the average of values in a column.
Calculate the average of values in a column.
## Examples
To get the average of the file sizes in a directory, simply pipe the size column from the ls command to the average command.

View File

@ -0,0 +1,26 @@
use nu_test_support::{nu, pipeline};
#[test]
fn can_average_numbers() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
open sgml_description.json
| get glossary.GlossDiv.GlossList.GlossEntry.Sections
| average
| echo $it
"#
));
assert_eq!(actual.out, "101.5")
}
#[test]
fn can_average_bytes() {
let actual = nu!(
cwd: "tests/fixtures/formats",
"ls | sort-by name | skip 1 | first 2 | get size | average | format \"{$it}\" | echo $it"
);
assert_eq!(actual.out, "1.6 KB");
}

View File

@ -1 +1,2 @@
mod core_inc;
mod decommission_average;