| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
#[cfg(not(target_os = "openbsd"))] |
| 7 |
use uucore::entries::{Locate, Passwd}; |
| 8 |
use uutests::new_ucmd; |
| 9 |
#[cfg(not(target_os = "openbsd"))] |
| 10 |
use uutests::util::{TestScenario, expected_result}; |
| 11 |
#[cfg(not(target_os = "openbsd"))] |
| 12 |
use uutests::{unwrap_or_return, util_name}; |
| 13 |
|
| 14 |
#[test] |
| 15 |
fn test_invalid_arg() { |
| 16 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 17 |
} |
| 18 |
|
| 19 |
#[test] |
| 20 |
#[cfg(not(target_os = "openbsd"))] |
| 21 |
fn test_long_format() { |
| 22 |
use pinky::capitalize; |
| 23 |
|
| 24 |
let login = "root"; |
| 25 |
let pw: Passwd = Passwd::locate(login).unwrap(); |
| 26 |
let user_info = pw.user_info.unwrap_or_default(); |
| 27 |
let user_dir = pw.user_dir.unwrap_or_default(); |
| 28 |
let user_shell = pw.user_shell.unwrap_or_default(); |
| 29 |
let real_name = user_info.replace('&', &capitalize(&pw.name)); |
| 30 |
let ts = TestScenario::new(util_name!()); |
| 31 |
ts.ucmd().arg("-l").arg(login).succeeds().stdout_is(format!( |
| 32 |
"Login name: {login:<28}In real life: {real_name}\nDirectory: {user_dir:<29}Shell: {user_shell}\n\n" |
| 33 |
)); |
| 34 |
|
| 35 |
ts.ucmd() |
| 36 |
.arg("-lb") |
| 37 |
.arg(login) |
| 38 |
.succeeds() |
| 39 |
.stdout_is(format!( |
| 40 |
"Login name: {login:<28}In real life: {real_name}\n\n" |
| 41 |
)); |
| 42 |
} |
| 43 |
|
| 44 |
#[cfg(unix)] |
| 45 |
#[test] |
| 46 |
#[cfg(not(target_os = "openbsd"))] |
| 47 |
fn test_long_format_multiple_users() { |
| 48 |
// multiple instances of one account we know exists, |
| 49 |
// the account of the test runner, |
| 50 |
// and an account that (probably) doesn't exist |
| 51 |
let runner = std::env::var("USER").unwrap_or_default(); |
| 52 |
let args = ["-l", "root", "root", "root", &runner, "no_such_user"]; |
| 53 |
let ts = TestScenario::new(util_name!()); |
| 54 |
let expect = unwrap_or_return!(expected_result(&ts, &args)); |
| 55 |
|
| 56 |
ts.ucmd() |
| 57 |
.args(&args) |
| 58 |
.succeeds() |
| 59 |
.stdout_is(expect.stdout_str()) |
| 60 |
.stderr_is(expect.stderr_str()); |
| 61 |
} |
| 62 |
|
| 63 |
#[test] |
| 64 |
fn test_long_format_wo_user() { |
| 65 |
// "no username specified; at least one must be specified when using -l" |
| 66 |
new_ucmd!().arg("-l").fails(); |
| 67 |
} |
| 68 |
|
| 69 |
#[cfg(unix)] |
| 70 |
#[test] |
| 71 |
#[cfg(not(target_os = "openbsd"))] |
| 72 |
fn test_short_format_i() { |
| 73 |
// allow whitespace variation |
| 74 |
// * minor whitespace differences occur between platform built-in outputs; specifically, the number of trailing TABs may be variant |
| 75 |
let args = ["-i"]; |
| 76 |
let ts = TestScenario::new(util_name!()); |
| 77 |
let actual = ts.ucmd().args(&args).succeeds().stdout_move_str(); |
| 78 |
let expect = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); |
| 79 |
let v_actual: Vec<&str> = actual.split_whitespace().collect(); |
| 80 |
let v_expect: Vec<&str> = expect.split_whitespace().collect(); |
| 81 |
assert_eq!(v_actual, v_expect); |
| 82 |
} |
| 83 |
|
| 84 |
/// Drop the clock-dependent tokens from a `pinky` output split on whitespace. |
| 85 |
/// |
| 86 |
/// The idle column holds a live `HH:MM` duration, so it can tick over between the |
| 87 |
/// run of our binary and the run of the reference one, which made these tests flaky. |
| 88 |
/// Remove the `Idle` header and every `HH:MM`-looking token so only the stable |
| 89 |
/// fields are compared. |
| 90 |
#[cfg(unix)] |
| 91 |
#[cfg(not(target_os = "openbsd"))] |
| 92 |
fn strip_volatile_fields(fields: &[&str]) -> Vec<String> { |
| 93 |
fn is_clock_value(s: &str) -> bool { |
| 94 |
let bytes = s.as_bytes(); |
| 95 |
bytes.len() == 5 |
| 96 |
&& bytes[2] == b':' |
| 97 |
&& bytes[..2].iter().chain(&bytes[3..]).all(u8::is_ascii_digit) |
| 98 |
} |
| 99 |
|
| 100 |
fields |
| 101 |
.iter() |
| 102 |
.filter(|s| **s != "Idle" && !is_clock_value(s)) |
| 103 |
.map(|s| (*s).to_string()) |
| 104 |
.collect() |
| 105 |
} |
| 106 |
|
| 107 |
#[cfg(unix)] |
| 108 |
#[test] |
| 109 |
#[cfg(not(target_os = "openbsd"))] |
| 110 |
fn test_lookup() { |
| 111 |
let args = ["--lookup"]; |
| 112 |
let ts = TestScenario::new(util_name!()); |
| 113 |
let actual = ts.ucmd().args(&args).succeeds().stdout_move_str(); |
| 114 |
let expect = unwrap_or_return!(expected_result(&ts, &[])).stdout_move_str(); |
| 115 |
let v_actual: Vec<&str> = actual.split_whitespace().collect(); |
| 116 |
let v_expect: Vec<&str> = expect.split_whitespace().collect(); |
| 117 |
assert_eq!( |
| 118 |
strip_volatile_fields(&v_actual), |
| 119 |
strip_volatile_fields(&v_expect) |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
#[cfg(unix)] |
| 124 |
#[test] |
| 125 |
#[cfg(not(target_os = "openbsd"))] |
| 126 |
fn test_short_format_q() { |
| 127 |
// allow whitespace variation |
| 128 |
// * minor whitespace differences occur between platform built-in outputs; specifically, the number of trailing TABs may be variant |
| 129 |
let args = ["-q"]; |
| 130 |
let ts = TestScenario::new(util_name!()); |
| 131 |
let actual = ts.ucmd().args(&args).succeeds().stdout_move_str(); |
| 132 |
let expect = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); |
| 133 |
let v_actual: Vec<&str> = actual.split_whitespace().collect(); |
| 134 |
let v_expect: Vec<&str> = expect.split_whitespace().collect(); |
| 135 |
assert_eq!( |
| 136 |
strip_volatile_fields(&v_actual), |
| 137 |
strip_volatile_fields(&v_expect) |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
#[cfg(unix)] |
| 142 |
#[test] |
| 143 |
#[cfg(not(target_os = "openbsd"))] |
| 144 |
fn test_no_flag() { |
| 145 |
let ts = TestScenario::new(util_name!()); |
| 146 |
let actual = ts.ucmd().succeeds().stdout_move_str(); |
| 147 |
let expect = unwrap_or_return!(expected_result(&ts, &[])).stdout_move_str(); |
| 148 |
let v_actual: Vec<&str> = actual.split_whitespace().collect(); |
| 149 |
let v_expect: Vec<&str> = expect.split_whitespace().collect(); |
| 150 |
assert_eq!( |
| 151 |
strip_volatile_fields(&v_actual), |
| 152 |
strip_volatile_fields(&v_expect) |
| 153 |
); |
| 154 |
} |