Debian Codegraph @

tests/by-util/test_test.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 (words) egid euid icacls pseudofloat
7
8 use uutests::util::TestScenario;
9 use uutests::{at_and_ucmd, new_ucmd, util_name};
10
11 #[test]
12 fn test_empty_test_equivalent_to_false() {
13 new_ucmd!().fails_with_code(1);
14 }
15
16 #[test]
17 fn test_empty_string_is_false() {
18 new_ucmd!().arg("").fails_with_code(1);
19 }
20
21 #[test]
22 fn test_solo_not() {
23 new_ucmd!().arg("!").succeeds();
24 }
25
26 #[test]
27 fn test_solo_and_or_or_is_a_literal() {
28 // /bin/test '' -a '' => 1; so test(1) must interpret `-a` by itself as
29 // a literal string
30 new_ucmd!().arg("-a").succeeds();
31 new_ucmd!().arg("-o").succeeds();
32 }
33
34 #[test]
35 fn test_some_literals() {
36 let scenario = TestScenario::new(util_name!());
37 let tests = [
38 "a string",
39 "(",
40 ")",
41 "-",
42 "--",
43 "-0",
44 "-f",
45 "--help",
46 "--version",
47 "-eq",
48 "-lt",
49 "-ef",
50 "[",
51 ];
52
53 for test in &tests {
54 scenario.ucmd().arg(test).succeeds();
55 }
56
57 // run the inverse of all these tests
58 for test in &tests {
59 scenario.ucmd().arg("!").arg(test).fails_with_code(1);
60 }
61 }
62
63 #[test]
64 fn test_double_not_is_false() {
65 new_ucmd!().args(&["!", "!"]).fails_with_code(1);
66 }
67
68 #[test]
69 fn test_and_not_is_false() {
70 new_ucmd!().args(&["-a", "!"]).fails_with_code(2);
71 }
72
73 #[test]
74 fn test_not_and_is_false() {
75 // `-a` is a literal here & has nonzero length
76 new_ucmd!().args(&["!", "-a"]).fails_with_code(1);
77 }
78
79 #[test]
80 fn test_not_and_not_succeeds() {
81 new_ucmd!().args(&["!", "-a", "!"]).succeeds();
82 }
83
84 #[test]
85 fn test_simple_or() {
86 new_ucmd!().args(&["foo", "-o", ""]).succeeds();
87 }
88
89 #[test]
90 fn test_errors_miss_and_or() {
91 new_ucmd!()
92 .args(&["-o", "arg"])
93 .fails_with_code(2)
94 .stderr_contains("'-o': unary operator expected");
95 new_ucmd!()
96 .args(&["-a", "arg"])
97 .fails()
98 .stderr_contains("'-a': unary operator expected");
99 }
100
101 #[test]
102 fn test_negated_or() {
103 new_ucmd!()
104 .args(&["!", "foo", "-o", "bar"])
105 .fails_with_code(1);
106 new_ucmd!().args(&["foo", "-o", "!", "bar"]).succeeds();
107 new_ucmd!()
108 .args(&["!", "foo", "-o", "!", "bar"])
109 .fails_with_code(1);
110 }
111
112 #[test]
113 fn test_string_length_of_nothing() {
114 // odd but matches GNU, which must interpret -n as a literal here
115 new_ucmd!().arg("-n").succeeds();
116 }
117
118 #[test]
119 fn test_string_length_of_empty() {
120 new_ucmd!().args(&["-n", ""]).fails_with_code(1);
121
122 // STRING equivalent to -n STRING
123 new_ucmd!().arg("").fails_with_code(1);
124 }
125
126 #[test]
127 fn test_nothing_is_empty() {
128 // -z is a literal here and has nonzero length
129 new_ucmd!().arg("-z").succeeds();
130 }
131
132 #[test]
133 fn test_zero_len_of_empty() {
134 new_ucmd!().args(&["-z", ""]).succeeds();
135 }
136
137 #[test]
138 fn test_zero_len_equals_zero_len() {
139 new_ucmd!().args(&["", "=", ""]).succeeds();
140 }
141
142 #[test]
143 fn test_zero_len_not_equals_zero_len_is_false() {
144 new_ucmd!().args(&["", "!=", ""]).fails_with_code(1);
145 }
146
147 #[test]
148 fn test_double_equal_is_string_comparison_op() {
149 // undocumented but part of the GNU test suite
150 new_ucmd!().args(&["t", "==", "t"]).succeeds();
151 new_ucmd!().args(&["t", "==", "f"]).fails_with_code(1);
152 }
153
154 #[test]
155 fn test_string_comparison() {
156 let scenario = TestScenario::new(util_name!());
157 let tests = [
158 ["foo", "!=", "bar"],
159 ["contained\nnewline", "=", "contained\nnewline"],
160 ["(", "=", "("],
161 ["(", "!=", ")"],
162 ["(", "!=", "="],
163 ["!", "=", "!"],
164 ["=", "=", "="],
165 ];
166
167 for test in &tests {
168 scenario.ucmd().args(&test[..]).succeeds();
169 }
170
171 // run the inverse of all these tests
172 for test in &tests {
173 scenario.ucmd().arg("!").args(&test[..]).fails_with_code(1);
174 }
175 }
176
177 #[test]
178 #[ignore = "fixme: error reporting"]
179 fn test_dangling_string_comparison_is_error() {
180 new_ucmd!()
181 .args(&["missing_something", "="])
182 .fails_with_code(2)
183 .stderr_is("test: missing argument after '='");
184 }
185
186 #[test]
187 fn test_string_operator_is_literal_after_bang() {
188 let scenario = TestScenario::new(util_name!());
189 let tests = [
190 ["!", "="],
191 ["!", "!="],
192 ["!", "-eq"],
193 ["!", "-ne"],
194 ["!", "-lt"],
195 ["!", "-le"],
196 ["!", "-gt"],
197 ["!", "-ge"],
198 ["!", "-ef"],
199 ["!", "-nt"],
200 ["!", "-ot"],
201 ];
202
203 for test in &tests {
204 scenario.ucmd().args(&test[..]).fails_with_code(1);
205 }
206 }
207
208 #[test]
209 fn test_a_bunch_of_not() {
210 new_ucmd!()
211 .args(&["!", "", "!=", "", "-a", "!", "", "!=", ""])
212 .succeeds();
213 }
214
215 #[test]
216 fn test_pseudofloat_equal() {
217 // string comparison; test(1) doesn't support comparison of actual floats
218 new_ucmd!().args(&["123.45", "=", "123.45"]).succeeds();
219 }
220
221 #[test]
222 fn test_pseudofloat_not_equal() {
223 // string comparison; test(1) doesn't support comparison of actual floats
224 new_ucmd!().args(&["123.45", "!=", "123.450"]).succeeds();
225 }
226
227 #[test]
228 fn test_negative_arg_is_a_string() {
229 new_ucmd!().arg("-12345").succeeds();
230 new_ucmd!().arg("--qwert").succeeds(); // spell-checker:disable-line
231 }
232
233 #[test]
234 fn test_some_int_compares() {
235 let scenario = TestScenario::new(util_name!());
236
237 let tests = [
238 ["0", "-eq", "0"],
239 ["0", "-ne", "1"],
240 ["421", "-lt", "3720"],
241 ["0", "-le", "0"],
242 ["11", "-gt", "10"],
243 ["1024", "-ge", "512"],
244 ["9223372036854775806", "-le", "9223372036854775807"],
245 ];
246
247 for test in &tests {
248 scenario.ucmd().args(&test[..]).succeeds();
249 }
250
251 // run the inverse of all these tests
252 for test in &tests {
253 scenario.ucmd().arg("!").args(&test[..]).fails_with_code(1);
254 }
255 }
256
257 #[test]
258 fn test_values_greater_than_i64_allowed() {
259 new_ucmd!()
260 .args(&["9223372036854775808", "-gt", "0"])
261 .succeeds();
262 }
263
264 /// The 71-digit operand reported in GNU compatibility issue #12874.
265 const BIG: &str = "16267277278126277227728782172782882627278282882172762677623672762783782";
266 /// `BIG` with its final digit incremented, so the two operands have the same
267 /// width and differ only in the least significant digit.
268 const BIG_PLUS_ONE: &str =
269 "16267277278126277227728782172782882627278282882172762677623672762783783";
270 /// One digit shorter than `BIG`, so the two operands differ in width.
271 const SMALLER: &str = "1626727727812627722772878217278288262727828288217276267762367276278378";
272
273 #[test]
274 fn test_values_greater_than_i128_allowed() {
275 // i128::MAX + 1
276 new_ucmd!()
277 .args(&["170141183460469231731687303715884105728", "-gt", "0"])
278 .succeeds();
279 // i128::MIN - 1
280 new_ucmd!()
281 .args(&["-170141183460469231731687303715884105729", "-lt", "0"])
282 .succeeds();
283 }
284
285 #[test]
286 fn test_large_int_compares() {
287 let scenario = TestScenario::new(util_name!());
288
289 let tests = [
290 [BIG, "-eq", BIG],
291 [BIG, "-ge", BIG],
292 [BIG, "-le", BIG],
293 [BIG, "-ne", "1"],
294 ["1", "-lt", BIG],
295 [BIG, "-gt", "1"],
296 // Same width, differing only in the least significant digit.
297 [BIG_PLUS_ONE, "-gt", BIG],
298 [BIG, "-lt", BIG_PLUS_ONE],
299 // Differing widths.
300 [BIG, "-gt", SMALLER],
301 [SMALLER, "-lt", BIG],
302 ];
303
304 for test in &tests {
305 scenario.ucmd().args(&test[..]).succeeds();
306 }
307
308 // run the inverse of all these tests
309 for test in &tests {
310 scenario.ucmd().arg("!").args(&test[..]).fails_with_code(1);
311 }
312 }
313
314 #[test]
315 fn test_large_negative_int_compares() {
316 let scenario = TestScenario::new(util_name!());
317 let neg_big = format!("-{BIG}");
318 let neg_smaller = format!("-{SMALLER}");
319
320 let tests = [
321 [neg_big.as_str(), "-eq", neg_big.as_str()],
322 [neg_big.as_str(), "-lt", "0"],
323 [neg_big.as_str(), "-lt", BIG],
324 [BIG, "-gt", neg_big.as_str()],
325 // A wider negative number is the smaller of the two.
326 [neg_big.as_str(), "-lt", neg_smaller.as_str()],
327 [neg_smaller.as_str(), "-gt", neg_big.as_str()],
328 ];
329
330 for test in &tests {
331 scenario.ucmd().args(&test[..]).succeeds();
332 }
333
334 // run the inverse of all these tests
335 for test in &tests {
336 scenario.ucmd().arg("!").args(&test[..]).fails_with_code(1);
337 }
338 }
339
340 #[test]
341 fn test_int_compares_ignore_redundant_sign_and_leading_zeros() {
342 let scenario = TestScenario::new(util_name!());
343 let padded_big = format!("+00{BIG}");
344
345 let tests = [
346 // Zero carries no sign.
347 ["-0", "-eq", "0"],
348 ["+0", "-eq", "-0"],
349 ["0", "-eq", "0000000000"],
350 ["007", "-eq", "7"],
351 ["-007", "-eq", "-7"],
352 [padded_big.as_str(), "-eq", BIG],
353 ];
354
355 for test in &tests {
356 scenario.ucmd().args(&test[..]).succeeds();
357 }
358 }
359
360 #[test]
361 fn test_malformed_integers_are_still_errors() {
362 // Accepting wider operands must not make any of these parse.
363 for operand in ["1_0", "0x10", "1e3", "++5", "5-", "-", "+", "", "4 2"] {
364 new_ucmd!()
365 .args(&[operand, "-eq", "0"])
366 .fails_with_code(2)
367 .stderr_is(format!("test: invalid integer '{operand}'\n"));
368 }
369 }
370
371 #[test]
372 fn test_negative_int_compare() {
373 let scenario = TestScenario::new(util_name!());
374
375 let tests = [
376 ["-1", "-eq", "-1"],
377 ["-1", "-ne", "-2"],
378 ["-3720", "-lt", "-421"],
379 ["-10", "-le", "-10"],
380 ["-21", "-gt", "-22"],
381 ["-128", "-ge", "-256"],
382 ["-9223372036854775808", "-le", "-9223372036854775807"],
383 ];
384
385 for test in &tests {
386 scenario.ucmd().args(&test[..]).succeeds();
387 }
388
389 // run the inverse of all these tests
390 for test in &tests {
391 scenario.ucmd().arg("!").args(&test[..]).fails_with_code(1);
392 }
393 }
394
395 #[test]
396 fn test_float_inequality_is_error() {
397 new_ucmd!()
398 .args(&["123.45", "-ge", "6"])
399 .fails_with_code(2)
400 .stderr_is("test: invalid integer '123.45'\n");
401 }
402
403 #[test]
404 #[cfg(not(windows))]
405 #[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")]
406 fn test_invalid_utf8_integer_compare() {
407 use std::ffi::OsStr;
408 use std::os::unix::ffi::OsStrExt;
409
410 let source = [0x66, 0x6f, 0x80, 0x6f];
411 let arg = OsStr::from_bytes(&source[..]);
412
413 new_ucmd!()
414 .args(&[OsStr::new("123"), OsStr::new("-ne"), arg])
415 .fails_with_code(2)
416 .stderr_is("test: invalid integer $'fo\\x80o'\n");
417
418 new_ucmd!()
419 .args(&[arg, OsStr::new("-eq"), OsStr::new("456")])
420 .fails_with_code(2)
421 .stderr_is("test: invalid integer $'fo\\x80o'\n");
422 }
423
424 #[test]
425 fn test_integer_whitespace_stripping() {
426 new_ucmd!().args(&["42", "-eq", " 42 "]).succeeds();
427 new_ucmd!().args(&["42", "-eq", " 42"]).succeeds();
428 new_ucmd!().args(&["42", "-eq", "42 "]).succeeds();
429 new_ucmd!().args(&[" 42 ", "-eq", "42"]).succeeds();
430
431 new_ucmd!().args(&["42", "-eq", "\t42"]).succeeds();
432 new_ucmd!().args(&["42", "-eq", "\n42"]).succeeds();
433 new_ucmd!().args(&["42", "-eq", "\x0b42"]).succeeds(); // Vertical tab
434 new_ucmd!().args(&["42", "-eq", "\x0c42"]).succeeds(); // Form feed
435 new_ucmd!().args(&["42", "-eq", "\r42"]).succeeds();
436 }
437
438 #[test]
439 fn test_isatty_whitespace_stripping() {
440 new_ucmd!().args(&["-t", " 0 "]).fails_with_code(1);
441 new_ucmd!().args(&["-t", "\n0\t"]).fails_with_code(1);
442 }
443
444 #[test]
445 fn test_isatty_invalid_fd_is_false() {
446 // Asking the CRT about an unopened descriptor aborted the process.
447 new_ucmd!().args(&["-t", "99"]).fails_with_code(1);
448 new_ucmd!().args(&["-t", "-1"]).fails_with_code(1);
449 }
450
451 #[test]
452 #[cfg(windows)]
453 fn test_isatty_unknown_fd_windows() {
454 // Only the standard streams are known on Windows.
455 new_ucmd!().args(&["-t", "3"]).fails_with_code(1);
456 }
457
458 #[test]
459 fn test_file_is_itself() {
460 new_ucmd!()
461 .args(&["regular_file", "-ef", "regular_file"])
462 .succeeds();
463 }
464
465 #[test]
466 #[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")]
467 // Disabled for android, since the temp dir doesn't allow creating hard links
468 #[cfg(not(target_os = "android"))]
469 fn test_hard_link_is_same_file() {
470 let (at, mut ucmd) = at_and_ucmd!();
471 at.hard_link("regular_file", "hard_link");
472 ucmd.args(&["regular_file", "-ef", "hard_link"]).succeeds();
473 }
474
475 #[test]
476 #[cfg(not(target_os = "android"))]
477 fn test_file_is_newer_than_and_older_than_itself() {
478 // odd but matches GNU
479 new_ucmd!()
480 .args(&["regular_file", "-nt", "regular_file"])
481 .fails_with_code(1);
482 new_ucmd!()
483 .args(&["regular_file", "-ot", "regular_file"])
484 .fails_with_code(1);
485 }
486
487 #[test]
488 fn test_file_is_newer_than_non_existing_file() {
489 new_ucmd!()
490 .args(&["non_existing_file", "-nt", "regular_file"])
491 .fails_with_code(1)
492 .no_output();
493
494 new_ucmd!()
495 .args(&["regular_file", "-nt", "non_existing_file"])
496 .succeeds()
497 .no_output();
498
499 new_ucmd!()
500 .args(&["non_existing_file", "-ot", "regular_file"])
501 .succeeds()
502 .no_output();
503
504 new_ucmd!()
505 .args(&["regular_file", "-ot", "non_existing_file"])
506 .fails_with_code(1)
507 .no_output();
508 }
509
510 #[test]
511 #[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")]
512 fn test_same_device_inode() {
513 let scenario = TestScenario::new(util_name!());
514 let at = &scenario.fixtures;
515
516 at.touch("regular_file_second");
517 at.symlink_file("regular_file", "symlink");
518
519 scenario
520 .ucmd()
521 .args(&["regular_file", "-ef", "regular_file_second"])
522 .fails();
523
524 scenario
525 .ucmd()
526 .args(&["regular_file", "-ef", "symlink"])
527 .succeeds();
528 }
529
530 #[test]
531 #[cfg(not(target_os = "android"))]
532 fn test_newer_file() {
533 let scenario = TestScenario::new(util_name!());
534
535 let older_file = scenario.fixtures.make_file("older_file");
536 older_file.set_modified(std::time::UNIX_EPOCH).unwrap();
537 scenario.fixtures.touch("newer_file");
538
539 scenario
540 .ucmd()
541 .args(&["newer_file", "-nt", "older_file"])
542 .succeeds();
543
544 scenario
545 .ucmd()
546 .args(&["older_file", "-nt", "newer_file"])
547 .fails();
548
549 scenario
550 .ucmd()
551 .args(&["older_file", "-ot", "newer_file"])
552 .succeeds();
553
554 scenario
555 .ucmd()
556 .args(&["newer_file", "-ot", "older_file"])
557 .fails();
558 }
559
560 #[test]
561 fn test_file_exists() {
562 new_ucmd!().args(&["-e", "regular_file"]).succeeds();
563 }
564
565 #[test]
566 fn test_nonexistent_file_does_not_exist() {
567 new_ucmd!()
568 .args(&["-e", "nonexistent_file"])
569 .fails_with_code(1);
570 }
571
572 #[test]
573 fn test_nonexistent_file_is_not_regular() {
574 new_ucmd!()
575 .args(&["-f", "nonexistent_file"])
576 .fails_with_code(1);
577 }
578
579 #[test]
580 fn test_file_exists_and_is_regular() {
581 new_ucmd!().args(&["-f", "regular_file"]).succeeds();
582 }
583
584 #[test]
585 fn test_file_is_readable() {
586 new_ucmd!().args(&["-r", "regular_file"]).succeeds();
587 }
588
589 #[test]
590 #[cfg(not(windows))]
591 #[cfg_attr(wasi_runner, ignore = "WASI: no permission bits")]
592 fn test_file_is_not_readable() {
593 let scenario = TestScenario::new(util_name!());
594 let mut ucmd = scenario.ucmd();
595 let mut chmod = scenario.cmd("chmod");
596
597 scenario.fixtures.touch("crypto_file");
598 chmod.args(&["u-r", "crypto_file"]).succeeds();
599
600 ucmd.args(&["!", "-r", "crypto_file"]).succeeds();
601 }
602
603 #[test]
604 #[cfg_attr(wasi_runner, ignore = "WASI: no permission bits")]
605 fn test_file_is_writable() {
606 new_ucmd!().args(&["-w", "regular_file"]).succeeds();
607 }
608
609 #[test]
610 #[cfg(not(windows))]
611 fn test_file_is_not_writable() {
612 let scenario = TestScenario::new(util_name!());
613 let mut ucmd = scenario.ucmd();
614 let mut chmod = scenario.cmd("chmod");
615
616 scenario.fixtures.touch("immutable_file");
617 chmod.args(&["u-w", "immutable_file"]).succeeds();
618
619 ucmd.args(&["!", "-w", "immutable_file"]).succeeds();
620 }
621
622 #[test]
623 fn test_file_is_not_executable() {
624 #[cfg(unix)]
625 let (at, mut ucmd) = at_and_ucmd!();
626 #[cfg(not(unix))]
627 let (_, mut ucmd) = at_and_ucmd!();
628
629 // WSL creates executable files by default, so if we are on unix, make sure
630 // to set make it non-executable.
631 // Files on other targets are non-executable by default.
632 #[cfg(unix)]
633 {
634 use std::os::unix::fs::PermissionsExt;
635 let metadata = std::fs::metadata(at.plus("regular_file")).unwrap();
636 let mut permissions = metadata.permissions();
637
638 // The conversion is useless on some platforms and casts from u16 to
639 // u32 on others
640 #[allow(clippy::useless_conversion)]
641 permissions.set_mode(permissions.mode() & !u32::from(libc::S_IXUSR));
642 std::fs::set_permissions(at.plus("regular_file"), permissions).unwrap();
643 }
644 ucmd.args(&["!", "-x", "regular_file"]).succeeds();
645 }
646
647 #[test]
648 #[cfg(not(windows))]
649 #[cfg_attr(wasi_runner, ignore = "WASI: no permission bits")]
650 fn test_file_is_executable() {
651 let scenario = TestScenario::new(util_name!());
652 let mut chmod = scenario.cmd("chmod");
653
654 chmod.args(&["u+x", "regular_file"]).succeeds();
655
656 scenario.ucmd().args(&["-x", "regular_file"]).succeeds();
657 }
658
659 #[test]
660 #[cfg(windows)]
661 fn test_file_is_not_writable_windows() {
662 let (at, mut ucmd) = at_and_ucmd!();
663 at.touch("readonly_file");
664 let mut perms = std::fs::metadata(at.plus("readonly_file"))
665 .unwrap()
666 .permissions();
667 perms.set_readonly(true);
668 std::fs::set_permissions(at.plus("readonly_file"), perms).unwrap();
669 ucmd.args(&["!", "-w", "readonly_file"]).succeeds();
670 }
671
672 #[test]
673 #[cfg(windows)]
674 fn test_readonly_directory_is_writable_windows() {
675 // NTFS ignores the read-only attribute on directories.
676 let (at, mut ucmd) = at_and_ucmd!();
677 at.mkdir("readonly_dir");
678 at.set_readonly("readonly_dir");
679 ucmd.args(&["-w", "readonly_dir"]).succeeds();
680 }
681
682 #[test]
683 #[cfg(windows)]
684 fn test_file_is_executable_windows() {
685 let (at, mut ucmd) = at_and_ucmd!();
686 at.touch("PROGRAM.EXE");
687 ucmd.args(&["-x", "PROGRAM.EXE"]).succeeds();
688 }
689
690 #[test]
691 #[cfg(windows)]
692 fn test_file_is_executable_from_pathext_windows() {
693 let scenario = TestScenario::new(util_name!());
694 scenario.fixtures.touch("script.Py");
695 scenario.fixtures.touch("program.exe");
696
697 scenario
698 .ucmd()
699 .env("PATHEXT", ".PY")
700 .args(&["-x", "script.Py"])
701 .succeeds();
702
703 scenario
704 .ucmd()
705 .env("PATHEXT", ".PY")
706 .args(&["!", "-x", "program.exe"])
707 .succeeds();
708 }
709
710 #[test]
711 #[cfg_attr(wasi_runner, ignore = "WASI: no permission bits")]
712 fn test_directory_is_executable() {
713 let (at, mut ucmd) = at_and_ucmd!();
714 at.mkdir("dir");
715 ucmd.args(&["-x", "dir"]).succeeds();
716 }
717
718 // Denies a single right so cleanup still works: it needs DELETE and directory
719 // listing.
720 #[cfg(windows)]
721 fn deny_everyone(scenario: &TestScenario, name: &str, rights: &str) {
722 let ace = format!("*S-1-1-0:({rights})");
723 scenario
724 .cmd("icacls")
725 .args(&[name, "/deny", ace.as_str()])
726 .succeeds();
727 }
728
729 #[test]
730 #[cfg(windows)]
731 fn test_file_is_not_readable_windows() {
732 let scenario = TestScenario::new(util_name!());
733 scenario.fixtures.touch("crypto_file");
734 deny_everyone(&scenario, "crypto_file", "RD");
735
736 scenario.ucmd().args(&["!", "-r", "crypto_file"]).succeeds();
737 }
738
739 #[test]
740 #[cfg(windows)]
741 fn test_file_is_not_writable_by_acl_windows() {
742 let scenario = TestScenario::new(util_name!());
743 let at = &scenario.fixtures;
744 at.touch("immutable_file");
745 at.mkdir("immutable_dir");
746 deny_everyone(&scenario, "immutable_file", "WD");
747 deny_everyone(&scenario, "immutable_dir", "WD");
748
749 scenario
750 .ucmd()
751 .args(&["!", "-w", "immutable_file"])
752 .succeeds();
753 scenario
754 .ucmd()
755 .args(&["!", "-w", "immutable_dir"])
756 .succeeds();
757 }
758
759 #[test]
760 #[cfg(windows)]
761 fn test_file_is_not_executable_by_acl_windows() {
762 let scenario = TestScenario::new(util_name!());
763 scenario.fixtures.touch("program.exe");
764 deny_everyone(&scenario, "program.exe", "X");
765
766 scenario.ucmd().args(&["!", "-x", "program.exe"]).succeeds();
767 }
768
769 #[test]
770 #[cfg(windows)]
771 fn test_directory_is_not_executable_by_acl_windows() {
772 let scenario = TestScenario::new(util_name!());
773 scenario.fixtures.mkdir("locked_dir");
774 deny_everyone(&scenario, "locked_dir", "X");
775
776 scenario.ucmd().args(&["!", "-x", "locked_dir"]).succeeds();
777 }
778
779 #[test]
780 fn test_is_not_empty() {
781 new_ucmd!().args(&["-s", "non_empty_file"]).succeeds();
782 }
783
784 #[test]
785 fn test_nonexistent_file_size_test_is_false() {
786 new_ucmd!()
787 .args(&["-s", "nonexistent_file"])
788 .fails_with_code(1);
789 }
790
791 #[test]
792 fn test_not_is_not_empty() {
793 new_ucmd!().args(&["!", "-s", "regular_file"]).succeeds();
794 }
795
796 #[test]
797 fn test_symlink_is_symlink() {
798 let scenario = TestScenario::new(util_name!());
799 let at = &scenario.fixtures;
800
801 at.symlink_file("regular_file", "symlink");
802
803 scenario.ucmd().args(&["-h", "symlink"]).succeeds();
804 scenario.ucmd().args(&["-L", "symlink"]).succeeds();
805 }
806
807 #[test]
808 fn test_file_is_not_symlink() {
809 let scenario = TestScenario::new(util_name!());
810
811 scenario
812 .ucmd()
813 .args(&["!", "-h", "regular_file"])
814 .succeeds();
815 scenario
816 .ucmd()
817 .args(&["!", "-L", "regular_file"])
818 .succeeds();
819 }
820
821 #[test]
822 fn test_nonexistent_file_is_not_symlink() {
823 let scenario = TestScenario::new(util_name!());
824
825 scenario
826 .ucmd()
827 .args(&["!", "-h", "nonexistent_file"])
828 .succeeds();
829 scenario
830 .ucmd()
831 .args(&["!", "-L", "nonexistent_file"])
832 .succeeds();
833 }
834
835 #[test]
836 // Only the superuser is allowed to set the sticky bit on files on FreeBSD/OpenBSD.
837 // Windows has no concept of sticky bit
838 #[cfg(not(any(windows, target_os = "freebsd", target_os = "openbsd")))]
839 #[cfg_attr(wasi_runner, ignore = "WASI: no permission bits")]
840 fn test_file_is_sticky() {
841 let scenario = TestScenario::new(util_name!());
842 let mut ucmd = scenario.ucmd();
843 let mut chmod = scenario.cmd("chmod");
844
845 scenario.fixtures.touch("sticky_file");
846 chmod.args(&["+t", "sticky_file"]).succeeds();
847
848 ucmd.args(&["-k", "sticky_file"]).succeeds();
849 }
850
851 #[test]
852 fn test_file_is_not_sticky() {
853 new_ucmd!().args(&["-k", "regular_file"]).fails_with_code(1);
854 }
855
856 #[test]
857 fn test_solo_empty_parenthetical_is_error() {
858 new_ucmd!().args(&["(", ")"]).fails_with_code(2);
859 }
860
861 #[test]
862 fn test_parenthesized_literal() {
863 let scenario = TestScenario::new(util_name!());
864 let tests = [
865 "a string",
866 "(",
867 ")",
868 "-",
869 "--",
870 "-0",
871 "-f",
872 "--help",
873 "--version",
874 "-e",
875 "-t",
876 "!",
877 "-n",
878 "-z",
879 "[",
880 "-a",
881 "-o",
882 ];
883
884 for test in &tests {
885 scenario.ucmd().arg("(").arg(test).arg(")").succeeds();
886 }
887
888 // run the inverse of all these tests
889 for test in &tests {
890 scenario
891 .ucmd()
892 .arg("!")
893 .arg("(")
894 .arg(test)
895 .arg(")")
896 .fails_with_code(1);
897 }
898 }
899
900 #[test]
901 fn test_parenthesized_op_compares_literal_parenthesis() {
902 // ensure we aren’t treating this case as “string length of literal equal
903 // sign”
904 new_ucmd!().args(&["(", "=", ")"]).fails_with_code(1);
905 }
906
907 #[test]
908 fn test_parenthesized_string_comparison() {
909 let scenario = TestScenario::new(util_name!());
910 let tests = [
911 ["(", "foo", "!=", "bar", ")"],
912 ["(", "contained\nnewline", "=", "contained\nnewline", ")"],
913 ["(", "(", "=", "(", ")"],
914 ["(", "(", "!=", ")", ")"],
915 ["(", "!", "=", "!", ")"],
916 ["(", "=", "=", "=", ")"],
917 ];
918
919 for test in &tests {
920 scenario.ucmd().args(&test[..]).succeeds();
921 }
922
923 // run the inverse of all these tests
924 for test in &tests {
925 scenario.ucmd().arg("!").args(&test[..]).fails_with_code(1);
926 }
927 }
928
929 #[test]
930 fn test_parenthesized_right_parenthesis_as_literal() {
931 new_ucmd!().args(&["(", "-f", ")", ")"]).fails_with_code(1);
932 }
933
934 #[test]
935 #[cfg_attr(wasi_runner, ignore = "WASI: no uid/gid")]
936 fn test_file_owned_by_euid() {
937 new_ucmd!().args(&["-O", "regular_file"]).succeeds();
938 }
939
940 #[test]
941 fn test_nonexistent_file_not_owned_by_euid() {
942 new_ucmd!()
943 .args(&["-O", "nonexistent_file"])
944 .fails_with_code(1);
945 }
946
947 #[test]
948 #[cfg(not(windows))]
949 #[cfg_attr(wasi_runner, ignore = "WASI: no uid/gid")]
950 fn test_file_not_owned_by_euid() {
951 new_ucmd!()
952 .args(&["-f", "/bin/sh", "-a", "!", "-O", "/bin/sh"])
953 .succeeds();
954 }
955
956 #[test]
957 #[cfg(not(windows))]
958 #[cfg_attr(wasi_runner, ignore = "WASI: no uid/gid")]
959 fn test_file_owned_by_egid() {
960 // On some platforms (mostly the BSDs) the test fixture files copied to the
961 // /tmp directory will have a different gid than the current egid (due to
962 // the sticky bit set on the /tmp directory). Fix this before running the
963 // test command.
964 use std::os::unix::fs::MetadataExt;
965 let scene = TestScenario::new(util_name!());
966 let at = &scene.fixtures;
967
968 let metadata = at.metadata("regular_file");
969 let file_gid = rustix::fs::Gid::from_raw(metadata.gid());
970 let user_gid = rustix::process::getegid();
971
972 if user_gid != file_gid {
973 let file_uid = rustix::fs::Uid::from_raw(metadata.uid());
974 let path = at.plus("regular_file");
975 rustix::fs::chown(&path, Some(file_uid), Some(user_gid)).expect("chown failed");
976 }
977
978 scene.ucmd().args(&["-G", "regular_file"]).succeeds();
979 }
980
981 #[test]
982 fn test_nonexistent_file_not_owned_by_egid() {
983 new_ucmd!()
984 .args(&["-G", "nonexistent_file"])
985 .fails_with_code(1);
986 }
987
988 #[test]
989 #[cfg(not(windows))]
990 #[cfg_attr(wasi_runner, ignore = "WASI: no uid/gid")]
991 fn test_file_not_owned_by_egid() {
992 let target_file = if cfg!(target_os = "freebsd") {
993 // The coreutils test runner user has a primary group id of "wheel",
994 // which matches the gid of /bin/sh, so use /sbin/shutdown which has gid
995 // of "operator".
996 "/sbin/shutdown"
997 } else {
998 "/bin/sh"
999 };
1000
1001 new_ucmd!()
1002 .args(&["-f", target_file, "-a", "!", "-G", target_file])
1003 .succeeds();
1004 }
1005
1006 #[test]
1007 #[cfg(windows)]
1008 fn test_file_owned_by_current_group_windows() {
1009 new_ucmd!().args(&["-G", "regular_file"]).succeeds();
1010 }
1011
1012 #[test]
1013 #[cfg(windows)]
1014 fn test_file_not_owned_by_current_token_windows() {
1015 // The system directory belongs to TrustedInstaller, so it is owned neither
1016 // by the user nor by the Administrators group an elevated shell runs as.
1017 let system_root = std::env::var("SystemRoot").expect("SystemRoot is not set");
1018
1019 new_ucmd!()
1020 .args(&["-d", &system_root, "-a", "!", "-O", &system_root])
1021 .succeeds();
1022 }
1023
1024 #[test]
1025 fn test_op_precedence_and_or_1() {
1026 new_ucmd!().args(&[" ", "-o", "", "-a", ""]).succeeds();
1027 }
1028
1029 #[test]
1030 fn test_op_precedence_and_or_1_overridden_by_parentheses() {
1031 new_ucmd!()
1032 .args(&["(", " ", "-o", "", ")", "-a", ""])
1033 .fails_with_code(1);
1034 }
1035
1036 #[test]
1037 fn test_op_precedence_and_or_2() {
1038 new_ucmd!()
1039 .args(&["", "-a", "", "-o", " ", "-a", " "])
1040 .succeeds();
1041 }
1042
1043 #[test]
1044 fn test_op_precedence_and_or_2_overridden_by_parentheses() {
1045 new_ucmd!()
1046 .args(&["", "-a", "(", "", "-o", " ", ")", "-a", " "])
1047 .fails_with_code(1);
1048 }
1049
1050 #[test]
1051 fn test_negated_boolean_precedence() {
1052 let scenario = TestScenario::new(util_name!());
1053
1054 let tests = [
1055 vec!["!", "(", "foo", ")", "-o", "bar"],
1056 vec!["!", "", "-o", "", "-a", ""],
1057 vec!["!", "(", "", "-a", "", ")", "-o", ""],
1058 ];
1059
1060 for test in &tests {
1061 scenario.ucmd().args(&test[..]).succeeds();
1062 }
1063
1064 let negative_tests = [
1065 vec!["!", "-n", "", "-a", ""],
1066 vec!["", "-a", "", "-o", ""],
1067 vec!["!", "", "-a", "", "-o", ""],
1068 vec!["!", "(", "", "-a", "", ")", "-a", ""],
1069 ];
1070
1071 for test in &negative_tests {
1072 scenario.ucmd().args(&test[..]).fails_with_code(1);
1073 }
1074 }
1075
1076 #[test]
1077 fn test_bang_bool_op_precedence() {
1078 // For a Boolean combination of two literals, bang inverts the entire expression
1079 new_ucmd!().args(&["!", "", "-a", ""]).succeeds();
1080 new_ucmd!().args(&["!", "", "-o", ""]).succeeds();
1081
1082 new_ucmd!()
1083 .args(&["!", "a value", "-o", "another value"])
1084 .fails_with_code(1);
1085
1086 // Introducing a UOP — even one that is equivalent to a bare string — causes
1087 // bang to invert only the first term
1088 new_ucmd!()
1089 .args(&["!", "-n", "", "-a", ""])
1090 .fails_with_code(1);
1091 new_ucmd!()
1092 .args(&["!", "", "-a", "-n", ""])
1093 .fails_with_code(1);
1094
1095 // for compound Boolean expressions, bang inverts the _next_ expression
1096 // only, not the entire compound expression
1097 new_ucmd!()
1098 .args(&["!", "", "-a", "", "-a", ""])
1099 .fails_with_code(1);
1100
1101 // parentheses can override this
1102 new_ucmd!()
1103 .args(&["!", "(", "", "-a", "", "-a", "", ")"])
1104 .succeeds();
1105 }
1106
1107 #[test]
1108 fn test_inverted_parenthetical_bool_op_precedence() {
1109 // For a Boolean combination of two literals, bang inverts the entire expression
1110 new_ucmd!()
1111 .args(&["!", "a value", "-o", "another value"])
1112 .fails_with_code(1);
1113
1114 // only the parenthetical is inverted, not the entire expression
1115 new_ucmd!()
1116 .args(&["!", "(", "a value", ")", "-o", "another value"])
1117 .succeeds();
1118 }
1119
1120 #[test]
1121 #[ignore = "fixme: error reporting"]
1122 fn test_dangling_parenthesis() {
1123 new_ucmd!()
1124 .args(&["(", "(", "a", "!=", "b", ")", "-o", "-n", "c"])
1125 .fails_with_code(2);
1126 new_ucmd!()
1127 .args(&["(", "(", "a", "!=", "b", ")", "-o", "-n", "c", ")"])
1128 .succeeds();
1129 }
1130
1131 #[test]
1132 fn test_complicated_parenthesized_expression() {
1133 new_ucmd!()
1134 .args(&[
1135 "(", "(", "!", "(", "a", "=", "b", ")", "-o", "c", "=", "d", ")", "-a", "(", "q", "!=",
1136 "r", ")", ")",
1137 ])
1138 .succeeds();
1139 }
1140
1141 #[test]
1142 fn test_erroneous_parenthesized_expression() {
1143 new_ucmd!()
1144 .args(&["a", "!=", "(", "b", "-a", "b", ")", "!=", "c"])
1145 .fails_with_code(2)
1146 .stderr_is("test: extra argument 'b'\n");
1147 }
1148
1149 #[test]
1150 fn test_or_as_filename() {
1151 new_ucmd!()
1152 .args(&["x", "-a", "-z", "-o"])
1153 .fails_with_code(1);
1154 }
1155
1156 #[test]
1157 #[ignore = "TODO: Busybox has this working"]
1158 fn test_filename_or_with_equal() {
1159 new_ucmd!().args(&["-f", "=", "a", "-o", "b"]).succeeds();
1160 }
1161
1162 #[test]
1163 fn test_string_length_and_nothing() {
1164 new_ucmd!().args(&["-n", "a", "-a"]).fails_with_code(2);
1165 }
1166
1167 #[test]
1168 fn test_boolop_without_right_operand() {
1169 for op in ["-a", "-o"] {
1170 new_ucmd!()
1171 .args(&["x", op])
1172 .fails_with_code(2)
1173 .stderr_is(format!("test: missing argument after '{op}'\n"));
1174
1175 // An empty left operand is still an operand.
1176 new_ucmd!().args(&["", op]).fails_with_code(2);
1177
1178 // Whatever precedes it, the trailing BOOLOP has nothing to join to.
1179 new_ucmd!().args(&["x", "-a", "y", op]).fails_with_code(2);
1180 new_ucmd!().args(&["(", "x", ")", op]).fails_with_code(2);
1181 new_ucmd!().args(&["!", "x", op]).fails_with_code(2);
1182 }
1183 }
1184
1185 #[test]
1186 fn test_lone_boolop_is_a_string() {
1187 // With no operand on either side, -a and -o are ordinary strings.
1188 for op in ["-a", "-o"] {
1189 new_ucmd!().arg(op).succeeds();
1190 new_ucmd!().args(&["!", op]).fails_with_code(1);
1191 }
1192
1193 // A unary operator still takes the BOOLOP as its operand.
1194 new_ucmd!().args(&["-n", "-a"]).succeeds();
1195 }
1196
1197 #[test]
1198 fn test_bracket_syntax_success() {
1199 let scenario = TestScenario::new("[");
1200 let mut ucmd = scenario.ucmd();
1201
1202 ucmd.args(&["1", "-eq", "1", "]"]).succeeds();
1203 }
1204
1205 #[test]
1206 fn test_bracket_syntax_failure() {
1207 let scenario = TestScenario::new("[");
1208 let mut ucmd = scenario.ucmd();
1209
1210 ucmd.args(&["1", "-eq", "2", "]"]).fails_with_code(1);
1211 }
1212
1213 #[test]
1214 fn test_bracket_syntax_missing_right_bracket() {
1215 let scenario = TestScenario::new("[");
1216 let mut ucmd = scenario.ucmd();
1217
1218 // Missing closing bracket takes precedence over other possible errors.
1219 ucmd.args(&["1", "-eq"])
1220 .fails_with_code(2)
1221 .stderr_is("[: missing ']'\n");
1222 }
1223
1224 #[test]
1225 fn test_bracket_syntax_help() {
1226 let scenario = TestScenario::new("[");
1227 let mut ucmd = scenario.ucmd();
1228
1229 ucmd.arg("--help").succeeds().stdout_contains("Usage:");
1230 }
1231
1232 #[test]
1233 fn test_bracket_syntax_version() {
1234 let scenario = TestScenario::new("[");
1235 let mut ucmd = scenario.ucmd();
1236
1237 ucmd.arg("--version")
1238 .succeeds()
1239 .stdout_matches(&r"\[ \(uutils coreutils\) \d+\.\d+\.\d+".parse().unwrap());
1240 }
1241
1242 #[test]
1243 #[allow(non_snake_case)]
1244 fn test_file_N() {
1245 use std::{fs::FileTimes, time::Duration};
1246
1247 let scene = TestScenario::new(util_name!());
1248 let at = &scene.fixtures;
1249
1250 let f = at.make_file("file");
1251
1252 // Set the times so that the file is accessed _after_ being modified
1253 // => test -N return false.
1254 let times = FileTimes::new()
1255 .set_accessed(std::time::UNIX_EPOCH + Duration::from_secs(123))
1256 .set_modified(std::time::UNIX_EPOCH);
1257 f.set_times(times).unwrap();
1258 // TODO: stat call for debugging #7570, remove?
1259 #[cfg(unix)]
1260 println!("{}", scene.cmd_shell("stat file").succeeds().stdout_str());
1261 scene.ucmd().args(&["-N", "file"]).fails();
1262
1263 // Set the times so that the file is modified _after_ being accessed
1264 // => test -N return true.
1265 let times = FileTimes::new()
1266 .set_accessed(std::time::UNIX_EPOCH)
1267 .set_modified(std::time::UNIX_EPOCH + Duration::from_secs(123));
1268 f.set_times(times).unwrap();
1269 // TODO: stat call for debugging #7570, remove?
1270 #[cfg(unix)]
1271 println!("{}", scene.cmd_shell("stat file").succeeds().stdout_str());
1272 scene.ucmd().args(&["-N", "file"]).succeeds();
1273 }
1274
1275 #[test]
1276 fn test_long_integer() {
1277 let scene = TestScenario::new(util_name!());
1278 scene
1279 .ucmd()
1280 .args(&["18446744073709551616", "-eq", "0"])
1281 .fails();
1282 scene
1283 .ucmd()
1284 .args(&["-9223372036854775809", "-ge", "18446744073709551616"])
1285 .fails();
1286 scene
1287 .ucmd()
1288 .args(&[
1289 "'('",
1290 "-9223372036854775809",
1291 "-ge",
1292 "18446744073709551616",
1293 "')'",
1294 ])
1295 .fails();
1296 }
1297
1298 #[test]
1299 fn test_missing_argument_after() {
1300 let mut ucmd = new_ucmd!();
1301
1302 let result = ucmd.args(&["(", "foo"]).fails();
1303 result.no_stdout();
1304 assert_eq!(result.exit_status().code().unwrap(), 2);
1305 assert_eq!(
1306 result.stderr_str().trim(),
1307 "test: missing argument after 'foo'"
1308 );
1309 }
1310
1311 #[test]
1312 fn test_string_lt_gt_operator() {
1313 let items = [
1314 ("a", "b"),
1315 ("a", "aa"),
1316 ("a", "a "),
1317 ("a", "a b"),
1318 ("", "b"),
1319 ("a", "ä"),
1320 ];
1321 for (left, right) in items {
1322 new_ucmd!().args(&[left, "<", right]).succeeds().no_output();
1323 new_ucmd!()
1324 .args(&[right, "<", left])
1325 .fails_with_code(1)
1326 .no_output();
1327
1328 new_ucmd!().args(&[right, ">", left]).succeeds().no_output();
1329 new_ucmd!()
1330 .args(&[left, ">", right])
1331 .fails_with_code(1)
1332 .no_output();
1333 }
1334 new_ucmd!()
1335 .args(&["", "<", ""])
1336 .fails_with_code(1)
1337 .no_output();
1338 new_ucmd!()
1339 .args(&["", ">", ""])
1340 .fails_with_code(1)
1341 .no_output();
1342 }
1343
1344 #[test]
1345 fn test_unary_op_as_literal_in_three_arg_form() {
1346 // `-f = a` is string comparison "-f" = "a", not file test
1347 new_ucmd!().args(&["-f", "=", "a"]).fails_with_code(1);
1348 new_ucmd!().args(&["-f", "=", "a", "-o", "b"]).succeeds();
1349 }
1350
1351 #[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))]
1352 mod diagnostics {
1353 use super::*;
1354
1355 #[cfg(unix)]
1356 #[test]
1357 fn test_snippet_points_at_the_offending_argument() {
1358 let result = new_ucmd!()
1359 .terminal_sim_stderr()
1360 .args(&["7", "-eq", "zap"])
1361 .fails_with_code(2);
1362
1363 // The whole report: the `test: ` prefix of the plain form, the
1364 // expression echoed back, a caret on `zap`, and the operator advice.
1365 assert_eq!(
1366 result.stderr_as_displayed(),
1367 "\
1368 test: invalid integer 'zap'
1369 ╭─[ test:1:7 ]
1370 │
1371 1 │ 7 -eq zap
1372 │ ───
1373 │
1374 │ Help: -eq, -ne, -lt, -le, -gt and -ge compare integers; use =, !=, < or > to compare strings
1375 │ -eq equal, -ne not equal, -lt less than, -le less than or equal, -gt greater than, -ge greater than or equal
1376 ───╯"
1377 );
1378 }
1379
1380 #[test]
1381 fn test_plain_message_is_the_default() {
1382 // The test harness pipes stderr, so the report must not appear.
1383 new_ucmd!()
1384 .args(&["7", "-eq", "zap"])
1385 .fails_with_code(2)
1386 .stderr_is("test: invalid integer 'zap'\n");
1387 }
1388
1389 #[cfg(unix)]
1390 #[test]
1391 fn test_extra_argument_points_past_the_expression() {
1392 let result = new_ucmd!()
1393 .terminal_sim_stderr()
1394 .args(&["k", "!=", "m", "spare"])
1395 .fails_with_code(2);
1396
1397 assert_eq!(
1398 result.stderr_as_displayed(),
1399 "\
1400 test: extra argument 'spare'
1401 ╭─[ test:1:8 ]
1402 │
1403 1 │ k != m spare
1404 │ ──┬──
1405 │ ╰──── the expression was already complete here
1406 │
1407 │ Help: an unquoted variable expanding to several words is the usual cause; quote it as \"$var\" to keep it a single operand
1408 ───╯"
1409 );
1410 }
1411
1412 #[cfg(unix)]
1413 #[test]
1414 fn test_missing_operand_suggests_quoting_the_variable() {
1415 // `test "$empty" -gt 1` with an unset variable ends up here: the
1416 // operator is left without a right-hand operand.
1417 let result = new_ucmd!()
1418 .terminal_sim_stderr()
1419 .args(&["31", "-gt"])
1420 .fails_with_code(2);
1421
1422 // The caret sits on the operator left dangling, and the advice names
1423 // the cause rather than restating the error.
1424 assert_eq!(
1425 result.stderr_as_displayed(),
1426 "\
1427 test: missing argument after '-gt'
1428 ╭─[ test:1:4 ]
1429 │
1430 1 │ 31 -gt
1431 │ ───
1432 │
1433 │ Help: an unset or empty variable expands to nothing, leaving the operator without an operand; quote it as \"$var\"
1434 ───╯"
1435 );
1436 }
1437
1438 #[test]
1439 fn test_missing_operand_plain_message_is_unchanged() {
1440 // Piped stderr keeps the one-line form scripts match on.
1441 new_ucmd!()
1442 .args(&["31", "-gt"])
1443 .fails_with_code(2)
1444 .stderr_is("test: missing argument after '-gt'\n");
1445 }
1446
1447 #[cfg(unix)]
1448 #[test]
1449 fn test_extra_operand_suggests_quoting_the_variable() {
1450 // What `test x = $fruit` looks like when fruit="ripe pear".
1451 let result = new_ucmd!()
1452 .terminal_sim_stderr()
1453 .args(&["x", "=", "ripe", "pear"])
1454 .fails_with_code(2);
1455
1456 // The caret lands on the word past the end of the comparison, and the
1457 // integer advice — which belongs to another error — stays out of it.
1458 assert_eq!(
1459 result.stderr_as_displayed(),
1460 "\
1461 test: extra argument 'pear'
1462 ╭─[ test:1:10 ]
1463 │
1464 1 │ x = ripe pear
1465 │ ──┬─
1466 │ ╰─── the expression was already complete here
1467 │
1468 │ Help: an unquoted variable expanding to several words is the usual cause; quote it as \"$var\" to keep it a single operand
1469 ───╯"
1470 );
1471 }
1472
1473 #[cfg(unix)]
1474 #[test]
1475 fn test_dash_t_advises_about_descriptors_not_comparisons() {
1476 let result = new_ucmd!()
1477 .terminal_sim_stderr()
1478 .args(&["-t", "stdout"])
1479 .fails_with_code(2);
1480
1481 // The message stays the one GNU prints; only the label and the advice
1482 // are specific to `-t`, which has nothing to do with `-eq` and friends.
1483 assert_eq!(
1484 result.stderr_as_displayed(),
1485 "\
1486 test: invalid integer 'stdout'
1487 ╭─[ test:1:4 ]
1488 │
1489 1 │ -t stdout
1490 │ ──────
1491 │
1492 │ Help: -t takes a file descriptor number: 0 is standard input, 1 standard output, 2 standard error
1493 ───╯"
1494 );
1495 }
1496
1497 #[cfg(unix)]
1498 #[test]
1499 fn test_integer_comparison_keeps_its_own_advice() {
1500 // The counterpart of the test above: the same operand under a real
1501 // comparison still gets the operator advice, not the descriptor one.
1502 let result = new_ucmd!()
1503 .terminal_sim_stderr()
1504 .args(&["1", "-eq", "stdout"])
1505 .fails_with_code(2);
1506
1507 assert_eq!(
1508 result.stderr_as_displayed(),
1509 "\
1510 test: invalid integer 'stdout'
1511 ╭─[ test:1:7 ]
1512 │
1513 1 │ 1 -eq stdout
1514 │ ──────
1515 │
1516 │ Help: -eq, -ne, -lt, -le, -gt and -ge compare integers; use =, !=, < or > to compare strings
1517 │ -eq equal, -ne not equal, -lt less than, -le less than or equal, -gt greater than, -ge greater than or equal
1518 ───╯"
1519 );
1520 }
1521
1522 #[test]
1523 fn test_dash_t_plain_message_is_unchanged() {
1524 new_ucmd!()
1525 .args(&["-t", "stdout"])
1526 .fails_with_code(2)
1527 .stderr_is("test: invalid integer 'stdout'\n");
1528 }
1529
1530 #[cfg(unix)]
1531 #[test]
1532 fn test_bracket_form_reports_under_its_own_name() {
1533 // Both the header and the snippet name `[`, and the trailing `]` is
1534 // dropped before the expression is echoed back.
1535 let result = TestScenario::new("[")
1536 .ucmd()
1537 .terminal_sim_stderr()
1538 .args(&["7", "-eq", "zap", "]"])
1539 .fails_with_code(2);
1540
1541 assert_eq!(
1542 result.stderr_as_displayed(),
1543 "\
1544 [: invalid integer 'zap'
1545 ╭─[ [:1:7 ]
1546 │
1547 1 │ 7 -eq zap
1548 │ ───
1549 │
1550 │ Help: -eq, -ne, -lt, -le, -gt and -ge compare integers; use =, !=, < or > to compare strings
1551 │ -eq equal, -ne not equal, -lt less than, -le less than or equal, -gt greater than, -ge greater than or equal
1552 ───╯"
1553 );
1554 }
1555 }