Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dircolors: add support for stdin "-" (fix: #3589) #3591

Merged
merged 2 commits into from
Jun 6, 2022
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
14 changes: 8 additions & 6 deletions src/uu/dircolors/src/dircolors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let result;
if files.is_empty() {
result = parse(INTERNAL_DB.lines(), &out_format, "");
} else if files.len() > 1 {
return Err(UUsageError::new(
sylvestre marked this conversation as resolved.
Show resolved Hide resolved
1,
format!("extra operand {}", files[1].quote()),
));
} else if files[0].eq("-") {
let fin = BufReader::new(std::io::stdin());
result = parse(fin.lines().filter_map(Result::ok), &out_format, files[0]);
} else {
if files.len() > 1 {
return Err(UUsageError::new(
1,
format!("extra operand {}", files[1].quote()),
));
}
match File::open(files[0]) {
Ok(f) => {
let fin = BufReader::new(f);
Expand Down
19 changes: 19 additions & 0 deletions tests/by-util/test_dircolors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,25 @@ fn test_exclusive_option() {
.stderr_contains("mutually exclusive");
}

#[test]
fn test_stdin() {
new_ucmd!()
.pipe_in("owt 40;33\n")
.args(&["-b", "-"])
.succeeds()
.stdout_is("LS_COLORS='tw=40;33:';\nexport LS_COLORS\n")
.no_stderr();
}

#[test]
fn test_extra_operand() {
new_ucmd!()
.args(&["-c", "file1", "file2"])
.fails()
.stderr_contains("dircolors: extra operand 'file2'\n")
.no_stdout();
}

fn test_helper(file_name: &str, term: &str) {
new_ucmd!()
.env("TERM", term)
Expand Down