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

id: add more tests for '--zero'

This commit is contained in:
Jan Scheer 2021-06-10 10:33:22 +02:00
parent 00c05b8687
commit 44d1790d1f
2 changed files with 59 additions and 146 deletions

View file

@ -5,8 +5,9 @@ use crate::common::util::*;
// considered okay. If we are not inside the CI this calls assert!(result.success). // 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)" // From the Logs: "Build (ubuntu-18.04, x86_64-unknown-linux-gnu, feat_os_unix, use-cross)"
// stderr: "whoami: cannot find name for user ID 1001" // whoami: cannot find name for user ID 1001
// stderr: "id: Could not find uid 1001: No such 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: // 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)" // id: "uid=1001(runner) gid=118(docker) groups=118(docker),4(adm),101(systemd-journal)"
@ -94,69 +95,6 @@ fn test_id_name_from_id() {
assert_eq!(username_id, username_whoami); assert_eq!(username_id, username_whoami);
} }
#[test]
fn test_id_group() {
let scene = TestScenario::new(util_name!());
let mut result = scene.ucmd().arg("-g").succeeds();
let s1 = result.stdout_str().trim();
assert!(s1.parse::<u64>().is_ok());
result = scene.ucmd().arg("--group").succeeds();
let s1 = result.stdout_str().trim();
assert!(s1.parse::<u64>().is_ok());
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
for flag in &["-g", "--group"] {
new_ucmd!()
.arg(flag)
.succeeds()
.stdout_is(expected_result(&[flag], false));
}
}
#[test]
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
fn test_id_groups() {
let scene = TestScenario::new(util_name!());
for g_flag in &["-G", "--groups"] {
scene
.ucmd()
.arg(g_flag)
.succeeds()
.stdout_is(expected_result(&[g_flag], false));
for &r_flag in &["-r", "--real"] {
let args = [g_flag, r_flag];
scene
.ucmd()
.args(&args)
.succeeds()
.stdout_is(expected_result(&args, false));
}
}
}
#[test]
fn test_id_user() {
let scene = TestScenario::new(util_name!());
let result = scene.ucmd().arg("-u").succeeds();
let s1 = result.stdout_str().trim();
assert!(s1.parse::<u64>().is_ok());
let result = scene.ucmd().arg("--user").succeeds();
let s1 = result.stdout_str().trim();
assert!(s1.parse::<u64>().is_ok());
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
for flag in &["-u", "--user"] {
new_ucmd!()
.arg(flag)
.succeeds()
.stdout_is(expected_result(&[flag], false));
}
}
#[test] #[test]
fn test_id_pretty_print() { fn test_id_pretty_print() {
let username = return_whoami_username(); let username = return_whoami_username();
@ -193,52 +131,13 @@ fn test_id_password_style() {
} }
#[test] #[test]
#[cfg(any(target_vendor = "apple", target_os = "linux"))] #[cfg(unix)]
fn test_id_default_format() { fn test_id_default_format() {
// These are the same tests like in test_id_zero but without --zero flag. // TODO: These are the same tests like in test_id_zero but without --zero flag.
let scene = 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()
.args(&args)
.fails()
.stderr_only(expected_result(&args, true));
for &opt2 in &["--user", "--group", "--groups"] {
// u/g/G n/r
let args = [opt2, opt1];
let result = scene.ucmd().args(&args).run();
if !result.succeeded()
&& is_ci()
&& result.stderr_str().contains("cannot find name for")
{
// '--name' does not work on CICD ubuntu-16/ubuntu-18
// id: cannot find name for user ID 1001
// id: cannot find name for group ID 116
scene
.ucmd()
.args(&args)
.fails()
.stderr_only(expected_result(&args, true));
} else {
result.stdout_only(expected_result(&args, false));
}
}
}
// u/g/G
for &opt2 in &["--user", "--group", "--groups"] {
let args = [opt2];
scene
.ucmd()
.args(&args)
.succeeds()
.stdout_only(expected_result(&args, false));
}
} }
#[test] #[test]
#[cfg(any(target_vendor = "apple", target_os = "linux"))] #[cfg(unix)]
fn test_id_zero() { fn test_id_zero() {
let scene = TestScenario::new(util_name!()); let scene = TestScenario::new(util_name!());
for z_flag in &["-z", "--zero"] { for z_flag in &["-z", "--zero"] {
@ -249,26 +148,15 @@ fn test_id_zero() {
.ucmd() .ucmd()
.args(&args) .args(&args)
.fails() .fails()
.stderr_only(expected_result(&args, true)); .stderr_only(expected_result(&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 = scene.ucmd().args(&args).run();
if !result.succeeded() let expected_result = expected_result(&args);
&& is_ci() result
&& result.stderr_str().contains("cannot find name for") .stdout_is_bytes(expected_result.stdout())
{ .stderr_is_bytes(expected_result.stderr());
// '--name' does not work on CICD ubuntu-16/ubuntu-18
// id: cannot find name for user ID 1001
// id: cannot find name for group ID 116
scene
.ucmd()
.args(&args)
.fails()
.stderr_only(expected_result(&args, true));
} else {
result.stdout_only(expected_result(&args, false));
}
} }
} }
// u/g/G z // u/g/G z
@ -278,37 +166,46 @@ fn test_id_zero() {
.ucmd() .ucmd()
.args(&args) .args(&args)
.succeeds() .succeeds()
.stdout_only(expected_result(&args, false)); .stdout_only_bytes(expected_result(&args).stdout());
} }
} }
} }
#[allow(clippy::needless_borrow)] #[allow(clippy::needless_borrow)]
#[cfg(any(target_vendor = "apple", target_os = "linux"))] #[cfg(unix)]
fn expected_result(args: &[&str], exp_fail: bool) -> String { fn expected_result(args: &[&str]) -> CmdResult {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
let util_name = util_name!(); let util_name = util_name!();
#[cfg(target_vendor = "apple")] #[cfg(all(unix, not(target_os = "linux")))]
let util_name = format!("g{}", util_name!()); let util_name = format!("g{}", util_name!());
let result = if !exp_fail { let result = TestScenario::new(&util_name)
TestScenario::new(&util_name) .cmd_keepenv(&util_name)
.cmd_keepenv(util_name)
.env("LANGUAGE", "C") .env("LANGUAGE", "C")
.args(args) .args(args)
.succeeds() .run();
.stdout_move_str()
let mut _o = 0;
let mut _e = 0;
#[cfg(all(unix, not(target_os = "linux")))]
{
_o = if result.stdout_str().starts_with(&util_name) {
1
} else { } else {
TestScenario::new(&util_name) 0
.cmd_keepenv(util_name)
.env("LANGUAGE", "C")
.args(args)
.fails()
.stderr_move_str()
}; };
if cfg!(target_os = "macos") && result.starts_with("gid") { _e = if result.stderr_str().starts_with(&util_name) {
result[1..].to_string() 1
} else { } else {
result 0
};
} }
CmdResult::new(
Some(result.tmpd()),
Some(result.code()),
result.succeeded(),
&result.stdout()[_o..],
&result.stderr()[_e..],
)
} }

View file

@ -69,6 +69,22 @@ pub struct CmdResult {
} }
impl CmdResult { impl CmdResult {
pub fn new(
tmpd: Option<Rc<TempDir>>,
code: Option<i32>,
success: bool,
stdout: &[u8],
stderr: &[u8],
) -> CmdResult {
CmdResult {
tmpd,
code,
success,
stdout: stdout.to_vec(),
stderr: stderr.to_vec(),
}
}
/// Returns a reference to the program's standard output as a slice of bytes /// Returns a reference to the program's standard output as a slice of bytes
pub fn stdout(&self) -> &[u8] { pub fn stdout(&self) -> &[u8] {
&self.stdout &self.stdout