From 095e53aed2ae5b116da857eb382888686234abbf Mon Sep 17 00:00:00 2001 From: Michael Debertol Date: Mon, 26 Jul 2021 17:38:53 +0200 Subject: [PATCH] sort: disallow equal lines for --check with --unique --- src/uu/sort/src/check.rs | 12 +++++++++--- tests/by-util/test_sort.rs | 12 ++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/uu/sort/src/check.rs b/src/uu/sort/src/check.rs index 44c71674e..de320ef77 100644 --- a/src/uu/sort/src/check.rs +++ b/src/uu/sort/src/check.rs @@ -26,6 +26,13 @@ use std::{ /// /// The code we should exit with. pub fn check(path: &str, settings: &GlobalSettings) -> i32 { + let max_allowed_cmp = if settings.unique { + // If `unique` is enabled, the previous line must compare _less_ to the next one. + Ordering::Less + } else { + // Otherwise, the line previous line must compare _less or equal_ to the next one. + Ordering::Equal + }; let file = open(path); let (recycled_sender, recycled_receiver) = sync_channel(2); let (loaded_sender, loaded_receiver) = sync_channel(2); @@ -53,7 +60,7 @@ pub fn check(path: &str, settings: &GlobalSettings) -> i32 { settings, prev_chunk.line_data(), chunk.line_data(), - ) == Ordering::Greater + ) > max_allowed_cmp { if !settings.check_silent { eprintln!("sort: {}:{}: disorder: {}", path, line_idx, new_first.line); @@ -65,8 +72,7 @@ pub fn check(path: &str, settings: &GlobalSettings) -> i32 { for (a, b) in chunk.lines().iter().tuple_windows() { line_idx += 1; - if compare_by(a, b, settings, chunk.line_data(), chunk.line_data()) == Ordering::Greater - { + if compare_by(a, b, settings, chunk.line_data(), chunk.line_data()) > max_allowed_cmp { if !settings.check_silent { eprintln!("sort: {}:{}: disorder: {}", path, line_idx, b.line); } diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index 5f44ce35f..a5ca80de9 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -796,6 +796,18 @@ fn test_check_silent() { } } +#[test] +fn test_check_unique() { + // Due to a clap bug the combination "-cu" does not work. "-c -u" works. + // See https://github.com/clap-rs/clap/issues/2624 + new_ucmd!() + .args(&["-c", "-u"]) + .pipe_in("A\nA\n") + .fails() + .code_is(1) + .stderr_only("sort: -:2: disorder: A"); +} + #[test] fn test_dictionary_and_nonprinting_conflicts() { let conflicting_args = ["n", "h", "g", "M"];