From a76d452f75e1a503e1e1369143555d9153004edb Mon Sep 17 00:00:00 2001 From: electricboogie <32370782+electricboogie@users.noreply.github.com> Date: Sat, 17 Apr 2021 03:06:19 -0500 Subject: [PATCH] Sort: More small fixes (#2065) * Various fixes and performance improvements * fix a typo Co-authored-by: Michael Debertol * Fix month parse for months with leading whitespace * Implement test for months whitespace fix * Confirm human numeric works as expected with whitespace with a test * Correct arg help value name for --parallel * Fix SemVer non version lines/empty line sorting with a test Co-authored-by: Sylvestre Ledru Co-authored-by: Michael Debertol --- Cargo.lock | 2 -- src/uu/sort/src/sort.rs | 19 +++++++++++++++---- tests/by-util/test_sort.rs | 19 +++++++++++++++++++ .../sort/human-numeric-whitespace.expected | 11 +++++++++++ .../sort/human-numeric-whitespace.txt | 11 +++++++++++ .../fixtures/sort/months-whitespace.expected | 8 ++++++++ tests/fixtures/sort/months-whitespace.txt | 8 ++++++++ .../sort/version-empty-lines.expected | 11 +++++++++++ tests/fixtures/sort/version-empty-lines.txt | 11 +++++++++++ 9 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 tests/fixtures/sort/human-numeric-whitespace.expected create mode 100644 tests/fixtures/sort/human-numeric-whitespace.txt create mode 100644 tests/fixtures/sort/months-whitespace.expected create mode 100644 tests/fixtures/sort/months-whitespace.txt create mode 100644 tests/fixtures/sort/version-empty-lines.expected create mode 100644 tests/fixtures/sort/version-empty-lines.txt diff --git a/Cargo.lock b/Cargo.lock index 430abf921..461716b1b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,7 +1,5 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 - [[package]] name = "advapi32-sys" version = "0.2.0" diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index 35ab71ba2..8bf6eb1e8 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -677,7 +677,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .arg( Arg::with_name(OPT_PARALLEL) .long(OPT_PARALLEL) - .help("change the number of threads running concurrently to N") + .help("change the number of threads running concurrently to NUM_THREADS") .takes_value(true) .value_name("NUM_THREADS"), ) @@ -1226,7 +1226,7 @@ fn month_parse(line: &str) -> Month { // GNU splits at any 3 letter match "JUNNNN" is JUN let pattern = if line.trim().len().ge(&3) { // Split a 3 and get first element of tuple ".0" - line.split_at(3).0 + line.trim().split_at(3).0 } else { "" }; @@ -1262,10 +1262,21 @@ fn month_compare(a: &str, b: &str) -> Ordering { } } +fn version_parse(a: &str) -> Version { + let result = Version::parse(a); + + match result { + Ok(vers_a) => vers_a, + // Non-version lines parse to 0.0.0 + Err(_e) => Version::parse("0.0.0").unwrap(), + } +} + fn version_compare(a: &str, b: &str) -> Ordering { #![allow(clippy::comparison_chain)] - let ver_a = Version::parse(a); - let ver_b = Version::parse(b); + let ver_a = version_parse(a); + let ver_b = version_parse(b); + // Version::cmp is not implemented; implement comparison directly if ver_a > ver_b { Ordering::Greater diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index 866beefff..0f8020688 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -8,6 +8,25 @@ fn test_helper(file_name: &str, args: &str) { .stdout_is_fixture(format!("{}.expected", file_name)); } +#[test] +fn test_months_whitespace() { + test_helper("months-whitespace", "-M"); +} + +#[test] +fn test_version_empty_lines() { + new_ucmd!() + .arg("-V") + .arg("version-empty-lines.txt") + .succeeds() + .stdout_is("\n\n\n\n\n\n\n1.2.3-alpha\n1.2.3-alpha2\n\t\t\t1.12.4\n11.2.3\n"); +} + +#[test] +fn test_human_numeric_whitespace() { + test_helper("human-numeric-whitespace", "-h"); +} + #[test] fn test_multiple_decimals_general() { new_ucmd!() diff --git a/tests/fixtures/sort/human-numeric-whitespace.expected b/tests/fixtures/sort/human-numeric-whitespace.expected new file mode 100644 index 000000000..6fb9291ff --- /dev/null +++ b/tests/fixtures/sort/human-numeric-whitespace.expected @@ -0,0 +1,11 @@ + + + + + + + +456K +4568K + 456M + 6.2G diff --git a/tests/fixtures/sort/human-numeric-whitespace.txt b/tests/fixtures/sort/human-numeric-whitespace.txt new file mode 100644 index 000000000..19db648b1 --- /dev/null +++ b/tests/fixtures/sort/human-numeric-whitespace.txt @@ -0,0 +1,11 @@ + + +456K + + 456M + + +4568K + + 6.2G + diff --git a/tests/fixtures/sort/months-whitespace.expected b/tests/fixtures/sort/months-whitespace.expected new file mode 100644 index 000000000..84a44d564 --- /dev/null +++ b/tests/fixtures/sort/months-whitespace.expected @@ -0,0 +1,8 @@ + + +JAN + FEb + apr + apr + JUNNNN +AUG diff --git a/tests/fixtures/sort/months-whitespace.txt b/tests/fixtures/sort/months-whitespace.txt new file mode 100644 index 000000000..45c477477 --- /dev/null +++ b/tests/fixtures/sort/months-whitespace.txt @@ -0,0 +1,8 @@ +JAN + JUNNNN +AUG + + apr + apr + + FEb diff --git a/tests/fixtures/sort/version-empty-lines.expected b/tests/fixtures/sort/version-empty-lines.expected new file mode 100644 index 000000000..c496c0ff5 --- /dev/null +++ b/tests/fixtures/sort/version-empty-lines.expected @@ -0,0 +1,11 @@ + + + + + + + +1.2.3-alpha +1.2.3-alpha2 +11.2.3 + 1.12.4 diff --git a/tests/fixtures/sort/version-empty-lines.txt b/tests/fixtures/sort/version-empty-lines.txt new file mode 100644 index 000000000..9b6b89788 --- /dev/null +++ b/tests/fixtures/sort/version-empty-lines.txt @@ -0,0 +1,11 @@ +11.2.3 + + + +1.2.3-alpha2 + + +1.2.3-alpha + + + 1.12.4