fix exit code (#5835)

* fix exit code

* fix usage

* add comment
This commit is contained in:
WindSoilder 2022-06-20 22:05:11 +08:00 committed by GitHub
parent dbcfcdae89
commit fab3f8fd40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 7 deletions

View File

@ -18,7 +18,7 @@ pub fn evaluate_commands(
input: PipelineData, input: PipelineData,
is_perf_true: bool, is_perf_true: bool,
table_mode: Option<Value>, table_mode: Option<Value>,
) -> Result<()> { ) -> Result<Option<i64>> {
// Run a command (or commands) given to us by the user // Run a command (or commands) given to us by the user
let (block, delta) = { let (block, delta) = {
if let Some(ref t_mode) = table_mode { if let Some(ref t_mode) = table_mode {
@ -74,7 +74,7 @@ pub fn evaluate_commands(
std::process::exit(1); std::process::exit(1);
} }
match eval_block(engine_state, stack, &block, input, false, false) { let exit_code = match eval_block(engine_state, stack, &block, input, false, false) {
Ok(pipeline_data) => { Ok(pipeline_data) => {
crate::eval_file::print_table_or_error(engine_state, stack, pipeline_data, &mut config) crate::eval_file::print_table_or_error(engine_state, stack, pipeline_data, &mut config)
} }
@ -84,11 +84,11 @@ pub fn evaluate_commands(
report_error(&working_set, &err); report_error(&working_set, &err);
std::process::exit(1); std::process::exit(1);
} }
} };
if is_perf_true { if is_perf_true {
info!("evaluate {}:{}:{}", file!(), line!(), column!()); info!("evaluate {}:{}:{}", file!(), line!(), column!());
} }
Ok(()) Ok(exit_code)
} }

View File

@ -66,7 +66,7 @@ pub fn print_table_or_error(
stack: &mut Stack, stack: &mut Stack,
mut pipeline_data: PipelineData, mut pipeline_data: PipelineData,
config: &mut Config, config: &mut Config,
) { ) -> Option<i64> {
let exit_code = match &mut pipeline_data { let exit_code = match &mut pipeline_data {
PipelineData::ExternalStream { exit_code, .. } => exit_code.take(), PipelineData::ExternalStream { exit_code, .. } => exit_code.take(),
_ => None, _ => None,
@ -130,6 +130,14 @@ pub fn print_table_or_error(
// Make sure everything has finished // Make sure everything has finished
if let Some(exit_code) = exit_code { if let Some(exit_code) = exit_code {
let _: Vec<_> = exit_code.into_iter().collect(); let mut exit_code: Vec<_> = exit_code.into_iter().collect();
exit_code
.pop()
.and_then(|last_exit_code| match last_exit_code {
Value::Int { val: code, .. } => Some(code),
_ => None,
})
} else {
None
} }
} }

View File

@ -237,7 +237,11 @@ fn main() -> Result<()> {
if is_perf_true() { if is_perf_true() {
info!("-c command execution {}:{}:{}", file!(), line!(), column!()); info!("-c command execution {}:{}:{}", file!(), line!(), column!());
} }
ret_val match ret_val {
Ok(Some(exit_code)) => std::process::exit(exit_code as i32),
Ok(None) => Ok(()),
Err(e) => Err(e),
}
} else if !script_name.is_empty() && binary_args.interactive_shell.is_none() { } else if !script_name.is_empty() && binary_args.interactive_shell.is_none() {
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
read_plugin_file( read_plugin_file(

View File

@ -287,6 +287,8 @@ mod external_words {
} }
mod nu_commands { mod nu_commands {
use nu_test_support::playground::Playground;
use super::nu; use super::nu;
#[test] #[test]
@ -298,6 +300,18 @@ mod nu_commands {
assert_eq!(actual.out, "foo"); assert_eq!(actual.out, "foo");
} }
#[test]
fn failed_with_proper_exit_code() {
Playground::setup("external failed", |dirs, _sandbox| {
let actual = nu!(cwd: dirs.test(), r#"
nu -c "cargo build; print $env.LAST_EXIT_CODE"
"#);
// cargo for non rust project's exit code is 101.
assert_eq!(actual.out, "101")
})
}
#[test] #[test]
fn better_arg_quoting() { fn better_arg_quoting() {
let actual = nu!(cwd: ".", r#" let actual = nu!(cwd: ".", r#"