mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
Merge pull request #2446 from jhscheer/groups_gnu_testsuite
groups: fixes to pass GNU's Testsuite
This commit is contained in:
commit
3b2d0d17fa
2 changed files with 219 additions and 67 deletions
|
@ -5,6 +5,13 @@
|
||||||
//
|
//
|
||||||
// For the full copyright and license information, please view the LICENSE
|
// For the full copyright and license information, please view the LICENSE
|
||||||
// file that was distributed with this source code.
|
// file that was distributed with this source code.
|
||||||
|
//
|
||||||
|
// ============================================================================
|
||||||
|
// Test suite summary for GNU coreutils 8.32.162-4eda
|
||||||
|
// ============================================================================
|
||||||
|
// PASS: tests/misc/groups-dash.sh
|
||||||
|
// PASS: tests/misc/groups-process-all.sh
|
||||||
|
// PASS: tests/misc/groups-version.sh
|
||||||
|
|
||||||
// spell-checker:ignore (ToDO) passwd
|
// spell-checker:ignore (ToDO) passwd
|
||||||
|
|
||||||
|
@ -14,11 +21,15 @@ use uucore::entries::{get_groups_gnu, gid2grp, Locate, Passwd};
|
||||||
|
|
||||||
use clap::{crate_version, App, Arg};
|
use clap::{crate_version, App, Arg};
|
||||||
|
|
||||||
static ABOUT: &str = "display current group names";
|
mod options {
|
||||||
static OPT_USER: &str = "user";
|
pub const USERS: &str = "USERNAME";
|
||||||
|
}
|
||||||
|
static ABOUT: &str = "Print group memberships for each USERNAME or, \
|
||||||
|
if no USERNAME is specified, for\nthe current process \
|
||||||
|
(which may differ if the groups data‐base has changed).";
|
||||||
|
|
||||||
fn get_usage() -> String {
|
fn get_usage() -> String {
|
||||||
format!("{0} [USERNAME]", executable!())
|
format!("{0} [OPTION]... [USERNAME]...", executable!())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
|
@ -28,36 +39,57 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
.version(crate_version!())
|
.version(crate_version!())
|
||||||
.about(ABOUT)
|
.about(ABOUT)
|
||||||
.usage(&usage[..])
|
.usage(&usage[..])
|
||||||
.arg(Arg::with_name(OPT_USER))
|
.arg(
|
||||||
|
Arg::with_name(options::USERS)
|
||||||
|
.multiple(true)
|
||||||
|
.takes_value(true)
|
||||||
|
.value_name(options::USERS),
|
||||||
|
)
|
||||||
.get_matches_from(args);
|
.get_matches_from(args);
|
||||||
|
|
||||||
match matches.value_of(OPT_USER) {
|
let users: Vec<String> = matches
|
||||||
None => {
|
.values_of(options::USERS)
|
||||||
|
.map(|v| v.map(ToString::to_string).collect())
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
let mut exit_code = 0;
|
||||||
|
|
||||||
|
if users.is_empty() {
|
||||||
println!(
|
println!(
|
||||||
"{}",
|
"{}",
|
||||||
get_groups_gnu(None)
|
get_groups_gnu(None)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|&g| gid2grp(g).unwrap())
|
.map(|&gid| gid2grp(gid).unwrap_or_else(|_| {
|
||||||
|
show_error!("cannot find name for group ID {}", gid);
|
||||||
|
exit_code = 1;
|
||||||
|
gid.to_string()
|
||||||
|
}))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(" ")
|
.join(" ")
|
||||||
);
|
);
|
||||||
0
|
return exit_code;
|
||||||
}
|
}
|
||||||
Some(user) => {
|
|
||||||
if let Ok(p) = Passwd::locate(user) {
|
for user in users {
|
||||||
|
if let Ok(p) = Passwd::locate(user.as_str()) {
|
||||||
println!(
|
println!(
|
||||||
"{}",
|
"{} : {}",
|
||||||
|
user,
|
||||||
p.belongs_to()
|
p.belongs_to()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|&g| gid2grp(g).unwrap())
|
.map(|&gid| gid2grp(gid).unwrap_or_else(|_| {
|
||||||
|
show_error!("cannot find name for group ID {}", gid);
|
||||||
|
exit_code = 1;
|
||||||
|
gid.to_string()
|
||||||
|
}))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(" ")
|
.join(" ")
|
||||||
);
|
);
|
||||||
0
|
|
||||||
} else {
|
} else {
|
||||||
crash!(1, "unknown user {}", user);
|
show_error!("'{}': no such user", user);
|
||||||
}
|
exit_code = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
exit_code
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,56 +1,176 @@
|
||||||
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 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() {
|
||||||
if !is_ci() {
|
let result = new_ucmd!().run();
|
||||||
new_ucmd!().succeeds().stdout_is(expected_result(&[]));
|
let exp_result = unwrap_or_return!(expected_result(&[]));
|
||||||
} else {
|
|
||||||
// TODO: investigate how this could be tested in CI
|
result
|
||||||
// stderr = groups: cannot find name for group ID 116
|
.stdout_is(exp_result.stdout_str())
|
||||||
println!("test skipped:");
|
.stderr_is(exp_result.stderr_str())
|
||||||
}
|
.code_is(exp_result.code());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
#[ignore = "fixme: 'groups USERNAME' needs more debugging"]
|
|
||||||
fn test_groups_username() {
|
fn test_groups_username() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let test_users = [&whoami()[..]];
|
||||||
let whoami_result = scene.cmd("whoami").run();
|
|
||||||
|
|
||||||
let username = if whoami_result.succeeded() {
|
let result = new_ucmd!().args(&test_users).run();
|
||||||
whoami_result.stdout_move_str()
|
let exp_result = unwrap_or_return!(expected_result(&test_users));
|
||||||
} else if is_ci() {
|
|
||||||
String::from("docker")
|
result
|
||||||
} else {
|
.stdout_is(exp_result.stdout_str())
|
||||||
println!("test skipped:");
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.code_is(exp_result.code());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(unix)]
|
||||||
|
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'
|
||||||
|
#[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_MULTIPLE_USERS);
|
||||||
|
if version_check_string.starts_with(UUTILS_WARNING) {
|
||||||
|
println!("{}\ntest skipped", version_check_string);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
let test_users = ["root", "man", "postfix", "sshd", &whoami()];
|
||||||
|
|
||||||
|
let result = new_ucmd!().args(&test_users).run();
|
||||||
|
let exp_result = unwrap_or_return!(expected_result(&test_users));
|
||||||
|
|
||||||
|
result
|
||||||
|
.stdout_is(exp_result.stdout_str())
|
||||||
|
.stderr_is(exp_result.stderr_str())
|
||||||
|
.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),
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: stdout should be in the form: "username : group1 group2 group3"
|
Ok(CmdResult::new(
|
||||||
|
Some(result.tmpd()),
|
||||||
scene
|
Some(result.code()),
|
||||||
.ucmd()
|
result.succeeded(),
|
||||||
.arg(&username)
|
stdout.as_bytes(),
|
||||||
.succeeds()
|
stderr.as_bytes(),
|
||||||
.stdout_is(expected_result(&[&username]));
|
))
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(unix)]
|
|
||||||
fn expected_result(args: &[&str]) -> String {
|
|
||||||
// We want to use GNU id. On most linux systems, this is "id", but on
|
|
||||||
// bsd-like systems (e.g. FreeBSD, MacOS), it is commonly "gid".
|
|
||||||
#[cfg(any(target_os = "linux"))]
|
|
||||||
let util_name = "id";
|
|
||||||
#[cfg(not(target_os = "linux"))]
|
|
||||||
let util_name = "gid";
|
|
||||||
|
|
||||||
TestScenario::new(util_name)
|
|
||||||
.cmd_keepenv(util_name)
|
|
||||||
.env("LANGUAGE", "C")
|
|
||||||
.args(args)
|
|
||||||
.args(&["-Gn"])
|
|
||||||
.succeeds()
|
|
||||||
.stdout_move_str()
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue