Debian Codegraph @

tests/by-util/test_unexpand.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 //
7 // spell-checker:ignore contenta edgecase behaviour tcaf
8
9 use uutests::{at_and_ucmd, new_ucmd};
10
11 #[test]
12 fn test_invalid_arg() {
13 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
14 }
15
16 #[test]
17 fn unexpand_init_0() {
18 new_ucmd!()
19 .args(&["-t4"])
20 .pipe_in(" 1\n 2\n 3\n 4\n")
21 .succeeds()
22 .stdout_is(" 1\n 2\n 3\n\t4\n");
23 }
24
25 #[test]
26 fn unexpand_init_1() {
27 new_ucmd!()
28 .args(&["-t4"])
29 .pipe_in(" 5\n 6\n 7\n 8\n")
30 .succeeds()
31 .stdout_is("\t 5\n\t 6\n\t 7\n\t\t8\n");
32 }
33
34 #[test]
35 fn unexpand_init_list_0() {
36 new_ucmd!()
37 .args(&["-t2,4"])
38 .pipe_in(" 1\n 2\n 3\n 4\n")
39 .succeeds()
40 .stdout_is(" 1\n\t2\n\t 3\n\t\t4\n");
41 }
42
43 #[test]
44 fn unexpand_init_list_1() {
45 // Once the list is exhausted, spaces are not converted anymore
46 new_ucmd!()
47 .args(&["-t2,4"])
48 .pipe_in(" 5\n 6\n 7\n 8\n")
49 .succeeds()
50 .stdout_is("\t\t 5\n\t\t 6\n\t\t 7\n\t\t 8\n");
51 }
52
53 #[test]
54 fn unexpand_flag_a_0() {
55 new_ucmd!()
56 .args(&["--"])
57 .pipe_in("e E\nf F\ng G\nh H\n")
58 .succeeds()
59 .stdout_is("e E\nf F\ng G\nh H\n");
60 }
61
62 #[test]
63 fn unexpand_flag_a_1() {
64 new_ucmd!()
65 .args(&["-a"])
66 .pipe_in("e E\nf F\ng G\nh H\n")
67 .succeeds()
68 .stdout_is("e E\nf F\ng\tG\nh\t H\n");
69 }
70
71 #[test]
72 fn unexpand_flag_a_2() {
73 new_ucmd!()
74 .args(&["-t8"])
75 .pipe_in("e E\nf F\ng G\nh H\n")
76 .succeeds()
77 .stdout_is("e E\nf F\ng\tG\nh\t H\n");
78 }
79
80 #[test]
81 fn unexpand_first_only_0() {
82 new_ucmd!()
83 .args(&["-t3"])
84 .pipe_in(" A B")
85 .succeeds()
86 .stdout_is("\t\t A\t B");
87 }
88
89 #[test]
90 fn unexpand_first_only_1() {
91 new_ucmd!()
92 .args(&["-t3", "--first-only"])
93 .pipe_in(" A B")
94 .succeeds()
95 .stdout_is("\t\t A B");
96 }
97
98 #[test]
99 fn unexpand_first_only_2() {
100 new_ucmd!()
101 .args(&["-t3", "-f"])
102 .pipe_in(" A B")
103 .succeeds()
104 .stdout_is("\t\t A B");
105 }
106
107 #[test]
108 fn unexpand_first_only_3() {
109 new_ucmd!()
110 .args(&["-f", "-t8"])
111 .pipe_in(" A B")
112 .succeeds()
113 .stdout_is("\tA B");
114 }
115
116 #[test]
117 fn unexpand_trailing_space_0() {
118 // evil
119 // Individual spaces before fields starting with non blanks should not be
120 // converted, unless they are at the beginning of the line.
121 new_ucmd!()
122 .args(&["-t4"])
123 .pipe_in("123 \t1\n123 1\n123 \n123 ")
124 .succeeds()
125 .stdout_is("123\t\t1\n123 1\n123 \n123 ");
126 }
127
128 #[test]
129 fn unexpand_trailing_space_1() {
130 // super evil
131 new_ucmd!()
132 .args(&["-t1"])
133 .pipe_in(" abc d e f g ")
134 .succeeds()
135 .stdout_is("\tabc d e\t\tf\t\tg ");
136 }
137
138 #[test]
139 fn unexpand_spaces_follow_tabs_0() {
140 // The two first spaces can be included into the first tab.
141 new_ucmd!()
142 .pipe_in(" \t\t A")
143 .succeeds()
144 .stdout_is("\t\t A");
145 }
146
147 #[test]
148 fn unexpand_spaces_follow_tabs_1() {
149 // evil
150 // Explanation of what is going on here:
151 // 'a' -> 'a' // first tabstop (1)
152 // ' \t' -> '\t' // second tabstop (4)
153 // ' ' -> '\t' // third tabstop (5)
154 // ' B \t' -> ' B \t' // after the list is exhausted, nothing must change
155 new_ucmd!()
156 .args(&["-t1,4,5"])
157 .pipe_in("a \t B \t")
158 .succeeds()
159 .stdout_is("a\t\t B \t");
160 }
161
162 #[test]
163 fn unexpand_spaces_after_fields() {
164 new_ucmd!()
165 .args(&["-a"])
166 .pipe_in(" \t A B C D A\t\n")
167 .succeeds()
168 .stdout_is("\t\tA B C D\t\t A\t\n");
169 }
170
171 #[test]
172 fn unexpand_leading_spaces_before_multibyte() {
173 // Leading whitespace is converted even in the default (UTF-8) mode, and the
174 // multibyte tail is copied verbatim.
175 new_ucmd!()
176 .pipe_in(" caf\u{e9}\n")
177 .succeeds()
178 .stdout_is("\tcaf\u{e9}\n");
179 }
180
181 #[test]
182 fn unexpand_deep_indent_stops_at_last_tabstop() {
183 // With several tab stops, conversion stops past the last one: the extra
184 // leading spaces beyond column 4 stay as spaces.
185 new_ucmd!()
186 .args(&["-t", "2,4"])
187 .pipe_in(" x\n")
188 .succeeds()
189 .stdout_is("\t\t x\n");
190 }
191
192 #[test]
193 fn unexpand_wide_char_width_before_tab() {
194 // A 3-byte, double-width character advances the column by two, so the
195 // following tab lands on the next tab stop.
196 new_ucmd!()
197 .args(&["-a", "-t", "4"])
198 .pipe_in(" \u{3000}X\ty\n")
199 .succeeds()
200 .stdout_is(" \u{3000}X\ty\n");
201 }
202
203 #[test]
204 fn unexpand_ideographic_space_absorbed_into_tab() {
205 // U+3000 is a width-2 blank: a run of them that reaches a tab stop is
206 // converted to a tab, just like a run of ASCII spaces.
207 new_ucmd!()
208 .arg("-a")
209 .pipe_in("\u{3000}\u{3000}\u{3000}\u{3000}Z\n")
210 .succeeds()
211 .stdout_is("\tZ\n");
212 }
213
214 #[test]
215 fn unexpand_ideographic_space_preserved_when_not_converted() {
216 // A blank run that does not reach a tab stop is emitted verbatim, so the
217 // multibyte blank keeps its original bytes rather than becoming spaces.
218 new_ucmd!()
219 .arg("-a")
220 .pipe_in("a\u{3000}b\n")
221 .succeeds()
222 .stdout_is("a\u{3000}b\n");
223 }
224
225 #[test]
226 fn unexpand_read_from_file() {
227 new_ucmd!().arg("with_spaces.txt").arg("-t4").succeeds();
228 }
229
230 #[test]
231 fn unexpand_read_from_two_file() {
232 new_ucmd!()
233 .arg("with_spaces.txt")
234 .arg("with_spaces.txt")
235 .arg("-t4")
236 .succeeds();
237 }
238
239 #[test]
240 fn test_tabs_shortcut() {
241 new_ucmd!()
242 .arg("-3")
243 .pipe_in(" a b")
244 .succeeds()
245 .stdout_is("\ta b");
246 }
247
248 #[test]
249 fn test_tabs_shortcut_combined_with_all_arg() {
250 fn run_cmd(all_arg: &str) {
251 new_ucmd!()
252 .args(&[all_arg, "-3"])
253 .pipe_in("a b c")
254 .succeeds()
255 .stdout_is("a\tb\tc");
256 }
257
258 let all_args = vec!["-a", "--all"];
259
260 for arg in all_args {
261 run_cmd(arg);
262 }
263 }
264
265 #[test]
266 fn test_comma_separated_tabs_shortcut() {
267 new_ucmd!()
268 .args(&["-a", "-3,9"])
269 .pipe_in("a b c")
270 .succeeds()
271 .stdout_is("a\tb\tc");
272 }
273
274 #[test]
275 fn test_blank_separated_tabs() {
276 // GNU accepts a space or a tab as a tab-list separator, just like a comma.
277 for sep in [" ", "\t"] {
278 new_ucmd!()
279 .args(&["-a", "-t", &format!("3{sep}9")])
280 .pipe_in("a b c")
281 .succeeds()
282 .stdout_is("a\tb\tc");
283 }
284 }
285
286 #[test]
287 fn test_tabs_cannot_be_zero() {
288 new_ucmd!()
289 .arg("--tabs=0")
290 .fails()
291 .stderr_contains("tab size cannot be 0");
292 }
293
294 #[test]
295 fn test_tabs_must_be_ascending() {
296 new_ucmd!()
297 .arg("--tabs=1,1")
298 .fails()
299 .stderr_contains("tab sizes must be ascending");
300 }
301
302 #[test]
303 fn test_tabs_with_invalid_chars() {
304 new_ucmd!()
305 .arg("--tabs=x")
306 .fails()
307 .stderr_contains("tab size contains invalid character(s): 'x'");
308 new_ucmd!()
309 .arg("--tabs=1x2")
310 .fails()
311 .stderr_contains("tab size contains invalid character(s): 'x2'");
312 }
313
314 #[test]
315 fn test_tabs_shortcut_with_too_large_size() {
316 let arg = format!("-{}", u128::MAX);
317 let expected_error = "tab stop value is too large";
318
319 new_ucmd!().arg(arg).fails().stderr_contains(expected_error);
320 }
321
322 #[test]
323 fn test_extended_tabstop_increment_overflow() {
324 // `--tabs=N,+M` with N near usize::MAX must be rejected, not overflow the N+M stop.
325 let arg = format!("--tabs={},+1", usize::MAX);
326 new_ucmd!()
327 .arg(arg)
328 .fails()
329 .stderr_contains("tab stop value is too large");
330 }
331
332 #[test]
333 fn test_is_directory() {
334 let (at, mut ucmd) = at_and_ucmd!();
335 let dir_name = "dir";
336 at.mkdir(dir_name);
337
338 ucmd.arg(dir_name)
339 .fails()
340 .stderr_contains(format!("unexpand: {dir_name}: Is a directory"));
341 }
342
343 #[test]
344 fn test_multiple_files() {
345 let (at, mut ucmd) = at_and_ucmd!();
346
347 at.write("file", "content");
348 at.write("file1", "a b");
349
350 ucmd.args(&["file", "file1"])
351 .succeeds()
352 .stdout_is("contenta b");
353 }
354
355 #[test]
356 fn test_one_nonexisting_file() {
357 new_ucmd!()
358 .arg("asdf.txt")
359 .fails()
360 .stderr_contains("asdf.txt: No such file or directory");
361 }
362
363 #[test]
364 #[cfg(all(target_os = "linux", not(target_env = "musl")))]
365 #[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")]
366 fn test_read_error() {
367 new_ucmd!()
368 .arg("/proc/self/mem")
369 .fails()
370 .stderr_contains("unexpand: /proc/self/mem: Input/output error");
371 }
372
373 #[test]
374 #[cfg(target_os = "linux")]
375 #[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")]
376 fn test_non_utf8_filename() {
377 use std::os::unix::ffi::OsStringExt;
378
379 let (at, mut ucmd) = at_and_ucmd!();
380
381 let filename = std::ffi::OsString::from_vec(vec![0xFF, 0xFE]);
382 std::fs::write(at.plus(&filename), b" a\n").unwrap();
383
384 ucmd.arg(&filename).succeeds().stdout_is("\ta\n");
385 }
386
387 #[test]
388 fn unexpand_multibyte_utf8_gnu_compat() {
389 // Column position uses display width, not byte count (matches GNU 9.11).
390 // "1ΔΔΔ5" is 5 columns wide (each Δ is a 2-byte char of display width 1),
391 // so the run of 3 spaces reaches tab stop 8 and collapses to a single tab.
392 new_ucmd!()
393 .args(&["-a"])
394 .pipe_in("1ΔΔΔ5 99999\n")
395 .succeeds()
396 .stdout_is("1ΔΔΔ5\t99999\n");
397 }
398
399 #[test]
400 fn unexpand_wide_multibyte_char_width() {
401 // A 3-byte character of display width 2 must advance the column by 2, not by
402 // its byte length. U+FF0D (full-width hyphen, 3 bytes, width 2) at column 0
403 // reaches column 2; the following 6 spaces then collapse to one tab at
404 // column 8. Regression for the byte-length-as-width bug.
405 new_ucmd!()
406 .args(&["-a"])
407 .pipe_in("\u{FF0D} X\n")
408 .succeeds()
409 .stdout_is("\u{FF0D}\tX\n");
410 }
411
412 #[test]
413 fn test_blanks_ext1() {
414 // Test unexpand with extended blank handling (blanks-ext1)
415 // ['blanks-ext1', '-t', '3,+6', {IN=> "\t "}, {OUT=> "\t\t"}],
416 new_ucmd!()
417 .args(&["-t", "3,+6"])
418 .pipe_in("\t ")
419 .succeeds()
420 .stdout_is("\t\t");
421 }
422
423 #[test]
424 fn test_blanks_ext2() {
425 // Test unexpand with extended blank handling (blanks-ext2)
426 // ['blanks-ext2', '-t', '3,/9', {IN=> "\t "}, {OUT=> "\t\t"}],
427 new_ucmd!()
428 .args(&["-t", "3,/9"])
429 .pipe_in("\t ")
430 .succeeds()
431 .stdout_is("\t\t");
432 }
433
434 #[test]
435 fn test_extended_tabstop_syntax() {
436 let test_cases = [
437 // Standalone /N: tabs at multiples of N
438 ("-t /9", " ", "\t"), // 9 spaces -> 1 tab
439 ("-t /9", " ", "\t\t"), // 18 spaces -> 2 tabs
440 // Standalone +N: tabs at multiples of N
441 ("-t +6", " ", "\t"), // 6 spaces -> 1 tab
442 ("-t +6", " ", "\t\t"), // 12 spaces -> 2 tabs
443 // 3,/0 and 3,+0 should behave like just 3
444 ("-t 3,/0", " ", "\t\t\t "), // 10 spaces -> 3 tabs + 1 space
445 ("-t 3,+0", " ", "\t\t\t "), // 10 spaces -> 3 tabs + 1 space
446 ("-t 3", " ", "\t\t\t "), // 10 spaces -> 3 tabs + 1 space
447 // 3,/0 with text
448 ("-t 3,/0", " test", "\ttest"), // 3 spaces + text -> 1 tab + text
449 // 3,+6 means tab stops at 3, 9, 15, 21, ...
450 ("-t 3,+6", " ", "\t\t\t "), // 20 spaces -> 3 tabs + 5 spaces
451 ];
452
453 for (args, input, expected) in test_cases {
454 new_ucmd!()
455 .args(&args.split_whitespace().collect::<Vec<_>>())
456 .pipe_in(input)
457 .succeeds()
458 .stdout_is(expected);
459 }
460 }
461
462 #[test]
463 fn test_buffered_read_edgecase_behaviour() {
464 // reads are done in 4096 chunks. Tests edgecase spaces around chunk bounds
465 let test_cases = [
466 {
467 // input has newlines in first chunk and has leading spaces after newline in chunk
468 let mut input = vec![b'0'; 180];
469 input.push(b'\n');
470 input.extend([b' '; 8]);
471 input.extend([b'0'; 3897]);
472 input.push(b'\n'); // 180 '0' -> 'n' -> 8 spaces -> 3897 '0' -> \n
473
474 let mut expected = vec![b'0'; 180];
475 expected.push(b'\n');
476 expected.push(b'\t');
477 expected.extend([b'0'; 3897]);
478 expected.push(b'\n'); // 180 '0' -> 'n' -> 1 tab -> 3897 '0' -> \n
479 (input, expected)
480 },
481 {
482 // input has newline after first chunk with leading spaces
483 let mut input = vec![b'0'; 4096];
484 input.extend("\n 0000\n".as_bytes()); // 4096 '0' -> \n -> 8 spaces -> 4 '0' -> \n
485
486 let mut expected = vec![b'0'; 4096];
487 expected.extend("\n\t0000\n".as_bytes()); // 4096 '0' -> \n -> 1 tab -> 4 '0' -> \n
488 (input, expected)
489 },
490 {
491 // fixture has newlines in first chunk and has leading spaces after newline in chunk
492 let mut input = vec![b'0'; 4095];
493 input.extend("\n 0000\n".as_bytes()); // 4095 '0' -> \n -> 8 spaces -> 4 '0' -> \n
494
495 let mut expected = vec![b'0'; 4095];
496 expected.extend("\n\t0000\n".as_bytes()); // 4095 '0' -> \n -> 1 tab -> 4 '0' -> \n
497 (input, expected)
498 },
499 {
500 // input has trailing spaces in the first chunk (should not be unexpanded) into newline with leading
501 // spaces which should be unexpanded
502 let mut input = vec![b'0'; 4088];
503 input.extend(" \n 0000\n".as_bytes()); // 4088 '0' -> 8 spaces -> \n -> 8 spaces -> 4 '0' -> \n
504
505 let mut expected = vec![b'0'; 4088];
506 expected.extend(" \n\t0000\n".as_bytes()); // 4088 '0' -> 8 spaces -> \n -> 8 spaces -> 4 '0' -> \n
507 (input, expected)
508 },
509 {
510 // input has a trailing normal chars after new line in first chunk into leading spaces for new
511 // chunk (should not be unexpanded)
512 let mut input = vec![b'0'; 4087];
513 input.extend("\n00000000 \n".as_bytes()); // 4087 '0' -> \n -> 8 '0' -> 8 spaces -> \n
514
515 let mut expected = vec![b'0'; 4087];
516 expected.extend("\n00000000 \n".as_bytes()); // 4087 '0' -> \n -> 8 '0' -> 8 spaces -> \n
517 (input, expected)
518 },
519 {
520 // input has a trailing blanks after new line in first chunk (should be unexpanded) into leading spaces for new
521 // chunk (should be unexpanded)
522 let mut input = vec![b'0'; 4087];
523 input.extend("\n \n".as_bytes()); // 4087 '0' -> 16 spaces -> \n
524
525 let mut expected = vec![b'0'; 4087];
526 expected.extend("\n\t\t\n".as_bytes()); // 4087 '0' -> 2 tabs -> \n
527 (input, expected)
528 },
529 {
530 // input has a trailing blanks after new line in first chunk (should be unexpanded) into leading spaces for new
531 // chunk (should be unexpanded) (tests space counting is done over chunk bounds)
532 let mut input = vec![b'0'; 4091];
533 input.extend("\n \n".as_bytes()); // 4091 '0' -> 8 spaces -> \n
534
535 let mut expected = vec![b'0'; 4091];
536 expected.extend("\n\t\n".as_bytes()); // 4091 '0' -> 1 tab -> \n
537 (input, expected)
538 },
539 ];
540
541 for (input, expected) in test_cases {
542 new_ucmd!()
543 .pipe_in(input)
544 .succeeds()
545 .stdout_only(String::from_utf8(expected).unwrap());
546 }
547 }