mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 00:13:21 +01:00
make stderr works for failed external command (#11914)
# Description Fixes: #11913 When running external command, nushell shouldn't consumes stderr messages, if user want to redirect stderr. # User-Facing Changes NaN # Tests + Formatting Done # After Submitting NaN
This commit is contained in:
parent
6e590fe0a2
commit
1058707a29
@ -171,6 +171,22 @@ fn redirection_keep_exit_codes() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn redirection_stderr_with_failed_program() {
|
||||||
|
Playground::setup("redirection stderr with failed program", |dirs, _| {
|
||||||
|
let out = nu!(
|
||||||
|
cwd: dirs.test(),
|
||||||
|
r#"$env.FOO = "bar"; nu --testbin echo_env_stderr_fail FOO e> file.txt; echo 3"#
|
||||||
|
);
|
||||||
|
// firstly echo 3 shouldn't run, because previous command runs to failed.
|
||||||
|
// second `file.txt` should contain "bar".
|
||||||
|
assert!(!out.out.contains('3'));
|
||||||
|
let expected_file = dirs.test().join("file.txt");
|
||||||
|
let actual = file_contents(expected_file);
|
||||||
|
assert_eq!(actual, "bar\n");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn redirection_with_non_zero_exit_code_should_stop_from_running() {
|
fn redirection_with_non_zero_exit_code_should_stop_from_running() {
|
||||||
Playground::setup("redirection with non zero exit code", |dirs, _| {
|
Playground::setup("redirection with non zero exit code", |dirs, _| {
|
||||||
|
@ -383,7 +383,21 @@ pub fn eval_expression_with_input(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(might_consume_external_result(input))
|
// Given input is PipelineData::ExternalStream
|
||||||
|
// `might_consume_external_result` will consume `stderr` stream if `stdout` is empty.
|
||||||
|
// it's not intended if user want to redirect stderr message.
|
||||||
|
//
|
||||||
|
// e.g:
|
||||||
|
// 1. cargo check e>| less
|
||||||
|
// 2. cargo check e> result.txt
|
||||||
|
//
|
||||||
|
// In these two cases, stdout will be empty, but nushell shouldn't consume the `stderr`
|
||||||
|
// stream it needs be passed to next command.
|
||||||
|
if !redirect_stderr {
|
||||||
|
Ok(might_consume_external_result(input))
|
||||||
|
} else {
|
||||||
|
Ok((input, false))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to catch and detect if external command runs to failed.
|
// Try to catch and detect if external command runs to failed.
|
||||||
|
@ -269,6 +269,7 @@ fn main() -> Result<()> {
|
|||||||
match testbin.item.as_str() {
|
match testbin.item.as_str() {
|
||||||
"echo_env" => test_bins::echo_env(true),
|
"echo_env" => test_bins::echo_env(true),
|
||||||
"echo_env_stderr" => test_bins::echo_env(false),
|
"echo_env_stderr" => test_bins::echo_env(false),
|
||||||
|
"echo_env_stderr_fail" => test_bins::echo_env_and_fail(false),
|
||||||
"echo_env_mixed" => test_bins::echo_env_mixed(),
|
"echo_env_mixed" => test_bins::echo_env_mixed(),
|
||||||
"cococo" => test_bins::cococo(),
|
"cococo" => test_bins::cococo(),
|
||||||
"meow" => test_bins::meow(),
|
"meow" => test_bins::meow(),
|
||||||
|
@ -16,6 +16,11 @@ pub fn echo_env(to_stdout: bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn echo_env_and_fail(to_stdout: bool) {
|
||||||
|
echo_env(to_stdout);
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
|
||||||
fn echo_one_env(arg: &str, to_stdout: bool) {
|
fn echo_one_env(arg: &str, to_stdout: bool) {
|
||||||
if let Ok(v) = std::env::var(arg) {
|
if let Ok(v) = std::env::var(arg) {
|
||||||
if to_stdout {
|
if to_stdout {
|
||||||
|
@ -158,6 +158,14 @@ fn basic_outerr_pipe_works() {
|
|||||||
assert_eq!(actual.out, "8");
|
assert_eq!(actual.out, "8");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn err_pipe_with_failed_external_works() {
|
||||||
|
let actual =
|
||||||
|
nu!(r#"with-env [FOO "bar"] { nu --testbin echo_env_stderr_fail FOO e>| str length }"#);
|
||||||
|
// there is a `newline` output from nu --testbin
|
||||||
|
assert_eq!(actual.out, "4");
|
||||||
|
}
|
||||||
|
|
||||||
mod it_evaluation {
|
mod it_evaluation {
|
||||||
use super::nu;
|
use super::nu;
|
||||||
use nu_test_support::fs::Stub::{EmptyFile, FileWithContent, FileWithContentToBeTrimmed};
|
use nu_test_support::fs::Stub::{EmptyFile, FileWithContent, FileWithContentToBeTrimmed};
|
||||||
|
Loading…
Reference in New Issue
Block a user