mirror of
https://github.com/nushell/nushell
synced 2024-12-27 21:43:09 +00:00
Validate input range of du
flags (#7962)
# Description Fix #7953 Fix flags on `du` which should be positive and added some tests. # User-Facing Changes _(List of all changes that impact the user experience here. This helps us keep track of breaking changes.)_ # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
parent
66e5e42fb1
commit
67d1249b2b
3 changed files with 62 additions and 8 deletions
|
@ -27,9 +27,9 @@ pub struct DuArgs {
|
||||||
deref: bool,
|
deref: bool,
|
||||||
exclude: Option<Spanned<String>>,
|
exclude: Option<Spanned<String>>,
|
||||||
#[serde(rename = "max-depth")]
|
#[serde(rename = "max-depth")]
|
||||||
max_depth: Option<i64>,
|
max_depth: Option<Spanned<i64>>,
|
||||||
#[serde(rename = "min-size")]
|
#[serde(rename = "min-size")]
|
||||||
min_size: Option<i64>,
|
min_size: Option<Spanned<i64>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Command for Du {
|
impl Command for Du {
|
||||||
|
@ -85,15 +85,25 @@ impl Command for Du {
|
||||||
_input: PipelineData,
|
_input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let tag = call.head;
|
let tag = call.head;
|
||||||
|
let min_size: Option<Spanned<i64>> = call.get_flag(engine_state, stack, "min-size")?;
|
||||||
|
let max_depth: Option<Spanned<i64>> = 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 {
|
let args = DuArgs {
|
||||||
path: call.opt(engine_state, stack, 0)?,
|
path: call.opt(engine_state, stack, 0)?,
|
||||||
all: call.has_flag("all"),
|
all: call.has_flag("all"),
|
||||||
deref: call.has_flag("deref"),
|
deref: call.has_flag("deref"),
|
||||||
exclude: call.get_flag(engine_state, stack, "exclude")?,
|
exclude: call.get_flag(engine_state, stack, "exclude")?,
|
||||||
max_depth: call
|
max_depth,
|
||||||
.get_flag::<i64>(engine_state, stack, "max-depth")?
|
min_size,
|
||||||
.map(|n| (n as u64).try_into().expect("error converting i64 to u64")),
|
|
||||||
min_size: call.get_flag(engine_state, stack, "min-size")?,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let exclude = args.exclude.map_or(Ok(None), move |x| {
|
let exclude = args.exclude.map_or(Ok(None), move |x| {
|
||||||
|
@ -145,8 +155,8 @@ impl Command for Du {
|
||||||
|
|
||||||
let all = args.all;
|
let all = args.all;
|
||||||
let deref = args.deref;
|
let deref = args.deref;
|
||||||
let max_depth = args.max_depth.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 as u64);
|
let min_size = args.min_size.map(|f| f.item as u64);
|
||||||
|
|
||||||
let params = DirBuilder {
|
let params = DirBuilder {
|
||||||
tag,
|
tag,
|
||||||
|
|
43
crates/nu-command/tests/commands/platform/du.rs
Normal file
43
crates/nu-command/tests/commands/platform/du.rs
Normal file
|
@ -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());
|
||||||
|
}
|
|
@ -1 +1,2 @@
|
||||||
|
mod du;
|
||||||
mod kill;
|
mod kill;
|
||||||
|
|
Loading…
Reference in a new issue