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:
WindSoilder
2023-12-01 01:52:02 +08:00
committed by GitHub
parent 250ee62e16
commit 80881c75f9
2 changed files with 47 additions and 51 deletions

View File

@ -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]