Skip to content

Commit

Permalink
Merge pull request #6854 from sylvestre/fmt
Browse files Browse the repository at this point in the history
fmt: generate an error if the input is a directory
  • Loading branch information
cakebaker authored Nov 18, 2024
2 parents bf05a3d + ab77eae commit 19a19f0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/uu/fmt/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ fn process_file(
_ => {
let f = File::open(file_name)
.map_err_context(|| format!("cannot open {} for reading", file_name.quote()))?;
if f.metadata()
.map_err_context(|| format!("cannot get metadata for {}", file_name.quote()))?
.is_dir()
{
return Err(USimpleError::new(1, "read error".to_string()));
}

Box::new(f) as Box<dyn Read + 'static>
}
});
Expand Down
5 changes: 5 additions & 0 deletions tests/by-util/test_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}

#[test]
fn test_invalid_input() {
new_ucmd!().arg(".").fails().code_is(1);
}

#[test]
fn test_fmt() {
new_ucmd!()
Expand Down

0 comments on commit 19a19f0

Please sign in to comment.