From bce2627e45282f140c9e1e1cee28fac61af49ca7 Mon Sep 17 00:00:00 2001 From: KITAGAWA Yasutaka Date: Sat, 17 Feb 2024 17:51:20 +0900 Subject: [PATCH] Fix panic in `seq date` (#11871) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description Fix #11732 # User-Facing Changes Invalid output format causes an error, not a panic. ```nu ❯ seq date --output-format '%H-%M-%S' Error: × Invalid output format ╭─[entry #1:1:1] 1 │ seq date --output-format '%H-%M-%S' · ────┬─── · ╰── an error occurred when formatting an argument ╰──── ``` # Tests + Formatting # After Submitting --- crates/nu-command/src/generators/seq_date.rs | 15 ++++++++++++++- crates/nu-command/tests/commands/mod.rs | 1 + crates/nu-command/tests/commands/seq_date.rs | 8 ++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 crates/nu-command/tests/commands/seq_date.rs diff --git a/crates/nu-command/src/generators/seq_date.rs b/crates/nu-command/src/generators/seq_date.rs index 9b4f393723..de2c1e2b20 100644 --- a/crates/nu-command/src/generators/seq_date.rs +++ b/crates/nu-command/src/generators/seq_date.rs @@ -7,6 +7,7 @@ use nu_protocol::{ Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value, }; +use std::fmt::Write; #[derive(Clone)] pub struct SeqDate; @@ -315,7 +316,19 @@ pub fn run_seq_dates( let mut ret = vec![]; loop { - let date_string = &next.format(&out_format).to_string(); + let mut date_string = String::new(); + match write!(date_string, "{}", next.format(&out_format)) { + Ok(_) => {} + Err(e) => { + return Err(ShellError::GenericError { + error: "Invalid output format".into(), + msg: e.to_string(), + span: Some(call_span), + help: None, + inner: vec![], + }); + } + } ret.push(Value::string(date_string, call_span)); next += Duration::days(step_size); diff --git a/crates/nu-command/tests/commands/mod.rs b/crates/nu-command/tests/commands/mod.rs index 3c040ec8b9..0177477205 100644 --- a/crates/nu-command/tests/commands/mod.rs +++ b/crates/nu-command/tests/commands/mod.rs @@ -93,6 +93,7 @@ mod select; mod semicolon; mod seq; mod seq_char; +mod seq_date; mod skip; mod sort; mod sort_by; diff --git a/crates/nu-command/tests/commands/seq_date.rs b/crates/nu-command/tests/commands/seq_date.rs new file mode 100644 index 0000000000..c333c87773 --- /dev/null +++ b/crates/nu-command/tests/commands/seq_date.rs @@ -0,0 +1,8 @@ +use nu_test_support::nu; + +#[test] +fn fails_when_output_format_contains_time() { + let actual = nu!("seq date --output-format '%H-%M-%S'"); + + assert!(actual.err.contains("Invalid output format")); +}