Skip to content

Commit 2fadd25

Browse files
committed
cksum: fix --binary reset (issue #6375)
1 parent 717a692 commit 2fadd25

File tree

2 files changed

+23
-120
lines changed

2 files changed

+23
-120
lines changed

src/uu/cksum/src/cksum.rs

Lines changed: 22 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -205,61 +205,33 @@ mod options {
205205
pub const ZERO: &str = "zero";
206206
}
207207

208-
/// Determines whether to prompt an asterisk (*) in the output.
209-
///
210-
/// This function checks the `tag`, `binary`, and `had_reset` flags and returns a boolean
211-
/// indicating whether to prompt an asterisk (*) in the output.
212-
/// It relies on the overrides provided by clap (i.e., `--binary` overrides `--text` and vice versa).
213-
/// Same for `--tag` and `--untagged`.
214-
fn prompt_asterisk(tag: bool, binary: bool, had_reset: bool) -> bool {
215-
if tag {
216-
return false;
217-
}
218-
if had_reset {
219-
return false;
220-
}
221-
binary
222-
}
223-
224-
/**
225-
* Determine if we had a reset.
226-
* This is basically a hack to support the behavior of cksum
227-
* when we have the following arguments:
228-
* --binary --tag --untagged
229-
* Don't do it with clap because if it struggling with the --overrides_with
230-
* marking the value as set even if not present
231-
*/
232-
fn had_reset(args: &[OsString]) -> bool {
233-
// Indices where "--binary" or "-b", "--tag", and "--untagged" are found
234-
let binary_index = args.iter().position(|x| x == "--binary" || x == "-b");
235-
let tag_index = args.iter().position(|x| x == "--tag");
236-
let untagged_index = args.iter().position(|x| x == "--untagged");
237-
238-
// Check if all arguments are present and in the correct order
239-
match (binary_index, tag_index, untagged_index) {
240-
(Some(b), Some(t), Some(u)) => b < t && t < u,
241-
_ => false,
242-
}
243-
}
244-
245208
/***
246209
* cksum has a bunch of legacy behavior.
247210
* We handle this in this function to make sure they are self contained
248211
* and "easier" to understand
249212
*/
250-
fn handle_tag_text_binary_flags(matches: &clap::ArgMatches) -> UResult<(bool, bool)> {
251-
let untagged = matches.get_flag(options::UNTAGGED);
252-
let tag = matches.get_flag(options::TAG);
253-
let tag = tag || !untagged;
254-
255-
let binary_flag = matches.get_flag(options::BINARY);
256-
257-
let args: Vec<OsString> = std::env::args_os().collect();
258-
let had_reset = had_reset(&args);
259-
260-
let asterisk = prompt_asterisk(tag, binary_flag, had_reset);
213+
fn handle_tag_text_binary_flags<S: AsRef<OsStr>>(
214+
args: impl Iterator<Item = S>,
215+
) -> UResult<(bool, bool)> {
216+
let mut tag = true;
217+
let mut binary = false;
218+
219+
// --binary, --tag and --untagged are tight together: none of them
220+
// conflicts with each other but --tag will reset "binary" and set "tag".
221+
222+
for arg in args {
223+
let arg = arg.as_ref();
224+
if arg == "-b" || arg == "--binary" {
225+
binary = true;
226+
} else if arg == "--tag" {
227+
tag = true;
228+
binary = false;
229+
} else if arg == "--untagged" {
230+
tag = false;
231+
}
232+
}
261233

262-
Ok((tag, asterisk))
234+
Ok((tag, !tag && binary))
263235
}
264236

265237
#[uucore::main]
@@ -336,7 +308,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
336308
return perform_checksum_validation(files.iter().copied(), algo_option, length, opts);
337309
}
338310

339-
let (tag, asterisk) = handle_tag_text_binary_flags(&matches)?;
311+
let (tag, asterisk) = handle_tag_text_binary_flags(std::env::args_os())?;
340312

341313
let algo = detect_algo(algo_name, length)?;
342314
let line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO));
@@ -501,75 +473,7 @@ pub fn uu_app() -> Command {
501473

502474
#[cfg(test)]
503475
mod tests {
504-
use super::had_reset;
505476
use crate::calculate_blake2b_length;
506-
use crate::prompt_asterisk;
507-
use std::ffi::OsString;
508-
509-
#[test]
510-
fn test_had_reset() {
511-
let args = ["--binary", "--tag", "--untagged"]
512-
.iter()
513-
.map(|&s| s.into())
514-
.collect::<Vec<OsString>>();
515-
assert!(had_reset(&args));
516-
517-
let args = ["-b", "--tag", "--untagged"]
518-
.iter()
519-
.map(|&s| s.into())
520-
.collect::<Vec<OsString>>();
521-
assert!(had_reset(&args));
522-
523-
let args = ["-b", "--binary", "--tag", "--untagged"]
524-
.iter()
525-
.map(|&s| s.into())
526-
.collect::<Vec<OsString>>();
527-
assert!(had_reset(&args));
528-
529-
let args = ["--untagged", "--tag", "--binary"]
530-
.iter()
531-
.map(|&s| s.into())
532-
.collect::<Vec<OsString>>();
533-
assert!(!had_reset(&args));
534-
535-
let args = ["--untagged", "--tag", "-b"]
536-
.iter()
537-
.map(|&s| s.into())
538-
.collect::<Vec<OsString>>();
539-
assert!(!had_reset(&args));
540-
541-
let args = ["--binary", "--tag"]
542-
.iter()
543-
.map(|&s| s.into())
544-
.collect::<Vec<OsString>>();
545-
assert!(!had_reset(&args));
546-
547-
let args = ["--tag", "--untagged"]
548-
.iter()
549-
.map(|&s| s.into())
550-
.collect::<Vec<OsString>>();
551-
assert!(!had_reset(&args));
552-
553-
let args = ["--text", "--untagged"]
554-
.iter()
555-
.map(|&s| s.into())
556-
.collect::<Vec<OsString>>();
557-
assert!(!had_reset(&args));
558-
559-
let args = ["--binary", "--untagged"]
560-
.iter()
561-
.map(|&s| s.into())
562-
.collect::<Vec<OsString>>();
563-
assert!(!had_reset(&args));
564-
}
565-
566-
#[test]
567-
fn test_prompt_asterisk() {
568-
assert!(!prompt_asterisk(true, false, false));
569-
assert!(!prompt_asterisk(false, false, true));
570-
assert!(prompt_asterisk(false, true, false));
571-
assert!(!prompt_asterisk(false, false, false));
572-
}
573477

574478
#[test]
575479
fn test_calculate_length() {

tests/by-util/test_cksum.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,6 @@ fn test_reset_binary() {
602602
.stdout_contains("d41d8cd98f00b204e9800998ecf8427e ");
603603
}
604604

605-
#[ignore = "issue #6375"]
606605
#[test]
607606
fn test_reset_binary_but_set() {
608607
let scene = TestScenario::new(util_name!());
@@ -619,7 +618,7 @@ fn test_reset_binary_but_set() {
619618
.arg("--algorithm=md5")
620619
.arg(at.subdir.join("f"))
621620
.succeeds()
622-
.stdout_contains("d41d8cd98f00b204e9800998ecf8427e *"); // currently, asterisk=false. It should be true
621+
.stdout_contains("d41d8cd98f00b204e9800998ecf8427e *");
623622
}
624623

625624
#[test]

0 commit comments

Comments
 (0)