From f4205132c74f5bee077ca353ed4fbcbad3fdb4e5 Mon Sep 17 00:00:00 2001 From: Wind Date: Thu, 30 Jan 2025 20:34:56 +0800 Subject: [PATCH] add tests with display_error=True (#14939) # Description This is a follow up for pr: https://github.com/nushell/nushell/pull/13873 In that pr, I left 2 TODOs about tests, this pr is going to resolve them. # User-Facing Changes NaN # Tests + Formatting Added 2 tests --- tests/shell/pipeline/commands/external.rs | 40 +++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/tests/shell/pipeline/commands/external.rs b/tests/shell/pipeline/commands/external.rs index 172ac8cbfc..7268133ab0 100644 --- a/tests/shell/pipeline/commands/external.rs +++ b/tests/shell/pipeline/commands/external.rs @@ -1,4 +1,4 @@ -use nu_test_support::fs::Stub::EmptyFile; +use nu_test_support::fs::Stub::{EmptyFile, FileWithContent}; use nu_test_support::nu; use nu_test_support::playground::Playground; use pretty_assertions::assert_eq; @@ -627,7 +627,6 @@ fn exit_code_stops_execution_closure() { assert!(actual.err.contains("exited with code 1")); } -// TODO: need to add tests under display_error.exit_code = true #[test] fn exit_code_stops_execution_custom_command() { let actual = nu!("def cmd [] { nu -c 'exit 42'; 'ok1' }; cmd; print 'ok2'"); @@ -635,7 +634,6 @@ fn exit_code_stops_execution_custom_command() { assert!(!actual.err.contains("exited with code 42")); } -// TODO: need to add tests under display_error.exit_code = true #[test] fn exit_code_stops_execution_for_loop() { let actual = nu!("for x in [0 1] { nu -c 'exit 42'; print $x }"); @@ -643,6 +641,42 @@ fn exit_code_stops_execution_for_loop() { assert!(!actual.err.contains("exited with code 42")); } +#[test] +fn display_error_with_exit_code_stops() { + Playground::setup("errexit", |dirs, sandbox| { + sandbox.with_files(&[FileWithContent( + "tmp_env.nu", + "$env.config.display_errors.exit_code = true", + )]); + + let actual = nu!( + env_config: "tmp_env.nu", + cwd: dirs.test(), + "def cmd [] { nu -c 'exit 42'; 'ok1' }; cmd; print 'ok2'", + ); + assert!(actual.err.contains("exited with code")); + assert_eq!(actual.out, ""); + }); +} + +#[test] +fn display_error_exit_code_stops_execution_for_loop() { + Playground::setup("errexit", |dirs, sandbox| { + sandbox.with_files(&[FileWithContent( + "tmp_env.nu", + "$env.config.display_errors.exit_code = true", + )]); + + let actual = nu!( + env_config: "tmp_env.nu", + cwd: dirs.test(), + "for x in [0 1] { nu -c 'exit 42'; print $x }", + ); + assert!(actual.err.contains("exited with code")); + assert_eq!(actual.out, ""); + }); +} + #[test] fn arg_dont_run_subcommand_if_surrounded_with_quote() { let actual = nu!("nu --testbin cococo `(echo aa)`");