mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
wc: correct some error messages for invalid inputs
Change the error messages that get printed to `stderr` for compatibility with GNU `wc` when an input is a directory and when an input does not exist. Fixes #2211.
This commit is contained in:
parent
204b051711
commit
e8d911d9d5
2 changed files with 48 additions and 2 deletions
|
@ -22,7 +22,7 @@ use thiserror::Error;
|
||||||
|
|
||||||
use std::cmp::max;
|
use std::cmp::max;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, Write};
|
use std::io::{self, ErrorKind, Write};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
|
@ -254,6 +254,29 @@ fn word_count_from_input(input: &Input, settings: &Settings) -> WcResult<WordCou
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Print a message appropriate for the particular error to `stderr`.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// This will print `wc: /tmp: Is a directory` to `stderr`.
|
||||||
|
///
|
||||||
|
/// ```rust,ignore
|
||||||
|
/// show_error(Input::Path("/tmp"), WcError::IsDirectory("/tmp"))
|
||||||
|
/// ```
|
||||||
|
fn show_error(input: &Input, err: WcError) {
|
||||||
|
match (input, err) {
|
||||||
|
(_, WcError::IsDirectory(path)) => {
|
||||||
|
show_error_custom_description!(path, "Is a directory");
|
||||||
|
}
|
||||||
|
(Input::Path(path), WcError::Io(e)) if e.kind() == ErrorKind::NotFound => {
|
||||||
|
show_error_custom_description!(path, "No such file or directory");
|
||||||
|
}
|
||||||
|
(_, e) => {
|
||||||
|
show_error!("{}", e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
fn wc(inputs: Vec<Input>, settings: &Settings) -> Result<(), u32> {
|
fn wc(inputs: Vec<Input>, settings: &Settings) -> Result<(), u32> {
|
||||||
let mut total_word_count = WordCount::default();
|
let mut total_word_count = WordCount::default();
|
||||||
let mut results = vec![];
|
let mut results = vec![];
|
||||||
|
@ -264,7 +287,7 @@ fn wc(inputs: Vec<Input>, settings: &Settings) -> Result<(), u32> {
|
||||||
|
|
||||||
for input in &inputs {
|
for input in &inputs {
|
||||||
let word_count = word_count_from_input(&input, settings).unwrap_or_else(|err| {
|
let word_count = word_count_from_input(&input, settings).unwrap_or_else(|err| {
|
||||||
show_error!("{}", err);
|
show_error(&input, err);
|
||||||
error_count += 1;
|
error_count += 1;
|
||||||
WordCount::default()
|
WordCount::default()
|
||||||
});
|
});
|
||||||
|
|
|
@ -168,3 +168,26 @@ fn test_file_one_long_word() {
|
||||||
.run()
|
.run()
|
||||||
.stdout_is(" 1 1 10001 10001 10000 onelongword.txt\n");
|
.stdout_is(" 1 1 10001 10001 10000 onelongword.txt\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Test that getting counts from a directory is an error.
|
||||||
|
#[test]
|
||||||
|
fn test_read_from_directory_error() {
|
||||||
|
// TODO To match GNU `wc`, the `stdout` should be:
|
||||||
|
//
|
||||||
|
// " 0 0 0 .\n"
|
||||||
|
//
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["."])
|
||||||
|
.fails()
|
||||||
|
.stderr_contains(".: Is a directory\n")
|
||||||
|
.stdout_is("0 0 0 .\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test that getting counts from nonexistent file is an error.
|
||||||
|
#[test]
|
||||||
|
fn test_read_from_nonexistent_file() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["bogusfile"])
|
||||||
|
.fails()
|
||||||
|
.stderr_contains("bogusfile: No such file or directory\n");
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue