mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 19:17:43 +00:00
Merge pull request #2484 from jhscheer/refactor_expected_result
tests/common: refactor to reduce duplicate code
This commit is contained in:
commit
f4de8e1bc8
9 changed files with 570 additions and 679 deletions
|
@ -39,13 +39,13 @@ fn _du_basics(s: &str) {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_du_basics_subdir() {
|
fn test_du_basics_subdir() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
|
|
||||||
let result = scene.ucmd().arg(SUB_DIR).succeeds();
|
let result = ts.ucmd().arg(SUB_DIR).succeeds();
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
{
|
{
|
||||||
let result_reference = scene.cmd("du").arg(SUB_DIR).run();
|
let result_reference = unwrap_or_return!(expected_result(&ts, &[SUB_DIR]));
|
||||||
if result_reference.succeeded() {
|
if result_reference.succeeded() {
|
||||||
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
||||||
return;
|
return;
|
||||||
|
@ -83,15 +83,16 @@ fn _du_basics_subdir(s: &str) {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_du_invalid_size() {
|
fn test_du_invalid_size() {
|
||||||
let args = &["block-size", "threshold"];
|
let args = &["block-size", "threshold"];
|
||||||
|
let ts = TestScenario::new(util_name!());
|
||||||
for s in args {
|
for s in args {
|
||||||
new_ucmd!()
|
ts.ucmd()
|
||||||
.arg(format!("--{}=1fb4t", s))
|
.arg(format!("--{}=1fb4t", s))
|
||||||
.arg("/tmp")
|
.arg("/tmp")
|
||||||
.fails()
|
.fails()
|
||||||
.code_is(1)
|
.code_is(1)
|
||||||
.stderr_only(format!("du: invalid --{} argument '1fb4t'", s));
|
.stderr_only(format!("du: invalid --{} argument '1fb4t'", s));
|
||||||
#[cfg(not(target_pointer_width = "128"))]
|
#[cfg(not(target_pointer_width = "128"))]
|
||||||
new_ucmd!()
|
ts.ucmd()
|
||||||
.arg(format!("--{}=1Y", s))
|
.arg(format!("--{}=1Y", s))
|
||||||
.arg("/tmp")
|
.arg("/tmp")
|
||||||
.fails()
|
.fails()
|
||||||
|
@ -110,16 +111,16 @@ fn test_du_basics_bad_name() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_du_soft_link() {
|
fn test_du_soft_link() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
let at = &scene.fixtures;
|
let at = &ts.fixtures;
|
||||||
|
|
||||||
at.symlink_file(SUB_FILE, SUB_LINK);
|
at.symlink_file(SUB_FILE, SUB_LINK);
|
||||||
|
|
||||||
let result = scene.ucmd().arg(SUB_DIR_LINKS).succeeds();
|
let result = ts.ucmd().arg(SUB_DIR_LINKS).succeeds();
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
{
|
{
|
||||||
let result_reference = scene.cmd("du").arg(SUB_DIR_LINKS).run();
|
let result_reference = unwrap_or_return!(expected_result(&ts, &[SUB_DIR_LINKS]));
|
||||||
if result_reference.succeeded() {
|
if result_reference.succeeded() {
|
||||||
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
||||||
return;
|
return;
|
||||||
|
@ -157,16 +158,16 @@ fn _du_soft_link(s: &str) {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_du_hard_link() {
|
fn test_du_hard_link() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
let at = &scene.fixtures;
|
let at = &ts.fixtures;
|
||||||
|
|
||||||
at.hard_link(SUB_FILE, SUB_LINK);
|
at.hard_link(SUB_FILE, SUB_LINK);
|
||||||
|
|
||||||
let result = scene.ucmd().arg(SUB_DIR_LINKS).succeeds();
|
let result = ts.ucmd().arg(SUB_DIR_LINKS).succeeds();
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
{
|
{
|
||||||
let result_reference = scene.cmd("du").arg(SUB_DIR_LINKS).run();
|
let result_reference = unwrap_or_return!(expected_result(&ts, &[SUB_DIR_LINKS]));
|
||||||
if result_reference.succeeded() {
|
if result_reference.succeeded() {
|
||||||
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
||||||
return;
|
return;
|
||||||
|
@ -204,13 +205,13 @@ fn _du_hard_link(s: &str) {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_du_d_flag() {
|
fn test_du_d_flag() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
|
|
||||||
let result = scene.ucmd().arg("-d1").succeeds();
|
let result = ts.ucmd().arg("-d1").succeeds();
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
{
|
{
|
||||||
let result_reference = scene.cmd("du").arg("-d1").run();
|
let result_reference = unwrap_or_return!(expected_result(&ts, &["-d1"]));
|
||||||
if result_reference.succeeded() {
|
if result_reference.succeeded() {
|
||||||
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
||||||
return;
|
return;
|
||||||
|
@ -247,16 +248,17 @@ fn _du_d_flag(s: &str) {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_du_dereference() {
|
fn test_du_dereference() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
let at = &scene.fixtures;
|
let at = &ts.fixtures;
|
||||||
|
|
||||||
at.symlink_dir(SUB_DEEPER_DIR, SUB_DIR_LINKS_DEEPER_SYM_DIR);
|
at.symlink_dir(SUB_DEEPER_DIR, SUB_DIR_LINKS_DEEPER_SYM_DIR);
|
||||||
|
|
||||||
let result = scene.ucmd().arg("-L").arg(SUB_DIR_LINKS).succeeds();
|
let result = ts.ucmd().arg("-L").arg(SUB_DIR_LINKS).succeeds();
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
{
|
{
|
||||||
let result_reference = scene.cmd("du").arg("-L").arg(SUB_DIR_LINKS).run();
|
let result_reference = unwrap_or_return!(expected_result(&ts, &["-L", SUB_DIR_LINKS]));
|
||||||
|
|
||||||
if result_reference.succeeded() {
|
if result_reference.succeeded() {
|
||||||
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
||||||
return;
|
return;
|
||||||
|
@ -294,12 +296,12 @@ fn _du_dereference(s: &str) {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_du_inodes_basic() {
|
fn test_du_inodes_basic() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
let result = scene.ucmd().arg("--inodes").succeeds();
|
let result = ts.ucmd().arg("--inodes").succeeds();
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
{
|
{
|
||||||
let result_reference = scene.cmd("du").arg("--inodes").run();
|
let result_reference = unwrap_or_return!(expected_result(&ts, &["--inodes"]));
|
||||||
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -335,20 +337,15 @@ fn _du_inodes_basic(s: &str) {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_du_inodes() {
|
fn test_du_inodes() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
|
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.arg("--summarize")
|
.arg("--summarize")
|
||||||
.arg("--inodes")
|
.arg("--inodes")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only("11\t.\n");
|
.stdout_only("11\t.\n");
|
||||||
|
|
||||||
let result = scene
|
let result = ts.ucmd().arg("--separate-dirs").arg("--inodes").succeeds();
|
||||||
.ucmd()
|
|
||||||
.arg("--separate-dirs")
|
|
||||||
.arg("--inodes")
|
|
||||||
.succeeds();
|
|
||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
result.stdout_contains("3\t.\\subdir\\links\n");
|
result.stdout_contains("3\t.\\subdir\\links\n");
|
||||||
|
@ -358,7 +355,8 @@ fn test_du_inodes() {
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
{
|
{
|
||||||
let result_reference = scene.cmd("du").arg("--separate-dirs").arg("--inodes").run();
|
let result_reference =
|
||||||
|
unwrap_or_return!(expected_result(&ts, &["--separate-dirs", "--inodes"]));
|
||||||
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -375,31 +373,29 @@ fn test_du_h_flag_empty_file() {
|
||||||
#[cfg(feature = "touch")]
|
#[cfg(feature = "touch")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_du_time() {
|
fn test_du_time() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
|
|
||||||
scene
|
ts.ccmd("touch")
|
||||||
.ccmd("touch")
|
|
||||||
.arg("-a")
|
.arg("-a")
|
||||||
.arg("-t")
|
.arg("-t")
|
||||||
.arg("201505150000")
|
.arg("201505150000")
|
||||||
.arg("date_test")
|
.arg("date_test")
|
||||||
.succeeds();
|
.succeeds();
|
||||||
|
|
||||||
scene
|
ts.ccmd("touch")
|
||||||
.ccmd("touch")
|
|
||||||
.arg("-m")
|
.arg("-m")
|
||||||
.arg("-t")
|
.arg("-t")
|
||||||
.arg("201606160000")
|
.arg("201606160000")
|
||||||
.arg("date_test")
|
.arg("date_test")
|
||||||
.succeeds();
|
.succeeds();
|
||||||
|
|
||||||
let result = scene.ucmd().arg("--time").arg("date_test").succeeds();
|
let result = ts.ucmd().arg("--time").arg("date_test").succeeds();
|
||||||
result.stdout_only("0\t2016-06-16 00:00\tdate_test\n");
|
result.stdout_only("0\t2016-06-16 00:00\tdate_test\n");
|
||||||
|
|
||||||
let result = scene.ucmd().arg("--time=atime").arg("date_test").succeeds();
|
let result = ts.ucmd().arg("--time=atime").arg("date_test").succeeds();
|
||||||
result.stdout_only("0\t2015-05-15 00:00\tdate_test\n");
|
result.stdout_only("0\t2015-05-15 00:00\tdate_test\n");
|
||||||
|
|
||||||
let result = scene.ucmd().arg("--time=ctime").arg("date_test").succeeds();
|
let result = ts.ucmd().arg("--time=ctime").arg("date_test").succeeds();
|
||||||
result.stdout_only("0\t2016-06-16 00:00\tdate_test\n");
|
result.stdout_only("0\t2016-06-16 00:00\tdate_test\n");
|
||||||
|
|
||||||
#[cfg(not(target_env = "musl"))]
|
#[cfg(not(target_env = "musl"))]
|
||||||
|
@ -408,7 +404,7 @@ fn test_du_time() {
|
||||||
|
|
||||||
let re_birth =
|
let re_birth =
|
||||||
Regex::new(r"0\t[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}\tdate_test").unwrap();
|
Regex::new(r"0\t[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}\tdate_test").unwrap();
|
||||||
let result = scene.ucmd().arg("--time=birth").arg("date_test").succeeds();
|
let result = ts.ucmd().arg("--time=birth").arg("date_test").succeeds();
|
||||||
result.stdout_matches(&re_birth);
|
result.stdout_matches(&re_birth);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -417,21 +413,21 @@ fn test_du_time() {
|
||||||
#[cfg(feature = "chmod")]
|
#[cfg(feature = "chmod")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_du_no_permission() {
|
fn test_du_no_permission() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
let at = &scene.fixtures;
|
let at = &ts.fixtures;
|
||||||
|
|
||||||
at.mkdir_all(SUB_DIR_LINKS);
|
at.mkdir_all(SUB_DIR_LINKS);
|
||||||
|
|
||||||
scene.ccmd("chmod").arg("-r").arg(SUB_DIR_LINKS).succeeds();
|
ts.ccmd("chmod").arg("-r").arg(SUB_DIR_LINKS).succeeds();
|
||||||
|
|
||||||
let result = scene.ucmd().arg(SUB_DIR_LINKS).run(); // TODO: replace with ".fails()" once `du` is fixed
|
let result = ts.ucmd().arg(SUB_DIR_LINKS).run(); // TODO: replace with ".fails()" once `du` is fixed
|
||||||
result.stderr_contains(
|
result.stderr_contains(
|
||||||
"du: cannot read directory 'subdir/links': Permission denied (os error 13)",
|
"du: cannot read directory 'subdir/links': Permission denied (os error 13)",
|
||||||
);
|
);
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
{
|
{
|
||||||
let result_reference = scene.cmd("du").arg(SUB_DIR_LINKS).fails();
|
let result_reference = unwrap_or_return!(expected_result(&ts, &[SUB_DIR_LINKS]));
|
||||||
if result_reference
|
if result_reference
|
||||||
.stderr_str()
|
.stderr_str()
|
||||||
.contains("du: cannot read directory 'subdir/links': Permission denied")
|
.contains("du: cannot read directory 'subdir/links': Permission denied")
|
||||||
|
@ -455,13 +451,13 @@ fn _du_no_permission(s: &str) {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_du_one_file_system() {
|
fn test_du_one_file_system() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
|
|
||||||
let result = scene.ucmd().arg("-x").arg(SUB_DIR).succeeds();
|
let result = ts.ucmd().arg("-x").arg(SUB_DIR).succeeds();
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
{
|
{
|
||||||
let result_reference = scene.cmd("du").arg("-x").arg(SUB_DIR).run();
|
let result_reference = unwrap_or_return!(expected_result(&ts, &["-x", SUB_DIR]));
|
||||||
if result_reference.succeeded() {
|
if result_reference.succeeded() {
|
||||||
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
||||||
return;
|
return;
|
||||||
|
@ -472,19 +468,17 @@ fn test_du_one_file_system() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_du_threshold() {
|
fn test_du_threshold() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
|
|
||||||
let threshold = if cfg!(windows) { "7K" } else { "10K" };
|
let threshold = if cfg!(windows) { "7K" } else { "10K" };
|
||||||
|
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.arg(format!("--threshold={}", threshold))
|
.arg(format!("--threshold={}", threshold))
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_contains("links")
|
.stdout_contains("links")
|
||||||
.stdout_does_not_contain("deeper_dir");
|
.stdout_does_not_contain("deeper_dir");
|
||||||
|
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.arg(format!("--threshold=-{}", threshold))
|
.arg(format!("--threshold=-{}", threshold))
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_does_not_contain("links")
|
.stdout_does_not_contain("links")
|
||||||
|
@ -493,12 +487,12 @@ fn test_du_threshold() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_du_apparent_size() {
|
fn test_du_apparent_size() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
let result = scene.ucmd().arg("--apparent-size").succeeds();
|
let result = ts.ucmd().arg("--apparent-size").succeeds();
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
{
|
{
|
||||||
let result_reference = scene.cmd("du").arg("--apparent-size").run();
|
let result_reference = unwrap_or_return!(expected_result(&ts, &["--apparent-size"]));
|
||||||
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -561,12 +555,12 @@ fn _du_apparent_size(s: &str) {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_du_bytes() {
|
fn test_du_bytes() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
let result = scene.ucmd().arg("--bytes").succeeds();
|
let result = ts.ucmd().arg("--bytes").succeeds();
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
{
|
{
|
||||||
let result_reference = scene.cmd("du").arg("--bytes").run();
|
let result_reference = unwrap_or_return!(expected_result(&ts, &["--bytes"]));
|
||||||
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
assert_eq!(result.stdout_str(), result_reference.stdout_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,55 +1,20 @@
|
||||||
|
// * 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.
|
||||||
|
|
||||||
|
//spell-checker: ignore coreutil
|
||||||
|
|
||||||
use crate::common::util::*;
|
use crate::common::util::*;
|
||||||
|
|
||||||
// spell-checker:ignore (ToDO) coreutil
|
|
||||||
|
|
||||||
// These tests run the GNU coreutils `(g)groups` binary in `$PATH` in order to gather reference values.
|
|
||||||
// If the `(g)groups` 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_MIN: &str = "8.30"; // minimum Version for the reference `groups` in $PATH
|
|
||||||
const VERSION_MIN_MULTIPLE_USERS: &str = "8.31"; // this feature was introduced in GNU's coreutils 8.31
|
const VERSION_MIN_MULTIPLE_USERS: &str = "8.31"; // this feature was introduced in GNU's coreutils 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
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"
|
|
||||||
|
|
||||||
// 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]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn test_groups() {
|
fn test_groups() {
|
||||||
let result = new_ucmd!().run();
|
let ts = TestScenario::new(util_name!());
|
||||||
let exp_result = unwrap_or_return!(expected_result(&[]));
|
let result = ts.ucmd().run();
|
||||||
|
let exp_result = unwrap_or_return!(expected_result(&ts, &[]));
|
||||||
|
|
||||||
result
|
result
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
@ -62,8 +27,9 @@ fn test_groups() {
|
||||||
fn test_groups_username() {
|
fn test_groups_username() {
|
||||||
let test_users = [&whoami()[..]];
|
let test_users = [&whoami()[..]];
|
||||||
|
|
||||||
let result = new_ucmd!().args(&test_users).run();
|
let ts = TestScenario::new(util_name!());
|
||||||
let exp_result = unwrap_or_return!(expected_result(&test_users));
|
let result = ts.ucmd().args(&test_users).run();
|
||||||
|
let exp_result = unwrap_or_return!(expected_result(&ts, &test_users));
|
||||||
|
|
||||||
result
|
result
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
@ -74,103 +40,18 @@ fn test_groups_username() {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn test_groups_username_multiple() {
|
fn test_groups_username_multiple() {
|
||||||
// TODO: [2021-06; jhscheer] refactor this as `let util_name = host_name_for(util_name!())` when that function is added to 'tests/common'
|
unwrap_or_return!(check_coreutil_version(
|
||||||
#[cfg(target_os = "linux")]
|
util_name!(),
|
||||||
let util_name = util_name!();
|
VERSION_MIN_MULTIPLE_USERS
|
||||||
#[cfg(all(unix, not(target_os = "linux")))]
|
));
|
||||||
let util_name = &format!("g{}", util_name!());
|
|
||||||
let version_check_string = check_coreutil_version(util_name, VERSION_MIN_MULTIPLE_USERS);
|
|
||||||
if version_check_string.starts_with(UUTILS_WARNING) {
|
|
||||||
println!("{}\ntest skipped", version_check_string);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let test_users = ["root", "man", "postfix", "sshd", &whoami()];
|
let test_users = ["root", "man", "postfix", "sshd", &whoami()];
|
||||||
|
|
||||||
let result = new_ucmd!().args(&test_users).run();
|
let ts = TestScenario::new(util_name!());
|
||||||
let exp_result = unwrap_or_return!(expected_result(&test_users));
|
let result = ts.ucmd().args(&test_users).run();
|
||||||
|
let exp_result = unwrap_or_return!(expected_result(&ts, &test_users));
|
||||||
|
|
||||||
result
|
result
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
.stderr_is(exp_result.stderr_str())
|
.stderr_is(exp_result.stderr_str())
|
||||||
.code_is(exp_result.code());
|
.code_is(exp_result.code());
|
||||||
}
|
}
|
||||||
|
|
||||||
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("LC_ALL", "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> {
|
|
||||||
// TODO: [2021-06; jhscheer] refactor this as `let util_name = host_name_for(util_name!())` when that function is added to 'tests/common'
|
|
||||||
#[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_MIN);
|
|
||||||
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("LC_ALL", "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(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,60 +1,28 @@
|
||||||
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.
|
||||||
|
|
||||||
// spell-checker:ignore (ToDO) coreutil
|
// spell-checker:ignore (ToDO) coreutil
|
||||||
|
|
||||||
// These tests run the GNU coreutils `(g)id` binary in `$PATH` in order to gather reference values.
|
use crate::common::util::*;
|
||||||
// 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_MIN: &str = "8.30"; // minimum Version for the reference `id` in $PATH
|
|
||||||
const VERSION_MIN_MULTIPLE_USERS: &str = "8.31"; // this feature was introduced in GNU's coreutils 8.31
|
const VERSION_MIN_MULTIPLE_USERS: &str = "8.31"; // this feature was introduced in GNU's coreutils 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
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"
|
|
||||||
|
|
||||||
// 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]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn test_id_no_specified_user() {
|
fn test_id_no_specified_user() {
|
||||||
let result = new_ucmd!().run();
|
let ts = TestScenario::new(util_name!());
|
||||||
let exp_result = unwrap_or_return!(expected_result(&[]));
|
let result = ts.ucmd().run();
|
||||||
|
let exp_result = unwrap_or_return!(expected_result(&ts, &[]));
|
||||||
let mut _exp_stdout = exp_result.stdout_str().to_string();
|
let mut _exp_stdout = exp_result.stdout_str().to_string();
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
{
|
{
|
||||||
// NOTE: (SELinux NotImplemented) strip 'context' part from exp_stdout:
|
// NOTE: (SELinux NotImplemented) strip 'context' part from exp_stdout:
|
||||||
|
// example:
|
||||||
|
// uid=1001(runner) gid=121(docker) groups=121(docker),4(adm),101(systemd-journal) \
|
||||||
|
// context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
|
||||||
if let Some(context_offset) = exp_result.stdout_str().find(" context=") {
|
if let Some(context_offset) = exp_result.stdout_str().find(" context=") {
|
||||||
_exp_stdout.replace_range(context_offset.._exp_stdout.len() - 1, "");
|
_exp_stdout.replace_range(context_offset.._exp_stdout.len() - 1, "");
|
||||||
}
|
}
|
||||||
|
@ -71,10 +39,9 @@ fn test_id_no_specified_user() {
|
||||||
fn test_id_single_user() {
|
fn test_id_single_user() {
|
||||||
let test_users = [&whoami()[..]];
|
let test_users = [&whoami()[..]];
|
||||||
|
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
let mut exp_result = unwrap_or_return!(expected_result(&test_users));
|
let mut exp_result = unwrap_or_return!(expected_result(&ts, &test_users));
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&test_users)
|
.args(&test_users)
|
||||||
.run()
|
.run()
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
@ -85,36 +52,32 @@ fn test_id_single_user() {
|
||||||
for &opt in &["--user", "--group", "--groups"] {
|
for &opt in &["--user", "--group", "--groups"] {
|
||||||
let mut args = vec![opt];
|
let mut args = vec![opt];
|
||||||
args.extend_from_slice(&test_users);
|
args.extend_from_slice(&test_users);
|
||||||
exp_result = unwrap_or_return!(expected_result(&args));
|
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.run()
|
.run()
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||||
.code_is(exp_result.code());
|
.code_is(exp_result.code());
|
||||||
args.push("--zero");
|
args.push("--zero");
|
||||||
exp_result = unwrap_or_return!(expected_result(&args));
|
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.run()
|
.run()
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||||
.code_is(exp_result.code());
|
.code_is(exp_result.code());
|
||||||
args.push("--name");
|
args.push("--name");
|
||||||
exp_result = unwrap_or_return!(expected_result(&args));
|
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.run()
|
.run()
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||||
.code_is(exp_result.code());
|
.code_is(exp_result.code());
|
||||||
args.pop();
|
args.pop();
|
||||||
exp_result = unwrap_or_return!(expected_result(&args));
|
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.run()
|
.run()
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
@ -127,8 +90,9 @@ fn test_id_single_user() {
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn test_id_single_user_non_existing() {
|
fn test_id_single_user_non_existing() {
|
||||||
let args = &["hopefully_non_existing_username"];
|
let args = &["hopefully_non_existing_username"];
|
||||||
let result = new_ucmd!().args(args).run();
|
let ts = TestScenario::new(util_name!());
|
||||||
let exp_result = unwrap_or_return!(expected_result(args));
|
let result = ts.ucmd().args(args).run();
|
||||||
|
let exp_result = unwrap_or_return!(expected_result(&ts, args));
|
||||||
|
|
||||||
// It is unknown why on macOS (and possibly others?) `id` adds "Invalid argument".
|
// It is unknown why on macOS (and possibly others?) `id` adds "Invalid argument".
|
||||||
// coreutils 8.32: $ LC_ALL=C id foobar
|
// coreutils 8.32: $ LC_ALL=C id foobar
|
||||||
|
@ -143,11 +107,11 @@ fn test_id_single_user_non_existing() {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn test_id_name() {
|
fn test_id_name() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
for &opt in &["--user", "--group", "--groups"] {
|
for &opt in &["--user", "--group", "--groups"] {
|
||||||
let args = [opt, "--name"];
|
let args = [opt, "--name"];
|
||||||
let result = scene.ucmd().args(&args).run();
|
let result = ts.ucmd().args(&args).run();
|
||||||
let exp_result = unwrap_or_return!(expected_result(&args));
|
let exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||||
result
|
result
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
.stderr_is(exp_result.stderr_str())
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
@ -162,11 +126,11 @@ fn test_id_name() {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn test_id_real() {
|
fn test_id_real() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
for &opt in &["--user", "--group", "--groups"] {
|
for &opt in &["--user", "--group", "--groups"] {
|
||||||
let args = [opt, "--real"];
|
let args = [opt, "--real"];
|
||||||
let result = scene.ucmd().args(&args).run();
|
let result = ts.ucmd().args(&args).run();
|
||||||
let exp_result = unwrap_or_return!(expected_result(&args));
|
let exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||||
result
|
result
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
.stderr_is(exp_result.stderr_str())
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
@ -206,23 +170,17 @@ fn test_id_password_style() {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn test_id_multiple_users() {
|
fn test_id_multiple_users() {
|
||||||
#[cfg(target_os = "linux")]
|
unwrap_or_return!(check_coreutil_version(
|
||||||
let util_name = util_name!();
|
util_name!(),
|
||||||
#[cfg(all(unix, not(target_os = "linux")))]
|
VERSION_MIN_MULTIPLE_USERS
|
||||||
let util_name = &format!("g{}", util_name!());
|
));
|
||||||
let version_check_string = check_coreutil_version(util_name, VERSION_MIN_MULTIPLE_USERS);
|
|
||||||
if version_check_string.starts_with(UUTILS_WARNING) {
|
|
||||||
println!("{}\ntest skipped", version_check_string);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Same typical users that GNU test suite is using.
|
// Same typical users that GNU test suite is using.
|
||||||
let test_users = ["root", "man", "postfix", "sshd", &whoami()];
|
let test_users = ["root", "man", "postfix", "sshd", &whoami()];
|
||||||
|
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
let mut exp_result = unwrap_or_return!(expected_result(&test_users));
|
let mut exp_result = unwrap_or_return!(expected_result(&ts, &test_users));
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&test_users)
|
.args(&test_users)
|
||||||
.run()
|
.run()
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
@ -233,36 +191,32 @@ fn test_id_multiple_users() {
|
||||||
for &opt in &["--user", "--group", "--groups"] {
|
for &opt in &["--user", "--group", "--groups"] {
|
||||||
let mut args = vec![opt];
|
let mut args = vec![opt];
|
||||||
args.extend_from_slice(&test_users);
|
args.extend_from_slice(&test_users);
|
||||||
exp_result = unwrap_or_return!(expected_result(&args));
|
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.run()
|
.run()
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||||
.code_is(exp_result.code());
|
.code_is(exp_result.code());
|
||||||
args.push("--zero");
|
args.push("--zero");
|
||||||
exp_result = unwrap_or_return!(expected_result(&args));
|
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.run()
|
.run()
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||||
.code_is(exp_result.code());
|
.code_is(exp_result.code());
|
||||||
args.push("--name");
|
args.push("--name");
|
||||||
exp_result = unwrap_or_return!(expected_result(&args));
|
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.run()
|
.run()
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||||
.code_is(exp_result.code());
|
.code_is(exp_result.code());
|
||||||
args.pop();
|
args.pop();
|
||||||
exp_result = unwrap_or_return!(expected_result(&args));
|
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.run()
|
.run()
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
@ -274,15 +228,10 @@ fn test_id_multiple_users() {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn test_id_multiple_users_non_existing() {
|
fn test_id_multiple_users_non_existing() {
|
||||||
#[cfg(target_os = "linux")]
|
unwrap_or_return!(check_coreutil_version(
|
||||||
let util_name = util_name!();
|
util_name!(),
|
||||||
#[cfg(all(unix, not(target_os = "linux")))]
|
VERSION_MIN_MULTIPLE_USERS
|
||||||
let util_name = &format!("g{}", util_name!());
|
));
|
||||||
let version_check_string = check_coreutil_version(util_name, VERSION_MIN_MULTIPLE_USERS);
|
|
||||||
if version_check_string.starts_with(UUTILS_WARNING) {
|
|
||||||
println!("{}\ntest skipped", version_check_string);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let test_users = [
|
let test_users = [
|
||||||
"root",
|
"root",
|
||||||
|
@ -297,10 +246,9 @@ fn test_id_multiple_users_non_existing() {
|
||||||
&whoami(),
|
&whoami(),
|
||||||
];
|
];
|
||||||
|
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
let mut exp_result = unwrap_or_return!(expected_result(&test_users));
|
let mut exp_result = unwrap_or_return!(expected_result(&ts, &test_users));
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&test_users)
|
.args(&test_users)
|
||||||
.run()
|
.run()
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
@ -311,36 +259,32 @@ fn test_id_multiple_users_non_existing() {
|
||||||
for &opt in &["--user", "--group", "--groups"] {
|
for &opt in &["--user", "--group", "--groups"] {
|
||||||
let mut args = vec![opt];
|
let mut args = vec![opt];
|
||||||
args.extend_from_slice(&test_users);
|
args.extend_from_slice(&test_users);
|
||||||
exp_result = unwrap_or_return!(expected_result(&args));
|
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.run()
|
.run()
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||||
.code_is(exp_result.code());
|
.code_is(exp_result.code());
|
||||||
args.push("--zero");
|
args.push("--zero");
|
||||||
exp_result = unwrap_or_return!(expected_result(&args));
|
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.run()
|
.run()
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||||
.code_is(exp_result.code());
|
.code_is(exp_result.code());
|
||||||
args.push("--name");
|
args.push("--name");
|
||||||
exp_result = unwrap_or_return!(expected_result(&args));
|
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.run()
|
.run()
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||||
.code_is(exp_result.code());
|
.code_is(exp_result.code());
|
||||||
args.pop();
|
args.pop();
|
||||||
exp_result = unwrap_or_return!(expected_result(&args));
|
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.run()
|
.run()
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
@ -352,20 +296,19 @@ fn test_id_multiple_users_non_existing() {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn test_id_default_format() {
|
fn test_id_default_format() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
for &opt1 in &["--name", "--real"] {
|
for &opt1 in &["--name", "--real"] {
|
||||||
// id: cannot print only names or real IDs in default format
|
// id: cannot print only names or real IDs in default format
|
||||||
let args = [opt1];
|
let args = [opt1];
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.fails()
|
.fails()
|
||||||
.stderr_only(unwrap_or_return!(expected_result(&args)).stderr_str());
|
.stderr_only(unwrap_or_return!(expected_result(&ts, &args)).stderr_str());
|
||||||
for &opt2 in &["--user", "--group", "--groups"] {
|
for &opt2 in &["--user", "--group", "--groups"] {
|
||||||
// u/g/G n/r
|
// u/g/G n/r
|
||||||
let args = [opt2, opt1];
|
let args = [opt2, opt1];
|
||||||
let result = scene.ucmd().args(&args).run();
|
let result = ts.ucmd().args(&args).run();
|
||||||
let exp_result = unwrap_or_return!(expected_result(&args));
|
let exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||||
result
|
result
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
.stderr_is(exp_result.stderr_str())
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
@ -375,38 +318,35 @@ fn test_id_default_format() {
|
||||||
for &opt2 in &["--user", "--group", "--groups"] {
|
for &opt2 in &["--user", "--group", "--groups"] {
|
||||||
// u/g/G
|
// u/g/G
|
||||||
let args = [opt2];
|
let args = [opt2];
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only(unwrap_or_return!(expected_result(&args)).stdout_str());
|
.stdout_only(unwrap_or_return!(expected_result(&ts, &args)).stdout_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn test_id_zero() {
|
fn test_id_zero() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
for z_flag in &["-z", "--zero"] {
|
for z_flag in &["-z", "--zero"] {
|
||||||
// id: option --zero not permitted in default format
|
// id: option --zero not permitted in default format
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&[z_flag])
|
.args(&[z_flag])
|
||||||
.fails()
|
.fails()
|
||||||
.stderr_only(unwrap_or_return!(expected_result(&[z_flag])).stderr_str());
|
.stderr_only(unwrap_or_return!(expected_result(&ts, &[z_flag])).stderr_str());
|
||||||
for &opt1 in &["--name", "--real"] {
|
for &opt1 in &["--name", "--real"] {
|
||||||
// id: cannot print only names or real IDs in default format
|
// id: cannot print only names or real IDs in default format
|
||||||
let args = [opt1, z_flag];
|
let args = [opt1, z_flag];
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.fails()
|
.fails()
|
||||||
.stderr_only(unwrap_or_return!(expected_result(&args)).stderr_str());
|
.stderr_only(unwrap_or_return!(expected_result(&ts, &args)).stderr_str());
|
||||||
for &opt2 in &["--user", "--group", "--groups"] {
|
for &opt2 in &["--user", "--group", "--groups"] {
|
||||||
// u/g/G n/r z
|
// u/g/G n/r z
|
||||||
let args = [opt2, z_flag, opt1];
|
let args = [opt2, z_flag, opt1];
|
||||||
let result = scene.ucmd().args(&args).run();
|
let result = ts.ucmd().args(&args).run();
|
||||||
let exp_result = unwrap_or_return!(expected_result(&args));
|
let exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||||
result
|
result
|
||||||
.stdout_is(exp_result.stdout_str())
|
.stdout_is(exp_result.stdout_str())
|
||||||
.stderr_is(exp_result.stderr_str())
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
@ -416,90 +356,10 @@ fn test_id_zero() {
|
||||||
for &opt2 in &["--user", "--group", "--groups"] {
|
for &opt2 in &["--user", "--group", "--groups"] {
|
||||||
// u/g/G z
|
// u/g/G z
|
||||||
let args = [opt2, z_flag];
|
let args = [opt2, z_flag];
|
||||||
scene
|
ts.ucmd()
|
||||||
.ucmd()
|
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only(unwrap_or_return!(expected_result(&args)).stdout_str());
|
.stdout_only(unwrap_or_return!(expected_result(&ts, &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("LC_ALL", "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_MIN);
|
|
||||||
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("LC_ALL", "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(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
|
// * 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.
|
||||||
|
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
use crate::common::util::*;
|
use crate::common::util::*;
|
||||||
|
@ -20,11 +25,8 @@ fn test_long_format() {
|
||||||
let login = "root";
|
let login = "root";
|
||||||
let pw: Passwd = Passwd::locate(login).unwrap();
|
let pw: Passwd = Passwd::locate(login).unwrap();
|
||||||
let real_name = pw.user_info().replace("&", &pw.name().capitalize());
|
let real_name = pw.user_info().replace("&", &pw.name().capitalize());
|
||||||
new_ucmd!()
|
let ts = TestScenario::new(util_name!());
|
||||||
.arg("-l")
|
ts.ucmd().arg("-l").arg(login).succeeds().stdout_is(format!(
|
||||||
.arg(login)
|
|
||||||
.succeeds()
|
|
||||||
.stdout_is(format!(
|
|
||||||
"Login name: {:<28}In real life: {}\nDirectory: {:<29}Shell: {}\n\n",
|
"Login name: {:<28}In real life: {}\nDirectory: {:<29}Shell: {}\n\n",
|
||||||
login,
|
login,
|
||||||
real_name,
|
real_name,
|
||||||
|
@ -32,7 +34,7 @@ fn test_long_format() {
|
||||||
pw.user_shell()
|
pw.user_shell()
|
||||||
));
|
));
|
||||||
|
|
||||||
new_ucmd!()
|
ts.ucmd()
|
||||||
.arg("-lb")
|
.arg("-lb")
|
||||||
.arg(login)
|
.arg(login)
|
||||||
.succeeds()
|
.succeeds()
|
||||||
|
@ -42,15 +44,18 @@ fn test_long_format() {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_long_format_multiple_users() {
|
fn test_long_format_multiple_users() {
|
||||||
let args = ["-l", "root", "root", "root"];
|
let args = ["-l", "root", "root", "root"];
|
||||||
|
let ts = TestScenario::new(util_name!());
|
||||||
|
let expect = unwrap_or_return!(expected_result(&ts, &args));
|
||||||
|
|
||||||
new_ucmd!()
|
ts.ucmd()
|
||||||
.args(&args)
|
.args(&args)
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_is(expected_result(&args));
|
.stdout_is(expect.stdout_str())
|
||||||
|
.stderr_is(expect.stderr_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -59,55 +64,41 @@ fn test_long_format_wo_user() {
|
||||||
new_ucmd!().arg("-l").fails().code_is(1);
|
new_ucmd!().arg("-l").fails().code_is(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_short_format_i() {
|
fn test_short_format_i() {
|
||||||
// allow whitespace variation
|
// allow whitespace variation
|
||||||
// * minor whitespace differences occur between platform built-in outputs; specifically, the number of trailing TABs may be variant
|
// * minor whitespace differences occur between platform built-in outputs; specifically, the number of trailing TABs may be variant
|
||||||
let args = ["-i"];
|
let args = ["-i"];
|
||||||
let actual = new_ucmd!().args(&args).succeeds().stdout_move_str();
|
let ts = TestScenario::new(util_name!());
|
||||||
let expect = expected_result(&args);
|
let actual = ts.ucmd().args(&args).succeeds().stdout_move_str();
|
||||||
|
let expect = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||||
let v_actual: Vec<&str> = actual.split_whitespace().collect();
|
let v_actual: Vec<&str> = actual.split_whitespace().collect();
|
||||||
let v_expect: Vec<&str> = expect.split_whitespace().collect();
|
let v_expect: Vec<&str> = expect.split_whitespace().collect();
|
||||||
assert_eq!(v_actual, v_expect);
|
assert_eq!(v_actual, v_expect);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_short_format_q() {
|
fn test_short_format_q() {
|
||||||
// allow whitespace variation
|
// allow whitespace variation
|
||||||
// * minor whitespace differences occur between platform built-in outputs; specifically, the number of trailing TABs may be variant
|
// * minor whitespace differences occur between platform built-in outputs; specifically, the number of trailing TABs may be variant
|
||||||
let args = ["-q"];
|
let args = ["-q"];
|
||||||
let actual = new_ucmd!().args(&args).succeeds().stdout_move_str();
|
let ts = TestScenario::new(util_name!());
|
||||||
let expect = expected_result(&args);
|
let actual = ts.ucmd().args(&args).succeeds().stdout_move_str();
|
||||||
|
let expect = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||||
let v_actual: Vec<&str> = actual.split_whitespace().collect();
|
let v_actual: Vec<&str> = actual.split_whitespace().collect();
|
||||||
let v_expect: Vec<&str> = expect.split_whitespace().collect();
|
let v_expect: Vec<&str> = expect.split_whitespace().collect();
|
||||||
assert_eq!(v_actual, v_expect);
|
assert_eq!(v_actual, v_expect);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_no_flag() {
|
fn test_no_flag() {
|
||||||
let actual = new_ucmd!().succeeds().stdout_move_str();
|
let ts = TestScenario::new(util_name!());
|
||||||
let expect = expected_result(&[]);
|
let actual = ts.ucmd().succeeds().stdout_move_str();
|
||||||
|
let expect = unwrap_or_return!(expected_result(&ts, &[])).stdout_move_str();
|
||||||
let v_actual: Vec<&str> = actual.split_whitespace().collect();
|
let v_actual: Vec<&str> = actual.split_whitespace().collect();
|
||||||
let v_expect: Vec<&str> = expect.split_whitespace().collect();
|
let v_expect: Vec<&str> = expect.split_whitespace().collect();
|
||||||
assert_eq!(v_actual, v_expect);
|
assert_eq!(v_actual, v_expect);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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!());
|
|
||||||
|
|
||||||
// note: clippy::needless_borrow *false positive*
|
|
||||||
#[allow(clippy::needless_borrow)]
|
|
||||||
TestScenario::new(&util_name)
|
|
||||||
.cmd_keepenv(util_name)
|
|
||||||
.env("LC_ALL", "C")
|
|
||||||
.args(args)
|
|
||||||
.succeeds()
|
|
||||||
.stdout_move_str()
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
|
// * 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.
|
||||||
|
|
||||||
extern crate regex;
|
extern crate regex;
|
||||||
|
|
||||||
use crate::common::util::*;
|
use crate::common::util::*;
|
||||||
|
@ -109,30 +114,29 @@ const FS_FORMAT_STR: &str = "%b %c %i %l %n %s %S %t %T"; // avoid "%a %d %f" wh
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
fn test_terse_fs_format() {
|
fn test_terse_fs_format() {
|
||||||
let args = ["-f", "-t", "/proc"];
|
let args = ["-f", "-t", "/proc"];
|
||||||
new_ucmd!()
|
let ts = TestScenario::new(util_name!());
|
||||||
.args(&args)
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||||
.run()
|
ts.ucmd().args(&args).run().stdout_is(expected_stdout);
|
||||||
.stdout_is(expected_result(&args));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
fn test_fs_format() {
|
fn test_fs_format() {
|
||||||
let args = ["-f", "-c", FS_FORMAT_STR, "/dev/shm"];
|
let args = ["-f", "-c", FS_FORMAT_STR, "/dev/shm"];
|
||||||
new_ucmd!()
|
let ts = TestScenario::new(util_name!());
|
||||||
.args(&args)
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||||
.run()
|
ts.ucmd().args(&args).run().stdout_is(expected_stdout);
|
||||||
.stdout_is(expected_result(&args));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_terse_normal_format() {
|
fn test_terse_normal_format() {
|
||||||
// note: contains birth/creation date which increases test fragility
|
// note: contains birth/creation date which increases test fragility
|
||||||
// * results may vary due to built-in `stat` limitations as well as linux kernel and rust version capability variations
|
// * results may vary due to built-in `stat` limitations as well as linux kernel and rust version capability variations
|
||||||
let args = ["-t", "/"];
|
let args = ["-t", "/"];
|
||||||
let actual = new_ucmd!().args(&args).succeeds().stdout_move_str();
|
let ts = TestScenario::new(util_name!());
|
||||||
let expect = expected_result(&args);
|
let actual = ts.ucmd().args(&args).succeeds().stdout_move_str();
|
||||||
|
let expect = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||||
println!("actual: {:?}", actual);
|
println!("actual: {:?}", actual);
|
||||||
println!("expect: {:?}", expect);
|
println!("expect: {:?}", expect);
|
||||||
let v_actual: Vec<&str> = actual.trim().split(' ').collect();
|
let v_actual: Vec<&str> = actual.trim().split(' ').collect();
|
||||||
|
@ -156,12 +160,13 @@ fn test_terse_normal_format() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_format_created_time() {
|
fn test_format_created_time() {
|
||||||
let args = ["-c", "%w", "/bin"];
|
let args = ["-c", "%w", "/bin"];
|
||||||
let actual = new_ucmd!().args(&args).succeeds().stdout_move_str();
|
let ts = TestScenario::new(util_name!());
|
||||||
let expect = expected_result(&args);
|
let actual = ts.ucmd().args(&args).succeeds().stdout_move_str();
|
||||||
|
let expect = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||||
println!("actual: {:?}", actual);
|
println!("actual: {:?}", actual);
|
||||||
println!("expect: {:?}", expect);
|
println!("expect: {:?}", expect);
|
||||||
// note: using a regex instead of `split_whitespace()` in order to detect whitespace differences
|
// note: using a regex instead of `split_whitespace()` in order to detect whitespace differences
|
||||||
|
@ -180,12 +185,13 @@ fn test_format_created_time() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_format_created_seconds() {
|
fn test_format_created_seconds() {
|
||||||
let args = ["-c", "%W", "/bin"];
|
let args = ["-c", "%W", "/bin"];
|
||||||
let actual = new_ucmd!().args(&args).succeeds().stdout_move_str();
|
let ts = TestScenario::new(util_name!());
|
||||||
let expect = expected_result(&args);
|
let actual = ts.ucmd().args(&args).succeeds().stdout_move_str();
|
||||||
|
let expect = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||||
println!("actual: {:?}", actual);
|
println!("actual: {:?}", actual);
|
||||||
println!("expect: {:?}", expect);
|
println!("expect: {:?}", expect);
|
||||||
// note: using a regex instead of `split_whitespace()` in order to detect whitespace differences
|
// note: using a regex instead of `split_whitespace()` in order to detect whitespace differences
|
||||||
|
@ -204,21 +210,20 @@ fn test_format_created_seconds() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_normal_format() {
|
fn test_normal_format() {
|
||||||
let args = ["-c", NORMAL_FORMAT_STR, "/bin"];
|
let args = ["-c", NORMAL_FORMAT_STR, "/bin"];
|
||||||
new_ucmd!()
|
let ts = TestScenario::new(util_name!());
|
||||||
.args(&args)
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||||
.succeeds()
|
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
|
||||||
.stdout_is(expected_result(&args));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_symlinks() {
|
fn test_symlinks() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
let at = &scene.fixtures;
|
let at = &ts.fixtures;
|
||||||
|
|
||||||
let mut tested: bool = false;
|
let mut tested: bool = false;
|
||||||
// arbitrarily chosen symlinks with hope that the CI environment provides at least one of them
|
// arbitrarily chosen symlinks with hope that the CI environment provides at least one of them
|
||||||
|
@ -232,18 +237,12 @@ fn test_symlinks() {
|
||||||
if at.file_exists(file) && at.is_symlink(file) {
|
if at.file_exists(file) && at.is_symlink(file) {
|
||||||
tested = true;
|
tested = true;
|
||||||
let args = ["-c", NORMAL_FORMAT_STR, file];
|
let args = ["-c", NORMAL_FORMAT_STR, file];
|
||||||
scene
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||||
.ucmd()
|
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
|
||||||
.args(&args)
|
|
||||||
.succeeds()
|
|
||||||
.stdout_is(expected_result(&args));
|
|
||||||
// -L, --dereference follow links
|
// -L, --dereference follow links
|
||||||
let args = ["-L", "-c", NORMAL_FORMAT_STR, file];
|
let args = ["-L", "-c", NORMAL_FORMAT_STR, file];
|
||||||
scene
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||||
.ucmd()
|
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
|
||||||
.args(&args)
|
|
||||||
.succeeds()
|
|
||||||
.stdout_is(expected_result(&args));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !tested {
|
if !tested {
|
||||||
|
@ -269,13 +268,12 @@ fn test_char() {
|
||||||
#[cfg(any(target_vendor = "apple"))]
|
#[cfg(any(target_vendor = "apple"))]
|
||||||
"/dev/ptmx",
|
"/dev/ptmx",
|
||||||
];
|
];
|
||||||
new_ucmd!()
|
let ts = TestScenario::new(util_name!());
|
||||||
.args(&args)
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||||
.succeeds()
|
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
|
||||||
.stdout_is(expected_result(&args));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_multi_files() {
|
fn test_multi_files() {
|
||||||
let args = [
|
let args = [
|
||||||
|
@ -287,38 +285,19 @@ fn test_multi_files() {
|
||||||
"/etc/fstab",
|
"/etc/fstab",
|
||||||
"/var",
|
"/var",
|
||||||
];
|
];
|
||||||
new_ucmd!()
|
let ts = TestScenario::new(util_name!());
|
||||||
.args(&args)
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||||
.succeeds()
|
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
|
||||||
.stdout_is(expected_result(&args));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_printf() {
|
fn test_printf() {
|
||||||
let args = [
|
let args = [
|
||||||
"--printf=123%-# 15q\\r\\\"\\\\\\a\\b\\e\\f\\v%+020.23m\\x12\\167\\132\\112\\n",
|
"--printf=123%-# 15q\\r\\\"\\\\\\a\\b\\e\\f\\v%+020.23m\\x12\\167\\132\\112\\n",
|
||||||
"/",
|
"/",
|
||||||
];
|
];
|
||||||
new_ucmd!()
|
let ts = TestScenario::new(util_name!());
|
||||||
.args(&args)
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||||
.succeeds()
|
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
|
||||||
.stdout_is(expected_result(&args));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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!());
|
|
||||||
|
|
||||||
// note: clippy::needless_borrow *false positive*
|
|
||||||
#[allow(clippy::needless_borrow)]
|
|
||||||
TestScenario::new(&util_name)
|
|
||||||
.cmd_keepenv(util_name)
|
|
||||||
.env("LC_ALL", "C")
|
|
||||||
.args(args)
|
|
||||||
.succeeds()
|
|
||||||
.stdout_move_str()
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,38 +1,42 @@
|
||||||
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.
|
||||||
|
|
||||||
// spell-checker:ignore (flags) runlevel mesg
|
// spell-checker:ignore (flags) runlevel mesg
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
use crate::common::util::*;
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_count() {
|
fn test_count() {
|
||||||
|
let ts = TestScenario::new(util_name!());
|
||||||
for opt in &["-q", "--count"] {
|
for opt in &["-q", "--count"] {
|
||||||
new_ucmd!()
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||||
.arg(opt)
|
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||||
.succeeds()
|
|
||||||
.stdout_is(expected_result(&[opt]));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_boot() {
|
fn test_boot() {
|
||||||
|
let ts = TestScenario::new(util_name!());
|
||||||
for opt in &["-b", "--boot"] {
|
for opt in &["-b", "--boot"] {
|
||||||
new_ucmd!()
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||||
.arg(opt)
|
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||||
.succeeds()
|
|
||||||
.stdout_is(expected_result(&[opt]));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_heading() {
|
fn test_heading() {
|
||||||
|
let ts = TestScenario::new(util_name!());
|
||||||
for opt in &["-H", "--heading"] {
|
for opt in &["-H", "--heading"] {
|
||||||
// allow whitespace variation
|
// allow whitespace variation
|
||||||
// * minor whitespace differences occur between platform built-in outputs;
|
// * minor whitespace differences occur between platform built-in outputs;
|
||||||
// specifically number of TABs between "TIME" and "COMMENT" may be variant
|
// specifically number of TABs between "TIME" and "COMMENT" may be variant
|
||||||
let actual = new_ucmd!().arg(opt).succeeds().stdout_move_str();
|
let actual = ts.ucmd().arg(opt).succeeds().stdout_move_str();
|
||||||
let expect = expected_result(&[opt]);
|
let expect = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||||
println!("actual: {:?}", actual);
|
println!("actual: {:?}", actual);
|
||||||
println!("expect: {:?}", expect);
|
println!("expect: {:?}", expect);
|
||||||
let v_actual: Vec<&str> = actual.split_whitespace().collect();
|
let v_actual: Vec<&str> = actual.split_whitespace().collect();
|
||||||
|
@ -41,76 +45,70 @@ fn test_heading() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_short() {
|
fn test_short() {
|
||||||
|
let ts = TestScenario::new(util_name!());
|
||||||
for opt in &["-s", "--short"] {
|
for opt in &["-s", "--short"] {
|
||||||
new_ucmd!()
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||||
.arg(opt)
|
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||||
.succeeds()
|
|
||||||
.stdout_is(expected_result(&[opt]));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_login() {
|
fn test_login() {
|
||||||
|
let ts = TestScenario::new(util_name!());
|
||||||
for opt in &["-l", "--login"] {
|
for opt in &["-l", "--login"] {
|
||||||
new_ucmd!()
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||||
.arg(opt)
|
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||||
.succeeds()
|
|
||||||
.stdout_is(expected_result(&[opt]));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_m() {
|
fn test_m() {
|
||||||
|
let ts = TestScenario::new(util_name!());
|
||||||
for opt in &["-m"] {
|
for opt in &["-m"] {
|
||||||
new_ucmd!()
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||||
.arg(opt)
|
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||||
.succeeds()
|
|
||||||
.stdout_is(expected_result(&[opt]));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_process() {
|
fn test_process() {
|
||||||
|
let ts = TestScenario::new(util_name!());
|
||||||
for opt in &["-p", "--process"] {
|
for opt in &["-p", "--process"] {
|
||||||
new_ucmd!()
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||||
.arg(opt)
|
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||||
.succeeds()
|
|
||||||
.stdout_is(expected_result(&[opt]));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_runlevel() {
|
fn test_runlevel() {
|
||||||
|
let ts = TestScenario::new(util_name!());
|
||||||
for opt in &["-r", "--runlevel"] {
|
for opt in &["-r", "--runlevel"] {
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||||
new_ucmd!()
|
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||||
.arg(opt)
|
|
||||||
.succeeds()
|
|
||||||
.stdout_is(expected_result(&[opt]));
|
|
||||||
|
|
||||||
#[cfg(not(target_os = "linux"))]
|
#[cfg(not(target_os = "linux"))]
|
||||||
new_ucmd!().arg(opt).succeeds().stdout_is("");
|
ts.ucmd().arg(opt).succeeds().stdout_is("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_time() {
|
fn test_time() {
|
||||||
|
let ts = TestScenario::new(util_name!());
|
||||||
for opt in &["-t", "--time"] {
|
for opt in &["-t", "--time"] {
|
||||||
new_ucmd!()
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||||
.arg(opt)
|
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||||
.succeeds()
|
|
||||||
.stdout_is(expected_result(&[opt]));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_mesg() {
|
fn test_mesg() {
|
||||||
// -T, -w, --mesg
|
// -T, -w, --mesg
|
||||||
|
@ -119,22 +117,20 @@ fn test_mesg() {
|
||||||
// same as -T
|
// same as -T
|
||||||
// --writable
|
// --writable
|
||||||
// same as -T
|
// same as -T
|
||||||
|
let ts = TestScenario::new(util_name!());
|
||||||
for opt in &["-T", "-w", "--mesg", "--message", "--writable"] {
|
for opt in &["-T", "-w", "--mesg", "--message", "--writable"] {
|
||||||
new_ucmd!()
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||||
.arg(opt)
|
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||||
.succeeds()
|
|
||||||
.stdout_is(expected_result(&[opt]));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_arg1_arg2() {
|
fn test_arg1_arg2() {
|
||||||
let args = ["am", "i"];
|
let args = ["am", "i"];
|
||||||
|
let ts = TestScenario::new(util_name!());
|
||||||
new_ucmd!()
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||||
.args(&args)
|
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
|
||||||
.succeeds()
|
|
||||||
.stdout_is(expected_result(&args));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -146,12 +142,13 @@ fn test_too_many_args() {
|
||||||
new_ucmd!().args(&args).fails().stderr_contains(EXPECTED);
|
new_ucmd!().args(&args).fails().stderr_contains(EXPECTED);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_users() {
|
fn test_users() {
|
||||||
|
let ts = TestScenario::new(util_name!());
|
||||||
for opt in &["-u", "--users"] {
|
for opt in &["-u", "--users"] {
|
||||||
let actual = new_ucmd!().arg(opt).succeeds().stdout_move_str();
|
let actual = ts.ucmd().arg(opt).succeeds().stdout_move_str();
|
||||||
let expect = expected_result(&[opt]);
|
let expect = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||||
println!("actual: {:?}", actual);
|
println!("actual: {:?}", actual);
|
||||||
println!("expect: {:?}", expect);
|
println!("expect: {:?}", expect);
|
||||||
|
|
||||||
|
@ -170,28 +167,26 @@ fn test_users() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_lookup() {
|
fn test_lookup() {
|
||||||
let opt = "--lookup";
|
let opt = "--lookup";
|
||||||
new_ucmd!()
|
let ts = TestScenario::new(util_name!());
|
||||||
.arg(opt)
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||||
.succeeds()
|
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||||
.stdout_is(expected_result(&[opt]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_dead() {
|
fn test_dead() {
|
||||||
|
let ts = TestScenario::new(util_name!());
|
||||||
for opt in &["-d", "--dead"] {
|
for opt in &["-d", "--dead"] {
|
||||||
new_ucmd!()
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||||
.arg(opt)
|
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||||
.succeeds()
|
|
||||||
.stdout_is(expected_result(&[opt]));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_all_separately() {
|
fn test_all_separately() {
|
||||||
if cfg!(target_os = "macos") {
|
if cfg!(target_os = "macos") {
|
||||||
|
@ -201,20 +196,14 @@ fn test_all_separately() {
|
||||||
|
|
||||||
// -a, --all same as -b -d --login -p -r -t -T -u
|
// -a, --all same as -b -d --login -p -r -t -T -u
|
||||||
let args = ["-b", "-d", "--login", "-p", "-r", "-t", "-T", "-u"];
|
let args = ["-b", "-d", "--login", "-p", "-r", "-t", "-T", "-u"];
|
||||||
let scene = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
scene
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||||
.ucmd()
|
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
|
||||||
.args(&args)
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &["--all"])).stdout_move_str();
|
||||||
.succeeds()
|
ts.ucmd().arg("--all").succeeds().stdout_is(expected_stdout);
|
||||||
.stdout_is(expected_result(&args));
|
|
||||||
scene
|
|
||||||
.ucmd()
|
|
||||||
.arg("--all")
|
|
||||||
.succeeds()
|
|
||||||
.stdout_is(expected_result(&args));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_all() {
|
fn test_all() {
|
||||||
if cfg!(target_os = "macos") {
|
if cfg!(target_os = "macos") {
|
||||||
|
@ -222,27 +211,9 @@ fn test_all() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let ts = TestScenario::new(util_name!());
|
||||||
for opt in &["-a", "--all"] {
|
for opt in &["-a", "--all"] {
|
||||||
new_ucmd!()
|
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||||
.arg(opt)
|
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||||
.succeeds()
|
|
||||||
.stdout_is(expected_result(&[opt]));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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!());
|
|
||||||
|
|
||||||
// note: clippy::needless_borrow *false positive*
|
|
||||||
#[allow(clippy::needless_borrow)]
|
|
||||||
TestScenario::new(&util_name)
|
|
||||||
.cmd_keepenv(util_name)
|
|
||||||
.env("LC_ALL", "C")
|
|
||||||
.args(args)
|
|
||||||
.succeeds()
|
|
||||||
.stdout_move_str()
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,63 +1,48 @@
|
||||||
|
// * 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.
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
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"]));
|
||||||
let result_ucmd = scene.ucmd().run();
|
if id_un.succeeded() {
|
||||||
if skipping_test_is_okay(&result_ucmd, "failed to get username") {
|
new_ucmd!().succeeds().stdout_is(id_un.stdout_str());
|
||||||
return;
|
} else if is_ci() && id_un.stderr_str().contains("cannot find name for user ID") {
|
||||||
|
println!("test skipped:");
|
||||||
|
} else {
|
||||||
|
id_un.success();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(unix)]
|
||||||
|
fn test_normal_compare_env() {
|
||||||
|
let whoami = whoami();
|
||||||
|
if whoami == "nobody" {
|
||||||
|
println!("test skipped:");
|
||||||
|
return;
|
||||||
|
} else if !is_ci() {
|
||||||
|
new_ucmd!().succeeds().stdout_is(format!("{}\n", whoami));
|
||||||
|
} else {
|
||||||
|
println!("test skipped:");
|
||||||
}
|
}
|
||||||
|
|
||||||
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()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
|
// * 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.
|
||||||
|
|
||||||
/// Platform-independent helper for constructing a PathBuf from individual elements
|
/// Platform-independent helper for constructing a PathBuf from individual elements
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! path_concat {
|
macro_rules! path_concat {
|
||||||
|
@ -66,3 +71,19 @@ macro_rules! at_and_ucmd {
|
||||||
(ts.fixtures.clone(), ts.ucmd())
|
(ts.fixtures.clone(), ts.ucmd())
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// If `common::util::expected_result` returns an error, i.e. the `util` in `$PATH` doesn't
|
||||||
|
/// include a coreutils version string or the version is too low,
|
||||||
|
/// this macro can be used to automatically skip the test and print the reason.
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! unwrap_or_return {
|
||||||
|
( $e:expr ) => {
|
||||||
|
match $e {
|
||||||
|
Ok(x) => x,
|
||||||
|
Err(e) => {
|
||||||
|
println!("test skipped: {}", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,17 @@
|
||||||
//spell-checker: ignore (linux) rlimit prlimit Rlim
|
// * 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.
|
||||||
|
|
||||||
|
//spell-checker: ignore (linux) rlimit prlimit Rlim coreutil
|
||||||
|
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
use rlimit::{prlimit, rlim};
|
use rlimit::{prlimit, rlim};
|
||||||
|
#[cfg(unix)]
|
||||||
|
use std::borrow::Cow;
|
||||||
use std::env;
|
use std::env;
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
|
@ -695,7 +702,7 @@ impl AtPath {
|
||||||
/// Fixtures can be found under `tests/fixtures/$util_name/`
|
/// Fixtures can be found under `tests/fixtures/$util_name/`
|
||||||
pub struct TestScenario {
|
pub struct TestScenario {
|
||||||
bin_path: PathBuf,
|
bin_path: PathBuf,
|
||||||
util_name: String,
|
pub util_name: String,
|
||||||
pub fixtures: AtPath,
|
pub fixtures: AtPath,
|
||||||
tmpd: Rc<TempDir>,
|
tmpd: Rc<TempDir>,
|
||||||
}
|
}
|
||||||
|
@ -1036,6 +1043,179 @@ pub fn vec_of_size(n: usize) -> Vec<u8> {
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub 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"
|
||||||
|
|
||||||
|
// 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()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Add prefix 'g' for `util_name` if not on linux
|
||||||
|
#[cfg(unix)]
|
||||||
|
pub fn host_name_for<'a>(util_name: &'a str) -> Cow<'a, str> {
|
||||||
|
// In some environments, e.g. macOS/freebsd, the GNU coreutils are prefixed with "g"
|
||||||
|
// to not interfere with the BSD counterparts already in `$PATH`.
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
|
return format!("g{}", util_name).into();
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
return util_name.into();
|
||||||
|
}
|
||||||
|
|
||||||
|
// GNU coreutils version 8.32 is the reference version since it is the latest version and the
|
||||||
|
// GNU test suite in "coreutils/.github/workflows/GnuTests.yml" runs against it.
|
||||||
|
// However, here 8.30 was chosen because right now there's no ubuntu image for the github actions
|
||||||
|
// CICD available with a higher version than 8.30.
|
||||||
|
// GNU coreutils versions from the CICD images for comparison:
|
||||||
|
// ubuntu-2004: 8.30 (latest)
|
||||||
|
// ubuntu-1804: 8.28
|
||||||
|
// macos-latest: 8.32
|
||||||
|
const VERSION_MIN: &str = "8.30"; // minimum Version for the reference `coreutil` in `$PATH`
|
||||||
|
|
||||||
|
const UUTILS_WARNING: &str = "uutils-tests-warning";
|
||||||
|
const UUTILS_INFO: &str = "uutils-tests-info";
|
||||||
|
|
||||||
|
/// Run `util_name --version` and return Ok if the version is >= `version_expected`.
|
||||||
|
/// Returns an error if
|
||||||
|
/// * `util_name` cannot run
|
||||||
|
/// * the version cannot be parsed
|
||||||
|
/// * the version is too low
|
||||||
|
///
|
||||||
|
/// This is used by `expected_result` to check if the coreutils version is >= `VERSION_MIN`.
|
||||||
|
/// It makes sense to use this manually in a test if a feature
|
||||||
|
/// is tested that was introduced after `VERSION_MIN`
|
||||||
|
///
|
||||||
|
/// Example:
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// use crate::common::util::*;
|
||||||
|
/// const VERSION_MIN_MULTIPLE_USERS: &str = "8.31";
|
||||||
|
///
|
||||||
|
/// #[test]
|
||||||
|
/// fn test_xyz() {
|
||||||
|
/// unwrap_or_return!(check_coreutil_version(
|
||||||
|
/// util_name!(),
|
||||||
|
/// VERSION_MIN_MULTIPLE_USERS
|
||||||
|
/// ));
|
||||||
|
/// // proceed with the test...
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
#[cfg(unix)]
|
||||||
|
pub fn check_coreutil_version(
|
||||||
|
util_name: &str,
|
||||||
|
version_expected: &str,
|
||||||
|
) -> std::result::Result<String, String> {
|
||||||
|
// example:
|
||||||
|
// $ id --version | head -n 1
|
||||||
|
// id (GNU coreutils) 8.32.162-4eda
|
||||||
|
|
||||||
|
let util_name = &host_name_for(util_name);
|
||||||
|
log_info("run", format!("{} --version", util_name));
|
||||||
|
let version_check = match Command::new(util_name.as_ref())
|
||||||
|
.env("LC_ALL", "C")
|
||||||
|
.arg("--version")
|
||||||
|
.output()
|
||||||
|
{
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(e) => {
|
||||||
|
return Err(format!(
|
||||||
|
"{}: '{}' {}",
|
||||||
|
UUTILS_WARNING,
|
||||||
|
util_name,
|
||||||
|
e.to_string()
|
||||||
|
))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
std::str::from_utf8(&version_check.stdout).unwrap()
|
||||||
|
.split('\n')
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.get(0)
|
||||||
|
.map_or_else(
|
||||||
|
|| Err(format!("{}: unexpected output format for reference coreutil: '{} --version'", UUTILS_WARNING, util_name)),
|
||||||
|
|s| {
|
||||||
|
if s.contains(&format!("(GNU coreutils) {}", version_expected)) {
|
||||||
|
Ok(format!("{}: {}", UUTILS_INFO, 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 {
|
||||||
|
Ok(format!("{}: version for the reference coreutil '{}' is higher than expected; expected: {}, found: {}", UUTILS_INFO, util_name, version_expected, version_found))
|
||||||
|
} else {
|
||||||
|
Err(format!("{}: version for the reference coreutil '{}' does not match; expected: {}, found: {}", UUTILS_WARNING, util_name, version_expected, version_found)) }
|
||||||
|
} else {
|
||||||
|
Err(format!("{}: no coreutils version string found for reference coreutils '{} --version'", UUTILS_WARNING, util_name))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This runs the GNU coreutils `util_name` binary in `$PATH` in order to
|
||||||
|
/// dynamically gather reference values on the system.
|
||||||
|
/// If the `util_name` in `$PATH` doesn't include a coreutils version string,
|
||||||
|
/// or the version is too low, this returns an error and the test should be skipped.
|
||||||
|
///
|
||||||
|
/// Example:
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// use crate::common::util::*;
|
||||||
|
/// #[test]
|
||||||
|
/// fn test_xyz() {
|
||||||
|
/// let ts = TestScenario::new(util_name!());
|
||||||
|
/// let result = ts.ucmd().run();
|
||||||
|
/// let exp_result = unwrap_or_return!(expected_result(&ts, &[]));
|
||||||
|
/// result
|
||||||
|
/// .stdout_is(exp_result.stdout_str())
|
||||||
|
/// .stderr_is(exp_result.stderr_str())
|
||||||
|
/// .code_is(exp_result.code());
|
||||||
|
/// }
|
||||||
|
///```
|
||||||
|
#[cfg(unix)]
|
||||||
|
pub fn expected_result(ts: &TestScenario, args: &[&str]) -> std::result::Result<CmdResult, String> {
|
||||||
|
let util_name = &host_name_for(&ts.util_name);
|
||||||
|
println!("{}", check_coreutil_version(util_name, VERSION_MIN)?);
|
||||||
|
|
||||||
|
let result = ts
|
||||||
|
.cmd_keepenv(util_name.as_ref())
|
||||||
|
.env("LC_ALL", "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 {
|
||||||
|
// `host_name_for` added prefix, 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(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
/// Sanity checks for test utils
|
/// Sanity checks for test utils
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
@ -1272,4 +1452,33 @@ mod tests {
|
||||||
|
|
||||||
res.normalized_newlines_stdout_is("A\r\nB\nC\n");
|
res.normalized_newlines_stdout_is("A\r\nB\nC\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(unix)]
|
||||||
|
fn test_check_coreutil_version() {
|
||||||
|
match check_coreutil_version("id", VERSION_MIN) {
|
||||||
|
Ok(s) => assert!(s.starts_with("uutils-tests-")),
|
||||||
|
Err(s) => assert!(s.starts_with("uutils-tests-warning")),
|
||||||
|
};
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
std::assert_eq!(
|
||||||
|
check_coreutil_version("no test name", VERSION_MIN),
|
||||||
|
Err("uutils-tests-warning: 'no test name' \
|
||||||
|
No such file or directory (os error 2)"
|
||||||
|
.to_string())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(unix)]
|
||||||
|
fn test_expected_result() {
|
||||||
|
let ts = TestScenario::new("id");
|
||||||
|
// assert!(expected_result(&ts, &[]).is_ok());
|
||||||
|
match expected_result(&ts, &[]) {
|
||||||
|
Ok(r) => assert!(r.succeeded()),
|
||||||
|
Err(s) => assert!(s.starts_with("uutils-tests-warning")),
|
||||||
|
}
|
||||||
|
let ts = TestScenario::new("no test name");
|
||||||
|
assert!(expected_result(&ts, &[]).is_err());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue