From 85d113ab79f5b59ae7fa6426391ebcf48539e73d Mon Sep 17 00:00:00 2001 From: gmnsii <95436780+gmnsii@users.noreply.github.com> Date: Sat, 16 Apr 2022 07:51:24 -0700 Subject: [PATCH 1/2] df: -h -H shouldn't cause an error #3366 --- src/uu/df/src/df.rs | 4 ++-- tests/by-util/test_df.rs | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index a7520117d..3de605364 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -443,14 +443,14 @@ pub fn uu_app<'a>() -> Command<'a> { Arg::new(OPT_HUMAN_READABLE_BINARY) .short('h') .long("human-readable") - .conflicts_with(OPT_HUMAN_READABLE_DECIMAL) + .overrides_with(OPT_HUMAN_READABLE_DECIMAL) .help("print sizes in human readable format (e.g., 1K 234M 2G)"), ) .arg( Arg::new(OPT_HUMAN_READABLE_DECIMAL) .short('H') .long("si") - .conflicts_with(OPT_HUMAN_READABLE_BINARY) + .overrides_with(OPT_HUMAN_READABLE_DECIMAL) .help("likewise, but use powers of 1000 not 1024"), ) .arg( diff --git a/tests/by-util/test_df.rs b/tests/by-util/test_df.rs index d91f1ac36..a76cf5fd7 100644 --- a/tests/by-util/test_df.rs +++ b/tests/by-util/test_df.rs @@ -26,6 +26,12 @@ fn test_df_compatible_si() { new_ucmd!().arg("-aH").succeeds(); } +#[test] +fn test_df_overriding() { + new_ucmd!().arg("-hH").succeeds(); + new_ucmd!().arg("-Hh").succeeds(); +} + #[test] fn test_df_output() { let expected = if cfg!(target_os = "macos") { @@ -42,6 +48,22 @@ fn test_df_output() { assert_eq!(actual, expected); } +#[test] +fn test_df_output_overridden() { + let expected = if cfg!(target_os = "macos") { + "Filesystem Size Used Available Capacity Use% Mounted on " + } else { + "Filesystem Size Used Available Use% Mounted on " + }; + let output = new_ucmd!() + .arg("-hH") + .arg("--total") + .succeeds() + .stdout_move_str(); + let actual = output.lines().take(1).collect::>()[0]; + assert_eq!(actual, expected); +} + #[test] fn test_total_option_with_single_dash() { // These should fail because `-total` should have two dashes, From c9bf31f97e105659ee61804b3fd687bc7da2bfd8 Mon Sep 17 00:00:00 2001 From: gmnsii Date: Sun, 17 Apr 2022 01:52:05 -0700 Subject: [PATCH 2/2] Args override themselves and conflicting arguments --- src/uu/df/src/df.rs | 25 +++++++++++++++++++------ tests/by-util/test_df.rs | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index 3de605364..512941fb8 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -349,6 +349,7 @@ impl fmt::Display for DfError { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app().get_matches_from(args); + #[cfg(windows)] { if matches.is_present(OPT_INODES) { @@ -422,6 +423,7 @@ pub fn uu_app<'a>() -> Command<'a> { Arg::new(OPT_ALL) .short('a') .long("all") + .overrides_with(OPT_ALL) .help("include dummy file systems"), ) .arg( @@ -429,47 +431,56 @@ pub fn uu_app<'a>() -> Command<'a> { .short('B') .long("block-size") .takes_value(true) + .overrides_with_all(&[OPT_KILO, OPT_BLOCKSIZE]) .help( "scale sizes by SIZE before printing them; e.g.\ - '-BM' prints sizes in units of 1,048,576 bytes", + '-BM' prints sizes in units of 1,048,576 bytes", ), ) .arg( Arg::new(OPT_TOTAL) .long("total") + .overrides_with(OPT_TOTAL) .help("produce a grand total"), ) .arg( Arg::new(OPT_HUMAN_READABLE_BINARY) .short('h') .long("human-readable") - .overrides_with(OPT_HUMAN_READABLE_DECIMAL) + .overrides_with_all(&[OPT_HUMAN_READABLE_DECIMAL, OPT_HUMAN_READABLE_BINARY]) .help("print sizes in human readable format (e.g., 1K 234M 2G)"), ) .arg( Arg::new(OPT_HUMAN_READABLE_DECIMAL) .short('H') .long("si") - .overrides_with(OPT_HUMAN_READABLE_DECIMAL) + .overrides_with_all(&[OPT_HUMAN_READABLE_BINARY, OPT_HUMAN_READABLE_DECIMAL]) .help("likewise, but use powers of 1000 not 1024"), ) .arg( Arg::new(OPT_INODES) .short('i') .long("inodes") + .overrides_with(OPT_INODES) .help("list inode information instead of block usage"), ) - .arg(Arg::new(OPT_KILO).short('k').help("like --block-size=1K")) + .arg( + Arg::new(OPT_KILO) + .short('k') + .help("like --block-size=1K") + .overrides_with_all(&[OPT_BLOCKSIZE, OPT_KILO]), + ) .arg( Arg::new(OPT_LOCAL) .short('l') .long("local") + .overrides_with(OPT_LOCAL) .help("limit listing to local file systems"), ) .arg( Arg::new(OPT_NO_SYNC) .long("no-sync") - .conflicts_with(OPT_SYNC) + .overrides_with_all(&[OPT_SYNC, OPT_NO_SYNC]) .help("do not invoke sync before getting usage info (default)"), ) .arg( @@ -493,12 +504,13 @@ pub fn uu_app<'a>() -> Command<'a> { Arg::new(OPT_PORTABILITY) .short('P') .long("portability") + .overrides_with(OPT_PORTABILITY) .help("use the POSIX output format"), ) .arg( Arg::new(OPT_SYNC) .long("sync") - .conflicts_with(OPT_NO_SYNC) + .overrides_with_all(&[OPT_NO_SYNC, OPT_SYNC]) .help("invoke sync before getting usage info"), ) .arg( @@ -514,6 +526,7 @@ pub fn uu_app<'a>() -> Command<'a> { Arg::new(OPT_PRINT_TYPE) .short('T') .long("print-type") + .overrides_with(OPT_PRINT_TYPE) .help("print file system type"), ) .arg( diff --git a/tests/by-util/test_df.rs b/tests/by-util/test_df.rs index a76cf5fd7..dee5d327c 100644 --- a/tests/by-util/test_df.rs +++ b/tests/by-util/test_df.rs @@ -27,9 +27,41 @@ fn test_df_compatible_si() { } #[test] -fn test_df_overriding() { +fn test_df_arguments_override_themselves() { + new_ucmd!().args(&["--help", "--help"]).succeeds(); + new_ucmd!().arg("-aa").succeeds(); + new_ucmd!() + .args(&["--block-size=3000", "--block-size=1000"]) + .succeeds(); + new_ucmd!().args(&["--total", "--total"]).succeeds(); + new_ucmd!().arg("-hh").succeeds(); + new_ucmd!().arg("-HH").succeeds(); + new_ucmd!().arg("-ii").succeeds(); + new_ucmd!().arg("-kk").succeeds(); + new_ucmd!().arg("-ll").succeeds(); + new_ucmd!().args(&["--no-sync", "--no-sync"]).succeeds(); + new_ucmd!().arg("-PP").succeeds(); + new_ucmd!().args(&["--sync", "--sync"]).succeeds(); + new_ucmd!().arg("-TT").succeeds(); +} + +#[test] +fn test_df_conflicts_overriding() { new_ucmd!().arg("-hH").succeeds(); new_ucmd!().arg("-Hh").succeeds(); + new_ucmd!().args(&["--no-sync", "--sync"]).succeeds(); + new_ucmd!().args(&["--sync", "--no-sync"]).succeeds(); + new_ucmd!().args(&["-k", "--block-size=3000"]).succeeds(); + new_ucmd!().args(&["--block-size=3000", "-k"]).succeeds(); +} + +#[test] +fn test_df_output_arg() { + new_ucmd!().args(&["--output=source", "-iPT"]).fails(); + new_ucmd!().args(&["-iPT", "--output=source"]).fails(); + new_ucmd!() + .args(&["--output=source", "--output=source"]) + .fails(); } #[test]