mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-02 22:17:45 +00:00
Merge pull request #2361 from jhscheer/id_zero_2351
id: revamp to pass more of GNU's Testsuite
This commit is contained in:
commit
f1e043ca1b
4 changed files with 830 additions and 345 deletions
|
@ -1,151 +1,186 @@
|
|||
use crate::common::util::*;
|
||||
|
||||
// Apparently some CI environments have configuration issues, e.g. with 'whoami' and 'id'.
|
||||
// If we are running inside the CI and "needle" is in "stderr" skipping this test is
|
||||
// considered okay. If we are not inside the CI this calls assert!(result.success).
|
||||
//
|
||||
// From the Logs: "Build (ubuntu-18.04, x86_64-unknown-linux-gnu, feat_os_unix, use-cross)"
|
||||
// stderr: "whoami: cannot find name for user ID 1001"
|
||||
// Maybe: "adduser --uid 1001 username" can put things right?
|
||||
// stderr = id: Could not find uid 1001: No such id: 1001
|
||||
fn skipping_test_is_okay(result: &CmdResult, needle: &str) -> bool {
|
||||
if !result.succeeded() {
|
||||
println!("result.stdout = {}", result.stdout_str());
|
||||
println!("result.stderr = {}", result.stderr_str());
|
||||
if is_ci() && result.stderr_str().contains(needle) {
|
||||
println!("test skipped:");
|
||||
return true;
|
||||
} else {
|
||||
result.success();
|
||||
// spell-checker:ignore (ToDO) testsuite coreutil
|
||||
|
||||
// These tests run the GNU coreutils `(g)id` binary in `$PATH` in order to gather reference values.
|
||||
// If the `(g)id` in `$PATH` doesn't include a coreutils version string,
|
||||
// or the version is too low, the test is skipped.
|
||||
|
||||
// The reference version is 8.32. Here 8.30 was chosen because right now there's no
|
||||
// ubuntu image for github action available with a higher version than 8.30.
|
||||
const VERSION_EXPECTED: &str = "8.30"; // Version expected for the reference `id` in $PATH
|
||||
const VERSION_MULTIPLE_USERS: &str = "8.31";
|
||||
const UUTILS_WARNING: &str = "uutils-tests-warning";
|
||||
const UUTILS_INFO: &str = "uutils-tests-info";
|
||||
|
||||
macro_rules! unwrap_or_return {
|
||||
( $e:expr ) => {
|
||||
match $e {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
println!("{}: test skipped: {}", UUTILS_INFO, e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
};
|
||||
}
|
||||
|
||||
fn return_whoami_username() -> String {
|
||||
let scene = TestScenario::new("whoami");
|
||||
let result = scene.cmd("whoami").run();
|
||||
if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
|
||||
println!("test skipped:");
|
||||
return String::from("");
|
||||
}
|
||||
fn whoami() -> String {
|
||||
// Apparently some CI environments have configuration issues, e.g. with 'whoami' and 'id'.
|
||||
//
|
||||
// From the Logs: "Build (ubuntu-18.04, x86_64-unknown-linux-gnu, feat_os_unix, use-cross)"
|
||||
// whoami: cannot find name for user ID 1001
|
||||
// id --name: cannot find name for user ID 1001
|
||||
// id --name: cannot find name for group ID 116
|
||||
//
|
||||
// However, when running "id" from within "/bin/bash" it looks fine:
|
||||
// id: "uid=1001(runner) gid=118(docker) groups=118(docker),4(adm),101(systemd-journal)"
|
||||
// whoami: "runner"
|
||||
|
||||
result.stdout_str().trim().to_string()
|
||||
// Use environment variable to get current user instead of
|
||||
// invoking `whoami` and fall back to user "nobody" on error.
|
||||
std::env::var("USER").unwrap_or_else(|e| {
|
||||
println!("{}: {}, using \"nobody\" instead", UUTILS_WARNING, e);
|
||||
"nobody".to_string()
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_id() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
#[cfg(unix)]
|
||||
fn test_id_no_specified_user() {
|
||||
let result = new_ucmd!().run();
|
||||
let exp_result = unwrap_or_return!(expected_result(&[]));
|
||||
let mut _exp_stdout = exp_result.stdout_str().to_string();
|
||||
|
||||
let result = scene.ucmd().arg("-u").succeeds();
|
||||
let uid = result.stdout_str().trim();
|
||||
|
||||
let result = scene.ucmd().run();
|
||||
if skipping_test_is_okay(&result, "Could not find uid") {
|
||||
return;
|
||||
}
|
||||
|
||||
// Verify that the id found by --user/-u exists in the list
|
||||
result.stdout_contains(uid);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_id_from_name() {
|
||||
let username = return_whoami_username();
|
||||
if username.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let result = scene.ucmd().arg(&username).run();
|
||||
if skipping_test_is_okay(&result, "Could not find uid") {
|
||||
return;
|
||||
}
|
||||
|
||||
let uid = result.stdout_str().trim();
|
||||
|
||||
let result = scene.ucmd().run();
|
||||
if skipping_test_is_okay(&result, "Could not find uid") {
|
||||
return;
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
// NOTE: (SELinux NotImplemented) strip 'context' part from exp_stdout:
|
||||
if let Some(context_offset) = exp_result.stdout_str().find(" context=") {
|
||||
_exp_stdout.replace_range(context_offset.._exp_stdout.len() - 1, "");
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
// Verify that the id found by --user/-u exists in the list
|
||||
.stdout_contains(uid)
|
||||
// Verify that the username found by whoami exists in the list
|
||||
.stdout_contains(username);
|
||||
.stdout_is(_exp_stdout)
|
||||
.stderr_is(exp_result.stderr_str())
|
||||
.code_is(exp_result.code());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_id_name_from_id() {
|
||||
let result = new_ucmd!().arg("-nu").run();
|
||||
#[cfg(unix)]
|
||||
fn test_id_single_user() {
|
||||
let test_users = [&whoami()[..]];
|
||||
|
||||
let username_id = result.stdout_str().trim();
|
||||
|
||||
let username_whoami = return_whoami_username();
|
||||
if username_whoami.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
assert_eq!(username_id, username_whoami);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_id_group() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let mut exp_result = unwrap_or_return!(expected_result(&test_users));
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&test_users)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
|
||||
let mut result = scene.ucmd().arg("-g").succeeds();
|
||||
let s1 = result.stdout_str().trim();
|
||||
assert!(s1.parse::<f64>().is_ok());
|
||||
|
||||
result = scene.ucmd().arg("--group").succeeds();
|
||||
let s1 = result.stdout_str().trim();
|
||||
assert!(s1.parse::<f64>().is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
||||
fn test_id_groups() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
for g_flag in &["-G", "--groups"] {
|
||||
// u/g/G z/n
|
||||
for &opt in &["--user", "--group", "--groups"] {
|
||||
let mut args = vec![opt];
|
||||
args.extend_from_slice(&test_users);
|
||||
exp_result = unwrap_or_return!(expected_result(&args));
|
||||
scene
|
||||
.ucmd()
|
||||
.arg(g_flag)
|
||||
.succeeds()
|
||||
.stdout_is(expected_result(&[g_flag], false));
|
||||
for &r_flag in &["-r", "--real"] {
|
||||
let args = [g_flag, r_flag];
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.succeeds()
|
||||
.stdout_is(expected_result(&args, false));
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.push("--zero");
|
||||
exp_result = unwrap_or_return!(expected_result(&args));
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.push("--name");
|
||||
exp_result = unwrap_or_return!(expected_result(&args));
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.pop();
|
||||
exp_result = unwrap_or_return!(expected_result(&args));
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn test_id_single_user_non_existing() {
|
||||
let args = &["hopefully_non_existing_username"];
|
||||
let result = new_ucmd!().args(args).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(args));
|
||||
|
||||
// It is unknown why on macOS (and possibly others?) `id` adds "Invalid argument".
|
||||
// coreutils 8.32: $ LC_ALL=C id foobar
|
||||
// macOS: stderr: "id: 'foobar': no such user: Invalid argument"
|
||||
// linux: stderr: "id: 'foobar': no such user"
|
||||
result
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn test_id_name() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
for &opt in &["--user", "--group", "--groups"] {
|
||||
let args = [opt, "--name"];
|
||||
let result = scene.ucmd().args(&args).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(&args));
|
||||
result
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str())
|
||||
.code_is(exp_result.code());
|
||||
|
||||
if opt == "--user" {
|
||||
assert_eq!(result.stdout_str().trim_end(), whoami());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_id_user() {
|
||||
#[cfg(unix)]
|
||||
fn test_id_real() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
|
||||
let result = scene.ucmd().arg("-u").succeeds();
|
||||
let s1 = result.stdout_str().trim();
|
||||
assert!(s1.parse::<f64>().is_ok());
|
||||
|
||||
let result = scene.ucmd().arg("--user").succeeds();
|
||||
let s1 = result.stdout_str().trim();
|
||||
assert!(s1.parse::<f64>().is_ok());
|
||||
for &opt in &["--user", "--group", "--groups"] {
|
||||
let args = [opt, "--real"];
|
||||
let result = scene.ucmd().args(&args).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(&args));
|
||||
result
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str())
|
||||
.code_is(exp_result.code());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(all(unix, not(target_os = "linux")))]
|
||||
fn test_id_pretty_print() {
|
||||
let username = return_whoami_username();
|
||||
if username.is_empty() {
|
||||
return;
|
||||
}
|
||||
// `-p` is BSD only and not supported on GNU's `id`
|
||||
let username = whoami();
|
||||
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let result = scene.ucmd().arg("-p").run();
|
||||
let result = new_ucmd!().arg("-p").run();
|
||||
if result.stdout_str().trim().is_empty() {
|
||||
// this fails only on: "MinRustV (ubuntu-latest, feat_os_unix)"
|
||||
// `rustc 1.40.0 (73528e339 2019-12-16)`
|
||||
|
@ -154,49 +189,317 @@ fn test_id_pretty_print() {
|
|||
// stdout =
|
||||
// stderr = ', tests/common/util.rs:157:13
|
||||
println!("test skipped:");
|
||||
return;
|
||||
} else {
|
||||
result.success().stdout_contains(username);
|
||||
}
|
||||
|
||||
result.success().stdout_contains(username);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(all(unix, not(target_os = "linux")))]
|
||||
fn test_id_password_style() {
|
||||
let username = return_whoami_username();
|
||||
if username.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let result = new_ucmd!().arg("-P").succeeds();
|
||||
|
||||
// `-P` is BSD only and not supported on GNU's `id`
|
||||
let username = whoami();
|
||||
let result = new_ucmd!().arg("-P").arg(&username).succeeds();
|
||||
assert!(result.stdout_str().starts_with(&username));
|
||||
}
|
||||
|
||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
||||
fn expected_result(args: &[&str], exp_fail: bool) -> String {
|
||||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn test_id_multiple_users() {
|
||||
#[cfg(target_os = "linux")]
|
||||
let util_name = util_name!();
|
||||
#[cfg(target_vendor = "apple")]
|
||||
let util_name = format!("g{}", util_name!());
|
||||
#[cfg(all(unix, not(target_os = "linux")))]
|
||||
let util_name = &format!("g{}", util_name!());
|
||||
let version_check_string = check_coreutil_version(util_name, VERSION_MULTIPLE_USERS);
|
||||
if version_check_string.starts_with(UUTILS_WARNING) {
|
||||
println!("{}\ntest skipped", version_check_string);
|
||||
return;
|
||||
}
|
||||
|
||||
let result = if !exp_fail {
|
||||
TestScenario::new(&util_name)
|
||||
.cmd_keepenv(util_name)
|
||||
.env("LANGUAGE", "C")
|
||||
.args(args)
|
||||
.succeeds()
|
||||
.stdout_move_str()
|
||||
} else {
|
||||
TestScenario::new(&util_name)
|
||||
.cmd_keepenv(util_name)
|
||||
.env("LANGUAGE", "C")
|
||||
.args(args)
|
||||
.fails()
|
||||
.stderr_move_str()
|
||||
};
|
||||
return if cfg!(target_os = "macos") && result.starts_with("gid") {
|
||||
result[1..].to_string()
|
||||
} else {
|
||||
result
|
||||
};
|
||||
// Same typical users that GNU testsuite is using.
|
||||
let test_users = ["root", "man", "postfix", "sshd", &whoami()];
|
||||
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let mut exp_result = unwrap_or_return!(expected_result(&test_users));
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&test_users)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
|
||||
// u/g/G z/n
|
||||
for &opt in &["--user", "--group", "--groups"] {
|
||||
let mut args = vec![opt];
|
||||
args.extend_from_slice(&test_users);
|
||||
exp_result = unwrap_or_return!(expected_result(&args));
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.push("--zero");
|
||||
exp_result = unwrap_or_return!(expected_result(&args));
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.push("--name");
|
||||
exp_result = unwrap_or_return!(expected_result(&args));
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.pop();
|
||||
exp_result = unwrap_or_return!(expected_result(&args));
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn test_id_multiple_users_non_existing() {
|
||||
#[cfg(target_os = "linux")]
|
||||
let util_name = util_name!();
|
||||
#[cfg(all(unix, not(target_os = "linux")))]
|
||||
let util_name = &format!("g{}", util_name!());
|
||||
let version_check_string = check_coreutil_version(util_name, VERSION_MULTIPLE_USERS);
|
||||
if version_check_string.starts_with(UUTILS_WARNING) {
|
||||
println!("{}\ntest skipped", version_check_string);
|
||||
return;
|
||||
}
|
||||
|
||||
let test_users = [
|
||||
"root",
|
||||
"hopefully_non_existing_username1",
|
||||
&whoami(),
|
||||
"man",
|
||||
"hopefully_non_existing_username2",
|
||||
"hopefully_non_existing_username3",
|
||||
"postfix",
|
||||
"sshd",
|
||||
"hopefully_non_existing_username4",
|
||||
&whoami(),
|
||||
];
|
||||
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let mut exp_result = unwrap_or_return!(expected_result(&test_users));
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&test_users)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
|
||||
// u/g/G z/n
|
||||
for &opt in &["--user", "--group", "--groups"] {
|
||||
let mut args = vec![opt];
|
||||
args.extend_from_slice(&test_users);
|
||||
exp_result = unwrap_or_return!(expected_result(&args));
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.push("--zero");
|
||||
exp_result = unwrap_or_return!(expected_result(&args));
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.push("--name");
|
||||
exp_result = unwrap_or_return!(expected_result(&args));
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.pop();
|
||||
exp_result = unwrap_or_return!(expected_result(&args));
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn test_id_default_format() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
for &opt1 in &["--name", "--real"] {
|
||||
// id: cannot print only names or real IDs in default format
|
||||
let args = [opt1];
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.fails()
|
||||
.stderr_only(unwrap_or_return!(expected_result(&args)).stderr_str());
|
||||
for &opt2 in &["--user", "--group", "--groups"] {
|
||||
// u/g/G n/r
|
||||
let args = [opt2, opt1];
|
||||
let result = scene.ucmd().args(&args).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(&args));
|
||||
result
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str())
|
||||
.code_is(exp_result.code());
|
||||
}
|
||||
}
|
||||
for &opt2 in &["--user", "--group", "--groups"] {
|
||||
// u/g/G
|
||||
let args = [opt2];
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.succeeds()
|
||||
.stdout_only(unwrap_or_return!(expected_result(&args)).stdout_str());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn test_id_zero() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
for z_flag in &["-z", "--zero"] {
|
||||
// id: option --zero not permitted in default format
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&[z_flag])
|
||||
.fails()
|
||||
.stderr_only(unwrap_or_return!(expected_result(&[z_flag])).stderr_str());
|
||||
for &opt1 in &["--name", "--real"] {
|
||||
// id: cannot print only names or real IDs in default format
|
||||
let args = [opt1, z_flag];
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.fails()
|
||||
.stderr_only(unwrap_or_return!(expected_result(&args)).stderr_str());
|
||||
for &opt2 in &["--user", "--group", "--groups"] {
|
||||
// u/g/G n/r z
|
||||
let args = [opt2, z_flag, opt1];
|
||||
let result = scene.ucmd().args(&args).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(&args));
|
||||
result
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str())
|
||||
.code_is(exp_result.code());
|
||||
}
|
||||
}
|
||||
for &opt2 in &["--user", "--group", "--groups"] {
|
||||
// u/g/G z
|
||||
let args = [opt2, z_flag];
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.succeeds()
|
||||
.stdout_only(unwrap_or_return!(expected_result(&args)).stdout_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_coreutil_version(util_name: &str, version_expected: &str) -> String {
|
||||
// example:
|
||||
// $ id --version | head -n 1
|
||||
// id (GNU coreutils) 8.32.162-4eda
|
||||
let scene = TestScenario::new(util_name);
|
||||
let version_check = scene
|
||||
.cmd_keepenv(&util_name)
|
||||
.env("LANGUAGE", "C")
|
||||
.arg("--version")
|
||||
.run();
|
||||
version_check
|
||||
.stdout_str()
|
||||
.split('\n')
|
||||
.collect::<Vec<_>>()
|
||||
.get(0)
|
||||
.map_or_else(
|
||||
|| format!("{}: unexpected output format for reference coreutil: '{} --version'", UUTILS_WARNING, util_name),
|
||||
|s| {
|
||||
if s.contains(&format!("(GNU coreutils) {}", version_expected)) {
|
||||
s.to_string()
|
||||
} else if s.contains("(GNU coreutils)") {
|
||||
let version_found = s.split_whitespace().last().unwrap()[..4].parse::<f32>().unwrap_or_default();
|
||||
let version_expected = version_expected.parse::<f32>().unwrap_or_default();
|
||||
if version_found > version_expected {
|
||||
format!("{}: version for the reference coreutil '{}' is higher than expected; expected: {}, found: {}", UUTILS_INFO, util_name, version_expected, version_found)
|
||||
} else {
|
||||
format!("{}: version for the reference coreutil '{}' does not match; expected: {}, found: {}", UUTILS_WARNING, util_name, version_expected, version_found) }
|
||||
} else {
|
||||
format!("{}: no coreutils version string found for reference coreutils '{} --version'", UUTILS_WARNING, util_name)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(clippy::needless_borrow)]
|
||||
#[cfg(unix)]
|
||||
fn expected_result(args: &[&str]) -> Result<CmdResult, String> {
|
||||
#[cfg(target_os = "linux")]
|
||||
let util_name = util_name!();
|
||||
#[cfg(all(unix, not(target_os = "linux")))]
|
||||
let util_name = &format!("g{}", util_name!());
|
||||
|
||||
let version_check_string = check_coreutil_version(util_name, VERSION_EXPECTED);
|
||||
if version_check_string.starts_with(UUTILS_WARNING) {
|
||||
return Err(version_check_string);
|
||||
}
|
||||
println!("{}", version_check_string);
|
||||
|
||||
let scene = TestScenario::new(util_name);
|
||||
let result = scene
|
||||
.cmd_keepenv(util_name)
|
||||
.env("LANGUAGE", "C")
|
||||
.args(args)
|
||||
.run();
|
||||
|
||||
let (stdout, stderr): (String, String) = if cfg!(target_os = "linux") {
|
||||
(
|
||||
result.stdout_str().to_string(),
|
||||
result.stderr_str().to_string(),
|
||||
)
|
||||
} else {
|
||||
// strip 'g' prefix from results:
|
||||
let from = util_name.to_string() + ":";
|
||||
let to = &from[1..];
|
||||
(
|
||||
result.stdout_str().replace(&from, to),
|
||||
result.stderr_str().replace(&from, to),
|
||||
)
|
||||
};
|
||||
|
||||
Ok(CmdResult::new(
|
||||
Some(result.tmpd()),
|
||||
Some(result.code()),
|
||||
result.succeeded(),
|
||||
stdout.as_bytes(),
|
||||
stderr.as_bytes(),
|
||||
))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue