Skip to content

Commit

Permalink
cksum: --tag & --untagged - the order of usage matters. manage it
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre committed Apr 28, 2024
1 parent a3aee48 commit cdf959d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
18 changes: 12 additions & 6 deletions src/uu/cksum/src/cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand All @@ -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<String> = 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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
31 changes: 29 additions & 2 deletions tests/by-util/test_cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit cdf959d

Please sign in to comment.