1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

tests/common: refactor: use TestScenario instead of util_name for expected_result()

This commit is contained in:
Jan Scheer 2021-07-07 22:46:16 +02:00
parent 31aa8b2062
commit abc59fbe85
No known key found for this signature in database
GPG key ID: C62AD4C29E2B9828
6 changed files with 189 additions and 221 deletions

View file

@ -10,8 +10,9 @@ const VERSION_MIN_MULTIPLE_USERS: &str = "8.31"; // this feature was introduced
#[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(util_name!(), &[])); 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())
@ -24,8 +25,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(util_name!(), &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())
@ -42,8 +44,9 @@ fn test_groups_username_multiple() {
)); ));
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(util_name!(), &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())

View file

@ -12,13 +12,17 @@ const VERSION_MIN_MULTIPLE_USERS: &str = "8.31"; // this feature was introduced
#[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(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(); 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, "");
} }
@ -35,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(util_name!(), &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())
@ -49,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(util_name!(), &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(util_name!(), &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(util_name!(), &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(util_name!(), &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())
@ -91,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(util_name!(), 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
@ -107,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(util_name!(), &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())
@ -126,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(util_name!(), &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())
@ -178,10 +178,9 @@ fn test_id_multiple_users() {
// 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(util_name!(), &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())
@ -192,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(util_name!(), &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(util_name!(), &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(util_name!(), &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(util_name!(), &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())
@ -251,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(util_name!(), &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())
@ -265,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(util_name!(), &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(util_name!(), &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(util_name!(), &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(util_name!(), &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())
@ -306,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(util_name!(), &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(util_name!(), &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())
@ -329,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(util_name!(), &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(util_name!(), &[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(util_name!(), &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(util_name!(), &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())
@ -370,11 +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(util_name!(), &args)).stdout_str()); .stdout_only(unwrap_or_return!(expected_result(&ts, &args)).stdout_str());
} }
} }
} }

View file

@ -25,19 +25,16 @@ 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) "Login name: {:<28}In real life: {}\nDirectory: {:<29}Shell: {}\n\n",
.succeeds() login,
.stdout_is(format!( real_name,
"Login name: {:<28}In real life: {}\nDirectory: {:<29}Shell: {}\n\n", pw.user_dir(),
login, pw.user_shell()
real_name, ));
pw.user_dir(),
pw.user_shell()
));
new_ucmd!() ts.ucmd()
.arg("-lb") .arg("-lb")
.arg(login) .arg(login)
.succeeds() .succeeds()
@ -51,9 +48,10 @@ fn test_long_format() {
#[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 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) .args(&args)
.succeeds() .succeeds()
.stdout_is(expect.stdout_str()) .stdout_is(expect.stdout_str())
@ -72,8 +70,9 @@ 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 = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str(); 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);
@ -85,8 +84,9 @@ 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 = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str(); 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);
@ -95,8 +95,9 @@ fn test_short_format_q() {
#[cfg(unix)] #[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 = unwrap_or_return!(expected_result(util_name!(), &[])).stdout_move_str(); 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);

View file

@ -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")] #[cfg(target_os = "linux")]
fn test_terse_fs_format() { fn test_terse_fs_format() {
let args = ["-f", "-t", "/proc"]; let args = ["-f", "-t", "/proc"];
let expected_stdout = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str(); let ts = TestScenario::new(util_name!());
new_ucmd!().args(&args).run().stdout_is(expected_stdout); let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
ts.ucmd().args(&args).run().stdout_is(expected_stdout);
} }
#[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"];
let expected_stdout = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str(); let ts = TestScenario::new(util_name!());
new_ucmd!().args(&args).run().stdout_is(expected_stdout); let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
ts.ucmd().args(&args).run().stdout_is(expected_stdout);
} }
#[cfg(unix)] #[cfg(unix)]
@ -132,8 +134,9 @@ 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 = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str(); 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();
@ -161,8 +164,9 @@ fn test_terse_normal_format() {
#[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 = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str(); 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
@ -185,8 +189,9 @@ fn test_format_created_time() {
#[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 = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str(); 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
@ -209,18 +214,16 @@ fn test_format_created_seconds() {
#[test] #[test]
fn test_normal_format() { fn test_normal_format() {
let args = ["-c", NORMAL_FORMAT_STR, "/bin"]; let args = ["-c", NORMAL_FORMAT_STR, "/bin"];
let expected_stdout = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str(); 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_stdout);
} }
#[cfg(unix)] #[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
@ -234,22 +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];
let expected_stdout = let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str(); ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
scene
.ucmd()
.args(&args)
.succeeds()
.stdout_is(expected_stdout);
// -L, --dereference follow links // -L, --dereference follow links
let args = ["-L", "-c", NORMAL_FORMAT_STR, file]; let args = ["-L", "-c", NORMAL_FORMAT_STR, file];
let expected_stdout = let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str(); ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
scene
.ucmd()
.args(&args)
.succeeds()
.stdout_is(expected_stdout);
} }
} }
if !tested { if !tested {
@ -275,11 +268,9 @@ fn test_char() {
#[cfg(any(target_vendor = "apple"))] #[cfg(any(target_vendor = "apple"))]
"/dev/ptmx", "/dev/ptmx",
]; ];
let expected_stdout = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str(); 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_stdout);
} }
#[cfg(unix)] #[cfg(unix)]
@ -294,11 +285,9 @@ fn test_multi_files() {
"/etc/fstab", "/etc/fstab",
"/var", "/var",
]; ];
let expected_stdout = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str(); 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_stdout);
} }
#[cfg(unix)] #[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", "--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(); 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_stdout);
} }

