From cdf959d2a882282dd27dd53075e7c47ff704569b Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Mon, 15 Apr 2024 17:52:06 +0200 Subject: [PATCH] cksum: --tag & --untagged - the order of usage matters. manage it --- src/uu/cksum/src/cksum.rs | 18 ++++++++++++------ tests/by-util/test_cksum.rs | 31 +++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index 4339ff33cc7..9801c3fd5e6 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -369,7 +369,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { None }; - if algo_name == "bsd" && check { + if ["bsd", "crc", "sysv"].contains(&algo_name) && check { return Err(io::Error::new( io::ErrorKind::InvalidInput, "--check is not supported with --algorithm={bsd,sysv,crc}", @@ -378,9 +378,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } let untagged: bool = matches.get_flag(options::UNTAGGED); - let tag: bool = matches.get_flag(options::TAG); - let binary = if untagged && tag { + let args: Vec = std::env::args().collect(); + let binary = if args.contains(&"--tag".to_string()) && args.contains(&"--untagged".to_string()) + { + // ugly workaround to detect if the two arguments have been passed at the same time + // clap with overrides_with false } else { matches.get_flag(options::BINARY) @@ -451,13 +454,15 @@ pub fn uu_app() -> Command { Arg::new(options::UNTAGGED) .long(options::UNTAGGED) .help("create a reversed style checksum, without digest type") - .action(ArgAction::SetTrue), + .action(ArgAction::SetTrue) + .overrides_with(options::TAG), ) .arg( Arg::new(options::TAG) .long(options::TAG) .help("create a BSD style checksum, undo --untagged (default)") - .action(ArgAction::SetTrue), + .action(ArgAction::SetTrue) + .overrides_with(options::UNTAGGED), ) .arg( Arg::new(options::LENGTH) @@ -503,7 +508,8 @@ pub fn uu_app() -> Command { Arg::new(options::TEXT) .long(options::TEXT) .short('t') - .hide(true) // for legacy compatibility, no action + .hide(true) + .overrides_with(options::BINARY) .action(ArgAction::SetTrue), ) .arg( diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index 31a3f52747f..40fed667428 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -148,7 +148,7 @@ fn test_tag_after_untagged() { .arg("-a=md5") .arg("lorem_ipsum.txt") .succeeds() - .stdout_is("cd724690f7dc61775dfac400a71f2caa lorem_ipsum.txt\n"); + .stdout_is_fixture("md5_single_file.expected"); } #[test] @@ -287,6 +287,22 @@ fn test_check_algo() { .no_stdout() .stderr_contains("cksum: --check is not supported with --algorithm={bsd,sysv,crc}") .code_is(1); + new_ucmd!() + .arg("-a=sysv") + .arg("--check") + .arg("lorem_ipsum.txt") + .fails() + .no_stdout() + .stderr_contains("cksum: --check is not supported with --algorithm={bsd,sysv,crc}") + .code_is(1); + new_ucmd!() + .arg("-a=crc") + .arg("--check") + .arg("lorem_ipsum.txt") + .fails() + .no_stdout() + .stderr_contains("cksum: --check is not supported with --algorithm={bsd,sysv,crc}") + .code_is(1); } #[test] @@ -474,13 +490,24 @@ fn test_binary_file() { // no "*" in this case scene .ucmd() - .arg("--untagged") .arg("--tag") + .arg("--untagged") .arg("--binary") .arg("--algorithm=md5") .arg(at.subdir.join("f")) .succeeds() .stdout_contains("d41d8cd98f00b204e9800998ecf8427e "); + + // no "*" in this case + scene + .ucmd() + .arg("--tag") + .arg("--untagged") + .arg("--binary") + .arg("--algorithm=md5") + .arg("raw/blake2b_single_file.expected") + .succeeds() + .stdout_contains("7e297c07ed8e053600092f91bdd1dad7 "); } #[test]