Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/uu/fmt/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fmt-error-invalid-tabwidth = Invalid TABWIDTH specification: {$tabwidth}
fmt-error-first-option-width = invalid option -- {$option}; -WIDTH is recognized only when it is the first
option; use -w N instead
Try 'fmt --help' for more information.
fmt-error-read = read error
fmt-error-reading-is-directory = error reading {$file}: Is a directory
fmt-error-invalid-width-malformed = invalid width: {$width}
fmt-error-cannot-open-for-reading = cannot open {$file} for reading
fmt-error-cannot-get-metadata = cannot get metadata for {$file}
Expand Down
2 changes: 1 addition & 1 deletion src/uu/fmt/locales/fr-FR.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fmt-error-invalid-tabwidth = Spécification TABWIDTH invalide : {$tabwidth}
fmt-error-first-option-width = option invalide -- {$option} ; -WIDTH n'est reconnu que lorsqu'il est la première
option ; utilisez -w N à la place
Essayez 'fmt --help' pour plus d'informations.
fmt-error-read = erreur de lecture
fmt-error-reading-is-directory = erreur de lecture de {$file} : C'est un répertoire
fmt-error-invalid-width-malformed = largeur invalide : {$width}
fmt-error-cannot-open-for-reading = impossible d'ouvrir {$file} en lecture
fmt-error-cannot-get-metadata = impossible d'obtenir les métadonnées pour {$file}
Expand Down
38 changes: 26 additions & 12 deletions src/uu/fmt/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::io::{BufReader, BufWriter, Read, Stdout, Write, stdin, stdout};
use std::path::Path;
use uucore::display::Quotable;
use uucore::error::{FromIo, UResult, USimpleError};
use uucore::show;
use uucore::translate;

use uucore::format_usage;
Expand All @@ -36,8 +37,6 @@ enum FmtError {
InvalidTabWidth(String),
#[error("{}", translate!("fmt-error-first-option-width", "option" => .0))]
FirstOptionWidth(char),
#[error("{}", translate!("fmt-error-read"))]
ReadError,
#[error("{}", translate!("fmt-error-invalid-width-malformed", "width" => .0.quote()))]
InvalidWidthMalformed(String),
}
Expand Down Expand Up @@ -210,16 +209,31 @@ fn process_file(
Box::new(stdin()) as Box<dyn Read + 'static>
} else {
let path = Path::new(file_name);
let f = File::open(path).map_err_context(
|| translate!("fmt-error-cannot-open-for-reading", "file" => path.quote()),
)?;
if f.metadata()
.map_err_context(
|| translate!("fmt-error-cannot-get-metadata", "file" => path.quote()),
)?
.is_dir()
{
return Err(FmtError::ReadError.into());
// Like GNU fmt, report a file that can't be read and go on with the next one.
let f = match File::open(path) {
Ok(f) => f,
Err(e) => {
show!(e.map_err_context(
|| translate!("fmt-error-cannot-open-for-reading", "file" => path.quote())
));
return Ok(());
}
};
match f.metadata() {
Ok(m) if m.is_dir() => {
show!(USimpleError::new(
1,
translate!("fmt-error-reading-is-directory", "file" => path.quote())
));
return Ok(());
}
Ok(_) => {}
Err(e) => {
show!(e.map_err_context(
|| translate!("fmt-error-cannot-get-metadata", "file" => path.quote())
));
return Ok(());
}
}

Box::new(f) as Box<dyn Read + 'static>
Expand Down
39 changes: 38 additions & 1 deletion tests/by-util/test_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#[cfg(target_os = "linux")]
use std::os::unix::ffi::OsStringExt;
use uutests::new_ucmd;
use uutests::{at_and_ucmd, new_ucmd};

#[test]
fn test_invalid_arg() {
Expand Down Expand Up @@ -253,6 +253,43 @@ fn test_fmt_non_existent_file() {
.stderr_is("fmt: cannot open 'non-existing' for reading: No such file or directory\n");
}

// Windows refuses to open a directory as a file, so the error differs there.
#[cfg(not(windows))]
#[test]
fn test_fmt_directory() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("dir");
ucmd.arg("dir")
.fails_with_code(1)
.no_stdout()
.stderr_is("fmt: error reading 'dir': Is a directory\n");
}

#[cfg(not(windows))]
#[test]
fn test_fmt_continues_after_directory() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("dir");
at.write("a.txt", "one\ntwo\n");
ucmd.args(&["dir", "non-existing", "a.txt"])
.fails_with_code(1)
.stdout_is("one two\n")
.stderr_is(
"fmt: error reading 'dir': Is a directory\n\
fmt: cannot open 'non-existing' for reading: No such file or directory\n",
);
}

#[test]
fn test_fmt_continues_after_missing_file() {
let (at, mut ucmd) = at_and_ucmd!();
at.write("a.txt", "one\ntwo\n");
ucmd.args(&["non-existing", "a.txt"])
.fails_with_code(1)
.stdout_is("one two\n")
.stderr_is("fmt: cannot open 'non-existing' for reading: No such file or directory\n");
}

#[test]
fn test_fmt_invalid_goal() {
for param in ["-g", "--goal"] {
Expand Down
Loading