mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
tests/common: refactor: use TestScenario instead of util_name for expected_result()
This commit is contained in:
parent
31aa8b2062
commit
abc59fbe85
6 changed files with 189 additions and 221 deletions
|
@ -10,8 +10,9 @@ const VERSION_MIN_MULTIPLE_USERS: &str = "8.31"; // this feature was introduced
|
|||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn test_groups() {
|
||||
let result = new_ucmd!().run();
|
||||
let exp_result = unwrap_or_return!(expected_result(util_name!(), &[]));
|
||||
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())
|
||||
|
@ -24,8 +25,9 @@ fn test_groups() {
|
|||
fn test_groups_username() {
|
||||
let test_users = [&whoami()[..]];
|
||||
|
||||
let result = new_ucmd!().args(&test_users).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(util_name!(), &test_users));
|
||||
let ts = TestScenario::new(util_name!());
|
||||
let result = ts.ucmd().args(&test_users).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(&ts, &test_users));
|
||||
|
||||
result
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
|
@ -42,8 +44,9 @@ fn test_groups_username_multiple() {
|
|||
));
|
||||
let test_users = ["root", "man", "postfix", "sshd", &whoami()];
|
||||
|
||||
let result = new_ucmd!().args(&test_users).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(util_name!(), &test_users));
|
||||
let ts = TestScenario::new(util_name!());
|
||||
let result = ts.ucmd().args(&test_users).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(&ts, &test_users));
|
||||
|
||||
result
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
|
|
|
@ -12,13 +12,17 @@ const VERSION_MIN_MULTIPLE_USERS: &str = "8.31"; // this feature was introduced
|
|||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn test_id_no_specified_user() {
|
||||
let result = new_ucmd!().run();
|
||||
let exp_result = unwrap_or_return!(expected_result(util_name!(), &[]));
|
||||
let ts = TestScenario::new(util_name!());
|
||||
let result = ts.ucmd().run();
|
||||
let exp_result = unwrap_or_return!(expected_result(&ts, &[]));
|
||||
let mut _exp_stdout = exp_result.stdout_str().to_string();
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
// 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=") {
|
||||
_exp_stdout.replace_range(context_offset.._exp_stdout.len() - 1, "");
|
||||
}
|
||||
|
@ -35,10 +39,9 @@ fn test_id_no_specified_user() {
|
|||
fn test_id_single_user() {
|
||||
let test_users = [&whoami()[..]];
|
||||
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let mut exp_result = unwrap_or_return!(expected_result(util_name!(), &test_users));
|
||||
scene
|
||||
.ucmd()
|
||||
let ts = TestScenario::new(util_name!());
|
||||
let mut exp_result = unwrap_or_return!(expected_result(&ts, &test_users));
|
||||
ts.ucmd()
|
||||
.args(&test_users)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
|
@ -49,36 +52,32 @@ fn test_id_single_user() {
|
|||
for &opt in &["--user", "--group", "--groups"] {
|
||||
let mut args = vec![opt];
|
||||
args.extend_from_slice(&test_users);
|
||||
exp_result = unwrap_or_return!(expected_result(util_name!(), &args));
|
||||
scene
|
||||
.ucmd()
|
||||
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||
ts.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.push("--zero");
|
||||
exp_result = unwrap_or_return!(expected_result(util_name!(), &args));
|
||||
scene
|
||||
.ucmd()
|
||||
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||
ts.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.push("--name");
|
||||
exp_result = unwrap_or_return!(expected_result(util_name!(), &args));
|
||||
scene
|
||||
.ucmd()
|
||||
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||
ts.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.pop();
|
||||
exp_result = unwrap_or_return!(expected_result(util_name!(), &args));
|
||||
scene
|
||||
.ucmd()
|
||||
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||
ts.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
|
@ -91,8 +90,9 @@ fn test_id_single_user() {
|
|||
#[cfg(unix)]
|
||||
fn test_id_single_user_non_existing() {
|
||||
let args = &["hopefully_non_existing_username"];
|
||||
let result = new_ucmd!().args(args).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(util_name!(), args));
|
||||
let ts = TestScenario::new(util_name!());
|
||||
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".
|
||||
// coreutils 8.32: $ LC_ALL=C id foobar
|
||||
|
@ -107,11 +107,11 @@ fn test_id_single_user_non_existing() {
|
|||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn test_id_name() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let ts = TestScenario::new(util_name!());
|
||||
for &opt in &["--user", "--group", "--groups"] {
|
||||
let args = [opt, "--name"];
|
||||
let result = scene.ucmd().args(&args).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(util_name!(), &args));
|
||||
let result = ts.ucmd().args(&args).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||
result
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str())
|
||||
|
@ -126,11 +126,11 @@ fn test_id_name() {
|
|||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn test_id_real() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let ts = TestScenario::new(util_name!());
|
||||
for &opt in &["--user", "--group", "--groups"] {
|
||||
let args = [opt, "--real"];
|
||||
let result = scene.ucmd().args(&args).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(util_name!(), &args));
|
||||
let result = ts.ucmd().args(&args).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||
result
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str())
|
||||
|
@ -178,10 +178,9 @@ fn test_id_multiple_users() {
|
|||
// Same typical users that GNU test suite is using.
|
||||
let test_users = ["root", "man", "postfix", "sshd", &whoami()];
|
||||
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let mut exp_result = unwrap_or_return!(expected_result(util_name!(), &test_users));
|
||||
scene
|
||||
.ucmd()
|
||||
let ts = TestScenario::new(util_name!());
|
||||
let mut exp_result = unwrap_or_return!(expected_result(&ts, &test_users));
|
||||
ts.ucmd()
|
||||
.args(&test_users)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
|
@ -192,36 +191,32 @@ fn test_id_multiple_users() {
|
|||
for &opt in &["--user", "--group", "--groups"] {
|
||||
let mut args = vec![opt];
|
||||
args.extend_from_slice(&test_users);
|
||||
exp_result = unwrap_or_return!(expected_result(util_name!(), &args));
|
||||
scene
|
||||
.ucmd()
|
||||
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||
ts.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.push("--zero");
|
||||
exp_result = unwrap_or_return!(expected_result(util_name!(), &args));
|
||||
scene
|
||||
.ucmd()
|
||||
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||
ts.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.push("--name");
|
||||
exp_result = unwrap_or_return!(expected_result(util_name!(), &args));
|
||||
scene
|
||||
.ucmd()
|
||||
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||
ts.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.pop();
|
||||
exp_result = unwrap_or_return!(expected_result(util_name!(), &args));
|
||||
scene
|
||||
.ucmd()
|
||||
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||
ts.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
|
@ -251,10 +246,9 @@ fn test_id_multiple_users_non_existing() {
|
|||
&whoami(),
|
||||
];
|
||||
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let mut exp_result = unwrap_or_return!(expected_result(util_name!(), &test_users));
|
||||
scene
|
||||
.ucmd()
|
||||
let ts = TestScenario::new(util_name!());
|
||||
let mut exp_result = unwrap_or_return!(expected_result(&ts, &test_users));
|
||||
ts.ucmd()
|
||||
.args(&test_users)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
|
@ -265,36 +259,32 @@ fn test_id_multiple_users_non_existing() {
|
|||
for &opt in &["--user", "--group", "--groups"] {
|
||||
let mut args = vec![opt];
|
||||
args.extend_from_slice(&test_users);
|
||||
exp_result = unwrap_or_return!(expected_result(util_name!(), &args));
|
||||
scene
|
||||
.ucmd()
|
||||
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||
ts.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.push("--zero");
|
||||
exp_result = unwrap_or_return!(expected_result(util_name!(), &args));
|
||||
scene
|
||||
.ucmd()
|
||||
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||
ts.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.push("--name");
|
||||
exp_result = unwrap_or_return!(expected_result(util_name!(), &args));
|
||||
scene
|
||||
.ucmd()
|
||||
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||
ts.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str().replace(": Invalid argument", ""))
|
||||
.code_is(exp_result.code());
|
||||
args.pop();
|
||||
exp_result = unwrap_or_return!(expected_result(util_name!(), &args));
|
||||
scene
|
||||
.ucmd()
|
||||
exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||
ts.ucmd()
|
||||
.args(&args)
|
||||
.run()
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
|
@ -306,20 +296,19 @@ fn test_id_multiple_users_non_existing() {
|
|||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn test_id_default_format() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let ts = TestScenario::new(util_name!());
|
||||
for &opt1 in &["--name", "--real"] {
|
||||
// id: cannot print only names or real IDs in default format
|
||||
let args = [opt1];
|
||||
scene
|
||||
.ucmd()
|
||||
ts.ucmd()
|
||||
.args(&args)
|
||||
.fails()
|
||||
.stderr_only(unwrap_or_return!(expected_result(util_name!(), &args)).stderr_str());
|
||||
.stderr_only(unwrap_or_return!(expected_result(&ts, &args)).stderr_str());
|
||||
for &opt2 in &["--user", "--group", "--groups"] {
|
||||
// u/g/G n/r
|
||||
let args = [opt2, opt1];
|
||||
let result = scene.ucmd().args(&args).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(util_name!(), &args));
|
||||
let result = ts.ucmd().args(&args).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||
result
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str())
|
||||
|
@ -329,38 +318,35 @@ fn test_id_default_format() {
|
|||
for &opt2 in &["--user", "--group", "--groups"] {
|
||||
// u/g/G
|
||||
let args = [opt2];
|
||||
scene
|
||||
.ucmd()
|
||||
ts.ucmd()
|
||||
.args(&args)
|
||||
.succeeds()
|
||||
.stdout_only(unwrap_or_return!(expected_result(util_name!(), &args)).stdout_str());
|
||||
.stdout_only(unwrap_or_return!(expected_result(&ts, &args)).stdout_str());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn test_id_zero() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let ts = TestScenario::new(util_name!());
|
||||
for z_flag in &["-z", "--zero"] {
|
||||
// id: option --zero not permitted in default format
|
||||
scene
|
||||
.ucmd()
|
||||
ts.ucmd()
|
||||
.args(&[z_flag])
|
||||
.fails()
|
||||
.stderr_only(unwrap_or_return!(expected_result(util_name!(), &[z_flag])).stderr_str());
|
||||
.stderr_only(unwrap_or_return!(expected_result(&ts, &[z_flag])).stderr_str());
|
||||
for &opt1 in &["--name", "--real"] {
|
||||
// id: cannot print only names or real IDs in default format
|
||||
let args = [opt1, z_flag];
|
||||
scene
|
||||
.ucmd()
|
||||
ts.ucmd()
|
||||
.args(&args)
|
||||
.fails()
|
||||
.stderr_only(unwrap_or_return!(expected_result(util_name!(), &args)).stderr_str());
|
||||
.stderr_only(unwrap_or_return!(expected_result(&ts, &args)).stderr_str());
|
||||
for &opt2 in &["--user", "--group", "--groups"] {
|
||||
// u/g/G n/r z
|
||||
let args = [opt2, z_flag, opt1];
|
||||
let result = scene.ucmd().args(&args).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(util_name!(), &args));
|
||||
let result = ts.ucmd().args(&args).run();
|
||||
let exp_result = unwrap_or_return!(expected_result(&ts, &args));
|
||||
result
|
||||
.stdout_is(exp_result.stdout_str())
|
||||
.stderr_is(exp_result.stderr_str())
|
||||
|
@ -370,11 +356,10 @@ fn test_id_zero() {
|
|||
for &opt2 in &["--user", "--group", "--groups"] {
|
||||
// u/g/G z
|
||||
let args = [opt2, z_flag];
|
||||
scene
|
||||
.ucmd()
|
||||
ts.ucmd()
|
||||
.args(&args)
|
||||
.succeeds()
|
||||
.stdout_only(unwrap_or_return!(expected_result(util_name!(), &args)).stdout_str());
|
||||
.stdout_only(unwrap_or_return!(expected_result(&ts, &args)).stdout_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,19 +25,16 @@ fn test_long_format() {
|
|||
let login = "root";
|
||||
let pw: Passwd = Passwd::locate(login).unwrap();
|
||||
let real_name = pw.user_info().replace("&", &pw.name().capitalize());
|
||||
new_ucmd!()
|
||||
.arg("-l")
|
||||
.arg(login)
|
||||
.succeeds()
|
||||
.stdout_is(format!(
|
||||
"Login name: {:<28}In real life: {}\nDirectory: {:<29}Shell: {}\n\n",
|
||||
login,
|
||||
real_name,
|
||||
pw.user_dir(),
|
||||
pw.user_shell()
|
||||
));
|
||||
let ts = TestScenario::new(util_name!());
|
||||
ts.ucmd().arg("-l").arg(login).succeeds().stdout_is(format!(
|
||||
"Login name: {:<28}In real life: {}\nDirectory: {:<29}Shell: {}\n\n",
|
||||
login,
|
||||
real_name,
|
||||
pw.user_dir(),
|
||||
pw.user_shell()
|
||||
));
|
||||
|
||||
new_ucmd!()
|
||||
ts.ucmd()
|
||||
.arg("-lb")
|
||||
.arg(login)
|
||||
.succeeds()
|
||||
|
@ -51,9 +48,10 @@ fn test_long_format() {
|
|||
#[test]
|
||||
fn test_long_format_multiple_users() {
|
||||
let args = ["-l", "root", "root", "root"];
|
||||
let expect = unwrap_or_return!(expected_result(util_name!(), &args));
|
||||
let ts = TestScenario::new(util_name!());
|
||||
let expect = unwrap_or_return!(expected_result(&ts, &args));
|
||||
|
||||
new_ucmd!()
|
||||
ts.ucmd()
|
||||
.args(&args)
|
||||
.succeeds()
|
||||
.stdout_is(expect.stdout_str())
|
||||
|
@ -72,8 +70,9 @@ fn test_short_format_i() {
|
|||
// allow whitespace variation
|
||||
// * minor whitespace differences occur between platform built-in outputs; specifically, the number of trailing TABs may be variant
|
||||
let args = ["-i"];
|
||||
let actual = new_ucmd!().args(&args).succeeds().stdout_move_str();
|
||||
let expect = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str();
|
||||
let ts = TestScenario::new(util_name!());
|
||||
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_expect: Vec<&str> = expect.split_whitespace().collect();
|
||||
assert_eq!(v_actual, v_expect);
|
||||
|
@ -85,8 +84,9 @@ fn test_short_format_q() {
|
|||
// allow whitespace variation
|
||||
// * minor whitespace differences occur between platform built-in outputs; specifically, the number of trailing TABs may be variant
|
||||
let args = ["-q"];
|
||||
let actual = new_ucmd!().args(&args).succeeds().stdout_move_str();
|
||||
let expect = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str();
|
||||
let ts = TestScenario::new(util_name!());
|
||||
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_expect: Vec<&str> = expect.split_whitespace().collect();
|
||||
assert_eq!(v_actual, v_expect);
|
||||
|
@ -95,8 +95,9 @@ fn test_short_format_q() {
|
|||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn test_no_flag() {
|
||||
let actual = new_ucmd!().succeeds().stdout_move_str();
|
||||
let expect = unwrap_or_return!(expected_result(util_name!(), &[])).stdout_move_str();
|
||||
let ts = TestScenario::new(util_name!());
|
||||
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_expect: Vec<&str> = expect.split_whitespace().collect();
|
||||
assert_eq!(v_actual, v_expect);
|
||||
|
|
|
@ -114,16 +114,18 @@ const FS_FORMAT_STR: &str = "%b %c %i %l %n %s %S %t %T"; // avoid "%a %d %f" wh
|
|||
#[cfg(target_os = "linux")]
|
||||
fn test_terse_fs_format() {
|
||||
let args = ["-f", "-t", "/proc"];
|
||||
let expected_stdout = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str();
|
||||
new_ucmd!().args(&args).run().stdout_is(expected_stdout);
|
||||
let ts = TestScenario::new(util_name!());
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||
ts.ucmd().args(&args).run().stdout_is(expected_stdout);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(target_os = "linux")]
|
||||
fn test_fs_format() {
|
||||
let args = ["-f", "-c", FS_FORMAT_STR, "/dev/shm"];
|
||||
let expected_stdout = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str();
|
||||
new_ucmd!().args(&args).run().stdout_is(expected_stdout);
|
||||
let ts = TestScenario::new(util_name!());
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||
ts.ucmd().args(&args).run().stdout_is(expected_stdout);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
|
@ -132,8 +134,9 @@ fn test_terse_normal_format() {
|
|||
// 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
|
||||
let args = ["-t", "/"];
|
||||
let actual = new_ucmd!().args(&args).succeeds().stdout_move_str();
|
||||
let expect = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str();
|
||||
let ts = TestScenario::new(util_name!());
|
||||
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!("expect: {:?}", expect);
|
||||
let v_actual: Vec<&str> = actual.trim().split(' ').collect();
|
||||
|
@ -161,8 +164,9 @@ fn test_terse_normal_format() {
|
|||
#[test]
|
||||
fn test_format_created_time() {
|
||||
let args = ["-c", "%w", "/bin"];
|
||||
let actual = new_ucmd!().args(&args).succeeds().stdout_move_str();
|
||||
let expect = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str();
|
||||
let ts = TestScenario::new(util_name!());
|
||||
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!("expect: {:?}", expect);
|
||||
// note: using a regex instead of `split_whitespace()` in order to detect whitespace differences
|
||||
|
@ -185,8 +189,9 @@ fn test_format_created_time() {
|
|||
#[test]
|
||||
fn test_format_created_seconds() {
|
||||
let args = ["-c", "%W", "/bin"];
|
||||
let actual = new_ucmd!().args(&args).succeeds().stdout_move_str();
|
||||
let expect = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str();
|
||||
let ts = TestScenario::new(util_name!());
|
||||
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!("expect: {:?}", expect);
|
||||
// note: using a regex instead of `split_whitespace()` in order to detect whitespace differences
|
||||
|
@ -209,18 +214,16 @@ fn test_format_created_seconds() {
|
|||
#[test]
|
||||
fn test_normal_format() {
|
||||
let args = ["-c", NORMAL_FORMAT_STR, "/bin"];
|
||||
let expected_stdout = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str();
|
||||
new_ucmd!()
|
||||
.args(&args)
|
||||
.succeeds()
|
||||
.stdout_is(expected_stdout);
|
||||
let ts = TestScenario::new(util_name!());
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn test_symlinks() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let at = &scene.fixtures;
|
||||
let ts = TestScenario::new(util_name!());
|
||||
let at = &ts.fixtures;
|
||||
|
||||
let mut tested: bool = false;
|
||||
// arbitrarily chosen symlinks with hope that the CI environment provides at least one of them
|
||||
|
@ -234,22 +237,12 @@ fn test_symlinks() {
|
|||
if at.file_exists(file) && at.is_symlink(file) {
|
||||
tested = true;
|
||||
let args = ["-c", NORMAL_FORMAT_STR, file];
|
||||
let expected_stdout =
|
||||
unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str();
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.succeeds()
|
||||
.stdout_is(expected_stdout);
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
|
||||
// -L, --dereference follow links
|
||||
let args = ["-L", "-c", NORMAL_FORMAT_STR, file];
|
||||
let expected_stdout =
|
||||
unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str();
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.succeeds()
|
||||
.stdout_is(expected_stdout);
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
}
|
||||
if !tested {
|
||||
|
@ -275,11 +268,9 @@ fn test_char() {
|
|||
#[cfg(any(target_vendor = "apple"))]
|
||||
"/dev/ptmx",
|
||||
];
|
||||
let expected_stdout = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str();
|
||||
new_ucmd!()
|
||||
.args(&args)
|
||||
.succeeds()
|
||||
.stdout_is(expected_stdout);
|
||||
let ts = TestScenario::new(util_name!());
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
|
@ -294,11 +285,9 @@ fn test_multi_files() {
|
|||
"/etc/fstab",
|
||||
"/var",
|
||||
];
|
||||
let expected_stdout = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str();
|
||||
new_ucmd!()
|
||||
.args(&args)
|
||||
.succeeds()
|
||||
.stdout_is(expected_stdout);
|
||||
let ts = TestScenario::new(util_name!());
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
|
@ -308,9 +297,7 @@ fn test_printf() {
|
|||
"--printf=123%-# 15q\\r\\\"\\\\\\a\\b\\e\\f\\v%+020.23m\\x12\\167\\132\\112\\n",
|
||||
"/",
|
||||
];
|
||||
let expected_stdout = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str();
|
||||
new_ucmd!()
|
||||
.args(&args)
|
||||
.succeeds()
|
||||
.stdout_is(expected_stdout);
|
||||
let ts = TestScenario::new(util_name!());
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
|
|
|
@ -10,32 +10,33 @@ use crate::common::util::*;
|
|||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn test_count() {
|
||||
let ts = TestScenario::new(util_name!());
|
||||
for opt in &["-q", "--count"] {
|
||||
let expected_stdout =
|
||||
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str();
|
||||
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn test_boot() {
|
||||
let ts = TestScenario::new(util_name!());
|
||||
for opt in &["-b", "--boot"] {
|
||||
let expected_stdout =
|
||||
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str();
|
||||
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn test_heading() {
|
||||
let ts = TestScenario::new(util_name!());
|
||||
for opt in &["-H", "--heading"] {
|
||||
// allow whitespace variation
|
||||
// * minor whitespace differences occur between platform built-in outputs;
|
||||
// specifically number of TABs between "TIME" and "COMMENT" may be variant
|
||||
let actual = new_ucmd!().arg(opt).succeeds().stdout_move_str();
|
||||
let expect = unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str();
|
||||
let actual = ts.ucmd().arg(opt).succeeds().stdout_move_str();
|
||||
let expect = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||
println!("actual: {:?}", actual);
|
||||
println!("expect: {:?}", expect);
|
||||
let v_actual: Vec<&str> = actual.split_whitespace().collect();
|
||||
|
@ -47,63 +48,63 @@ fn test_heading() {
|
|||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn test_short() {
|
||||
let ts = TestScenario::new(util_name!());
|
||||
for opt in &["-s", "--short"] {
|
||||
let expected_stdout =
|
||||
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str();
|
||||
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn test_login() {
|
||||
let ts = TestScenario::new(util_name!());
|
||||
for opt in &["-l", "--login"] {
|
||||
let expected_stdout =
|
||||
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str();
|
||||
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn test_m() {
|
||||
let ts = TestScenario::new(util_name!());
|
||||
for opt in &["-m"] {
|
||||
let expected_stdout =
|
||||
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str();
|
||||
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn test_process() {
|
||||
let ts = TestScenario::new(util_name!());
|
||||
for opt in &["-p", "--process"] {
|
||||
let expected_stdout =
|
||||
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str();
|
||||
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn test_runlevel() {
|
||||
let ts = TestScenario::new(util_name!());
|
||||
for opt in &["-r", "--runlevel"] {
|
||||
let expected_stdout =
|
||||
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str();
|
||||
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
new_ucmd!().arg(opt).succeeds().stdout_is("");
|
||||
ts.ucmd().arg(opt).succeeds().stdout_is("");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn test_time() {
|
||||
let ts = TestScenario::new(util_name!());
|
||||
for opt in &["-t", "--time"] {
|
||||
let expected_stdout =
|
||||
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str();
|
||||
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -116,10 +117,10 @@ fn test_mesg() {
|
|||
// same as -T
|
||||
// --writable
|
||||
// same as -T
|
||||
let ts = TestScenario::new(util_name!());
|
||||
for opt in &["-T", "-w", "--mesg", "--message", "--writable"] {
|
||||
let expected_stdout =
|
||||
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str();
|
||||
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -127,12 +128,9 @@ fn test_mesg() {
|
|||
#[test]
|
||||
fn test_arg1_arg2() {
|
||||
let args = ["am", "i"];
|
||||
let expected_stdout = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str();
|
||||
|
||||
new_ucmd!()
|
||||
.args(&args)
|
||||
.succeeds()
|
||||
.stdout_is(expected_stdout);
|
||||
let ts = TestScenario::new(util_name!());
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -147,9 +145,10 @@ fn test_too_many_args() {
|
|||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn test_users() {
|
||||
let ts = TestScenario::new(util_name!());
|
||||
for opt in &["-u", "--users"] {
|
||||
let actual = new_ucmd!().arg(opt).succeeds().stdout_move_str();
|
||||
let expect = unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str();
|
||||
let actual = ts.ucmd().arg(opt).succeeds().stdout_move_str();
|
||||
let expect = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||
println!("actual: {:?}", actual);
|
||||
println!("expect: {:?}", expect);
|
||||
|
||||
|
@ -172,18 +171,18 @@ fn test_users() {
|
|||
#[test]
|
||||
fn test_lookup() {
|
||||
let opt = "--lookup";
|
||||
let expected_stdout =
|
||||
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str();
|
||||
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
let ts = TestScenario::new(util_name!());
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn test_dead() {
|
||||
let ts = TestScenario::new(util_name!());
|
||||
for opt in &["-d", "--dead"] {
|
||||
let expected_stdout =
|
||||
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str();
|
||||
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -197,20 +196,11 @@ fn test_all_separately() {
|
|||
|
||||
// -a, --all same as -b -d --login -p -r -t -T -u
|
||||
let args = ["-b", "-d", "--login", "-p", "-r", "-t", "-T", "-u"];
|
||||
let expected_stdout = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str();
|
||||
let scene = TestScenario::new(util_name!());
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&args)
|
||||
.succeeds()
|
||||
.stdout_is(expected_stdout);
|
||||
let expected_stdout =
|
||||
unwrap_or_return!(expected_result(util_name!(), &["--all"])).stdout_move_str();
|
||||
scene
|
||||
.ucmd()
|
||||
.arg("--all")
|
||||
.succeeds()
|
||||
.stdout_is(expected_stdout);
|
||||
let ts = TestScenario::new(util_name!());
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
|
||||
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &["--all"])).stdout_move_str();
|
||||
ts.ucmd().arg("--all").succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
|
@ -221,9 +211,9 @@ fn test_all() {
|
|||
return;
|
||||
}
|
||||
|
||||
let ts = TestScenario::new(util_name!());
|
||||
for opt in &["-a", "--all"] {
|
||||
let expected_stdout =
|
||||
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str();
|
||||
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
|
||||
ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -701,7 +701,7 @@ impl AtPath {
|
|||
/// Fixtures can be found under `tests/fixtures/$util_name/`
|
||||
pub struct TestScenario {
|
||||
bin_path: PathBuf,
|
||||
util_name: String,
|
||||
pub util_name: String,
|
||||
pub fixtures: AtPath,
|
||||
tmpd: Rc<TempDir>,
|
||||
}
|
||||
|
@ -1171,8 +1171,9 @@ pub fn check_coreutil_version(
|
|||
/// use crate::common::util::*;
|
||||
/// #[test]
|
||||
/// fn test_xyz() {
|
||||
/// let result = new_ucmd!().run();
|
||||
/// let exp_result = unwrap_or_return!(expected_result(util_name!(), &[]));
|
||||
/// 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())
|
||||
|
@ -1180,12 +1181,11 @@ pub fn check_coreutil_version(
|
|||
/// }
|
||||
///```
|
||||
#[cfg(unix)]
|
||||
pub fn expected_result(util_name: &str, args: &[&str]) -> std::result::Result<CmdResult, String> {
|
||||
let util_name = &host_name_for(util_name);
|
||||
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 scene = TestScenario::new(util_name);
|
||||
let result = scene
|
||||
let result = ts
|
||||
.cmd_keepenv(util_name.as_ref())
|
||||
.env("LC_ALL", "C")
|
||||
.args(args)
|
||||
|
@ -1470,7 +1470,9 @@ mod tests {
|
|||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn test_expected_result() {
|
||||
assert!(expected_result("id", &[]).is_ok());
|
||||
assert!(expected_result("no test name", &[]).is_err());
|
||||
let ts = TestScenario::new("id");
|
||||
assert!(expected_result(&ts, &[]).is_ok());
|
||||
let ts = TestScenario::new("no test name");
|
||||
assert!(expected_result(&ts, &[]).is_err());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue