Show error when trying to sort by invalid column (#1880)

* Show error when trying to sort by invalid column

* Added test for changes

* Addressed comments, updated test

* Removed unnecessary mutable keyword

* Changed split-column to solt column after rebase from upstream
This commit is contained in:
Shaarad Dalvi 2020-05-24 23:07:08 +05:30 committed by GitHub
parent d488fddfe1
commit 9c14fb6c02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -69,6 +69,18 @@ fn sort_by(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream
return;
}
for sort_arg in rest.iter() {
let match_test = get_data_by_key(&vec[0], sort_arg.borrow_spanned());
if match_test == None {
yield Err(ShellError::labeled_error(
"Can not find column to sort by",
"invalid column",
sort_arg.borrow_spanned().span,
));
return;
}
}
match &vec[0] {
Value {
value: UntaggedValue::Primitive(_),

View File

@ -22,6 +22,29 @@ fn by_column() {
assert_eq!(actual.out, "description");
}
#[test]
fn by_invalid_column() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
open cargo_sample.toml --raw
| lines
| skip 1
| first 4
| split column "="
| sort-by ColumnThatDoesNotExist
| skip 1
| first 1
| get Column1
| trim
| echo $it
"#
));
assert!(actual.err.contains("Can not find column to sort by"));
assert!(actual.err.contains("invalid column"));
}
#[test]
fn sort_primitive_values() {
let actual = nu!(