From ece1e432381ec3184be9f99d1a5f2796c3793a38 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Fri, 4 Feb 2022 13:26:08 -0600 Subject: [PATCH] fix into filesize tests and filesize (#932) * fix into filesize tests and filesize * tweaks * added span back for like the 10th time * Update filesize.rs Co-authored-by: JT <547158+jntrnr@users.noreply.github.com> --- .../src/conversions/into/filesize.rs | 43 +++++++++++++------ .../tests/commands/into_filesize.rs | 4 -- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/crates/nu-command/src/conversions/into/filesize.rs b/crates/nu-command/src/conversions/into/filesize.rs index 3ffe85edd5..4dc27c941c 100644 --- a/crates/nu-command/src/conversions/into/filesize.rs +++ b/crates/nu-command/src/conversions/into/filesize.rs @@ -111,27 +111,42 @@ fn into_filesize( } pub fn action(input: &Value, span: Span) -> Value { - match input { - Value::Filesize { .. } => input.clone(), - Value::Int { val, .. } => Value::Filesize { val: *val, span }, - Value::Float { val, .. } => Value::Filesize { - val: *val as i64, - span, - }, - Value::String { val, .. } => match int_from_string(val, span) { - Ok(val) => Value::Filesize { val, span }, - Err(error) => Value::Error { error }, - }, - _ => Value::Error { + if let Ok(value_span) = input.span() { + match input { + Value::Filesize { .. } => input.clone(), + Value::Int { val, .. } => Value::Filesize { + val: *val, + span: value_span, + }, + Value::Float { val, .. } => Value::Filesize { + val: *val as i64, + span: value_span, + }, + Value::String { val, .. } => match int_from_string(val, value_span) { + Ok(val) => Value::Filesize { + val, + span: value_span, + }, + Err(error) => Value::Error { error }, + }, + _ => Value::Error { + error: ShellError::UnsupportedInput( + "'into filesize' for unsupported type".into(), + value_span, + ), + }, + } + } else { + Value::Error { error: ShellError::UnsupportedInput( "'into filesize' for unsupported type".into(), span, ), - }, + } } } fn int_from_string(a_string: &str, span: Span) -> Result { - match a_string.parse::() { + match a_string.trim().parse::() { Ok(n) => Ok(n.0 as i64), Err(_) => Err(ShellError::CantConvert("int".into(), "string".into(), span)), } diff --git a/crates/nu-command/tests/commands/into_filesize.rs b/crates/nu-command/tests/commands/into_filesize.rs index d57665613c..40edb39e94 100644 --- a/crates/nu-command/tests/commands/into_filesize.rs +++ b/crates/nu-command/tests/commands/into_filesize.rs @@ -36,8 +36,6 @@ fn into_filesize_str() { assert!(actual.out.contains("2.0 KiB")); } -// FIXME: jt: needs more work -#[ignore] #[test] fn into_filesize_str_newline() { let actual = nu!( @@ -51,8 +49,6 @@ fn into_filesize_str_newline() { assert!(actual.out.contains("2.0 KiB")); } -// FIXME: jt: needs more work -#[ignore] #[test] fn into_filesize_str_many_newlines() { let actual = nu!(