test_whoami: refactor use expected_result and whoami from common/util.rs
-use crate::common::util::*;
+// * 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.
-// 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
-}
+use crate::common::util::*;
#[test]
+#[cfg(unix)]
fn test_normal() {
- let (_, mut ucmd) = at_and_ucmd!();
-
- let result = ucmd.run();
-
- // use std::env;
- // println!("env::var(CI).is_ok() = {}", env::var("CI").is_ok());
- // for (key, value) in env::vars() {
- // println!("{}: {}", key, value);
- // }
+ let ts = TestScenario::new(util_name!());
+ let result = ts.ucmd().run();
+ let exp_result = unwrap_or_return!(expected_result(&ts, &[]));
- if skipping_test_is_okay(&result, "failed to get username") {
- return;
- }
-
- result.no_stderr();
- assert!(!result.stdout_str().trim().is_empty());
+ result
+ .stdout_is(exp_result.stdout_str())
+ .stderr_is(exp_result.stderr_str())
+ .code_is(exp_result.code());
}
#[test]
-#[cfg(not(windows))]
+#[cfg(unix)]
fn test_normal_compare_id() {
- let scene = TestScenario::new(util_name!());
-
- let result_ucmd = scene.ucmd().run();
- if skipping_test_is_okay(&result_ucmd, "failed to get username") {
- return;
+ 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_cmd = scene.cmd("id").arg("-un").run();
- if skipping_test_is_okay(&result_cmd, "cannot find name for user ID") {
+#[test]
+#[cfg(unix)]
+fn test_normal_compare_env() {
+ let whoami = whoami();
+ if whoami == "nobody" {
+ println!("test skipped:");
return;
+ } else {
+ new_ucmd!().succeeds().stdout_is(format!("{}\n", whoami));
}
-
- assert_eq!(
- result_ucmd.stdout_str().trim(),
- result_cmd.stdout_str().trim()
- );
}
