coreutils/tests/by-util/test_who.rs

248 lines
6.2 KiB
Rust
Raw Normal View History

use crate::common::util::*;
2016-08-11 07:37:39 +00:00
// spell-checker:ignore (flags) runlevel mesg
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
2016-08-11 07:37:39 +00:00
#[test]
fn test_count() {
2021-05-29 12:32:35 +00:00
for opt in &["-q", "--count"] {
2021-04-28 20:58:28 +00:00
new_ucmd!()
.arg(opt)
.succeeds()
.stdout_is(expected_result(&[opt]));
2016-08-11 07:37:39 +00:00
}
}
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
2016-08-11 07:37:39 +00:00
#[test]
fn test_boot() {
2021-05-29 12:32:35 +00:00
for opt in &["-b", "--boot"] {
2021-04-28 20:58:28 +00:00
new_ucmd!()
.arg(opt)
.succeeds()
.stdout_is(expected_result(&[opt]));
2016-08-11 07:37:39 +00:00
}
}
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
2016-08-11 07:37:39 +00:00
#[test]
fn test_heading() {
2021-05-29 12:32:35 +00:00
for opt in &["-H", "--heading"] {
// allow whitespace variation
2021-04-28 20:58:28 +00:00
// * minor whitespace differences occur between platform built-in outputs;
// specifically number of TABs between "TIME" and "COMMENT" may be variant
let actual = new_ucmd!().arg(opt).succeeds().stdout_move_str();
let expect = expected_result(&[opt]);
println!("actual: {:?}", actual);
println!("expect: {:?}", expect);
let v_actual: Vec<&str> = actual.split_whitespace().collect();
let v_expect: Vec<&str> = expect.split_whitespace().collect();
assert_eq!(v_actual, v_expect);
2016-08-11 07:37:39 +00:00
}
}
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
2016-08-11 07:37:39 +00:00
#[test]
fn test_short() {
2021-05-29 12:32:35 +00:00
for opt in &["-s", "--short"] {
2021-04-28 20:58:28 +00:00
new_ucmd!()
.arg(opt)
.succeeds()
.stdout_is(expected_result(&[opt]));
2016-08-11 07:37:39 +00:00
}
}
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
2016-08-11 07:37:39 +00:00
#[test]
fn test_login() {
2021-05-29 12:32:35 +00:00
for opt in &["-l", "--login"] {
2021-04-28 20:58:28 +00:00
new_ucmd!()
.arg(opt)
.succeeds()
.stdout_is(expected_result(&[opt]));
2016-08-11 07:37:39 +00:00
}
}
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
2016-08-11 07:37:39 +00:00
#[test]
fn test_m() {
2021-05-29 12:32:35 +00:00
for opt in &["-m"] {
2021-04-28 20:58:28 +00:00
new_ucmd!()
.arg(opt)
.succeeds()
.stdout_is(expected_result(&[opt]));
2021-04-28 20:58:28 +00:00
}
}
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
2021-04-28 20:58:28 +00:00
#[test]
fn test_process() {
2021-05-29 12:32:35 +00:00
for opt in &["-p", "--process"] {
2021-04-28 20:58:28 +00:00
new_ucmd!()
.arg(opt)
.succeeds()
.stdout_is(expected_result(&[opt]));
2021-04-28 20:58:28 +00:00
}
}
#[test]
fn test_runlevel() {
2021-05-29 12:32:35 +00:00
for opt in &["-r", "--runlevel"] {
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
2021-04-28 20:58:28 +00:00
new_ucmd!()
.arg(opt)
.succeeds()
.stdout_is(expected_result(&[opt]));
2021-04-28 20:58:28 +00:00
#[cfg(not(target_os = "linux"))]
new_ucmd!().arg(opt).succeeds().stdout_is("");
}
}
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
2021-04-28 20:58:28 +00:00
#[test]
fn test_time() {
2021-05-29 12:32:35 +00:00
for opt in &["-t", "--time"] {
2021-04-28 20:58:28 +00:00
new_ucmd!()
.arg(opt)
.succeeds()
.stdout_is(expected_result(&[opt]));
2021-04-28 20:58:28 +00:00
}
}
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
2021-04-28 20:58:28 +00:00
#[test]
fn test_mesg() {
// -T, -w, --mesg
// add user's message status as +, - or ?
// --message
// same as -T
// --writable
// same as -T
2021-05-29 12:32:35 +00:00
for opt in &["-T", "-w", "--mesg", "--message", "--writable"] {
2021-04-28 20:58:28 +00:00
new_ucmd!()
.arg(opt)
.succeeds()
.stdout_is(expected_result(&[opt]));
2021-04-28 20:58:28 +00:00
}
}
#[test]
fn test_arg1_arg2() {
let args = ["am", "i"];
2021-04-28 20:58:28 +00:00
new_ucmd!()
.args(&args)
2021-04-28 20:58:28 +00:00
.succeeds()
.stdout_is(expected_result(&args));
2021-04-28 20:58:28 +00:00
}
#[test]
fn test_too_many_args() {
const EXPECTED: &str =
"error: The value 'u' was provided to '<FILE>...', but it wasn't expecting any more values";
2021-04-28 20:58:28 +00:00
let args = ["am", "i", "u"];
new_ucmd!().args(&args).fails().stderr_contains(EXPECTED);
2021-04-28 20:58:28 +00:00
}
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
2021-04-28 20:58:28 +00:00
#[test]
fn test_users() {
2021-05-29 12:32:35 +00:00
for opt in &["-u", "--users"] {
let actual = new_ucmd!().arg(opt).succeeds().stdout_move_str();
let expect = expected_result(&[opt]);
println!("actual: {:?}", actual);
println!("expect: {:?}", expect);
let mut v_actual: Vec<&str> = actual.split_whitespace().collect();
let mut v_expect: Vec<&str> = expect.split_whitespace().collect();
// TODO: `--users` differs from GNU's output on macOS
// Diff < left / right > :
// <"runner console 2021-05-20 22:03 00:08 196\n"
// >"runner console 2021-05-20 22:03 old 196\n"
if cfg!(target_os = "macos") {
v_actual.remove(4);
v_expect.remove(4);
}
assert_eq!(v_actual, v_expect);
2021-04-28 20:58:28 +00:00
}
}
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
2021-04-28 20:58:28 +00:00
#[test]
fn test_lookup() {
2021-05-29 12:32:35 +00:00
let opt = "--lookup";
new_ucmd!()
.arg(opt)
.succeeds()
.stdout_is(expected_result(&[opt]));
2016-08-11 07:37:39 +00:00
}
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
2016-08-11 07:37:39 +00:00
#[test]
fn test_dead() {
2021-05-29 12:32:35 +00:00
for opt in &["-d", "--dead"] {
2021-04-28 20:58:28 +00:00
new_ucmd!()
.arg(opt)
.succeeds()
.stdout_is(expected_result(&[opt]));
2016-08-11 07:37:39 +00:00
}
}
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
2021-04-28 20:58:28 +00:00
#[test]
fn test_all_separately() {
if cfg!(target_os = "macos") {
// TODO: fix `-u`, see: test_users
return;
}
2021-04-28 20:58:28 +00:00
// -a, --all same as -b -d --login -p -r -t -T -u
let args = ["-b", "-d", "--login", "-p", "-r", "-t", "-T", "-u"];
2021-04-28 20:58:28 +00:00
let scene = TestScenario::new(util_name!());
scene
.ucmd()
.args(&args)
2021-04-28 20:58:28 +00:00
.succeeds()
.stdout_is(expected_result(&args));
2021-04-28 20:58:28 +00:00
scene
.ucmd()
.arg("--all")
.succeeds()
.stdout_is(expected_result(&args));
2021-04-28 20:58:28 +00:00
}
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
2016-08-11 07:37:39 +00:00
#[test]
fn test_all() {
if cfg!(target_os = "macos") {
// TODO: fix `-u`, see: test_users
return;
}
2021-05-29 12:32:35 +00:00
for opt in &["-a", "--all"] {
2021-04-28 20:58:28 +00:00
new_ucmd!()
.arg(opt)
.succeeds()
.stdout_is(expected_result(&[opt]));
2016-08-11 07:37:39 +00:00
}
}
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
fn expected_result(args: &[&str]) -> String {
#[cfg(target_os = "linux")]
let util_name = util_name!();
#[cfg(target_vendor = "apple")]
let util_name = format!("g{}", util_name!());
TestScenario::new(&util_name)
.cmd_keepenv(util_name)
2020-04-13 18:36:03 +00:00
.env("LANGUAGE", "C")
.args(args)
2021-04-28 20:58:28 +00:00
.succeeds()
2021-04-22 20:37:44 +00:00
.stdout_move_str()
2016-08-11 07:37:39 +00:00
}