1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 20:17:45 +00:00

Merge pull request #3376 from jfinkels/df-error-on-nonexistent-files

df: show error when file argument does not exist
This commit is contained in:
Sylvestre Ledru 2022-04-12 09:27:59 +02:00 committed by GitHub
commit f114d63b4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 15 deletions

View file

@ -12,9 +12,9 @@ mod filesystem;
mod table; mod table;
use uucore::display::Quotable; use uucore::display::Quotable;
use uucore::error::{UError, UResult}; use uucore::error::{UError, UResult, USimpleError};
use uucore::format_usage;
use uucore::fsext::{read_fs_list, MountInfo}; use uucore::fsext::{read_fs_list, MountInfo};
use uucore::{format_usage, show};
use clap::{crate_version, Arg, ArgMatches, Command}; use clap::{crate_version, Arg, ArgMatches, Command};
@ -311,10 +311,17 @@ where
// Convert each path into a `Filesystem`, which contains // Convert each path into a `Filesystem`, which contains
// both the mount information and usage information. // both the mount information and usage information.
paths let mut result = vec![];
.iter() for path in paths {
.filter_map(|p| Filesystem::from_path(&mounts, p)) match Filesystem::from_path(&mounts, path) {
.collect() Some(fs) => result.push(fs),
None => show!(USimpleError::new(
1,
format!("{}: No such file or directory", path.as_ref().display())
)),
}
}
result
} }
#[derive(Debug)] #[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. // The running total of filesystem sizes and usage.
// //
// This accumulator is computed in case we need to display the // This accumulator is computed in case we need to display the

View file

@ -28,16 +28,18 @@ fn test_df_compatible_si() {
#[test] #[test]
fn test_df_output() { fn test_df_output() {
// TODO These should fail because `-total` should have two dashes, let expected = if cfg!(target_os = "macos") {
// not just one. But they don't fail. "Filesystem Size Used Available Capacity Use% Mounted on "
if cfg!(target_os = "macos") {
new_ucmd!().arg("-H").arg("-total").succeeds().
stdout_only("Filesystem Size Used Available Capacity Use% Mounted on \n");
} else { } else {
new_ucmd!().arg("-H").arg("-total").succeeds().stdout_only( "Filesystem Size Used Available Use% Mounted on "
"Filesystem Size Used Available Use% Mounted on \n", };
); let output = new_ucmd!()
} .arg("-H")
.arg("--total")
.succeeds()
.stdout_move_str();
let actual = output.lines().take(1).collect::<Vec<&str>>()[0];
assert_eq!(actual, expected);
} }
/// Test that the order of rows in the table does not change across executions. /// Test that the order of rows in the table does not change across executions.
@ -295,3 +297,16 @@ fn test_output_field_no_more_than_once() {
.fails() .fails()
.usage_error("option --output: field 'target' used more than once"); .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");
}