Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 13 additions & 5 deletions src/uu/ptx/src/ptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ impl Default for Config {
}
}

/// The name of the file given to `option`, for error messages.
fn filter_file_name(matches: &clap::ArgMatches, option: &str) -> String {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read_word_filter_file/read_char_filter_file already have filename, could we do the map_err_context there and drop this helper?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, the helper is gone and the readers add the context themselves.

matches
.get_one::<OsString>(option)
.map(|name| name.maybe_quote().to_string())
.unwrap_or_default()
}

fn read_word_filter_file(
matches: &clap::ArgMatches,
option: &str,
Expand Down Expand Up @@ -129,24 +137,24 @@ impl WordFilter {
#[allow(clippy::cognitive_complexity)]
fn new(matches: &clap::ArgMatches, config: &Config) -> UResult<Self> {
let (o, oset): (bool, FxHashSet<String>) = if matches.contains_id(options::ONLY_FILE) {
let words =
read_word_filter_file(matches, options::ONLY_FILE).map_err_context(String::new)?;
let words = read_word_filter_file(matches, options::ONLY_FILE)
.map_err_context(|| filter_file_name(matches, options::ONLY_FILE))?;
(true, words)
} else {
(false, FxHashSet::default())
};
let (i, iset): (bool, FxHashSet<String>) = if matches.contains_id(options::IGNORE_FILE) {
let words = read_word_filter_file(matches, options::IGNORE_FILE)
.map_err_context(String::new)?;
.map_err_context(|| filter_file_name(matches, options::IGNORE_FILE))?;
(true, words)
} else {
(false, FxHashSet::default())
};
let break_set: Option<FxHashSet<char>> = if matches.contains_id(options::BREAK_FILE)
&& !matches.contains_id(options::WORD_REGEXP)
{
let mut chars =
read_char_filter_file(matches, options::BREAK_FILE).map_err_context(String::new)?;
let mut chars = read_char_filter_file(matches, options::BREAK_FILE)
.map_err_context(|| filter_file_name(matches, options::BREAK_FILE))?;
if !config.gnu_ext {
// GNU off means at least these are considered
chars.extend([' ', '\t', '\n']);
Expand Down
13 changes: 13 additions & 0 deletions tests/by-util/test_ptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,16 @@ fn test_nullable_word_regexp_no_empty_matches() {
.succeeds()
.stdout_only(expected);
}

// A missing break, ignore or only file is named in the error, as in GNU.
#[test]
fn test_missing_filter_file_is_named_in_error() {
for option in ["-b", "-i", "-o"] {
new_ucmd!()
.args(&[option, "nonexistent"])
.pipe_in("x\n")
.fails_with_code(1)
.no_stdout()
.stderr_contains("ptx: nonexistent: ");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be stderr_is("ptx: nonexistent: No such file or directory\n"), no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, changed.

}
}
Loading