From c5413167e25a8865d52b5edf5d24e8944ed8b89c Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Sat, 9 Apr 2022 12:40:52 -0400 Subject: [PATCH 1/2] df: correct --total argument used in unit test Add a missing dash to the `--total` argument applied in the `test_df_output` test case. Before this commit, the argument `-total` was treated as a path argument. After this commit, `--total` is treated as a command-line option that causes the total file usage to be displayed. --- tests/by-util/test_df.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/tests/by-util/test_df.rs b/tests/by-util/test_df.rs index fa77a8096..a88212146 100644 --- a/tests/by-util/test_df.rs +++ b/tests/by-util/test_df.rs @@ -28,16 +28,18 @@ fn test_df_compatible_si() { #[test] fn test_df_output() { - // TODO These should fail because `-total` should have two dashes, - // not just one. But they don't fail. - if cfg!(target_os = "macos") { - new_ucmd!().arg("-H").arg("-total").succeeds(). - stdout_only("Filesystem Size Used Available Capacity Use% Mounted on \n"); + let expected = if cfg!(target_os = "macos") { + "Filesystem Size Used Available Capacity Use% Mounted on " } else { - new_ucmd!().arg("-H").arg("-total").succeeds().stdout_only( - "Filesystem Size Used Available Use% Mounted on \n", - ); - } + "Filesystem Size Used Available Use% Mounted on " + }; + let output = new_ucmd!() + .arg("-H") + .arg("--total") + .succeeds() + .stdout_move_str(); + let actual = output.lines().take(1).collect::>()[0]; + assert_eq!(actual, expected); } /// Test that the order of rows in the table does not change across executions. From 460bd6705069f6051137236d87b3bff957ac4325 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Sat, 9 Apr 2022 00:17:32 -0400 Subject: [PATCH 2/2] df: show error when file argument does not exist For example: $ df not-a-file df: not-a-file: No such file or directory Fixes #3373. --- src/uu/df/src/df.rs | 25 +++++++++++++++++++------ tests/by-util/test_df.rs | 13 +++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index 97f1a2e6e..552ae1387 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -12,9 +12,9 @@ mod filesystem; mod table; use uucore::display::Quotable; -use uucore::error::{UError, UResult}; -use uucore::format_usage; +use uucore::error::{UError, UResult, USimpleError}; use uucore::fsext::{read_fs_list, MountInfo}; +use uucore::{format_usage, show}; use clap::{crate_version, Arg, ArgMatches, Command}; @@ -311,10 +311,17 @@ where // Convert each path into a `Filesystem`, which contains // both the mount information and usage information. - paths - .iter() - .filter_map(|p| Filesystem::from_path(&mounts, p)) - .collect() + let mut result = vec![]; + for path in paths { + match Filesystem::from_path(&mounts, path) { + Some(fs) => result.push(fs), + None => show!(USimpleError::new( + 1, + format!("{}: No such file or directory", path.as_ref().display()) + )), + } + } + result } #[derive(Debug)] @@ -361,6 +368,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } }; + // 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 a88212146..aba9f69f0 100644 --- a/tests/by-util/test_df.rs +++ b/tests/by-util/test_df.rs @@ -297,3 +297,16 @@ fn test_output_field_no_more_than_once() { .fails() .usage_error("option --output: field 'target' used more than once"); } + +#[test] +fn test_nonexistent_file() { + new_ucmd!() + .arg("does-not-exist") + .fails() + .stderr_only("df: does-not-exist: No such file or directory"); + new_ucmd!() + .args(&["--output=file", "does-not-exist", "."]) + .fails() + .stderr_is("df: does-not-exist: No such file or directory\n") + .stdout_is("File \n. \n"); +}