hashsum: Implement the quiet mode

This commit is contained in:
Sylvestre Ledru 2024-05-25 13:10:52 +02:00
parent 89b7a1a8fb
commit 6acc8e695f
4 changed files with 48 additions and 5 deletions

View file

@ -182,6 +182,7 @@ mod options {
pub const STATUS: &str = "status";
pub const WARN: &str = "warn";
pub const IGNORE_MISSING: &str = "ignore-missing";
pub const QUIET: &str = "quiet";
}
/// Determines whether to prompt an asterisk (*) in the output.
@ -282,7 +283,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let strict = matches.get_flag(options::STRICT);
let status = matches.get_flag(options::STATUS);
let warn = matches.get_flag(options::WARN);
let ignore_missing = matches.get_flag(options::IGNORE_MISSING);
let ignore_missing: bool = matches.get_flag(options::IGNORE_MISSING);
let quiet: bool = matches.get_flag(options::QUIET);
if (binary_flag || text_flag) && check {
return Err(ChecksumError::BinaryTextConflict.into());
@ -307,6 +309,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
warn,
binary_flag,
ignore_missing,
quiet,
algo_option,
length,
);
@ -447,6 +450,13 @@ pub fn uu_app() -> Command {
.help("don't output anything, status code shows success")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::QUIET)
.short('q')
.long(options::QUIET)
.help("don't print OK for each successfully verified file")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::IGNORE_MISSING)
.long(options::IGNORE_MISSING)

View file

@ -220,7 +220,7 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
.unwrap_or(None)
.unwrap_or(&false);
let status = matches.get_flag("status");
//let quiet = matches.get_flag("quiet") || status;
let quiet = matches.get_flag("quiet") || status;
//let strict = matches.get_flag("strict");
let warn = matches.get_flag("warn") && !status;
let zero: bool = matches.get_flag("zero");
@ -277,6 +277,7 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
warn,
binary_flag,
ignore_missing,
quiet,
algo_option,
Some(algo.bits),
);
@ -303,6 +304,7 @@ mod options {
pub const BINARY: &str = "binary";
pub const STATUS: &str = "status";
pub const WARN: &str = "warn";
pub const QUIET: &str = "quiet";
}
pub fn uu_app_common() -> Command {
@ -351,9 +353,9 @@ pub fn uu_app_common() -> Command {
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("quiet")
Arg::new(options::QUIET)
.short('q')
.long("quiet")
.long(options::QUIET)
.help("don't print OK for each successfully verified file")
.action(ArgAction::SetTrue),
)

View file

@ -310,6 +310,7 @@ pub fn perform_checksum_validation<'a, I>(
warn: bool,
binary: bool,
ignore_missing: bool,
quiet: bool,
algo_name_input: Option<&str>,
length_input: Option<usize>,
) -> UResult<()>
@ -471,7 +472,9 @@ where
// Do the checksum validation
if expected_checksum == calculated_checksum {
println!("{prefix}{filename_to_check}: OK");
if !quiet {
println!("{prefix}{filename_to_check}: OK");
}
correct_format += 1;
} else {
if !status {

View file

@ -729,3 +729,31 @@ fn test_check_directory_error() {
.fails()
.stderr_contains("md5sum: d: Is a directory\n");
}
#[test]
fn test_check_quiet() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427e f\n");
scene
.ccmd("md5sum")
.arg("--quiet")
.arg("--check")
.arg(at.subdir.join("in.md5"))
.succeeds()
.stderr_is("")
.stdout_is("");
// incorrect md5
at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427f f\n");
scene
.ccmd("md5sum")
.arg("--quiet")
.arg("--check")
.arg(at.subdir.join("in.md5"))
.fails()
.stdout_contains("f: FAILED")
.stderr_contains("WARNING: 1 computed checksum did NOT match");
}