add echo_env_mixed testbin to reduce windows only tests (#11172)

# Description
We have seen some test cases which requires to output message to both
stdout and stderr, especially in redirection scenario.

This pr is going to introduce a new echo_env_mixed testbin, so we can
have less tests which only runs on windows.

# User-Facing Changes
NaN

# Tests + Formatting
NaN

# After Submitting
NaN
This commit is contained in:
WindSoilder
2023-11-28 20:42:35 +08:00
committed by GitHub
parent fa83458a6d
commit 182b0ab4fb
3 changed files with 110 additions and 169 deletions

View File

@ -228,6 +228,7 @@ fn main() -> Result<()> {
match testbin.item.as_str() {
"echo_env" => test_bins::echo_env(true),
"echo_env_stderr" => test_bins::echo_env(false),
"echo_env_mixed" => test_bins::echo_env_mixed(),
"cococo" => test_bins::cococo(),
"meow" => test_bins::meow(),
"meowb" => test_bins::meowb(),

View File

@ -12,16 +12,51 @@ use std::io::{self, BufRead, Read, Write};
pub fn echo_env(to_stdout: bool) {
let args = args();
for arg in args {
if let Ok(v) = std::env::var(arg) {
if to_stdout {
println!("{v}");
} else {
eprintln!("{v}");
}
echo_one_env(&arg, to_stdout)
}
}
fn echo_one_env(arg: &str, to_stdout: bool) {
if let Ok(v) = std::env::var(arg) {
if to_stdout {
println!("{v}");
} else {
eprintln!("{v}");
}
}
}
/// Mix echo of env keys from input
/// Example:
/// * nu --testbin echo_env_mixed out-err FOO BAR
/// * nu --testbin echo_env_mixed err-out FOO BAR
/// If it's not present, panic instead
pub fn echo_env_mixed() {
let args = args();
let args = &args[1..];
if args.len() != 3 {
panic!(
r#"Usage examples:
* nu --testbin echo_env_mixed out-err FOO BAR
* nu --testbin echo_env_mixed err-out FOO BAR"#
)
}
match args[0].as_str() {
"out-err" => {
let (out_arg, err_arg) = (&args[1], &args[2]);
echo_one_env(out_arg, true);
echo_one_env(err_arg, false);
}
"err-out" => {
let (err_arg, out_arg) = (&args[1], &args[2]);
echo_one_env(err_arg, false);
echo_one_env(out_arg, true);
}
_ => panic!("The mixed type must be `out_err`, `err_out`"),
}
}
/// Cross platform echo using println!()
/// Example: nu --testbin echo a b c
/// a b c