Debian Codegraph @

tests/by-util/test_dircolors.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 // spell-checker:ignore overridable colorterm
7
8 #[cfg(target_os = "linux")]
9 use uutests::at_and_ucmd;
10 use uutests::new_ucmd;
11
12 #[test]
13 #[cfg(target_os = "linux")]
14 fn test_dircolors_non_utf8_paths() {
15 use std::os::unix::ffi::OsStringExt;
16 let (at, mut ucmd) = at_and_ucmd!();
17
18 let filename = std::ffi::OsString::from_vec(vec![0xFF, 0xFE]);
19 std::fs::write(at.plus(&filename), b"NORMAL 00\n*.txt 32\n").unwrap();
20
21 ucmd.env("SHELL", "bash")
22 .arg(&filename)
23 .succeeds()
24 .stdout_contains("LS_COLORS=")
25 .stdout_contains("*.txt=32");
26 }
27
28 #[test]
29 fn test_invalid_arg() {
30 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
31 }
32
33 #[test]
34 fn test1() {
35 test_helper("test1", "gnome");
36 }
37
38 #[test]
39 fn test_keywords() {
40 test_helper("keywords", "");
41 }
42
43 #[test]
44 fn test_internal_db() {
45 new_ucmd!()
46 .arg("-p")
47 .succeeds()
48 .stdout_is_fixture("internal.expected");
49 }
50
51 #[test]
52 fn test_ls_colors() {
53 new_ucmd!()
54 .arg("--print-ls-colors")
55 .succeeds()
56 .stdout_is_fixture("ls_colors.expected");
57 }
58
59 #[test]
60 fn test_bash_default() {
61 new_ucmd!()
62 .env("TERM", "screen")
63 .arg("-b")
64 .succeeds()
65 .stdout_is_fixture("bash_def.expected");
66 }
67
68 #[test]
69 fn test_csh_default() {
70 new_ucmd!()
71 .env("TERM", "screen")
72 .arg("-c")
73 .succeeds()
74 .stdout_is_fixture("csh_def.expected");
75 }
76 #[test]
77 fn test_overridable_args() {
78 new_ucmd!()
79 .env("TERM", "screen")
80 .arg("-bc")
81 .succeeds()
82 .stdout_is_fixture("csh_def.expected");
83 new_ucmd!()
84 .env("TERM", "screen")
85 .arg("-cb")
86 .succeeds()
87 .stdout_is_fixture("bash_def.expected");
88 }
89
90 #[test]
91 fn test_no_env() {
92 // no SHELL and TERM
93 new_ucmd!()
94 .fails()
95 .stderr_only("dircolors: no SHELL environment variable, and no shell type option given\n");
96 }
97
98 #[test]
99 fn test_exclusive_option() {
100 new_ucmd!()
101 .arg("-bp")
102 .fails()
103 .stderr_contains("mutually exclusive");
104 new_ucmd!()
105 .arg("-cp")
106 .fails()
107 .stderr_contains("mutually exclusive");
108 new_ucmd!()
109 .args(&["-b", "--print-ls-colors"])
110 .fails()
111 .stderr_contains("mutually exclusive");
112 new_ucmd!()
113 .args(&["-c", "--print-ls-colors"])
114 .fails()
115 .stderr_contains("mutually exclusive");
116 new_ucmd!()
117 .args(&["-p", "--print-ls-colors"])
118 .fails()
119 .stderr_contains("mutually exclusive");
120 }
121
122 #[test]
123 fn test_stdin() {
124 new_ucmd!()
125 .pipe_in("owt 40;33\n")
126 .args(&["-b", "-"])
127 .succeeds()
128 .stdout_only("LS_COLORS='tw=40;33:';\nexport LS_COLORS\n");
129 }
130
131 #[test]
132 fn test_quoting() {
133 new_ucmd!()
134 .pipe_in("exec 'echo Hello;:'\n")
135 .args(&["-b", "-"])
136 .succeeds()
137 .stdout_only("LS_COLORS='ex='\\''echo Hello;\\:'\\'':';\nexport LS_COLORS\n");
138 }
139
140 /*
141 #[test]
142 fn test_print_ls_colors() {
143 new_ucmd!()
144 .pipe_in("OWT 40;33\n")
145 .args(&["--print-ls-colors"])
146 .succeeds()
147 .stdout_is("\x1B[40;33mtw\t40;33\x1B[0m\n")
148 .no_stderr();
149 }
150 */
151
152 #[test]
153 fn test_extra_operand() {
154 new_ucmd!()
155 .args(&["-c", "file1", "file2"])
156 .fails()
157 .stderr_contains("dircolors: extra operand 'file2'\n")
158 .no_stdout();
159 }
160
161 #[test]
162 fn test_term_matching() {
163 fn check(term_pattern: &str, term: &str, expectation: &str) {
164 let theme = format!(
165 "
166 TERM {term_pattern}
167
168 .term_matching 00;38;5;61
169 "
170 );
171
172 new_ucmd!()
173 .env("TERM", term)
174 .pipe_in(theme)
175 .args(&["-b", "-"])
176 .succeeds()
177 .stdout_only(expectation);
178 }
179
180 let expectation_if_match = r"
181 LS_COLORS='*.term_matching=00;38;5;61:';
182 export LS_COLORS
183 "
184 .trim_start();
185 let expectation_if_no_match = r"
186 LS_COLORS='';
187 export LS_COLORS
188 "
189 .trim_start();
190
191 // sanity checks
192 check("matches", "matches", expectation_if_match);
193 check("matches", "no_match", expectation_if_no_match);
194 // character set negation should treat ^ like !
195 check("[!a]_negation", "a_negation", expectation_if_no_match);
196 check("[!a]_negation", "b_negation", expectation_if_match);
197 check("[^a]_negation", "a_negation", expectation_if_no_match);
198 check("[^a]_negation", "b_negation", expectation_if_match);
199 }
200
201 fn test_helper(file_name: &str, term: &str) {
202 new_ucmd!()
203 .env("TERM", term)
204 .arg("-c")
205 .arg(format!("{file_name}.txt"))
206 .succeeds()
207 .stdout_is_fixture(format!("{file_name}.csh.expected"));
208
209 new_ucmd!()
210 .env("TERM", term)
211 .arg("-b")
212 .arg(format!("{file_name}.txt"))
213 .succeeds()
214 .stdout_is_fixture(format!("{file_name}.sh.expected"));
215 }
216
217 #[test]
218 fn test_dircolors_for_dir_as_file() {
219 let result = new_ucmd!().args(&["-c", "/"]).fails();
220 result.no_stdout();
221 assert_eq!(
222 result.stderr_str().trim(),
223 "dircolors: expected file, got directory '/'",
224 );
225 }
226
227 #[test]
228 fn test_repeated() {
229 for arg in ["-b", "-c", "--print-database", "--print-ls-colors"] {
230 new_ucmd!().arg(arg).arg(arg).succeeds().no_stderr();
231 }
232 }
233
234 #[test]
235 fn test_colorterm_empty_with_wildcard() {
236 new_ucmd!()
237 .env("COLORTERM", "")
238 .pipe_in("COLORTERM ?*\nowt 40;33\n")
239 .args(&["-b", "-"])
240 .succeeds()
241 .stdout_only("LS_COLORS='';\nexport LS_COLORS\n");
242 }
243
244 #[test]
245 fn test_invalid_term_glob() {
246 new_ucmd!()
247 .pipe_in("TERM [\nDIR 01;34\n")
248 .args(&["-b", "-"])
249 .succeeds()
250 .stdout_only("LS_COLORS='';\nexport LS_COLORS\n");
251 }