From f1b38e94cd059ecdd4c0bd19191ab8f06071c177 Mon Sep 17 00:00:00 2001 From: Jan Scheer Date: Fri, 3 Jun 2022 02:08:09 +0200 Subject: [PATCH 1/2] dircolors: add support for stdin "-" (fix: #3589) --- src/uu/dircolors/src/dircolors.rs | 14 ++++++++------ tests/by-util/test_dircolors.rs | 10 ++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/uu/dircolors/src/dircolors.rs b/src/uu/dircolors/src/dircolors.rs index 6bc96adbf..4062b0b7a 100644 --- a/src/uu/dircolors/src/dircolors.rs +++ b/src/uu/dircolors/src/dircolors.rs @@ -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( + 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); diff --git a/tests/by-util/test_dircolors.rs b/tests/by-util/test_dircolors.rs index a4ad0df32..8ddbac9f4 100644 --- a/tests/by-util/test_dircolors.rs +++ b/tests/by-util/test_dircolors.rs @@ -131,6 +131,16 @@ 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(); +} + fn test_helper(file_name: &str, term: &str) { new_ucmd!() .env("TERM", term) From 18bfc03008dc1e4257f9f291c1e40e3a7195cd99 Mon Sep 17 00:00:00 2001 From: Jan Scheer Date: Mon, 6 Jun 2022 00:20:39 +0200 Subject: [PATCH 2/2] test_dircolors: add test_extra_operand --- tests/by-util/test_dircolors.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/by-util/test_dircolors.rs b/tests/by-util/test_dircolors.rs index 8ddbac9f4..f34f5573b 100644 --- a/tests/by-util/test_dircolors.rs +++ b/tests/by-util/test_dircolors.rs @@ -141,6 +141,15 @@ fn test_stdin() { .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)