From a5477960a577bc27d3db01cc0b9a65c251ee38b0 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Mon, 21 Mar 2022 16:45:30 +0100 Subject: [PATCH] df: show error if specified type doesn't exist Fixes #3252. As a side effect, "df -H -total" will now fail, too, as expected. --- src/uu/df/src/df.rs | 27 ++++++++++++++++++--------- tests/by-util/test_df.rs | 19 ++++++++++++++++++- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index 552ae1387..a7520117d 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -358,22 +358,31 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } let opt = Options::from(&matches).map_err(DfError::OptionsError)?; - // Get the list of filesystems to display in the output table. let filesystems: Vec = match matches.values_of(OPT_PATHS) { - None => get_all_filesystems(&opt), + None => { + let filesystems = get_all_filesystems(&opt); + + if filesystems.is_empty() { + return Err(USimpleError::new(1, "No file systems processed")); + } + + filesystems + } Some(paths) => { let paths: Vec<&str> = paths.collect(); - get_named_filesystems(&paths) + let filesystems = get_named_filesystems(&paths); + + // This can happen if paths are given as command-line arguments + // but none of the paths exist. + if filesystems.is_empty() { + return Ok(()); + } + + filesystems } }; - // This can happen if paths are given as command-line arguments - // but none of the paths exist. - if filesystems.is_empty() { - return Ok(()); - } - // The running total of filesystem sizes and usage. // // This accumulator is computed in case we need to display the diff --git a/tests/by-util/test_df.rs b/tests/by-util/test_df.rs index 1a31109a3..d91f1ac36 100644 --- a/tests/by-util/test_df.rs +++ b/tests/by-util/test_df.rs @@ -42,6 +42,13 @@ fn test_df_output() { assert_eq!(actual, expected); } +#[test] +fn test_total_option_with_single_dash() { + // These should fail because `-total` should have two dashes, + // not just one. + new_ucmd!().arg("-total").fails(); +} + /// Test that the order of rows in the table does not change across executions. #[test] fn test_order_same() { @@ -89,7 +96,17 @@ fn test_output_option_without_equals_sign() { #[test] fn test_type_option() { - new_ucmd!().args(&["-t", "ext4", "-t", "ext3"]).succeeds(); + let fs_types = new_ucmd!() + .arg("--output=fstype") + .succeeds() + .stdout_move_str(); + let fs_type = fs_types.lines().nth(1).unwrap().trim(); + + new_ucmd!().args(&["-t", fs_type]).succeeds(); + new_ucmd!() + .args(&["-t", fs_type, "-t", "nonexisting"]) + .succeeds(); + new_ucmd!().args(&["-t", "nonexisting"]).fails(); } #[test]