1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

uutests: adjust the tests to use them

This commit is contained in:
Sylvestre Ledru 2025-03-28 09:51:51 +01:00
parent ccfcda531e
commit a0179ea239
102 changed files with 1018 additions and 157 deletions

View file

@ -2,12 +2,9 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore (words) bamf chdir rlimit prlimit COMSPEC cout cerr FFFD
// spell-checker:ignore (words) bamf chdir rlimit prlimit COMSPEC cout cerr FFFD winsize xpixel ypixel
#![allow(clippy::missing_errors_doc)]
use crate::common::util::TestScenario;
#[cfg(unix)]
use crate::common::util::UChild;
#[cfg(unix)]
use nix::sys::signal::Signal;
#[cfg(feature = "echo")]
@ -17,6 +14,13 @@ use std::path::Path;
#[cfg(unix)]
use std::process::Command;
use tempfile::tempdir;
use uutests::new_ucmd;
#[cfg(unix)]
use uutests::util::TerminalSimulation;
use uutests::util::TestScenario;
#[cfg(unix)]
use uutests::util::UChild;
use uutests::util_name;
#[cfg(unix)]
struct Target {
@ -520,7 +524,7 @@ fn test_split_string_into_args_debug_output_whitespace_handling() {
fn test_gnu_e20() {
let scene = TestScenario::new(util_name!());
let env_bin = String::from(crate::common::util::TESTS_BINARY) + " " + util_name!();
let env_bin = String::from(uutests::util::get_tests_binary()) + " " + util_name!();
let (input, output) = (
[
@ -1516,3 +1520,205 @@ mod test_raw_string_parser {
);
}
}
#[cfg(unix)]
#[test]
fn test_simulation_of_terminal_false() {
let scene = TestScenario::new("util");
let out = scene.ccmd("env").arg("sh").arg("is_a_tty.sh").succeeds();
std::assert_eq!(
String::from_utf8_lossy(out.stdout()),
"stdin is not a tty\nstdout is not a tty\nstderr is not a tty\n"
);
std::assert_eq!(
String::from_utf8_lossy(out.stderr()),
"This is an error message.\n"
);
}
#[cfg(unix)]
#[test]
fn test_simulation_of_terminal_true() {
let scene = TestScenario::new("util");
let out = scene
.ccmd("env")
.arg("sh")
.arg("is_a_tty.sh")
.terminal_simulation(true)
.succeeds();
std::assert_eq!(
String::from_utf8_lossy(out.stdout()),
"stdin is a tty\r\nterminal size: 30 80\r\nstdout is a tty\r\nstderr is a tty\r\n"
);
std::assert_eq!(
String::from_utf8_lossy(out.stderr()),
"This is an error message.\r\n"
);
}
#[cfg(unix)]
#[test]
fn test_simulation_of_terminal_for_stdin_only() {
let scene = TestScenario::new("util");
let out = scene
.ccmd("env")
.arg("sh")
.arg("is_a_tty.sh")
.terminal_sim_stdio(TerminalSimulation {
stdin: true,
stdout: false,
stderr: false,
..Default::default()
})
.succeeds();
std::assert_eq!(
String::from_utf8_lossy(out.stdout()),
"stdin is a tty\nterminal size: 30 80\nstdout is not a tty\nstderr is not a tty\n"
);
std::assert_eq!(
String::from_utf8_lossy(out.stderr()),
"This is an error message.\n"
);
}
#[cfg(unix)]
#[test]
fn test_simulation_of_terminal_for_stdout_only() {
let scene = TestScenario::new("util");
let out = scene
.ccmd("env")
.arg("sh")
.arg("is_a_tty.sh")
.terminal_sim_stdio(TerminalSimulation {
stdin: false,
stdout: true,
stderr: false,
..Default::default()
})
.succeeds();
std::assert_eq!(
String::from_utf8_lossy(out.stdout()),
"stdin is not a tty\r\nstdout is a tty\r\nstderr is not a tty\r\n"
);
std::assert_eq!(
String::from_utf8_lossy(out.stderr()),
"This is an error message.\n"
);
}
#[cfg(unix)]
#[test]
fn test_simulation_of_terminal_for_stderr_only() {
let scene = TestScenario::new("util");
let out = scene
.ccmd("env")
.arg("sh")
.arg("is_a_tty.sh")
.terminal_sim_stdio(TerminalSimulation {
stdin: false,
stdout: false,
stderr: true,
..Default::default()
})
.succeeds();
std::assert_eq!(
String::from_utf8_lossy(out.stdout()),
"stdin is not a tty\nstdout is not a tty\nstderr is a tty\n"
);
std::assert_eq!(
String::from_utf8_lossy(out.stderr()),
"This is an error message.\r\n"
);
}
#[cfg(unix)]
#[test]
fn test_simulation_of_terminal_size_information() {
let scene = TestScenario::new("util");
let out = scene
.ccmd("env")
.arg("sh")
.arg("is_a_tty.sh")
.terminal_sim_stdio(TerminalSimulation {
size: Some(libc::winsize {
ws_col: 40,
ws_row: 10,
ws_xpixel: 40 * 8,
ws_ypixel: 10 * 10,
}),
stdout: true,
stdin: true,
stderr: true,
})
.succeeds();
std::assert_eq!(
String::from_utf8_lossy(out.stdout()),
"stdin is a tty\r\nterminal size: 10 40\r\nstdout is a tty\r\nstderr is a tty\r\n"
);
std::assert_eq!(
String::from_utf8_lossy(out.stderr()),
"This is an error message.\r\n"
);
}
#[cfg(unix)]
#[test]
fn test_simulation_of_terminal_pty_sends_eot_automatically() {
let scene = TestScenario::new("util");
let mut cmd = scene.ccmd("env");
cmd.timeout(std::time::Duration::from_secs(10));
cmd.args(&["cat", "-"]);
cmd.terminal_simulation(true);
let child = cmd.run_no_wait();
let out = child.wait().unwrap(); // cat would block if there is no eot
std::assert_eq!(String::from_utf8_lossy(out.stderr()), "");
std::assert_eq!(String::from_utf8_lossy(out.stdout()), "\r\n");
}
#[cfg(unix)]
#[test]
fn test_simulation_of_terminal_pty_pipes_into_data_and_sends_eot_automatically() {
let scene = TestScenario::new("util");
let message = "Hello stdin forwarding!";
let mut cmd = scene.ccmd("env");
cmd.args(&["cat", "-"]);
cmd.terminal_simulation(true);
cmd.pipe_in(message);
let child = cmd.run_no_wait();
let out = child.wait().unwrap();
std::assert_eq!(
String::from_utf8_lossy(out.stdout()),
format!("{message}\r\n")
);
std::assert_eq!(String::from_utf8_lossy(out.stderr()), "");
}
#[cfg(unix)]
#[test]
fn test_simulation_of_terminal_pty_write_in_data_and_sends_eot_automatically() {
let scene = TestScenario::new("util");
let mut cmd = scene.ccmd("env");
cmd.args(&["cat", "-"]);
cmd.terminal_simulation(true);
let mut child = cmd.run_no_wait();
child.write_in("Hello stdin forwarding via write_in!");
let out = child.wait().unwrap();
std::assert_eq!(
String::from_utf8_lossy(out.stdout()),
"Hello stdin forwarding via write_in!\r\n"
);
std::assert_eq!(String::from_utf8_lossy(out.stderr()), "");
}