From 1d5518a214550a251e99907c2206fcc8571f2799 Mon Sep 17 00:00:00 2001 From: Luccas Mateus Date: Tue, 18 Aug 2020 02:37:45 -0300 Subject: [PATCH] Err message for sort-by invalid types now shows the mismatched types (#2366) * make sort-by fail gracefully if mismatched types are compared * Added a test to check if sorted-by with invalid types exists gracefully * Linter changes * removed redundant pattern matching * Changed the error message * Added a comma after every argument * Changed the test to accomodate the new err messages * Err message for sort-by invalid types now shows the mismatched types * Lints problems * Changed unwrap to expect --- Cargo.lock | 11 +++++++++++ crates/nu-cli/src/commands/sort_by.rs | 13 ++++++++++--- crates/nu-cli/tests/commands/sort_by.rs | 4 +++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 33029102d2..c7898fe83a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,15 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "Inflector" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3" +dependencies = [ + "lazy_static 1.4.0", + "regex 1.3.9", +] + [[package]] name = "addr2line" version = "0.13.0" @@ -2956,6 +2966,7 @@ dependencies = [ name = "nu-cli" version = "0.18.2" dependencies = [ + "Inflector", "ansi_term 0.12.1", "app_dirs2", "async-recursion", diff --git a/crates/nu-cli/src/commands/sort_by.rs b/crates/nu-cli/src/commands/sort_by.rs index a2cb055f20..671e7b7783 100644 --- a/crates/nu-cli/src/commands/sort_by.rs +++ b/crates/nu-cli/src/commands/sort_by.rs @@ -135,13 +135,20 @@ pub fn sort( } => { let should_sort_case_insensitively = insensitive && vec.iter().all(|x| x.is_string()); - if !vec + if let Some(values) = vec .windows(2) - .all(|elem| coerce_compare(&elem[0], &elem[1]).is_ok()) + .map(|elem| coerce_compare(&elem[0], &elem[1])) + .find(|elem| elem.is_err()) { + let (type_1, type_2) = values + .err() + .expect("An error ocourred in the checking of types"); return Err(ShellError::labeled_error( "Not all values can be compared", - "not all values compare", + format!( + "Unable to sort values, as \"{}\" cannot compare against \"{}\"", + type_1, type_2 + ), tag, )); } diff --git a/crates/nu-cli/tests/commands/sort_by.rs b/crates/nu-cli/tests/commands/sort_by.rs index 98bdd51d89..a155c38187 100644 --- a/crates/nu-cli/tests/commands/sort_by.rs +++ b/crates/nu-cli/tests/commands/sort_by.rs @@ -57,7 +57,9 @@ fn by_invalid_types() { )); assert!(actual.err.contains("Not all values can be compared")); - assert!(actual.err.contains("not all values compare")); + assert!(actual + .err + .contains("Unable to sort values, as \"integer\" cannot compare against \"string\"")); } #[test]