mirror of
https://github.com/nushell/nushell.git
synced 2024-12-23 23:49:44 +01:00
Solving the issue "sort-by
should fail gracefully if mismatched types are compared" (#2360)
This commit is contained in:
parent
e292bb46bb
commit
a224cd38ab
@ -135,6 +135,17 @@ pub fn sort(
|
||||
} => {
|
||||
let should_sort_case_insensitively = insensitive && vec.iter().all(|x| x.is_string());
|
||||
|
||||
if !vec
|
||||
.windows(2)
|
||||
.all(|elem| coerce_compare(&elem[0], &elem[1]).is_ok())
|
||||
{
|
||||
return Err(ShellError::labeled_error(
|
||||
"Not all values can be compared",
|
||||
"not all values compare",
|
||||
tag,
|
||||
));
|
||||
}
|
||||
|
||||
vec.sort_by(|a, b| {
|
||||
if should_sort_case_insensitively {
|
||||
let lowercase_a_string = a.expect_string().to_ascii_lowercase();
|
||||
|
@ -45,6 +45,21 @@ fn by_invalid_column() {
|
||||
assert!(actual.err.contains("invalid column"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn by_invalid_types() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open cargo_sample.toml --raw
|
||||
| echo [1 "foo"]
|
||||
| sort-by
|
||||
"#
|
||||
));
|
||||
|
||||
assert!(actual.err.contains("Not all values can be compared"));
|
||||
assert!(actual.err.contains("not all values compare"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sort_primitive_values() {
|
||||
let actual = nu!(
|
||||
|
Loading…
Reference in New Issue
Block a user