From a54f9719e5560974aeadede9ff7426391ddce5c6 Mon Sep 17 00:00:00 2001 From: pwygab <88221256+merelymyself@users.noreply.github.com> Date: Tue, 12 Jul 2022 19:03:50 +0800 Subject: [PATCH] add `unspanned` flag to error make, add tests (#6017) * add `unspanned` flag to error make, add tests * fmt --- .../src/core_commands/error_make.rs | 42 +++++++++++++------ .../nu-command/tests/commands/error_make.rs | 26 ++++++++++++ crates/nu-command/tests/commands/mod.rs | 1 + 3 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 crates/nu-command/tests/commands/error_make.rs diff --git a/crates/nu-command/src/core_commands/error_make.rs b/crates/nu-command/src/core_commands/error_make.rs index 64dd25101d..a98aaaa881 100644 --- a/crates/nu-command/src/core_commands/error_make.rs +++ b/crates/nu-command/src/core_commands/error_make.rs @@ -16,6 +16,11 @@ impl Command for ErrorMake { fn signature(&self) -> Signature { Signature::build("error make") .required("error_struct", SyntaxShape::Record, "the error to create") + .switch( + "unspanned", + "remove the origin label from the error", + Some('u'), + ) .category(Category::Core) } @@ -36,16 +41,29 @@ impl Command for ErrorMake { ) -> Result { let span = call.head; let arg: Value = call.req(engine_state, stack, 0)?; + let unspanned = call.has_flag("unspanned"); - Err(make_error(&arg, span).unwrap_or_else(|| { - ShellError::GenericError( - "Creating error value not supported.".into(), - "unsupported error format".into(), - Some(span), - None, - Vec::new(), - ) - })) + if unspanned { + Err(make_error(&arg, None).unwrap_or_else(|| { + ShellError::GenericError( + "Creating error value not supported.".into(), + "unsupported error format".into(), + Some(span), + None, + Vec::new(), + ) + })) + } else { + Err(make_error(&arg, Some(span)).unwrap_or_else(|| { + ShellError::GenericError( + "Creating error value not supported.".into(), + "unsupported error format".into(), + Some(span), + None, + Vec::new(), + ) + })) + } } fn examples(&self) -> Vec { @@ -69,7 +87,7 @@ impl Command for ErrorMake { } } -fn make_error(value: &Value, throw_span: Span) -> Option { +fn make_error(value: &Value, throw_span: Option) -> Option { if let Value::Record { .. } = &value { let msg = value.get_data_by_key("msg"); let label = value.get_data_by_key("label"); @@ -106,7 +124,7 @@ fn make_error(value: &Value, throw_span: Span) -> Option { ) => Some(ShellError::GenericError( message, label_text, - Some(throw_span), + throw_span, None, Vec::new(), )), @@ -116,7 +134,7 @@ fn make_error(value: &Value, throw_span: Span) -> Option { (Some(Value::String { val: message, .. }), None) => Some(ShellError::GenericError( message, "originates from here".to_string(), - Some(throw_span), + throw_span, None, Vec::new(), )), diff --git a/crates/nu-command/tests/commands/error_make.rs b/crates/nu-command/tests/commands/error_make.rs new file mode 100644 index 0000000000..c34e666f50 --- /dev/null +++ b/crates/nu-command/tests/commands/error_make.rs @@ -0,0 +1,26 @@ +use nu_test_support::{nu, pipeline}; + +#[test] +fn error_label_works() { + let actual = nu!( + cwd: ".", pipeline( + r#" + error make {msg:foo label:{text:unseen}} + "# + )); + + assert!(actual.err.contains("unseen")); + assert!(actual.err.contains("╰──")); +} + +#[test] +fn no_span_if_unspanned() { + let actual = nu!( + cwd: ".", pipeline( + r#" + error make -u {msg:foo label:{text:unseen}} + "# + )); + + assert!(!actual.err.contains("unseen")); +} diff --git a/crates/nu-command/tests/commands/mod.rs b/crates/nu-command/tests/commands/mod.rs index fd127dc32b..5977e35efe 100644 --- a/crates/nu-command/tests/commands/mod.rs +++ b/crates/nu-command/tests/commands/mod.rs @@ -15,6 +15,7 @@ mod each; mod echo; mod empty; mod enter; +mod error_make; mod every; mod find; mod first;