From 6acc8e695f6217a245667a32905ebfcd4828e471 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 25 May 2024 13:10:52 +0200 Subject: [PATCH] hashsum: Implement the quiet mode --- src/uu/cksum/src/cksum.rs | 12 ++++++++++- src/uu/hashsum/src/hashsum.rs | 8 ++++--- src/uucore/src/lib/features/checksum.rs | 5 ++++- tests/by-util/test_hashsum.rs | 28 +++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index fe3edbc44..ed37fa39a 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -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) diff --git a/src/uu/hashsum/src/hashsum.rs b/src/uu/hashsum/src/hashsum.rs index 59a9a3352..6aa6e869f 100644 --- a/src/uu/hashsum/src/hashsum.rs +++ b/src/uu/hashsum/src/hashsum.rs @@ -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), ) diff --git a/src/uucore/src/lib/features/checksum.rs b/src/uucore/src/lib/features/checksum.rs index a2b32d641..b13b39e49 100644 --- a/src/uucore/src/lib/features/checksum.rs +++ b/src/uucore/src/lib/features/checksum.rs @@ -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, ) -> 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 { diff --git a/tests/by-util/test_hashsum.rs b/tests/by-util/test_hashsum.rs index 2ad32fa31..419ed43d8 100644 --- a/tests/by-util/test_hashsum.rs +++ b/tests/by-util/test_hashsum.rs @@ -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"); +}