diff --git a/crates/nu-command/src/platform/du.rs b/crates/nu-command/src/platform/du.rs index 9b135c07ed..72e365ea69 100644 --- a/crates/nu-command/src/platform/du.rs +++ b/crates/nu-command/src/platform/du.rs @@ -27,9 +27,9 @@ pub struct DuArgs { deref: bool, exclude: Option>, #[serde(rename = "max-depth")] - max_depth: Option, + max_depth: Option>, #[serde(rename = "min-size")] - min_size: Option, + min_size: Option>, } impl Command for Du { @@ -85,15 +85,25 @@ impl Command for Du { _input: PipelineData, ) -> Result { let tag = call.head; + let min_size: Option> = call.get_flag(engine_state, stack, "min-size")?; + let max_depth: Option> = call.get_flag(engine_state, stack, "max-depth")?; + if let Some(ref max_depth) = max_depth { + if max_depth.item < 0 { + return Err(ShellError::NeedsPositiveValue(max_depth.span)); + } + } + if let Some(ref min_size) = min_size { + if min_size.item < 0 { + return Err(ShellError::NeedsPositiveValue(min_size.span)); + } + } let args = DuArgs { path: call.opt(engine_state, stack, 0)?, all: call.has_flag("all"), deref: call.has_flag("deref"), exclude: call.get_flag(engine_state, stack, "exclude")?, - max_depth: call - .get_flag::(engine_state, stack, "max-depth")? - .map(|n| (n as u64).try_into().expect("error converting i64 to u64")), - min_size: call.get_flag(engine_state, stack, "min-size")?, + max_depth, + min_size, }; let exclude = args.exclude.map_or(Ok(None), move |x| { @@ -145,8 +155,8 @@ impl Command for Du { let all = args.all; let deref = args.deref; - let max_depth = args.max_depth.map(|f| f as u64); - let min_size = args.min_size.map(|f| f as u64); + let max_depth = args.max_depth.map(|f| f.item as u64); + let min_size = args.min_size.map(|f| f.item as u64); let params = DirBuilder { tag, diff --git a/crates/nu-command/tests/commands/platform/du.rs b/crates/nu-command/tests/commands/platform/du.rs new file mode 100644 index 0000000000..ab35f5c265 --- /dev/null +++ b/crates/nu-command/tests/commands/platform/du.rs @@ -0,0 +1,43 @@ +use nu_test_support::{nu, pipeline}; + +#[test] +fn test_du_flag_min_size() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + du -m -1 + "# + )); + assert!(actual + .err + .contains("Negative value passed when positive one is required")); + + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + du -m 1 + "# + )); + assert!(actual.err.is_empty()); +} + +#[test] +fn test_du_flag_max_depth() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + du -d -2 + "# + )); + assert!(actual + .err + .contains("Negative value passed when positive one is required")); + + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + du -d 2 + "# + )); + assert!(actual.err.is_empty()); +} diff --git a/crates/nu-command/tests/commands/platform/mod.rs b/crates/nu-command/tests/commands/platform/mod.rs index f9d2ffbcc3..e6a8e34748 100644 --- a/crates/nu-command/tests/commands/platform/mod.rs +++ b/crates/nu-command/tests/commands/platform/mod.rs @@ -1 +1,2 @@ +mod du; mod kill;