View file

@ -10,32 +10,33 @@ use crate::common::util::*;
#[cfg(unix)] #[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"] {
let expected_stdout = let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str(); ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
} }
} }
#[cfg(unix)] #[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"] {
let expected_stdout = let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str(); ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
} }
} }
#[cfg(unix)] #[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 = unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str(); 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();
@ -47,63 +48,63 @@ fn test_heading() {
#[cfg(unix)] #[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"] {
let expected_stdout = let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str(); ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
} }
} }
#[cfg(unix)] #[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"] {
let expected_stdout = let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str(); ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
} }
} }
#[cfg(unix)] #[cfg(unix)]
#[test] #[test]
fn test_m() { fn test_m() {
let ts = TestScenario::new(util_name!());
for opt in &["-m"] { for opt in &["-m"] {
let expected_stdout = let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str(); ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
} }
} }
#[cfg(unix)] #[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"] {
let expected_stdout = let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str(); ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
} }
} }
#[cfg(unix)] #[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"] {
let expected_stdout = let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str(); ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
#[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(unix)] #[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"] {
let expected_stdout = let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str(); ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
} }
} }
@ -116,10 +117,10 @@ 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"] {
let expected_stdout = let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str(); ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
} }
} }
@ -127,12 +128,9 @@ fn test_mesg() {
#[test] #[test]
fn test_arg1_arg2() { fn test_arg1_arg2() {
let args = ["am", "i"]; let args = ["am", "i"];
let expected_stdout = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str(); let ts = TestScenario::new(util_name!());
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
new_ucmd!() ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
.args(&args)
.succeeds()
.stdout_is(expected_stdout);
} }
#[test] #[test]
@ -147,9 +145,10 @@ fn test_too_many_args() {
#[cfg(unix)] #[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 = unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str(); let expect = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
println!("actual: {:?}", actual); println!("actual: {:?}", actual);
println!("expect: {:?}", expect); println!("expect: {:?}", expect);
@ -172,18 +171,18 @@ fn test_users() {
#[test] #[test]
fn test_lookup() { fn test_lookup() {
let opt = "--lookup"; let opt = "--lookup";
let expected_stdout = let ts = TestScenario::new(util_name!());
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str(); let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout); ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
} }
#[cfg(unix)] #[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"] {
let expected_stdout = let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str(); ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
new_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 // -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 expected_stdout = unwrap_or_return!(expected_result(util_name!(), &args)).stdout_move_str(); let ts = TestScenario::new(util_name!());
let scene = TestScenario::new(util_name!()); let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
scene ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
.ucmd() let expected_stdout = unwrap_or_return!(expected_result(&ts, &["--all"])).stdout_move_str();
.args(&args) ts.ucmd().arg("--all").succeeds().stdout_is(expected_stdout);
.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);
} }
#[cfg(unix)] #[cfg(unix)]
@ -221,9 +211,9 @@ fn test_all() {
return; return;
} }
let ts = TestScenario::new(util_name!());
for opt in &["-a", "--all"] { for opt in &["-a", "--all"] {
let expected_stdout = let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
unwrap_or_return!(expected_result(util_name!(), &[opt])).stdout_move_str(); ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
new_ucmd!().arg(opt).succeeds().stdout_is(expected_stdout);
} }
} }

View file

@ -701,7 +701,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>,
} }
@ -1171,8 +1171,9 @@ pub fn check_coreutil_version(
/// use crate::common::util::*; /// use crate::common::util::*;
/// #[test] /// #[test]
/// fn test_xyz() { /// fn test_xyz() {
/// let result = new_ucmd!().run(); /// let ts = TestScenario::new(util_name!());
/// let exp_result = unwrap_or_return!(expected_result(util_name!(), &[])); /// 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())
/// .stderr_is(exp_result.stderr_str()) /// .stderr_is(exp_result.stderr_str())
@ -1180,12 +1181,11 @@ pub fn check_coreutil_version(
/// } /// }
///``` ///```
#[cfg(unix)] #[cfg(unix)]
pub fn expected_result(util_name: &str, args: &[&str]) -> std::result::Result<CmdResult, String> { pub fn expected_result(ts: &TestScenario, args: &[&str]) -> std::result::Result<CmdResult, String> {
let util_name = &host_name_for(util_name); let util_name = &host_name_for(&ts.util_name);
println!("{}", check_coreutil_version(util_name, VERSION_MIN)?); println!("{}", check_coreutil_version(util_name, VERSION_MIN)?);
let scene = TestScenario::new(util_name); let result = ts
let result = scene
.cmd_keepenv(util_name.as_ref()) .cmd_keepenv(util_name.as_ref())
.env("LC_ALL", "C") .env("LC_ALL", "C")
.args(args) .args(args)
@ -1470,7 +1470,9 @@ mod tests {
#[test] #[test]
#[cfg(unix)] #[cfg(unix)]
fn test_expected_result() { fn test_expected_result() {
assert!(expected_result("id", &[]).is_ok()); let ts = TestScenario::new("id");
assert!(expected_result("no test name", &[]).is_err()); assert!(expected_result(&ts, &[]).is_ok());
let ts = TestScenario::new("no test name");
assert!(expected_result(&ts, &[]).is_err());
} }
} }