From 86a0e770651ab94e09cac3f3398e32f930ea8562 Mon Sep 17 00:00:00 2001 From: nibon7 Date: Tue, 26 Jul 2022 09:11:46 +0800 Subject: [PATCH] Fix print_table_or_error when `table` is overridden (#6130) Related #6113 Signed-off-by: nibon7 --- crates/nu-cli/src/eval_file.rs | 33 ++++++++++++++++++------------- src/tests/test_custom_commands.rs | 7 +++++++ 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/crates/nu-cli/src/eval_file.rs b/crates/nu-cli/src/eval_file.rs index be7630c52d..c1a481bbe6 100644 --- a/crates/nu-cli/src/eval_file.rs +++ b/crates/nu-cli/src/eval_file.rs @@ -77,23 +77,28 @@ pub fn print_table_or_error( match engine_state.find_decl("table".as_bytes(), &[]) { Some(decl_id) => { - let table = engine_state.get_decl(decl_id).run( - engine_state, - stack, - &Call::new(Span::new(0, 0)), - pipeline_data, - ); + let command = engine_state.get_decl(decl_id); + if command.get_block_id().is_some() { + print_or_exit(pipeline_data, engine_state, config); + } else { + let table = command.run( + engine_state, + stack, + &Call::new(Span::new(0, 0)), + pipeline_data, + ); - match table { - Ok(table) => { - print_or_exit(table, engine_state, config); - } - Err(error) => { - let working_set = StateWorkingSet::new(engine_state); + match table { + Ok(table) => { + print_or_exit(table, engine_state, config); + } + Err(error) => { + let working_set = StateWorkingSet::new(engine_state); - report_error(&working_set, &error); + report_error(&working_set, &error); - std::process::exit(1); + std::process::exit(1); + } } } } diff --git a/src/tests/test_custom_commands.rs b/src/tests/test_custom_commands.rs index 2c668eb15f..f961438b2a 100644 --- a/src/tests/test_custom_commands.rs +++ b/src/tests/test_custom_commands.rs @@ -1,4 +1,5 @@ use crate::tests::{fail_test, run_test, run_test_contains, TestResult}; +use nu_test_support::nu; #[test] fn no_scope_leak1() -> TestResult { @@ -135,3 +136,9 @@ fn help_not_present_in_extern() -> TestResult { fn override_table() -> TestResult { run_test(r#"def table [] { "hi" }; table"#, "hi") } + +#[test] +fn override_table_eval_file() { + let actual = nu!(cwd: ".", r#"def table [] { "hi" }; table"#); + assert_eq!(actual.out, "hi"); +}