mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 11:45:50 +02:00
When using redirection, if a command generates non-zero exit code, the script should stop running (#11191)
# Description Fixes: #11153 To make sure scripts stop from running on non-zero exit code, we need to invoke `might_consume_external_result` on `PipelineData::ExternalStream`, so it can tell nushell if this command exists with non-zero exit code. And this pr also adjusts some test cases. # User-Facing Changes ```nushell ^false out> /dev/null; print "ok" ``` After this pr, it shouldn't print ok. # Tests + Formatting Done
This commit is contained in:
@ -161,29 +161,32 @@ fn same_target_redirection_with_too_much_stderr_not_hang_nushell() {
|
||||
|
||||
#[test]
|
||||
fn redirection_keep_exit_codes() {
|
||||
Playground::setup(
|
||||
"redirection should keep exit code the same",
|
||||
|dirs, sandbox| {
|
||||
let script_body = r#"exit 10"#;
|
||||
#[cfg(not(windows))]
|
||||
let output = {
|
||||
sandbox.with_files(vec![FileWithContent("test.sh", script_body)]);
|
||||
nu!(
|
||||
cwd: dirs.test(),
|
||||
"bash test.sh out> out.txt err> err.txt; echo $env.LAST_EXIT_CODE"
|
||||
)
|
||||
};
|
||||
#[cfg(windows)]
|
||||
let output = {
|
||||
sandbox.with_files(vec![FileWithContent("test.bat", script_body)]);
|
||||
nu!(
|
||||
cwd: dirs.test(),
|
||||
"cmd /D /c test.bat out> out.txt err> err.txt; echo $env.LAST_EXIT_CODE"
|
||||
)
|
||||
};
|
||||
assert_eq!(output.out, "10")
|
||||
},
|
||||
)
|
||||
let out = nu!("do -i { nu --testbin fail e> a.txt } | complete | get exit_code");
|
||||
// needs to use contains "1", because it complete will output `Some(RawStream)`.
|
||||
assert!(out.out.contains('1'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redirection_with_non_zero_exit_code_should_stop_from_running() {
|
||||
Playground::setup("redirection with non zero exit code", |dirs, _| {
|
||||
for redirection in ["o>", "o>>", "e>", "e>>", "o+e>", "o+e>>"] {
|
||||
let output = nu!(
|
||||
cwd: dirs.test(),
|
||||
&format!("nu --testbin fail {redirection} log.txt; echo 3")
|
||||
);
|
||||
assert!(!output.out.contains('3'));
|
||||
}
|
||||
});
|
||||
|
||||
Playground::setup("redirection with non zero exit code", |dirs, _| {
|
||||
for (out, err) in [("o>", "e>"), ("o>>", "e>"), ("o>", "e>>"), ("o>>", "e>>")] {
|
||||
let output = nu!(
|
||||
cwd: dirs.test(),
|
||||
&format!("nu --testbin fail {out} log.txt {err} err_log.txt; echo 3")
|
||||
);
|
||||
assert!(!output.out.contains('3'));
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Reference in New Issue
Block a user