mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
test_whoami: refactor use expected_result and whoami from common/util.rs
This commit is contained in:
parent
abc59fbe85
commit
d57f474ae1
1 changed files with 34 additions and 52 deletions
|
@ -1,63 +1,45 @@
|
||||||
|
// * This file is part of the uutils coreutils package.
|
||||||
|
// *
|
||||||
|
// * For the full copyright and license information, please view the LICENSE
|
||||||
|
// * file that was distributed with this source code.
|
||||||
|
|
||||||
use crate::common::util::*;
|
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: failed to get username"
|
|
||||||
// Maybe: "adduser --uid 1001 username" can put things right?
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(unix)]
|
||||||
fn test_normal() {
|
fn test_normal() {
|
||||||
let (_, mut ucmd) = at_and_ucmd!();
|
let ts = TestScenario::new(util_name!());
|
||||||
|
let result = ts.ucmd().run();
|
||||||
|
let exp_result = unwrap_or_return!(expected_result(&ts, &[]));
|
||||||
|
|
||||||
let result = ucmd.run();
|
result
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
// use std::env;
|
.stderr_is(exp_result.stderr_str())
|
||||||
// println!("env::var(CI).is_ok() = {}", env::var("CI").is_ok());
|
.code_is(exp_result.code());
|
||||||
// for (key, value) in env::vars() {
|
|
||||||
// println!("{}: {}", key, value);
|
|
||||||
// }
|
|
||||||
|
|
||||||
if skipping_test_is_okay(&result, "failed to get username") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
result.no_stderr();
|
|
||||||
assert!(!result.stdout_str().trim().is_empty());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(not(windows))]
|
#[cfg(unix)]
|
||||||
fn test_normal_compare_id() {
|
fn test_normal_compare_id() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new("id");
|
||||||
|
let id_un = unwrap_or_return!(expected_result(&ts, &["-un"]));
|
||||||
|
if id_un.succeeded() {
|
||||||
|
new_ucmd!().succeeds().stdout_is(id_un.stdout_str());
|
||||||
|
} else if is_ci() && id_un.stderr_str().contains("cannot find name for user ID") {
|
||||||
|
println!("test skipped:");
|
||||||
|
} else {
|
||||||
|
id_un.success();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let result_ucmd = scene.ucmd().run();
|
#[test]
|
||||||
if skipping_test_is_okay(&result_ucmd, "failed to get username") {
|
#[cfg(unix)]
|
||||||
|
fn test_normal_compare_env() {
|
||||||
|
let whoami = whoami();
|
||||||
|
if whoami == "nobody" {
|
||||||
|
println!("test skipped:");
|
||||||
return;
|
return;
|
||||||
|
} else {
|
||||||
|
new_ucmd!().succeeds().stdout_is(format!("{}\n", whoami));
|
||||||
}
|
}
|
||||||
|
|
||||||
let result_cmd = scene.cmd("id").arg("-un").run();
|
|
||||||
if skipping_test_is_okay(&result_cmd, "cannot find name for user ID") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
result_ucmd.stdout_str().trim(),
|
|
||||||
result_cmd.stdout_str().trim()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue