Merge pull request #2522 from miDeb/sort/unique-check

sort: disallow equal lines for --check with --unique
This commit is contained in:
Sylvestre Ledru 2021-07-31 20:59:25 +02:00 committed by GitHub
commit 897693c997
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View file

@ -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);
}

View file

@ -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"];