mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge pull request #2212 from jfinkels/2211-wc-error-messages
wc: correct some error messages for invalid inputs
This commit is contained in:
commit
46cf61b5b7
2 changed files with 48 additions and 2 deletions
|
@ -22,7 +22,7 @@ use thiserror::Error;
|
|||
|
||||
use std::cmp::max;
|
||||
use std::fs::File;
|
||||
use std::io::{self, Write};
|
||||
use std::io::{self, ErrorKind, Write};
|
||||
use std::path::Path;
|
||||
|
||||
#[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> {
|
||||
let mut total_word_count = WordCount::default();
|
||||
let mut results = vec![];
|
||||
|
@ -264,7 +287,7 @@ fn wc(inputs: Vec<Input>, settings: &Settings) -> Result<(), u32> {
|
|||
|
||||
for input in &inputs {
|
||||
let word_count = word_count_from_input(&input, settings).unwrap_or_else(|err| {
|
||||
show_error!("{}", err);
|
||||
show_error(&input, err);
|
||||
error_count += 1;
|
||||
WordCount::default()
|
||||
});
|
||||
|
|
|
@ -168,3 +168,26 @@ fn test_file_one_long_word() {
|
|||
.run()
|
||||
.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