From 918ec9daa839d55f1e607f588313e55a76b60a37 Mon Sep 17 00:00:00 2001 From: Herlon Aguiar Date: Thu, 25 Aug 2022 18:03:18 +0200 Subject: [PATCH] nu-command/filters: drop column check positive value (#6412) --- crates/nu-command/src/filters/drop/column.rs | 7 +++++++ crates/nu-command/tests/commands/drop.rs | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/crates/nu-command/src/filters/drop/column.rs b/crates/nu-command/src/filters/drop/column.rs index 83f2b621e4..6d917d50de 100644 --- a/crates/nu-command/src/filters/drop/column.rs +++ b/crates/nu-command/src/filters/drop/column.rs @@ -49,6 +49,13 @@ impl Command for DropColumn { 1 }; + // Make columns to drop is positive + if columns_to_drop < 0 { + if let Some(expr) = call.positional_nth(0) { + return Err(ShellError::NeedsPositiveValue(expr.span)); + } + } + dropcol(engine_state, span, input, columns_to_drop) } diff --git a/crates/nu-command/tests/commands/drop.rs b/crates/nu-command/tests/commands/drop.rs index 06d2668420..7a02eb09ca 100644 --- a/crates/nu-command/tests/commands/drop.rs +++ b/crates/nu-command/tests/commands/drop.rs @@ -16,6 +16,17 @@ fn columns() { assert_eq!(actual.out, "1"); } +#[test] +fn drop_columns_positive_value() { + let actual = nu!( + cwd: ".", pipeline(r#" + echo [[a, b];[1,2]] | drop column -1 + "#) + ); + + assert!(actual.err.contains("use a positive value")); +} + #[test] fn more_columns_than_table_has() { let actual = nu!(