From de02ebbef535cc997a295c920fcbe773f2285cf0 Mon Sep 17 00:00:00 2001 From: Ackerley Tng Date: Sun, 31 Jul 2022 10:38:56 -0700 Subject: [PATCH] dircolors: align TERM matching behavior with that of GNU dircolors --- src/uu/dircolors/src/dircolors.rs | 4 +-- tests/by-util/test_dircolors.rs | 41 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/uu/dircolors/src/dircolors.rs b/src/uu/dircolors/src/dircolors.rs index 6d9ec86266e..82633095386 100644 --- a/src/uu/dircolors/src/dircolors.rs +++ b/src/uu/dircolors/src/dircolors.rs @@ -265,7 +265,7 @@ impl StrUtils for str { } fn fnmatch(&self, pat: &str) -> bool { - pat.parse::().unwrap().matches(self) + parse_glob::from_str(pat).unwrap().matches(self) } } @@ -278,7 +278,7 @@ enum ParseState { } use std::collections::HashMap; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::{format_usage, parse_glob, InvalidEncodingHandling}; fn parse(lines: T, fmt: &OutputFmt, fp: &str) -> Result where diff --git a/tests/by-util/test_dircolors.rs b/tests/by-util/test_dircolors.rs index 2a93cbfa9c5..74c13462a03 100644 --- a/tests/by-util/test_dircolors.rs +++ b/tests/by-util/test_dircolors.rs @@ -160,6 +160,47 @@ fn test_extra_operand() { .no_stdout(); } +#[test] +fn test_term_matching() { + fn check(term_pattern: &str, term: &str, expectation: &str) { + let theme = format!( + " +TERM {term_pattern} + +.term_matching 00;38;5;61 +" + ); + + new_ucmd!() + .env("TERM", term) + .pipe_in(theme) + .args(&["-b", "-"]) + .succeeds() + .stdout_is(expectation) + .no_stderr(); + } + + let expectation_if_match = r#" +LS_COLORS='*.term_matching=00;38;5;61:'; +export LS_COLORS +"# + .trim_start(); + let expectation_if_no_match = r#" +LS_COLORS=''; +export LS_COLORS +"# + .trim_start(); + + // sanity checks + check("matches", "matches", expectation_if_match); + check("matches", "no_match", expectation_if_no_match); + // character set negation should treat ^ like ! + check("[!a]_negation", "a_negation", expectation_if_no_match); + check("[!a]_negation", "b_negation", expectation_if_match); + check("[^a]_negation", "a_negation", expectation_if_no_match); + check("[^a]_negation", "b_negation", expectation_if_match); +} + fn test_helper(file_name: &str, term: &str) { new_ucmd!() .env("TERM", term)