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
This commit is contained in:
Wind 2025-01-30 20:34:56 +08:00 committed by GitHub
parent 5eae08ac76
commit f4205132c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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)`");