1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-05 07:27:46 +00:00

Merge pull request #2419 from miDeb/tty/tests

tty: fix tests and exit codes
This commit is contained in:
Terts Diepraam 2021-06-16 23:49:19 +02:00 committed by GitHub
commit 939349f052
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 23 deletions

View file

@ -14,6 +14,7 @@ extern crate uucore;
use clap::{crate_version, App, Arg}; use clap::{crate_version, App, Arg};
use std::ffi::CStr; use std::ffi::CStr;
use std::io::Write;
use uucore::InvalidEncodingHandling; use uucore::InvalidEncodingHandling;
static ABOUT: &str = "Print the file name of the terminal connected to standard input."; static ABOUT: &str = "Print the file name of the terminal connected to standard input.";
@ -44,7 +45,15 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.help("print nothing, only return an exit status") .help("print nothing, only return an exit status")
.required(false), .required(false),
) )
.get_matches_from(args); .get_matches_from_safe(args);
let matches = match matches {
Ok(m) => m,
Err(e) => {
eprint!("{}", e);
return 2;
}
};
let silent = matches.is_present(options::SILENT); let silent = matches.is_present(options::SILENT);
@ -58,11 +67,18 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
} }
}; };
let mut stdout = std::io::stdout();
if !silent { if !silent {
if !tty.chars().all(|c| c.is_whitespace()) { let write_result = if !tty.chars().all(|c| c.is_whitespace()) {
println!("{}", tty); writeln!(stdout, "{}", tty)
} else { } else {
println!("not a tty"); writeln!(stdout, "not a tty")
};
if write_result.is_err() || stdout.flush().is_err() {
// Don't return to prevent a panic later when another flush is attempted
// because the `uucore_procs::main` macro inserts a flush after execution for every utility.
std::process::exit(3);
} }
} }

View file

@ -1,11 +1,14 @@
use std::fs::File;
use crate::common::util::*; use crate::common::util::*;
#[test] #[test]
#[cfg(not(windows))] #[cfg(not(windows))]
fn test_dev_null() { fn test_dev_null() {
new_ucmd!() new_ucmd!()
.pipe_in("</dev/null") .set_stdin(File::open("/dev/null").unwrap())
.fails() .fails()
.code_is(1)
.stdout_is("not a tty\n"); .stdout_is("not a tty\n");
} }
@ -14,44 +17,58 @@ fn test_dev_null() {
fn test_dev_null_silent() { fn test_dev_null_silent() {
new_ucmd!() new_ucmd!()
.args(&["-s"]) .args(&["-s"])
.pipe_in("</dev/null") .set_stdin(File::open("/dev/null").unwrap())
.fails() .fails()
.code_is(1)
.stdout_is(""); .stdout_is("");
} }
#[test] #[test]
fn test_close_stdin() { fn test_close_stdin() {
new_ucmd!().pipe_in("<&-").fails().stdout_is("not a tty\n"); let mut child = new_ucmd!().run_no_wait();
drop(child.stdin.take());
let output = child.wait_with_output().unwrap();
assert_eq!(output.status.code(), Some(1));
assert_eq!(std::str::from_utf8(&output.stdout), Ok("not a tty\n"));
} }
#[test] #[test]
fn test_close_stdin_silent() { fn test_close_stdin_silent() {
new_ucmd!() let mut child = new_ucmd!().arg("-s").run_no_wait();
.args(&["-s"]) drop(child.stdin.take());
.pipe_in("<&-") let output = child.wait_with_output().unwrap();
.fails() assert_eq!(output.status.code(), Some(1));
.stdout_is(""); assert!(output.stdout.is_empty());
} }
#[test] #[test]
fn test_close_stdin_silent_long() { fn test_close_stdin_silent_long() {
new_ucmd!() let mut child = new_ucmd!().arg("--silent").run_no_wait();
.args(&["--silent"]) drop(child.stdin.take());
.pipe_in("<&-") let output = child.wait_with_output().unwrap();
.fails() assert_eq!(output.status.code(), Some(1));
.stdout_is(""); assert!(output.stdout.is_empty());
} }
#[test] #[test]
fn test_close_stdin_silent_alias() { fn test_close_stdin_silent_alias() {
new_ucmd!() let mut child = new_ucmd!().arg("--quiet").run_no_wait();
.args(&["--quiet"]) drop(child.stdin.take());
.pipe_in("<&-") let output = child.wait_with_output().unwrap();
.fails() assert_eq!(output.status.code(), Some(1));
.stdout_is(""); assert!(output.stdout.is_empty());
} }
#[test] #[test]
fn test_wrong_argument() { fn test_wrong_argument() {
new_ucmd!().args(&["a"]).fails(); new_ucmd!().args(&["a"]).fails().code_is(2);
}
#[test]
#[cfg(not(windows))]
fn test_stdout_fail() {
let mut child = new_ucmd!().run_no_wait();
drop(child.stdout.take());
let status = child.wait().unwrap();
assert_eq!(status.code(), Some(3));
} }