Debian Codegraph @

tests/by-util/test_du.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 (paths) atim sublink subwords azerty azeaze xcwww azeaz amaz azea qzerty tazerty tsublink testfile1 testfile2 filelist fpath testdir testfile
7 // spell-checker:ignore selfref ELOOP smallfile
8
9 #[cfg(not(windows))]
10 use regex::Regex;
11
12 #[cfg(not(windows))]
13 use uutests::unwrap_or_return;
14 use uutests::util::TestScenario;
15 #[cfg(not(windows))]
16 use uutests::util::expected_result;
17 use uutests::{at_and_ucmd, new_ucmd, util_name};
18
19 #[cfg(not(target_os = "openbsd"))]
20 const SUB_DIR: &str = "subdir/deeper";
21 const SUB_DEEPER_DIR: &str = "subdir/deeper/deeper_dir";
22 const SUB_DIR_LINKS: &str = "subdir/links";
23 const SUB_DIR_LINKS_DEEPER_SYM_DIR: &str = "subdir/links/deeper_dir";
24 #[cfg(all(not(target_os = "android"), not(target_os = "openbsd")))]
25 const SUB_FILE: &str = "subdir/links/subwords.txt";
26 #[cfg(all(not(target_os = "android"), not(target_os = "openbsd")))]
27 const SUB_LINK: &str = "subdir/links/sublink.txt";
28
29 #[test]
30 #[cfg(not(target_os = "openbsd"))]
31 fn test_du_basics() {
32 let ts = TestScenario::new(util_name!());
33
34 let result = ts.ucmd().succeeds();
35
36 #[cfg(any(target_os = "linux", target_os = "android"))]
37 {
38 let result_reference = unwrap_or_return!(expected_result(&ts, &[]));
39 if result_reference.succeeded() {
40 assert_eq!(result.stdout_str(), result_reference.stdout_str());
41 return;
42 }
43 }
44 du_basics(result.stdout_str());
45 }
46
47 #[cfg(target_vendor = "apple")]
48 fn du_basics(s: &str) {
49 let answer = concat!(
50 "4\t./subdir/deeper/deeper_dir\n",
51 "8\t./subdir/deeper\n",
52 "12\t./subdir/links\n",
53 "20\t./subdir\n",
54 "24\t.\n"
55 );
56 assert_eq!(s, answer);
57 }
58
59 #[cfg(windows)]
60 fn du_basics(s: &str) {
61 let answer = concat!(
62 "0\t.\\subdir\\deeper\\deeper_dir\n",
63 "0\t.\\subdir\\deeper\n",
64 "8\t.\\subdir\\links\n",
65 "8\t.\\subdir\n",
66 "8\t.\n"
67 );
68 assert_eq!(s, answer);
69 }
70
71 #[cfg(all(not(target_vendor = "apple"), not(target_os = "openbsd"), not(windows)))]
72 fn du_basics(s: &str) {
73 let answer = concat!(
74 "8\t./subdir/deeper/deeper_dir\n",
75 "16\t./subdir/deeper\n",
76 "16\t./subdir/links\n",
77 "36\t./subdir\n",
78 "44\t.\n"
79 );
80 assert_eq!(s, answer);
81 }
82
83 #[test]
84 fn test_all_summarize() {
85 new_ucmd!().arg("-a").arg("-s").fails_with_code(1); //clap provided message
86 }
87
88 #[test]
89 fn test_invalid_arg() {
90 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
91 }
92
93 #[test]
94 #[cfg(not(target_os = "openbsd"))]
95 fn test_du_basics_subdir() {
96 let ts = TestScenario::new(util_name!());
97
98 let result = ts.ucmd().arg(SUB_DIR).succeeds();
99
100 #[cfg(any(target_os = "linux", target_os = "android"))]
101 {
102 let result_reference = unwrap_or_return!(expected_result(&ts, &[SUB_DIR]));
103 if result_reference.succeeded() {
104 assert_eq!(result.stdout_str(), result_reference.stdout_str());
105 return;
106 }
107 }
108 du_basics_subdir(result.stdout_str());
109 }
110
111 #[cfg(target_vendor = "apple")]
112 fn du_basics_subdir(s: &str) {
113 assert_eq!(s, "4\tsubdir/deeper/deeper_dir\n8\tsubdir/deeper\n");
114 }
115 #[cfg(windows)]
116 fn du_basics_subdir(s: &str) {
117 assert_eq!(s, "0\tsubdir/deeper\\deeper_dir\n0\tsubdir/deeper\n");
118 }
119 #[cfg(target_os = "freebsd")]
120 fn du_basics_subdir(s: &str) {
121 assert_eq!(s, "8\tsubdir/deeper/deeper_dir\n16\tsubdir/deeper\n");
122 }
123 #[cfg(all(
124 not(target_vendor = "apple"),
125 not(target_os = "freebsd"),
126 not(target_os = "openbsd"),
127 not(windows)
128 ))]
129 fn du_basics_subdir(s: &str) {
130 // MS-WSL linux has altered expected output
131 if uucore::os::is_wsl_1() {
132 assert_eq!(s, "0\tsubdir/deeper\n");
133 } else {
134 assert_eq!(s, "8\tsubdir/deeper\n");
135 }
136 }
137
138 #[test]
139 fn test_du_invalid_size() {
140 let args = &["block-size", "threshold"];
141 let ts = TestScenario::new(util_name!());
142 for s in args {
143 ts.ucmd()
144 .arg(format!("--{s}=1fb4t"))
145 .arg("/tmp")
146 .fails_with_code(1)
147 .stderr_only(format!("du: invalid suffix in --{s} argument '1fb4t'\n"));
148 ts.ucmd()
149 .arg(format!("--{s}=x"))
150 .arg("/tmp")
151 .fails_with_code(1)
152 .stderr_only(format!("du: invalid --{s} argument 'x'\n"));
153 ts.ucmd()
154 .arg(format!("--{s}=1Y"))
155 .arg("/tmp")
156 .fails_with_code(1)
157 .stderr_only(format!("du: --{s} argument '1Y' too large\n"));
158 }
159 }
160
161 #[test]
162 fn test_du_with_posixly_correct() {
163 let ts = TestScenario::new(util_name!());
164 let at = &ts.fixtures;
165 let dir = "a";
166
167 at.mkdir(dir);
168 at.write(&format!("{dir}/file"), "some content");
169
170 let expected = ts
171 .ucmd()
172 .arg(dir)
173 .arg("--block-size=512")
174 .succeeds()
175 .stdout_move_str();
176
177 let result = ts
178 .ucmd()
179 .arg(dir)
180 .env("POSIXLY_CORRECT", "1")
181 .succeeds()
182 .stdout_move_str();
183
184 assert_eq!(expected, result);
185 }
186
187 #[test]
188 fn test_du_time_style_empty() {
189 let ts = TestScenario::new(util_name!());
190 ts.fixtures.mkdir("a");
191 ts.ucmd()
192 .args(&["--time", "--time-style=", "a"])
193 .fails_with_code(1)
194 .stderr_contains("du: invalid argument '' for 'time style'");
195 ts.ucmd()
196 .args(&["--time", "a"])
197 .env("TIME_STYLE", "posix-")
198 .fails_with_code(1)
199 .stderr_contains("du: invalid argument '' for 'time style'");
200 }
201
202 #[test]
203 fn test_du_zero_env_block_size() {
204 let ts = TestScenario::new(util_name!());
205 let at = &ts.fixtures;
206 let dir = "a";
207
208 at.mkdir(dir);
209 at.write(&format!("{dir}/file"), "some content");
210
211 let expected = ts
212 .ucmd()
213 .arg(dir)
214 .arg("--block-size=1024")
215 .succeeds()
216 .stdout_move_str();
217
218 let result = ts
219 .ucmd()
220 .arg(dir)
221 .env("DU_BLOCK_SIZE", "0")
222 .succeeds()
223 .stdout_move_str();
224
225 assert_eq!(expected, result);
226 }
227
228 #[test]
229 fn test_du_zero_env_block_size_hierarchy() {
230 let ts = TestScenario::new(util_name!());
231 let at = &ts.fixtures;
232 let dir = "a";
233
234 at.mkdir(dir);
235 at.write(&format!("{dir}/file"), "some content");
236
237 let expected = ts
238 .ucmd()
239 .arg(dir)
240 .arg("--block-size=1024")
241 .succeeds()
242 .stdout_move_str();
243
244 let result1 = ts
245 .ucmd()
246 .arg(dir)
247 .env("BLOCK_SIZE", "1")
248 .env("DU_BLOCK_SIZE", "0")
249 .succeeds()
250 .stdout_move_str();
251
252 let result2 = ts
253 .ucmd()
254 .arg(dir)
255 .env("BLOCK_SIZE", "1")
256 .env("BLOCKSIZE", "1")
257 .env("DU_BLOCK_SIZE", "0")
258 .succeeds()
259 .stdout_move_str();
260
261 assert_eq!(expected, result1);
262 assert_eq!(expected, result2);
263 }
264
265 #[test]
266 fn test_du_env_block_size_hierarchy() {
267 let ts = TestScenario::new(util_name!());
268 let at = &ts.fixtures;
269 let dir = "a";
270
271 at.mkdir(dir);
272 at.write(&format!("{dir}/file"), "some content");
273
274 let expected = ts
275 .ucmd()
276 .arg(dir)
277 .arg("--block-size=1")
278 .succeeds()
279 .stdout_move_str();
280
281 let result1 = ts
282 .ucmd()
283 .arg(dir)
284 .env("BLOCK_SIZE", "0")
285 .env("DU_BLOCK_SIZE", "1")
286 .succeeds()
287 .stdout_move_str();
288
289 let result2 = ts
290 .ucmd()
291 .arg(dir)
292 .env("BLOCK_SIZE", "1")
293 .env("BLOCKSIZE", "0")
294 .succeeds()
295 .stdout_move_str();
296
297 assert_eq!(expected, result1);
298 assert_eq!(expected, result2);
299 }
300
301 #[test]
302 fn test_du_binary_block_size() {
303 let ts = TestScenario::new(util_name!());
304 let at = &ts.fixtures;
305 let dir = "a";
306
307 at.mkdir(dir);
308 let fpath = at.plus(format!("{dir}/file"));
309 std::fs::File::create(&fpath)
310 .expect("cannot create test file")
311 .set_len(100_000)
312 .expect("cannot set file size");
313
314 let test_cases = [
315 ("0b1", "1"),
316 ("0b10100", "20"),
317 ("0b1000000000", "512"),
318 ("0b10K", "2K"),
319 ];
320
321 for (binary, decimal) in test_cases {
322 let decimal = ts
323 .ucmd()
324 .arg(dir)
325 .arg(format!("--block-size={decimal}"))
326 .succeeds()
327 .stdout_move_str();
328
329 let binary = ts
330 .ucmd()
331 .arg(dir)
332 .arg(format!("--block-size={binary}"))
333 .succeeds()
334 .stdout_move_str();
335
336 assert_eq!(
337 decimal, binary,
338 "Binary {binary} should equal decimal {decimal}"
339 );
340 }
341 }
342
343 #[test]
344 fn test_du_binary_env_block_size() {
345 let ts = TestScenario::new(util_name!());
346 let at = &ts.fixtures;
347 let dir = "a";
348
349 at.mkdir(dir);
350 let fpath = at.plus(format!("{dir}/file"));
351 std::fs::File::create(&fpath)
352 .expect("cannot create test file")
353 .set_len(100_000)
354 .expect("cannot set file size");
355
356 let expected = ts
357 .ucmd()
358 .arg(dir)
359 .arg("--block-size=1024")
360 .succeeds()
361 .stdout_move_str();
362
363 let result = ts
364 .ucmd()
365 .arg(dir)
366 .env("DU_BLOCK_SIZE", "0b10000000000")
367 .succeeds()
368 .stdout_move_str();
369
370 assert_eq!(expected, result);
371 }
372
373 #[test]
374 fn test_du_invalid_binary_size() {
375 let ts = TestScenario::new(util_name!());
376
377 ts.ucmd()
378 .arg("--block-size=0b123")
379 .arg("/tmp")
380 .fails_with_code(1)
381 .stderr_only("du: invalid suffix in --block-size argument '0b123'\n");
382
383 ts.ucmd()
384 .arg("--threshold=0b123")
385 .arg("/tmp")
386 .fails_with_code(1)
387 .stderr_only("du: invalid suffix in --threshold argument '0b123'\n");
388 }
389
390 #[test]
391 fn test_du_invalid_env_block_size_stops_lookup() {
392 // When DU_BLOCK_SIZE is set but invalid, it should stop the lookup
393 // and use the default block size — not fall through to BLOCK_SIZE.
394 let ts = TestScenario::new(util_name!());
395 let at = &ts.fixtures;
396 let dir = "a";
397
398 at.mkdir(dir);
399 at.write(&format!("{dir}/file"), "some content");
400
401 let default_output = ts
402 .ucmd()
403 .arg(dir)
404 .arg("--block-size=1024")
405 .succeeds()
406 .stdout_move_str();
407
408 // Invalid DU_BLOCK_SIZE should use default (1024), not BLOCK_SIZE=1
409 let result = ts
410 .ucmd()
411 .arg(dir)
412 .env("DU_BLOCK_SIZE", "invalid")
413 .env("BLOCK_SIZE", "1")
414 .succeeds()
415 .stdout_move_str();
416
417 assert_eq!(default_output, result);
418
419 // Empty DU_BLOCK_SIZE should also use default
420 let result = ts
421 .ucmd()
422 .arg(dir)
423 .env("DU_BLOCK_SIZE", "")
424 .env("BLOCK_SIZE", "1")
425 .succeeds()
426 .stdout_move_str();
427
428 assert_eq!(default_output, result);
429 }
430
431 #[test]
432 fn test_du_posixly_correct_default() {
433 // With POSIXLY_CORRECT and no block size env vars, default is 512
434 let ts = TestScenario::new(util_name!());
435 let at = &ts.fixtures;
436 let dir = "a";
437
438 at.mkdir(dir);
439 at.write(&format!("{dir}/file"), "some content");
440
441 let expected = ts
442 .ucmd()
443 .arg(dir)
444 .arg("--block-size=512")
445 .succeeds()
446 .stdout_move_str();
447
448 let result = ts
449 .ucmd()
450 .arg(dir)
451 .env("POSIXLY_CORRECT", "1")
452 .succeeds()
453 .stdout_move_str();
454
455 assert_eq!(expected, result);
456 }
457
458 #[test]
459 fn test_du_binary_edge_cases() {
460 let ts = TestScenario::new(util_name!());
461 let at = &ts.fixtures;
462 at.write("foo", "test");
463
464 ts.ucmd()
465 .arg("-B0b")
466 .arg("foo")
467 .fails()
468 .stderr_only("du: invalid --block-size argument '0b'\n");
469
470 ts.ucmd()
471 .arg("-B0B")
472 .arg("foo")
473 .fails()
474 .stderr_only("du: invalid suffix in --block-size argument '0B'\n");
475
476 ts.ucmd()
477 .arg("--block-size=0b1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")
478 .arg("foo")
479 .fails_with_code(1)
480 .stderr_contains("too large");
481 }
482
483 #[test]
484 fn test_du_non_existing_files() {
485 new_ucmd!()
486 .arg("non_existing_a")
487 .arg("non_existing_b")
488 .fails()
489 .stderr_only(concat!(
490 "du: cannot access 'non_existing_a': No such file or directory\n",
491 "du: cannot access 'non_existing_b': No such file or directory\n"
492 ));
493 }
494
495 #[test]
496 #[cfg(not(target_os = "openbsd"))]
497 fn test_du_soft_link() {
498 let ts = TestScenario::new(util_name!());
499 let at = &ts.fixtures;
500
501 // Create the directory and file structure explicitly for this test
502 at.mkdir_all("subdir/links");
503 at.write("subdir/links/subwords.txt", &"hello world\n".repeat(100));
504 at.symlink_file("subdir/links/subwords.txt", "subdir/links/sublink.txt");
505
506 let result = ts.ucmd().arg("subdir/links").succeeds();
507
508 #[cfg(any(target_os = "linux", target_os = "android"))]
509 {
510 let result_reference = unwrap_or_return!(expected_result(&ts, &["subdir/links"]));
511 if result_reference.succeeded() {
512 assert_eq!(result.stdout_str(), result_reference.stdout_str());
513 return;
514 }
515 }
516
517 let s = result.stdout_str();
518 println!("Output: {s}");
519
520 // Helper closure to assert output matches one of the valid sizes
521 #[cfg(any(target_vendor = "apple", windows, target_os = "freebsd"))]
522 let assert_valid_size = |output: &str, valid_sizes: &[&str]| {
523 assert!(
524 valid_sizes.contains(&output),
525 "Expected one of {valid_sizes:?}, got {output}"
526 );
527 };
528
529 #[cfg(target_vendor = "apple")]
530 {
531 // 'macos' host variants may have `du` output variation for soft links
532 let valid_sizes = [
533 "8\tsubdir/links\n",
534 "12\tsubdir/links\n",
535 "16\tsubdir/links\n",
536 ];
537 assert_valid_size(s, &valid_sizes);
538 }
539
540 #[cfg(windows)]
541 {
542 let valid_sizes = ["4\tsubdir/links\n", "8\tsubdir/links\n"];
543 assert_valid_size(s, &valid_sizes);
544 }
545
546 #[cfg(target_os = "freebsd")]
547 {
548 // FreeBSD may have different block allocations depending on filesystem
549 // Accept both common sizes
550 let valid_sizes = ["12\tsubdir/links\n", "16\tsubdir/links\n"];
551 assert_valid_size(s, &valid_sizes);
552 }
553
554 #[cfg(all(not(target_vendor = "apple"), not(target_os = "freebsd"), not(windows)))]
555 {
556 // MS-WSL linux has altered expected output
557 if uucore::os::is_wsl_1() {
558 assert_eq!(s, "8\tsubdir/links\n");
559 } else {
560 assert_eq!(s, "16\tsubdir/links\n");
561 }
562 }
563 }
564
565 #[cfg(all(not(target_os = "android"), not(target_os = "openbsd")))]
566 #[test]
567 fn test_du_hard_link() {
568 let ts = TestScenario::new(util_name!());
569 let at = &ts.fixtures;
570
571 at.hard_link(SUB_FILE, SUB_LINK);
572
573 let result = ts.ucmd().arg(SUB_DIR_LINKS).succeeds();
574
575 #[cfg(target_os = "linux")]
576 {
577 let result_reference = unwrap_or_return!(expected_result(&ts, &[SUB_DIR_LINKS]));
578 if result_reference.succeeded() {
579 assert_eq!(result.stdout_str(), result_reference.stdout_str());
580 return;
581 }
582 }
583 // We do not double count hard links as the inodes are identical
584 du_hard_link(result.stdout_str());
585 }
586
587 #[cfg(target_vendor = "apple")]
588 fn du_hard_link(s: &str) {
589 assert_eq!(s, "12\tsubdir/links\n");
590 }
591 #[cfg(windows)]
592 fn du_hard_link(s: &str) {
593 assert_eq!(s, "8\tsubdir/links\n");
594 }
595 #[cfg(target_os = "freebsd")]
596 fn du_hard_link(s: &str) {
597 assert_eq!(s, "16\tsubdir/links\n");
598 }
599 #[cfg(all(
600 not(target_vendor = "apple"),
601 not(target_os = "freebsd"),
602 not(target_os = "openbsd"),
603 not(target_os = "android"),
604 not(windows)
605 ))]
606 fn du_hard_link(s: &str) {
607 // MS-WSL linux has altered expected output
608 if uucore::os::is_wsl_1() {
609 assert_eq!(s, "8\tsubdir/links\n");
610 } else {
611 assert_eq!(s, "16\tsubdir/links\n");
612 }
613 }
614
615 #[test]
616 #[cfg(not(target_os = "openbsd"))]
617 fn test_du_d_flag() {
618 let ts = TestScenario::new(util_name!());
619
620 let result = ts.ucmd().arg("-d1").succeeds();
621
622 #[cfg(any(target_os = "linux", target_os = "android"))]
623 {
624 let result_reference = unwrap_or_return!(expected_result(&ts, &["-d1"]));
625 if result_reference.succeeded() {
626 assert_eq!(result.stdout_str(), result_reference.stdout_str());
627 return;
628 }
629 }
630 du_d_flag(result.stdout_str());
631 }
632
633 #[cfg(target_vendor = "apple")]
634 fn du_d_flag(s: &str) {
635 assert_eq!(s, "20\t./subdir\n24\t.\n");
636 }
637 #[cfg(windows)]
638 fn du_d_flag(s: &str) {
639 assert_eq!(s, "8\t.\\subdir\n8\t.\n");
640 }
641 #[cfg(target_os = "freebsd")]
642 fn du_d_flag(s: &str) {
643 assert_eq!(s, "36\t./subdir\n44\t.\n");
644 }
645 #[cfg(all(
646 not(target_vendor = "apple"),
647 not(target_os = "freebsd"),
648 not(target_os = "openbsd"),
649 not(windows)
650 ))]
651 fn du_d_flag(s: &str) {
652 // MS-WSL linux has altered expected output
653 if uucore::os::is_wsl_1() {
654 assert_eq!(s, "8\t./subdir\n8\t.\n");
655 } else {
656 assert_eq!(s, "28\t./subdir\n36\t.\n");
657 }
658 }
659
660 #[test]
661 #[cfg(not(target_os = "openbsd"))]
662 fn test_du_dereference() {
663 let ts = TestScenario::new(util_name!());
664 let at = &ts.fixtures;
665
666 at.symlink_dir(SUB_DEEPER_DIR, SUB_DIR_LINKS_DEEPER_SYM_DIR);
667
668 let result = ts.ucmd().arg("-L").arg(SUB_DIR_LINKS).succeeds();
669
670 #[cfg(any(target_os = "linux", target_os = "android"))]
671 {
672 let result_reference = unwrap_or_return!(expected_result(&ts, &["-L", SUB_DIR_LINKS]));
673
674 if result_reference.succeeded() {
675 assert_eq!(result.stdout_str(), result_reference.stdout_str());
676 return;
677 }
678 }
679
680 du_dereference(result.stdout_str());
681 }
682
683 #[cfg(not(windows))]
684 #[test]
685 fn test_du_dereference_args() {
686 let ts = TestScenario::new(util_name!());
687 let at = &ts.fixtures;
688
689 at.mkdir("dir");
690 at.write("dir/file-ignore1", "azeaze");
691 at.write("dir/file-ignore2", "amaz?ng");
692 at.symlink_dir("dir", "sublink");
693
694 for arg in ["-D", "-H", "--dereference-args"] {
695 let result = ts.ucmd().arg(arg).arg("-s").arg("sublink").succeeds();
696 let stdout = result.stdout_str();
697
698 assert!(!stdout.starts_with('0'));
699 assert!(stdout.contains("sublink"));
700 }
701
702 // Without the option
703 let result = ts.ucmd().arg("-s").arg("sublink").succeeds();
704 result.stdout_contains("0\tsublink\n");
705 }
706
707 #[cfg(target_vendor = "apple")]
708 fn du_dereference(s: &str) {
709 assert_eq!(s, "4\tsubdir/links/deeper_dir\n16\tsubdir/links\n");
710 }
711 #[cfg(windows)]
712 fn du_dereference(s: &str) {
713 assert_eq!(s, "0\tsubdir/links\\deeper_dir\n8\tsubdir/links\n");
714 }
715 #[cfg(target_os = "freebsd")]
716 fn du_dereference(s: &str) {
717 assert_eq!(s, "8\tsubdir/links/deeper_dir\n24\tsubdir/links\n");
718 }
719 #[cfg(all(
720 not(target_vendor = "apple"),
721 not(target_os = "freebsd"),
722 not(target_os = "openbsd"),
723 not(windows)
724 ))]
725 fn du_dereference(s: &str) {
726 // MS-WSL linux has altered expected output
727 if uucore::os::is_wsl_1() {
728 assert_eq!(s, "0\tsubdir/links/deeper_dir\n8\tsubdir/links\n");
729 } else {
730 assert_eq!(s, "8\tsubdir/links/deeper_dir\n24\tsubdir/links\n");
731 }
732 }
733
734 #[cfg(not(any(
735 target_os = "android",
736 target_os = "freebsd",
737 target_os = "openbsd",
738 windows
739 )))]
740 #[test]
741 fn test_du_no_dereference() {
742 let ts = TestScenario::new(util_name!());
743 let at = &ts.fixtures;
744 let dir = "a_dir";
745 let symlink = "symlink";
746
747 at.mkdir(dir);
748 at.symlink_dir(dir, symlink);
749
750 for arg in ["-P", "--no-dereference"] {
751 ts.ucmd()
752 .arg(arg)
753 .succeeds()
754 .stdout_contains(dir)
755 .stdout_does_not_contain(symlink);
756
757 // ensure no-dereference "wins"
758 ts.ucmd()
759 .arg("--dereference")
760 .arg(arg)
761 .succeeds()
762 .stdout_contains(dir)
763 .stdout_does_not_contain(symlink);
764
765 // ensure dereference "wins"
766 let result = ts.ucmd().arg(arg).arg("--dereference").succeeds();
767
768 #[cfg(target_os = "linux")]
769 {
770 let result_reference = unwrap_or_return!(expected_result(&ts, &[arg, "--dereference"]));
771
772 if result_reference.succeeded() {
773 assert_eq!(result.stdout_str(), result_reference.stdout_str());
774 }
775 }
776
777 #[cfg(not(target_os = "linux"))]
778 result.stdout_contains(symlink).stdout_does_not_contain(dir);
779 }
780 }
781
782 #[test]
783 fn test_du_inodes_basic() {
784 let ts = TestScenario::new(util_name!());
785 let result = ts.ucmd().arg("--inodes").succeeds();
786
787 #[cfg(not(windows))]
788 {
789 let result_reference = unwrap_or_return!(expected_result(&ts, &["--inodes"]));
790 assert_eq!(result.stdout_str(), result_reference.stdout_str());
791 }
792
793 #[cfg(windows)]
794 assert_eq!(
795 result.stdout_str(),
796 concat!(
797 "2\t.\\subdir\\deeper\\deeper_dir\n",
798 "4\t.\\subdir\\deeper\n",
799 "3\t.\\subdir\\links\n",
800 "8\t.\\subdir\n",
801 "11\t.\n",
802 )
803 );
804 }
805
806 #[test]
807 fn test_du_inodes() {
808 let ts = TestScenario::new(util_name!());
809
810 ts.ucmd()
811 .arg("--summarize")
812 .arg("--inodes")
813 .succeeds()
814 .stdout_only("11\t.\n");
815
816 let result = ts.ucmd().arg("--separate-dirs").arg("--inodes").succeeds();
817
818 #[cfg(windows)]
819 result.stdout_contains("3\t.\\subdir\\links\n");
820 #[cfg(not(windows))]
821 result.stdout_contains("3\t./subdir/links\n");
822 result.stdout_contains("3\t.\n");
823
824 #[cfg(any(target_os = "linux", target_os = "android"))]
825 {
826 let result_reference =
827 unwrap_or_return!(expected_result(&ts, &["--separate-dirs", "--inodes"]));
828 assert_eq!(result.stdout_str(), result_reference.stdout_str());
829 }
830 }
831
832 #[cfg(not(target_os = "android"))]
833 #[test]
834 fn test_du_inodes_with_count_links() {
835 let ts = TestScenario::new(util_name!());
836 let at = &ts.fixtures;
837
838 at.mkdir("dir");
839 at.touch("dir/file");
840 at.hard_link("dir/file", "dir/hard_link_a");
841 at.hard_link("dir/file", "dir/hard_link_b");
842
843 // ensure the hard links are not counted without --count-links
844 ts.ucmd()
845 .arg("--inodes")
846 .arg("dir")
847 .succeeds()
848 .stdout_is("2\tdir\n");
849
850 for arg in ["-l", "--count-links"] {
851 ts.ucmd()
852 .arg("--inodes")
853 .arg(arg)
854 .arg("dir")
855 .succeeds()
856 .stdout_is("4\tdir\n");
857 }
858 }
859
860 #[cfg(not(target_os = "android"))]
861 #[test]
862 fn test_du_inodes_with_count_links_all() {
863 let ts = TestScenario::new(util_name!());
864 let at = &ts.fixtures;
865
866 at.mkdir("d");
867 at.mkdir("d/d");
868 at.touch("d/f");
869 at.hard_link("d/f", "d/h");
870
871 let result = ts.ucmd().arg("--inodes").arg("-al").arg("d").succeeds();
872 result.no_stderr();
873
874 let mut result_seq: Vec<String> = result
875 .stdout_str()
876 .split('\n')
877 .filter(|x| !x.is_empty())
878 .map(|x| x.parse().unwrap())
879 .collect();
880 result_seq.sort_unstable();
881 #[cfg(windows)]
882 assert_eq!(result_seq, ["1\td\\d", "1\td\\f", "1\td\\h", "4\td"]);
883 #[cfg(not(windows))]
884 assert_eq!(result_seq, ["1\td/d", "1\td/f", "1\td/h", "4\td"]);
885 }
886
887 #[cfg(not(target_os = "android"))]
888 #[test]
889 fn test_du_count_links_hardlinks_separately() {
890 let ts = TestScenario::new(util_name!());
891 let at = &ts.fixtures;
892
893 at.mkdir("dir");
894 at.touch("dir/file");
895 at.hard_link("dir/file", "dir/hard_link");
896
897 let result_without_l = ts.ucmd().arg("-b").arg("dir").succeeds();
898 let size_without_l: u64 = result_without_l
899 .stdout_str()
900 .split('\t')
901 .next()
902 .unwrap()
903 .trim()
904 .parse()
905 .unwrap();
906
907 for arg in ["-l", "--count-links"] {
908 let result_with_l = ts.ucmd().arg("-b").arg(arg).arg("dir").succeeds();
909 let size_with_l: u64 = result_with_l
910 .stdout_str()
911 .split('\t')
912 .next()
913 .unwrap()
914 .trim()
915 .parse()
916 .unwrap();
917
918 assert!(
919 size_with_l >= size_without_l,
920 "With {arg}, size ({size_with_l}) should be >= size without -l ({size_without_l})"
921 );
922 }
923 }
924
925 #[test]
926 fn test_du_h_flag_empty_file() {
927 new_ucmd!()
928 .arg("-h")
929 .arg("empty.txt")
930 .succeeds()
931 .stdout_only("0\tempty.txt\n");
932 }
933
934 #[test]
935 fn test_du_h_precision() {
936 let test_cases = [
937 (133_456_345, "128M"),
938 (12 * 1024 * 1024, "12M"),
939 (8500, "8.4K"),
940 ];
941
942 for &(test_len, expected_output) in &test_cases {
943 let (at, mut ucmd) = at_and_ucmd!();
944
945 let fpath = at.plus("test.txt");
946 std::fs::File::create(&fpath)
947 .expect("cannot create test file")
948 .set_len(test_len)
949 .expect("cannot truncate test len to size");
950 ucmd.arg("-h")
951 .arg("--apparent-size")
952 .arg(&fpath)
953 .succeeds()
954 .stdout_only(format!("{expected_output}\t{}\n", fpath.to_string_lossy()));
955 }
956 }
957
958 #[test]
959 #[cfg_attr(wasi_runner, ignore = "WASI: locale env vars not propagated")]
960 fn test_du_h_locale_decimal_separator() {
961 for (locale, expected) in [("fr_FR.UTF-8", "8,4K"), ("C", "8.4K")] {
962 let (at, mut ucmd) = at_and_ucmd!();
963
964 let fpath = at.plus("test.txt");
965 std::fs::File::create(&fpath)
966 .expect("cannot create test file")
967 .set_len(8500)
968 .expect("cannot truncate test len to size");
969 ucmd.env("LC_ALL", locale)
970 .arg("-h")
971 .arg("--apparent-size")
972 .arg(&fpath)
973 .succeeds()
974 .stdout_only(format!("{expected}\t{}\n", fpath.to_string_lossy()));
975 }
976 }
977
978 #[allow(clippy::too_many_lines)]
979 #[cfg(feature = "touch")]
980 #[test]
981 fn test_du_time() {
982 use regex::Regex;
983
984 let ts = TestScenario::new(util_name!());
985
986 // du --time formats the timestamp according to the local timezone. We set the TZ
987 // environment variable to UTC in the commands below to ensure consistent outputs
988 // and test results regardless of the timezone of the machine this test runs in.
989
990 ts.ccmd("touch")
991 .env("TZ", "UTC")
992 .arg("-a")
993 .arg("-t")
994 .arg("201505150000")
995 .arg("date_test")
996 .succeeds();
997
998 ts.ccmd("touch")
999 .env("TZ", "UTC")
1000 .arg("-m")
1001 .arg("-t")
1002 .arg("201606160000")
1003 .arg("date_test")
1004 .succeeds();
1005
1006 let result = ts
1007 .ucmd()
1008 .env("TZ", "UTC")
1009 .arg("--time")
1010 .arg("date_test")
1011 .succeeds();
1012 result.stdout_only("0\t2016-06-16 00:00\tdate_test\n");
1013
1014 // long-iso (same as default)
1015 let result = ts
1016 .ucmd()
1017 .env("TZ", "UTC")
1018 .arg("--time")
1019 .arg("--time-style=long-iso")
1020 .arg("date_test")
1021 .succeeds();
1022 result.stdout_only("0\t2016-06-16 00:00\tdate_test\n");
1023
1024 // full-iso
1025 let result = ts
1026 .ucmd()
1027 .env("TZ", "UTC")
1028 .arg("--time")
1029 .arg("--time-style=full-iso")
1030 .arg("date_test")
1031 .succeeds();
1032 result.stdout_only("0\t2016-06-16 00:00:00.000000000 +0000\tdate_test\n");
1033
1034 // iso
1035 let result = ts
1036 .ucmd()
1037 .env("TZ", "UTC")
1038 .arg("--time")
1039 .arg("--time-style=iso")
1040 .arg("date_test")
1041 .succeeds();
1042 result.stdout_only("0\t2016-06-16\tdate_test\n");
1043
1044 // custom +FORMAT
1045 let result = ts
1046 .ucmd()
1047 .env("TZ", "UTC")
1048 .arg("--time")
1049 .arg("--time-style=+%Y__%H")
1050 .arg("date_test")
1051 .succeeds();
1052 result.stdout_only("0\t2016__00\tdate_test\n");
1053
1054 // ls has special handling for new line in format, du doesn't.
1055 let result = ts
1056 .ucmd()
1057 .env("TZ", "UTC")
1058 .arg("--time")
1059 .arg("--time-style=+%Y_\n_%H")
1060 .arg("date_test")
1061 .succeeds();
1062 result.stdout_only("0\t2016_\n_00\tdate_test\n");
1063
1064 // Time style can also be setup from environment
1065 let result = ts
1066 .ucmd()
1067 .env("TZ", "UTC")
1068 .env("TIME_STYLE", "full-iso")
1069 .arg("--time")
1070 .arg("date_test")
1071 .succeeds();
1072 result.stdout_only("0\t2016-06-16 00:00:00.000000000 +0000\tdate_test\n");
1073
1074 // For compatibility reason, we also allow posix- prefix.
1075 let result = ts
1076 .ucmd()
1077 .env("TZ", "UTC")
1078 .env("TIME_STYLE", "posix-full-iso")
1079 .arg("--time")
1080 .arg("date_test")
1081 .succeeds();
1082 result.stdout_only("0\t2016-06-16 00:00:00.000000000 +0000\tdate_test\n");
1083
1084 // ... and we strip content after a new line
1085 let result = ts
1086 .ucmd()
1087 .env("TZ", "UTC")
1088 .env("TIME_STYLE", "+XXX\nYYY")
1089 .arg("--time")
1090 .arg("date_test")
1091 .succeeds();
1092 result.stdout_only("0\tXXX\tdate_test\n");
1093
1094 // ... and we ignore "locale", fall back to full-iso.
1095 let result = ts
1096 .ucmd()
1097 .env("TZ", "UTC")
1098 .env("TIME_STYLE", "locale")
1099 .arg("--time")
1100 .arg("date_test")
1101 .succeeds();
1102 result.stdout_only("0\t2016-06-16 00:00\tdate_test\n");
1103
1104 // Command line option takes precedence
1105 let result = ts
1106 .ucmd()
1107 .env("TZ", "UTC")
1108 .env("TIME_STYLE", "full-iso")
1109 .arg("--time")
1110 .arg("--time-style=iso")
1111 .arg("date_test")
1112 .succeeds();
1113 result.stdout_only("0\t2016-06-16\tdate_test\n");
1114
1115 for argument in ["--time=atime", "--time=atim", "--time=a"] {
1116 let result = ts
1117 .ucmd()
1118 .env("TZ", "UTC")
1119 .arg(argument)
1120 .arg("date_test")
1121 .succeeds();
1122 result.stdout_only("0\t2015-05-15 00:00\tdate_test\n");
1123 }
1124
1125 // Change (and birth) times can't be easily modified, so we just do a regex
1126 let re_change_birth =
1127 Regex::new(r"0\t[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}\tdate_test").unwrap();
1128 let result = ts.ucmd().arg("--time=ctime").arg("date_test").succeeds();
1129 #[cfg(windows)]
1130 result.stdout_only("0\t???\tdate_test\n"); // ctime not supported on Windows
1131 #[cfg(not(windows))]
1132 result.stdout_matches(&re_change_birth);
1133
1134 if birth_supported() {
1135 let result = ts.ucmd().arg("--time=birth").arg("date_test").succeeds();
1136 result.stdout_matches(&re_change_birth);
1137 }
1138 }
1139
1140 #[cfg(feature = "touch")]
1141 fn birth_supported() -> bool {
1142 let ts = TestScenario::new(util_name!());
1143 let m = match std::fs::metadata(&ts.fixtures.subdir) {
1144 Ok(m) => m,
1145 Err(e) => panic!("{e}"),
1146 };
1147 m.created().is_ok()
1148 }
1149
1150 #[cfg(feature = "touch")]
1151 #[test]
1152 fn test_du_time_directory() {
1153 let ts = TestScenario::new(util_name!());
1154 let at = &ts.fixtures;
1155
1156 at.mkdir("d");
1157 at.touch("d/old");
1158
1159 ts.ccmd("touch")
1160 .env("TZ", "UTC")
1161 .arg("-m")
1162 .arg("-t")
1163 .arg("202001010000")
1164 .arg(at.plus("d/old"))
1165 .succeeds();
1166
1167 at.touch("d/new");
1168
1169 ts.ccmd("touch")
1170 .env("TZ", "UTC")
1171 .arg("-m")
1172 .arg("-t")
1173 .arg("202301010000")
1174 .arg(at.plus("d/new"))
1175 .succeeds();
1176
1177 // Backdate dir mtime to 2019-01-01 so the aggregated max comes from a child
1178 ts.ccmd("touch")
1179 .env("TZ", "UTC")
1180 .arg("-m")
1181 .arg("-t")
1182 .arg("201901010000")
1183 .arg(at.plus("d"))
1184 .succeeds();
1185
1186 let result = ts
1187 .ucmd()
1188 .env("TZ", "UTC")
1189 .arg("--time")
1190 .arg("d/old")
1191 .succeeds();
1192
1193 result.stdout_only("0\t2020-01-01 00:00\td/old\n");
1194
1195 let result = ts.ucmd().env("TZ", "UTC").arg("--time").arg("d").succeeds();
1196 let stdout = result.stdout_str();
1197
1198 assert!(
1199 stdout.contains("2023-01-01 00:00"),
1200 "wrong time in: {stdout}"
1201 );
1202 assert!(stdout.contains("\td\n"), "missing dir entry: {stdout}");
1203
1204 let result = ts
1205 .ucmd()
1206 .env("TZ", "UTC")
1207 .arg("--time")
1208 .arg("-a")
1209 .arg("d")
1210 .succeeds();
1211 let stdout = result.stdout_str();
1212
1213 #[cfg(not(windows))]
1214 {
1215 assert!(stdout.contains("2020-01-01 00:00\td/old\n"), "{stdout}");
1216 assert!(stdout.contains("2023-01-01 00:00\td/new\n"), "{stdout}");
1217 }
1218 #[cfg(windows)]
1219 {
1220 assert!(stdout.contains("2020-01-01 00:00\td\\old\n"), "{stdout}");
1221 assert!(stdout.contains("2023-01-01 00:00\td\\new\n"), "{stdout}");
1222 }
1223 assert!(stdout.contains("2023-01-01 00:00\td\n"), "{stdout}");
1224 }
1225
1226 #[cfg(feature = "touch")]
1227 #[test]
1228 fn test_du_time_directory_nested() {
1229 let ts = TestScenario::new(util_name!());
1230 let at = &ts.fixtures;
1231
1232 at.mkdir_all("d/sub");
1233 at.touch("d/old");
1234
1235 ts.ccmd("touch")
1236 .env("TZ", "UTC")
1237 .arg("-m")
1238 .arg("-t")
1239 .arg("202001010000")
1240 .arg(at.plus("d/old"))
1241 .succeeds();
1242
1243 at.touch("d/sub/new");
1244
1245 ts.ccmd("touch")
1246 .env("TZ", "UTC")
1247 .arg("-m")
1248 .arg("-t")
1249 .arg("202301010000")
1250 .arg(at.plus("d/sub/new"))
1251 .succeeds();
1252
1253 // Backdate dir mtime to 2019-01-01 so the aggregated max comes from a child
1254 ts.ccmd("touch")
1255 .env("TZ", "UTC")
1256 .arg("-m")
1257 .arg("-t")
1258 .arg("201901010000")
1259 .arg(at.plus("d/sub"))
1260 .succeeds();
1261
1262 ts.ccmd("touch")
1263 .env("TZ", "UTC")
1264 .arg("-m")
1265 .arg("-t")
1266 .arg("201901010000")
1267 .arg(at.plus("d"))
1268 .succeeds();
1269
1270 // The root directory should show the max time from its subtree
1271 let result = ts
1272 .ucmd()
1273 .env("TZ", "UTC")
1274 .arg("--time")
1275 .arg("-a")
1276 .arg("d")
1277 .succeeds();
1278 let stdout = result.stdout_str();
1279
1280 #[cfg(not(windows))]
1281 {
1282 assert!(stdout.contains("2020-01-01 00:00\td/old\n"), "{stdout}");
1283 assert!(stdout.contains("2023-01-01 00:00\td/sub/new\n"), "{stdout}");
1284 assert!(stdout.contains("2023-01-01 00:00\td/sub\n"), "{stdout}");
1285 }
1286 #[cfg(windows)]
1287 {
1288 assert!(stdout.contains("2020-01-01 00:00\td\\old\n"), "{stdout}");
1289 assert!(
1290 stdout.contains("2023-01-01 00:00\td\\sub\\new\n"),
1291 "{stdout}"
1292 );
1293 assert!(stdout.contains("2023-01-01 00:00\td\\sub\n"), "{stdout}");
1294 }
1295 assert!(stdout.contains("2023-01-01 00:00\td\n"), "{stdout}");
1296 }
1297
1298 #[cfg(feature = "touch")]
1299 #[test]
1300 fn test_du_time_atime() {
1301 let ts = TestScenario::new(util_name!());
1302 let at = &ts.fixtures;
1303
1304 at.touch("f");
1305
1306 ts.ccmd("touch")
1307 .env("TZ", "UTC")
1308 .arg("-a")
1309 .arg("-t")
1310 .arg("202201010000")
1311 .arg(at.plus("f"))
1312 .succeeds();
1313
1314 let result = ts
1315 .ucmd()
1316 .env("TZ", "UTC")
1317 .arg("--time=atime")
1318 .arg("f")
1319 .succeeds();
1320 result.stdout_only("0\t2022-01-01 00:00\tf\n");
1321 }
1322
1323 #[cfg(feature = "touch")]
1324 #[test]
1325 fn test_du_time_unaffected_by_exclude() {
1326 let ts = TestScenario::new(util_name!());
1327 let at = &ts.fixtures;
1328
1329 at.mkdir("d");
1330 at.touch("d/keep");
1331 at.touch("d/ignore");
1332
1333 ts.ccmd("touch")
1334 .env("TZ", "UTC")
1335 .arg("-m")
1336 .arg("-t")
1337 .arg("202001010000")
1338 .arg(at.plus("d/keep"))
1339 .succeeds();
1340
1341 ts.ccmd("touch")
1342 .env("TZ", "UTC")
1343 .arg("-m")
1344 .arg("-t")
1345 .arg("202301010000")
1346 .arg(at.plus("d/ignore"))
1347 .succeeds();
1348
1349 // Backdate dir mtime to 2019-01-01 so the aggregated max comes from a child
1350 ts.ccmd("touch")
1351 .env("TZ", "UTC")
1352 .arg("-m")
1353 .arg("-t")
1354 .arg("201901010000")
1355 .arg(at.plus("d"))
1356 .succeeds();
1357
1358 // Exclude "ignore" so the reported time for "d" should reflect only "keep"'s mtime
1359 let result = ts
1360 .ucmd()
1361 .env("TZ", "UTC")
1362 .arg("--time")
1363 .arg("--exclude=ignore")
1364 .arg("d")
1365 .succeeds();
1366 let stdout = result.stdout_str();
1367
1368 assert!(
1369 stdout.contains("2020-01-01 00:00"),
1370 "wrong time in: {stdout}"
1371 );
1372 assert!(stdout.contains("\td\n"), "missing dir entry: {stdout}");
1373 }
1374
1375 #[cfg(not(any(windows, target_os = "openbsd")))]
1376 #[cfg(feature = "chmod")]
1377 #[test]
1378 fn test_du_no_permission() {
1379 let ts = TestScenario::new(util_name!());
1380 let at = &ts.fixtures;
1381
1382 at.mkdir_all(SUB_DIR_LINKS);
1383
1384 ts.ccmd("chmod").arg("-r").arg(SUB_DIR_LINKS).succeeds();
1385
1386 let result = ts.ucmd().arg(SUB_DIR_LINKS).fails();
1387 result.stderr_contains("du: cannot read directory 'subdir/links': Permission denied");
1388
1389 #[cfg(any(target_os = "linux", target_os = "android"))]
1390 {
1391 let result_reference = unwrap_or_return!(expected_result(&ts, &[SUB_DIR_LINKS]));
1392 if result_reference
1393 .stderr_str()
1394 .contains("du: cannot read directory 'subdir/links': Permission denied")
1395 {
1396 assert_eq!(result.stdout_str(), result_reference.stdout_str());
1397 return;
1398 }
1399 }
1400 #[cfg(not(target_vendor = "apple"))]
1401 assert_eq!(result.stdout_str(), "4\tsubdir/links\n");
1402 #[cfg(target_vendor = "apple")]
1403 assert_eq!(result.stdout_str(), "0\tsubdir/links\n");
1404 }
1405
1406 #[cfg(not(windows))]
1407 #[cfg(feature = "chmod")]
1408 #[test]
1409 fn test_du_no_exec_permission() {
1410 let ts = TestScenario::new(util_name!());
1411 let at = &ts.fixtures;
1412
1413 at.mkdir_all("d/no-x/y");
1414
1415 ts.ccmd("chmod").arg("u=rw").arg("d/no-x").succeeds();
1416
1417 let result = ts.ucmd().arg("d/no-x").fails();
1418 result.stderr_contains("du: cannot access 'd/no-x/y': Permission denied");
1419 }
1420
1421 #[test]
1422 #[cfg(not(target_os = "openbsd"))]
1423 fn test_du_one_file_system() {
1424 let ts = TestScenario::new(util_name!());
1425 let at = &ts.fixtures;
1426
1427 // Create the directory structure explicitly for this test
1428 at.mkdir_all("subdir/deeper/deeper_dir");
1429 at.write("subdir/deeper/deeper_dir/deeper_words.txt", "hello world");
1430 at.write("subdir/deeper/words.txt", "world");
1431
1432 let result = ts.ucmd().arg("-x").arg("subdir/deeper").succeeds();
1433
1434 #[cfg(any(target_os = "linux", target_os = "android"))]
1435 {
1436 let result_reference = unwrap_or_return!(expected_result(&ts, &["-x", "subdir/deeper"]));
1437 if result_reference.succeeded() {
1438 assert_eq!(result.stdout_str(), result_reference.stdout_str());
1439 return;
1440 }
1441 }
1442 du_basics_subdir(result.stdout_str());
1443 }
1444
1445 #[test]
1446 #[cfg(not(target_os = "openbsd"))]
1447 fn test_du_threshold() {
1448 let ts = TestScenario::new(util_name!());
1449 let at = &ts.fixtures;
1450
1451 // Create the directory structure explicitly for this test
1452 at.mkdir_all("subdir/links");
1453 at.mkdir_all("subdir/deeper/deeper_dir");
1454 // Create files with specific sizes to test threshold
1455 at.write("subdir/links/bigfile.txt", &"x".repeat(10000)); // ~10K file
1456 at.write("subdir/deeper/deeper_dir/smallfile.txt", "small"); // small file
1457
1458 let threshold = "10K";
1459
1460 ts.ucmd()
1461 .arg("--apparent-size")
1462 .arg(format!("--threshold={threshold}"))
1463 .succeeds()
1464 .stdout_contains("links")
1465 .stdout_does_not_contain("deeper_dir");
1466
1467 ts.ucmd()
1468 .arg("--apparent-size")
1469 .arg(format!("--threshold=-{threshold}"))
1470 .succeeds()
1471 .stdout_does_not_contain("links")
1472 .stdout_contains("deeper_dir");
1473 }
1474
1475 #[test]
1476 #[cfg(not(target_os = "openbsd"))]
1477 fn test_du_binary_threshold() {
1478 let ts = TestScenario::new(util_name!());
1479 let at = &ts.fixtures;
1480
1481 at.mkdir_all("subdir/links");
1482 at.mkdir_all("subdir/deeper/deeper_dir");
1483 at.write("subdir/links/bigfile.txt", &"x".repeat(10000));
1484 at.write("subdir/deeper/deeper_dir/smallfile.txt", "small");
1485
1486 let threshold_bin = "0b10011100010000";
1487
1488 ts.ucmd()
1489 .arg("--apparent-size")
1490 .arg(format!("--threshold={threshold_bin}"))
1491 .succeeds()
1492 .stdout_contains("links")
1493 .stdout_does_not_contain("deeper_dir");
1494 }
1495
1496 #[test]
1497 fn test_du_invalid_threshold() {
1498 let ts = TestScenario::new(util_name!());
1499
1500 let threshold = "-0";
1501
1502 ts.ucmd().arg(format!("--threshold={threshold}")).fails();
1503 }
1504
1505 /// GNU du hands `--threshold` to `xstrtoumax` without trimming, so a padded
1506 /// value is rejected rather than read as the size it surrounds.
1507 #[test]
1508 fn test_du_threshold_with_leading_whitespace() {
1509 new_ucmd!()
1510 .arg("--threshold= -1K")
1511 .fails()
1512 .stderr_only("du: invalid --threshold argument ' -1K'\n");
1513 }
1514
1515 #[test]
1516 fn test_du_threshold_error_handling() {
1517 // Test missing threshold value
1518 new_ucmd!()
1519 .arg("--threshold")
1520 .fails()
1521 .stderr_contains(
1522 "error: a value is required for '--threshold <SIZE>' but none was supplied",
1523 )
1524 .stderr_contains("For more information, try '--help'.");
1525 }
1526
1527 #[test]
1528 fn test_du_apparent_size() {
1529 let (at, mut ucmd) = at_and_ucmd!();
1530
1531 at.mkdir_all("a/b");
1532
1533 at.write("a/b/file1", "foo");
1534 at.write("a/b/file2", "foobar");
1535
1536 let result = ucmd.args(&["--apparent-size", "--all", "a"]).succeeds();
1537
1538 #[cfg(not(windows))]
1539 {
1540 result.stdout_contains_line("1\ta/b/file2");
1541 result.stdout_contains_line("1\ta/b/file1");
1542 result.stdout_contains_line("1\ta/b");
1543 result.stdout_contains_line("1\ta");
1544 }
1545
1546 #[cfg(windows)]
1547 {
1548 result.stdout_contains_line("1\ta\\b\\file2");
1549 result.stdout_contains_line("1\ta\\b\\file1");
1550 result.stdout_contains_line("1\ta\\b");
1551 result.stdout_contains_line("1\ta");
1552 }
1553 }
1554
1555 #[test]
1556 fn test_du_bytes() {
1557 let (at, mut ucmd) = at_and_ucmd!();
1558
1559 at.mkdir_all("a/b");
1560
1561 at.write("a/b/file1", "foo");
1562 at.write("a/b/file2", "foobar");
1563
1564 let result = ucmd.args(&["--bytes", "--all", "a"]).succeeds();
1565
1566 #[cfg(not(windows))]
1567 {
1568 result.stdout_contains_line("6\ta/b/file2");
1569 result.stdout_contains_line("3\ta/b/file1");
1570 result.stdout_contains_line("9\ta/b");
1571 result.stdout_contains_line("9\ta");
1572 }
1573
1574 #[cfg(windows)]
1575 {
1576 result.stdout_contains_line("6\ta\\b\\file2");
1577 result.stdout_contains_line("3\ta\\b\\file1");
1578 result.stdout_contains_line("9\ta\\b");
1579 result.stdout_contains_line("9\ta");
1580 }
1581 }
1582
1583 #[test]
1584 fn test_du_exclude() {
1585 let ts = TestScenario::new(util_name!());
1586 let at = &ts.fixtures;
1587
1588 at.symlink_dir(SUB_DEEPER_DIR, SUB_DIR_LINKS_DEEPER_SYM_DIR);
1589 at.mkdir_all(SUB_DIR_LINKS);
1590
1591 ts.ucmd()
1592 .arg("--exclude=subdir")
1593 .arg(SUB_DEEPER_DIR)
1594 .succeeds()
1595 .stdout_contains("subdir/deeper/deeper_dir");
1596 ts.ucmd()
1597 .arg("--exclude=subdir")
1598 .arg("subdir")
1599 .succeeds()
1600 .no_output();
1601 ts.ucmd()
1602 .arg("--exclude=subdir")
1603 .arg("--verbose")
1604 .arg("subdir")
1605 .succeeds()
1606 .stdout_contains("'subdir' ignored");
1607 }
1608
1609 #[test]
1610 // Disable on Windows because we are looking for /
1611 // And the tests would be more complex if we have to support \ too
1612 #[cfg(not(windows))]
1613 fn test_du_exclude_2() {
1614 let ts = TestScenario::new(util_name!());
1615 let at = &ts.fixtures;
1616
1617 at.mkdir_all("azerty/xcwww/azeaze");
1618
1619 let result = ts.ucmd().arg("azerty").succeeds();
1620
1621 let path_regexp = r"(.*)azerty/xcwww/azeaze(.*)azerty/xcwww(.*)azerty";
1622 let re = Regex::new(path_regexp).unwrap();
1623 assert!(re.is_match(result.stdout_str().replace('\n', "").trim()));
1624
1625 // Exact match
1626 ts.ucmd()
1627 .arg("--exclude=azeaze")
1628 .arg("azerty")
1629 .succeeds()
1630 .stdout_does_not_contain("azerty/xcwww/azeaze");
1631 // Partial match and NOT a glob
1632 ts.ucmd()
1633 .arg("--exclude=azeaz")
1634 .arg("azerty")
1635 .succeeds()
1636 .stdout_contains("azerty/xcwww/azeaze");
1637 // Partial match and a various glob
1638 ts.ucmd()
1639 .arg("--exclude=azea?")
1640 .arg("azerty")
1641 .succeeds()
1642 .stdout_contains("azerty/xcwww/azeaze");
1643 ts.ucmd()
1644 .arg("--exclude=azea{z,b}")
1645 .arg("azerty")
1646 .succeeds()
1647 .stdout_contains("azerty/xcwww/azeaze");
1648 ts.ucmd()
1649 .arg("--exclude=azea*")
1650 .arg("azerty")
1651 .succeeds()
1652 .stdout_does_not_contain("azerty/xcwww/azeaze");
1653 ts.ucmd()
1654 .arg("--exclude=azeaz?")
1655 .arg("azerty")
1656 .succeeds()
1657 .stdout_does_not_contain("azerty/xcwww/azeaze");
1658 }
1659
1660 #[test]
1661 // Disable on Windows because we are looking for /
1662 // And the tests would be more complex if we have to support \ too
1663 #[cfg(not(windows))]
1664 fn test_du_exclude_mix() {
1665 let ts = TestScenario::new(util_name!());
1666 let at = &ts.fixtures;
1667
1668 at.write("file-ignore1", "azeaze");
1669 at.write("file-ignore2", "amaz?ng");
1670
1671 at.mkdir_all("azerty/xcwww/azeaze");
1672 at.mkdir_all("azerty/xcwww/qzerty");
1673 at.mkdir_all("azerty/xcwww/amazing");
1674
1675 ts.ucmd()
1676 .arg("azerty")
1677 .succeeds()
1678 .stdout_contains("azerty/xcwww/azeaze");
1679 ts.ucmd()
1680 .arg("--exclude=azeaze")
1681 .arg("azerty")
1682 .succeeds()
1683 .stdout_does_not_contain("azerty/xcwww/azeaze");
1684
1685 // Just exclude one file name
1686 let result = ts.ucmd().arg("--exclude=qzerty").arg("azerty").succeeds();
1687 assert!(!result.stdout_str().contains("qzerty"));
1688 assert!(result.stdout_str().contains("azerty"));
1689 assert!(result.stdout_str().contains("xcwww"));
1690
1691 // Exclude from file
1692 let result = ts
1693 .ucmd()
1694 .arg("--exclude-from=file-ignore1")
1695 .arg("azerty")
1696 .succeeds();
1697 assert!(!result.stdout_str().contains("azeaze"));
1698 assert!(result.stdout_str().contains("qzerty"));
1699 assert!(result.stdout_str().contains("xcwww"));
1700
1701 // Mix two files and string
1702 let result = ts
1703 .ucmd()
1704 .arg("--exclude=qzerty")
1705 .arg("--exclude-from=file-ignore1")
1706 .arg("--exclude-from=file-ignore2")
1707 .arg("azerty")
1708 .succeeds();
1709 assert!(!result.stdout_str().contains("amazing"));
1710 assert!(!result.stdout_str().contains("qzerty"));
1711 assert!(!result.stdout_str().contains("azeaze"));
1712 assert!(result.stdout_str().contains("xcwww"));
1713 }
1714
1715 #[test]
1716 // Disable on Windows because we are looking for /
1717 // And the tests would be more complex if we have to support \ too
1718 #[cfg(not(windows))]
1719 fn test_du_complex_exclude_patterns() {
1720 let ts = TestScenario::new(util_name!());
1721 let at = &ts.fixtures;
1722
1723 at.mkdir_all("azerty/xcwww/azeaze");
1724 at.mkdir_all("azerty/xcwww/qzerty");
1725 at.mkdir_all("azerty/xcwww/amazing");
1726
1727 // Negation in glob should work with both ^ and !
1728 let result = ts
1729 .ucmd()
1730 .arg("--exclude=azerty/*/[^q]*")
1731 .arg("azerty")
1732 .succeeds();
1733 assert!(!result.stdout_str().contains("amazing"));
1734 assert!(result.stdout_str().contains("qzerty"));
1735 assert!(!result.stdout_str().contains("azeaze"));
1736 assert!(result.stdout_str().contains("xcwww"));
1737
1738 let result = ts
1739 .ucmd()
1740 .arg("--exclude=azerty/*/[!q]*")
1741 .arg("azerty")
1742 .succeeds();
1743 assert!(!result.stdout_str().contains("amazing"));
1744 assert!(result.stdout_str().contains("qzerty"));
1745 assert!(!result.stdout_str().contains("azeaze"));
1746 assert!(result.stdout_str().contains("xcwww"));
1747 }
1748
1749 #[test]
1750 fn test_du_exclude_several_components() {
1751 let ts = TestScenario::new(util_name!());
1752 let at = &ts.fixtures;
1753
1754 at.mkdir_all("a/b/c");
1755 at.mkdir_all("a/x/y");
1756 at.mkdir_all("a/u/y");
1757
1758 // Exact match
1759 let result = ts
1760 .ucmd()
1761 .arg("--exclude=a/u")
1762 .arg("--exclude=a/b")
1763 .arg("a")
1764 .succeeds();
1765 assert!(!result.stdout_str().contains("a/u"));
1766 assert!(!result.stdout_str().contains("a/b"));
1767 }
1768
1769 #[test]
1770 fn test_du_exclude_invalid_syntax() {
1771 let ts = TestScenario::new(util_name!());
1772 let at = &ts.fixtures;
1773
1774 at.mkdir_all("azerty/xcwww/azeaze");
1775
1776 ts.ucmd()
1777 .arg("--exclude=a[ze")
1778 .arg("azerty")
1779 .fails()
1780 .stderr_contains("du: Invalid exclude syntax");
1781 }
1782
1783 #[test]
1784 fn test_du_exclude_from_nonexistent_file() {
1785 new_ucmd!()
1786 .arg("--exclude-from=nonexistent-file")
1787 .fails()
1788 .stderr_contains("du: No such file or directory");
1789 }
1790
1791 #[cfg(all(target_os = "linux", not(target_env = "musl")))]
1792 #[test]
1793 fn test_du_exclude_from_read_error() {
1794 new_ucmd!()
1795 .arg("--exclude-from=/proc/self/mem")
1796 .fails()
1797 .stderr_contains("du: Input/output error");
1798 }
1799
1800 #[cfg(not(windows))]
1801 #[test]
1802 fn test_du_symlink_fail() {
1803 let ts = TestScenario::new(util_name!());
1804 let at = &ts.fixtures;
1805
1806 at.symlink_file("non-existing.txt", "target.txt");
1807
1808 ts.ucmd().arg("-L").arg("target.txt").fails_with_code(1);
1809 }
1810
1811 #[cfg(not(windows))]
1812 #[cfg(not(target_os = "openbsd"))]
1813 #[test]
1814 fn test_du_symlink_multiple_fail() {
1815 let ts = TestScenario::new(util_name!());
1816 let at = &ts.fixtures;
1817
1818 at.symlink_file("non-existing.txt", "target.txt");
1819 at.write("file1", "azeaze");
1820
1821 let result = ts.ucmd().arg("-L").arg("target.txt").arg("file1").fails();
1822 assert_eq!(result.code(), 1);
1823 result.stdout_contains("4\tfile1\n");
1824 }
1825
1826 #[test]
1827 // Disable on Windows because of different path separators and handling of null characters
1828 #[cfg(not(windows))]
1829 fn test_du_files0_from() {
1830 let ts = TestScenario::new(util_name!());
1831 let at = &ts.fixtures;
1832
1833 at.write("testfile1", "content1");
1834 at.write("testfile2", "content2");
1835
1836 at.mkdir("testdir");
1837 at.write("testdir/testfile3", "content3");
1838
1839 at.write("filelist", "testfile1\0testfile2\0testdir\0");
1840
1841 ts.ucmd()
1842 .arg("--files0-from=filelist")
1843 .succeeds()
1844 .stdout_contains("testfile1")
1845 .stdout_contains("testfile2")
1846 .stdout_contains("testdir");
1847 }
1848
1849 #[test]
1850 fn test_du_files0_from_ignore_duplicate_file_names() {
1851 // The same file listed twice is counted once via inode tracking.
1852 let ts = TestScenario::new(util_name!());
1853 let at = &ts.fixtures;
1854 let file = "testfile";
1855
1856 at.touch(file);
1857 at.write("filelist", &format!("{file}\0{file}\0"));
1858
1859 ts.ucmd()
1860 .arg("--files0-from=filelist")
1861 .succeeds()
1862 .stdout_is(format!("0\t{file}\n"));
1863 }
1864
1865 #[test]
1866 fn test_du_files0_from_duplicate_file_names_with_count_links() {
1867 // With -l the inode dedup is disabled, so a repeated name is listed each time.
1868 let ts = TestScenario::new(util_name!());
1869 let at = &ts.fixtures;
1870 let file = "testfile";
1871
1872 at.touch(file);
1873 at.write("filelist", &format!("{file}\0{file}\0"));
1874
1875 ts.ucmd()
1876 .arg("-l")
1877 .arg("--files0-from=filelist")
1878 .succeeds()
1879 .stdout_is(format!("0\t{file}\n0\t{file}\n"));
1880 }
1881
1882 #[test]
1883 fn test_du_files0_from_missing_file_listed_twice() {
1884 // A missing file listed twice must be reported each time, not deduplicated.
1885 let ts = TestScenario::new(util_name!());
1886 let at = &ts.fixtures;
1887
1888 at.write("filelist", "missing\0missing\0");
1889
1890 ts.ucmd()
1891 .arg("--files0-from=filelist")
1892 .fails_with_code(1)
1893 .stderr_only(
1894 "du: cannot access 'missing': No such file or directory\n\
1895 du: cannot access 'missing': No such file or directory\n",
1896 );
1897 }
1898
1899 #[test]
1900 fn test_du_files0_from_with_invalid_zero_length_file_names() {
1901 let ts = TestScenario::new(util_name!());
1902 let at = &ts.fixtures;
1903
1904 at.touch("testfile");
1905
1906 at.write("filelist", "\0testfile\0\0");
1907
1908 ts.ucmd()
1909 .arg("--files0-from=filelist")
1910 .fails_with_code(1)
1911 .stdout_contains("testfile")
1912 .stderr_contains("filelist:1: invalid zero-length file name")
1913 .stderr_contains("filelist:3: invalid zero-length file name");
1914 }
1915
1916 #[test]
1917 fn test_du_files0_from_stdin() {
1918 let ts = TestScenario::new(util_name!());
1919 let at = &ts.fixtures;
1920
1921 at.write("testfile1", "content1");
1922 at.write("testfile2", "content2");
1923
1924 let input = "testfile1\0testfile2\0";
1925
1926 ts.ucmd()
1927 .arg("--files0-from=-")
1928 .pipe_in(input)
1929 .succeeds()
1930 .stdout_contains("testfile1")
1931 .stdout_contains("testfile2");
1932 }
1933
1934 #[test]
1935 fn test_du_files0_from_stdin_ignore_duplicate_file_names() {
1936 let ts = TestScenario::new(util_name!());
1937 let at = &ts.fixtures;
1938 let file = "testfile";
1939
1940 at.touch(file);
1941
1942 let input = format!("{file}\0{file}");
1943
1944 ts.ucmd()
1945 .arg("--files0-from=-")
1946 .pipe_in(input)
1947 .succeeds()
1948 .stdout_is(format!("0\t{file}\n"));
1949 }
1950
1951 #[test]
1952 fn test_du_files0_from_stdin_with_invalid_zero_length_file_names() {
1953 new_ucmd!()
1954 .arg("--files0-from=-")
1955 .pipe_in("\0\0")
1956 .fails_with_code(1)
1957 .stderr_contains("-:1: invalid zero-length file name")
1958 .stderr_contains("-:2: invalid zero-length file name");
1959 }
1960
1961 #[test]
1962 fn test_du_files0_from_stdin_with_stdin_as_input() {
1963 new_ucmd!()
1964 .arg("--files0-from=-")
1965 .pipe_in("-")
1966 .fails_with_code(1)
1967 .stderr_is(
1968 "du: when reading file names from standard input, no file name of '-' allowed\n",
1969 );
1970 }
1971
1972 #[test]
1973 fn test_du_files0_from_dir() {
1974 let ts = TestScenario::new(util_name!());
1975 let at = &ts.fixtures;
1976
1977 at.mkdir("dir");
1978
1979 let result = ts.ucmd().arg("--files0-from=dir").fails();
1980 assert_eq!(result.stderr_str(), "du: dir: read error: Is a directory\n");
1981 }
1982
1983 #[test]
1984 fn test_du_files0_from_combined() {
1985 let ts = TestScenario::new(util_name!());
1986 let at = &ts.fixtures;
1987
1988 at.mkdir("dir");
1989
1990 let result = ts.ucmd().arg("--files0-from=-").arg("foo").fails();
1991 let stderr = result.stderr_str();
1992
1993 assert!(stderr.contains("file operands cannot be combined with --files0-from"));
1994 }
1995
1996 #[test]
1997 fn test_invalid_time_style() {
1998 let ts = TestScenario::new(util_name!());
1999 ts.ucmd()
2000 .arg("-s")
2001 .arg("--time-style=banana")
2002 .succeeds()
2003 .stdout_does_not_contain("du: invalid argument 'banana' for 'time style'");
2004 }
2005
2006 #[test]
2007 fn test_human_size() {
2008 use std::fs::File;
2009
2010 let ts = TestScenario::new(util_name!());
2011 let at = &ts.fixtures;
2012 let dir = at.plus_as_string("d");
2013 at.mkdir(&dir);
2014
2015 for i in 1..=1023 {
2016 let file_path = format!("{dir}/file{i}");
2017 File::create(&file_path).expect("Failed to create file");
2018 }
2019
2020 ts.ucmd()
2021 .arg("--inodes")
2022 .arg("-h")
2023 .arg(&dir)
2024 .succeeds()
2025 .stdout_contains(format!("1.0K {dir}"));
2026 }
2027
2028 #[cfg(not(target_os = "android"))]
2029 #[test]
2030 fn test_du_deduplicated_input_args() {
2031 let ts = TestScenario::new(util_name!());
2032 let at = &ts.fixtures;
2033
2034 at.mkdir("d");
2035 at.mkdir("d/d");
2036 at.touch("d/f");
2037 at.hard_link("d/f", "d/h");
2038
2039 let result = ts
2040 .ucmd()
2041 .arg("--inodes")
2042 .arg("d")
2043 .arg("d")
2044 .arg("d")
2045 .succeeds();
2046 result.no_stderr();
2047
2048 let result_seq: Vec<String> = result
2049 .stdout_str()
2050 .lines()
2051 .map(|x| x.parse().unwrap())
2052 .collect();
2053 #[cfg(windows)]
2054 assert_eq!(result_seq, ["1\td\\d", "3\td"]);
2055 #[cfg(not(windows))]
2056 assert_eq!(result_seq, ["1\td/d", "3\td"]);
2057 }
2058
2059 #[cfg(not(target_os = "android"))]
2060 #[test]
2061 fn test_du_no_deduplicated_input_args() {
2062 let ts = TestScenario::new(util_name!());
2063 let at = &ts.fixtures;
2064
2065 at.mkdir("d");
2066 at.touch("d/d");
2067
2068 let result = ts
2069 .ucmd()
2070 .arg("--inodes")
2071 .arg("-l")
2072 .arg("d")
2073 .arg("d")
2074 .arg("d")
2075 .succeeds();
2076 result.no_stderr();
2077
2078 let result_seq: Vec<String> = result
2079 .stdout_str()
2080 .lines()
2081 .map(|x| x.parse().unwrap())
2082 .collect();
2083 assert_eq!(result_seq, ["2\td", "2\td", "2\td"]);
2084 }
2085
2086 #[test]
2087 fn test_du_blocksize_zero_do_not_panic() {
2088 let ts = TestScenario::new(util_name!());
2089 let at = &ts.fixtures;
2090 at.write("foo", "some content");
2091 for block_size in ["0", "00", "000", "0x0", "0b0"] {
2092 ts.ucmd()
2093 .arg(format!("-B{block_size}"))
2094 .arg("foo")
2095 .fails()
2096 .stderr_only(format!(
2097 "du: invalid --block-size argument '{block_size}'\n"
2098 ));
2099 }
2100 }
2101
2102 #[test]
2103 fn test_du_inodes_blocksize_ineffective() {
2104 let ts = TestScenario::new(util_name!());
2105 let at = &ts.fixtures;
2106 let fpath = at.plus("test.txt");
2107 at.touch(fpath);
2108 for method in ["-B3", "--block-size=3"] {
2109 // No warning
2110 ts.ucmd()
2111 .arg(method)
2112 .arg("--inodes")
2113 .arg("test.txt")
2114 .succeeds()
2115 .stdout_only("1\ttest.txt\n");
2116 }
2117 for method in ["--apparent-size", "-b"] {
2118 // A warning appears!
2119 ts.ucmd()
2120 .arg(method)
2121 .arg("--inodes")
2122 .arg("test.txt")
2123 .succeeds()
2124 .stdout_is("1\ttest.txt\n")
2125 .stderr_is(
2126 "du: warning: options --apparent-size and -b are ineffective with --inodes\n",
2127 );
2128 }
2129 }
2130
2131 #[test]
2132 fn test_du_inodes_total_text() {
2133 let ts = TestScenario::new(util_name!());
2134 let at = &ts.fixtures;
2135
2136 at.mkdir_all("d/d");
2137
2138 let result = ts.ucmd().arg("--inodes").arg("-c").arg("d").succeeds();
2139
2140 let lines: Vec<&str> = result.stdout_str().lines().collect();
2141
2142 assert_eq!(lines.len(), 3);
2143
2144 let total_line = lines.last().unwrap();
2145 assert!(total_line.contains("total"));
2146
2147 let parts: Vec<&str> = total_line.split('\t').collect();
2148 assert_eq!(parts.len(), 2);
2149
2150 assert!(parts[0].parse::<u64>().is_ok());
2151 }
2152
2153 #[test]
2154 fn test_du_threshold_no_suggested_values() {
2155 // tested by tests/du/threshold
2156 let ts = TestScenario::new(util_name!());
2157
2158 let result = ts.ucmd().arg("--threshold").fails();
2159 assert!(!result.stderr_str().contains("[possible values: ]"));
2160 }
2161
2162 #[test]
2163 #[cfg(target_os = "linux")]
2164 fn test_du_long_path_safe_traversal() {
2165 let ts = TestScenario::new(util_name!());
2166 let at = &ts.fixtures;
2167
2168 let mut deep_path = String::from("long_path_test");
2169 at.mkdir(&deep_path);
2170
2171 for i in 0..15 {
2172 let long_dir_name = format!("{}{i}", "a".repeat(100));
2173 deep_path = format!("{deep_path}/{long_dir_name}");
2174 at.mkdir_all(&deep_path);
2175 }
2176
2177 let test_file = format!("{deep_path}/test.txt");
2178 at.write(&test_file, "test content");
2179
2180 let result = ts.ucmd().arg("-s").arg("long_path_test").succeeds();
2181 assert!(result.stdout_str().contains("long_path_test"));
2182
2183 let result = ts.ucmd().arg("long_path_test").succeeds();
2184 let lines: Vec<&str> = result.stdout_str().trim().lines().collect();
2185 assert!(lines.len() >= 15);
2186 }
2187 #[test]
2188 #[cfg(unix)]
2189 fn test_du_very_deep_directory() {
2190 let ts = TestScenario::new(util_name!());
2191 let at = &ts.fixtures;
2192
2193 let mut current_path = String::from("x");
2194 at.mkdir(&current_path);
2195
2196 for _ in 0..10 {
2197 current_path = format!("{current_path}/x");
2198 at.mkdir_all(&current_path);
2199 }
2200
2201 at.write(&format!("{current_path}/file.txt"), "deep file");
2202
2203 let result = ts.ucmd().arg("-s").arg("x").succeeds();
2204 assert!(result.stdout_str().contains('x'));
2205
2206 let result = ts.ucmd().arg("-a").arg("x").succeeds();
2207 let output = result.stdout_str();
2208 assert!(output.contains("file.txt"));
2209 }
2210 #[test]
2211 #[cfg(unix)]
2212 fn test_du_safe_traversal_with_symlinks() {
2213 let ts = TestScenario::new(util_name!());
2214 let at = &ts.fixtures;
2215
2216 let mut deep_path = String::from("symlink_test");
2217 at.mkdir(&deep_path);
2218
2219 for i in 0..8 {
2220 let dir_name = format!("{}{i}", "b".repeat(50));
2221 deep_path = format!("{deep_path}/{dir_name}");
2222 at.mkdir_all(&deep_path);
2223 }
2224
2225 at.write(&format!("{deep_path}/target.txt"), "target content");
2226
2227 at.symlink_file(&format!("{deep_path}/target.txt"), "shallow_link.txt");
2228
2229 let result = ts.ucmd().arg("-L").arg("shallow_link.txt").succeeds();
2230 assert!(!result.stdout_str().is_empty());
2231
2232 let result = ts.ucmd().arg("shallow_link.txt").succeeds();
2233 assert!(!result.stdout_str().is_empty());
2234 }
2235 #[test]
2236 #[cfg(unix)]
2237 fn test_du_inaccessible_directory() {
2238 // tested by tests/du/no-x
2239 let ts = TestScenario::new(util_name!());
2240 let at = ts.fixtures.clone();
2241
2242 at.mkdir("d");
2243 at.mkdir("d/no-x");
2244 at.mkdir("d/no-x/y");
2245
2246 at.set_mode("d/no-x", 0o600);
2247
2248 let result = ts.ucmd().arg("d").fails();
2249 result.stderr_contains("du: cannot access 'd/no-x/y': Permission denied");
2250 }
2251
2252 #[test]
2253 #[cfg(unix)]
2254 fn test_du_symlink_self_reference() {
2255 // Test symlink that points to its own directory
2256 let ts = TestScenario::new(util_name!());
2257 let at = &ts.fixtures;
2258
2259 at.mkdir("selfref");
2260 at.symlink_dir("selfref", "selfref/self");
2261
2262 let result = ts.ucmd().arg("-L").arg("selfref").succeeds();
2263
2264 result.stdout_contains("selfref");
2265 // Should not show the self-referencing symlink to avoid infinite recursion
2266 result.stdout_does_not_contain("selfref/self");
2267 }
2268
2269 #[test]
2270 #[cfg(unix)]
2271 fn test_du_long_symlink_chain() {
2272 // Test that very long symlink chains are handled gracefully
2273 let ts = TestScenario::new(util_name!());
2274 let at = &ts.fixtures;
2275
2276 // Create a simple structure that tests symlink depth limits
2277 // Instead of trying to create a chain that causes ELOOP, test that reasonable chains work
2278 at.mkdir_all("deep/level1/level2/level3/level4/level5");
2279 at.write(
2280 "deep/level1/level2/level3/level4/level5/file.txt",
2281 "content",
2282 );
2283
2284 at.symlink_dir("deep/level1", "link1");
2285 at.symlink_dir("link1/level2", "link2");
2286 at.symlink_dir("link2/level3", "link3");
2287
2288 let result = ts.ucmd().arg("-L").arg("link3").succeeds();
2289 result.stdout_contains("link3");
2290 }
2291
2292 #[test]
2293 #[cfg(all(unix, not(target_vendor = "apple")))]
2294 fn test_du_bind_mount_simulation() {
2295 // Simulate bind mount scenario using hard links where possible
2296 // Note: This test simulates what bind mounts do - making the same directory
2297 // appear in multiple places with the same inode
2298 let ts = TestScenario::new(util_name!());
2299 let at = &ts.fixtures;
2300
2301 at.mkdir_all("mount_test/subdir");
2302 at.write("mount_test/file1.txt", "content1");
2303 at.write("mount_test/subdir/file2.txt", "content2");
2304
2305 // On systems where we can't create actual bind mounts,
2306 // we test that cycle detection works with symlinks that would create similar cycles
2307 at.symlink_dir("../mount_test", "mount_test/subdir/cycle_link");
2308
2309 let result = ts.ucmd().arg("mount_test").succeeds();
2310
2311 result.stdout_contains("mount_test/subdir");
2312 result.stdout_contains("mount_test");
2313
2314 result.stdout_does_not_contain("mount_test/subdir/cycle_link");
2315 }
2316
2317 #[test]
2318 #[cfg(unix)]
2319 fn test_du_symlink_depth_tracking() {
2320 // Test that du can handle reasonable symlink chains without hitting depth limits
2321 let ts = TestScenario::new(util_name!());
2322 let at = &ts.fixtures;
2323
2324 at.mkdir_all("chain/dir1/dir2/dir3");
2325 at.write("chain/dir1/dir2/dir3/file.txt", "content");
2326
2327 at.symlink_dir("chain/dir1/dir2", "shortcut");
2328
2329 let result = ts.ucmd().arg("-L").arg("shortcut").succeeds();
2330 result.stdout_contains("shortcut/dir3");
2331 result.stdout_contains("shortcut");
2332 }
2333
2334 #[test]
2335 #[cfg(target_os = "linux")]
2336 fn test_du_long_path_from_unreadable() {
2337 // Test du behavior with unreadable directories
2338 // This verifies that du can handle very long paths when the current directory is unreadable
2339 use std::env;
2340 use std::fs;
2341 use std::os::unix::fs::PermissionsExt;
2342
2343 let ts = TestScenario::new(util_name!());
2344 let at = &ts.fixtures;
2345
2346 // Create a deep hierarchy
2347 // Use a more reasonable depth for unit tests
2348 let dir_name = "x".repeat(200);
2349 let mut current_path = String::new();
2350
2351 for i in 0..20 {
2352 if i == 0 {
2353 current_path = dir_name.clone();
2354 } else {
2355 current_path = format!("{current_path}/{dir_name}");
2356 }
2357 at.mkdir_all(&current_path);
2358 }
2359
2360 at.write(&format!("{current_path}/test.txt"), "test content");
2361
2362 at.mkdir("inaccessible");
2363
2364 let original_cwd = env::current_dir().unwrap();
2365
2366 let inaccessible_path = at.plus("inaccessible");
2367 env::set_current_dir(&inaccessible_path).unwrap();
2368
2369 // Remove read permission from the directory
2370 let mut perms = fs::metadata(&inaccessible_path).unwrap().permissions();
2371 perms.set_mode(0o000);
2372 fs::set_permissions(&inaccessible_path, perms).unwrap();
2373
2374 // Try to run du on the long path from the unreadable directory
2375 let target_path = at.plus(&dir_name);
2376 let result = ts.ucmd().arg("-s").arg(&target_path).succeeds(); // Should succeed with safe traversal
2377
2378 assert!(!result.stdout_str().is_empty());
2379 let output = result.stdout_str().trim();
2380 let parts: Vec<&str> = output.split_whitespace().collect();
2381 assert_eq!(parts.len(), 2);
2382
2383 assert!(parts[0].parse::<u64>().is_ok());
2384 assert!(parts[1].contains(&dir_name[..50])); // Check first part of the long name
2385
2386 env::set_current_dir(&original_cwd).unwrap();
2387
2388 // Restore permissions so the directory can be cleaned up
2389 let mut perms = fs::metadata(&inaccessible_path).unwrap().permissions();
2390 perms.set_mode(0o755);
2391 fs::set_permissions(&inaccessible_path, perms).unwrap();
2392 }
2393
2394 #[test]
2395 #[cfg(all(unix, not(target_os = "android")))]
2396 fn test_du_hard_links_multiple_dirs_in_args() {
2397 let ts = TestScenario::new(util_name!());
2398 let at = &ts.fixtures;
2399
2400 at.mkdir("dir1");
2401 at.mkdir("dir2");
2402 at.write("dir1/file", "hello world");
2403 at.hard_link("dir1/file", "dir2/link");
2404
2405 let result = ts.ucmd().args(&["dir1", "dir2"]).succeeds();
2406 let lines: Vec<&str> = result.stdout_str().lines().collect();
2407 let size = |i: usize| lines[i].split_once('\t').unwrap().0.parse::<u64>().unwrap();
2408 assert!(size(0) > size(1));
2409 }
2410
2411 #[test]
2412 #[cfg(all(unix, not(target_os = "android")))]
2413 fn test_du_hard_links_multiple_links_in_args() {
2414 let ts = TestScenario::new(util_name!());
2415 let at = &ts.fixtures;
2416
2417 at.mkdir("dir1");
2418 at.write("dir1/file", "hello world");
2419 at.hard_link("dir1/file", "dir1/link");
2420
2421 let result = ts.ucmd().args(&["dir1/file", "dir1/link"]).succeeds();
2422 result.stdout_contains("dir1/file");
2423 result.stdout_does_not_contain("dir1/link");
2424
2425 let result = ts.ucmd().args(&["-L", "dir1/file", "dir1/link"]).succeeds();
2426 result.stdout_contains("dir1/file");
2427 result.stdout_does_not_contain("dir1/link");
2428 }
2429
2430 #[test]
2431 #[cfg(unix)]
2432 fn test_du_symlinks_multiple_links_in_args() {
2433 let ts = TestScenario::new(util_name!());
2434 let at = &ts.fixtures;
2435
2436 at.mkdir("dir1");
2437 at.write("dir1/file", "hello world");
2438 at.symlink_file("dir1/file", "dir1/link");
2439
2440 let result = ts.ucmd().args(&["dir1/file", "dir1/link"]).succeeds();
2441 result.stdout_contains("dir1/file");
2442 result.stdout_contains("dir1/link");
2443
2444 let result = ts.ucmd().args(&["-L", "dir1/file", "dir1/link"]).succeeds();
2445 result.stdout_contains("dir1/file");
2446 result.stdout_does_not_contain("dir1/link");
2447 }
2448
2449 #[test]
2450 fn test_block_size_args_override() {
2451 let ts = TestScenario::new(util_name!());
2452 let at = &ts.fixtures;
2453 let dir = "override_args_dir";
2454 let nested_dir = "override_args_dir/nested_dir";
2455 let nested_dir_2 = "override_args_dir/nested_dir_2";
2456
2457 at.mkdir_all(nested_dir);
2458 at.mkdir_all(nested_dir_2);
2459 let fpath = at.plus(format!("{nested_dir}/file"));
2460 std::fs::File::create(fpath)
2461 .expect("cannot create test file")
2462 .set_len(100_000_000)
2463 .expect("cannot set file size");
2464
2465 let fpath2 = at.plus(format!("{nested_dir}/file_2"));
2466 std::fs::File::create(fpath2)
2467 .expect("cannot create test file")
2468 .set_len(100_000_000)
2469 .expect("cannot set file size");
2470
2471 let fpath = at.plus(format!("{nested_dir_2}/file_3"));
2472 std::fs::File::create(fpath)
2473 .expect("cannot create test file")
2474 .set_len(100_000)
2475 .expect("cannot set file size");
2476
2477 let fpath2 = at.plus(format!("{nested_dir_2}/file_4"));
2478 std::fs::File::create(fpath2)
2479 .expect("cannot create test file")
2480 .set_len(100)
2481 .expect("cannot set file size");
2482
2483 let test_cases = [
2484 (["-sk", "-m"], vec!["-sm"]),
2485 (["-sk", "-b"], vec!["-sb"]),
2486 (["-sm", "-k"], vec!["-sk"]),
2487 (["-sk", "--si"], vec!["-s", "--si"]),
2488 (["-sk", "-h"], vec!["-s", "-h"]),
2489 (["-sm", "--block-size=128"], vec!["-s", "--block-size=128"]),
2490 (["--block-size=128", "-b"], vec!["-b"]),
2491 (["--si", "-b"], vec!["-b"]),
2492 (["-h", "-b"], vec!["-b"]),
2493 ];
2494
2495 for (idx, (overwriting_args, expected)) in test_cases.into_iter().enumerate() {
2496 let overridden_args = ts
2497 .ucmd()
2498 .arg(dir)
2499 .args(&overwriting_args)
2500 .succeeds()
2501 .stdout_move_str();
2502
2503 let single_args = ts
2504 .ucmd()
2505 .arg(dir)
2506 .args(&expected)
2507 .succeeds()
2508 .stdout_move_str();
2509
2510 assert_eq!(
2511 overridden_args, single_args,
2512 "The last argument of m, k and b should overwrite. Run: {idx}"
2513 );
2514 }
2515 }
2516
2517 #[test]
2518 fn test_block_override_b_still_has_apparent_size() {
2519 let ts = TestScenario::new(util_name!());
2520 let at = &ts.fixtures;
2521 let dir = "override_args_dir";
2522 let nested_dir = "override_args_dir/nested_dir";
2523
2524 at.mkdir_all(nested_dir);
2525 let fpath = at.plus(format!("{nested_dir}/file"));
2526 std::fs::File::create(fpath)
2527 .expect("cannot create test file")
2528 .set_len(100_000_000)
2529 .expect("cannot set file size");
2530
2531 let fpath2 = at.plus(format!("{nested_dir}/file_2"));
2532 std::fs::File::create(fpath2)
2533 .expect("cannot create test file")
2534 .set_len(100_000_000)
2535 .expect("cannot set file size");
2536
2537 let test_cases = [
2538 (["-b", "-m"], ["-m", "--apparent-size"]),
2539 (["-b", "-k"], ["-k", "--apparent-size"]),
2540 (["-b", "--si"], ["--si", "--apparent-size"]),
2541 (["-b", "-h"], ["-h", "--apparent-size"]),
2542 (
2543 ["-b", "--block-size=128"],
2544 ["--block-size=128", "--apparent-size"],
2545 ),
2546 ];
2547
2548 for (idx, (overwriting_args, expected)) in test_cases.into_iter().enumerate() {
2549 let overridden_args = ts
2550 .ucmd()
2551 .arg(dir)
2552 .args(&overwriting_args)
2553 .succeeds()
2554 .stdout_move_str();
2555
2556 let single_args = ts
2557 .ucmd()
2558 .arg(dir)
2559 .args(&expected)
2560 .succeeds()
2561 .stdout_move_str();
2562
2563 assert_eq!(
2564 overridden_args, single_args,
2565 "Overwriting the b flag should still leave --apparent-size active. Run: {idx}"
2566 );
2567 }
2568 }
2569
2570 #[test]
2571 fn test_overriding_block_size_arg_with_invalid_value_still_errors() {
2572 new_ucmd!()
2573 .args(&["--block-size=abc", "-m"])
2574 .fails_with_code(1)
2575 .stderr_contains("invalid --block-size argument 'abc'");
2576 new_ucmd!()
2577 .args(&["--block-size=abc", "-k"])
2578 .fails_with_code(1)
2579 .stderr_contains("invalid --block-size argument 'abc'");
2580 new_ucmd!()
2581 .args(&["--block-size=abc", "-b"])
2582 .fails_with_code(1)
2583 .stderr_contains("invalid --block-size argument 'abc'");
2584 new_ucmd!()
2585 .args(&["--block-size=abc", "-h"])
2586 .fails_with_code(1)
2587 .stderr_contains("invalid --block-size argument 'abc'");
2588 new_ucmd!()
2589 .args(&["--block-size=abc", "--si"])
2590 .fails_with_code(1)
2591 .stderr_contains("invalid --block-size argument 'abc'");
2592 }
2593
2594 #[test]
2595 fn test_du_repeated_h() {
2596 new_ucmd!().args(&["-s", "-h", "-h"]).succeeds();
2597 }
2598
2599 #[test]
2600 fn test_du_repeated_a() {
2601 new_ucmd!().args(&["-a", "-a"]).succeeds();
2602 }
2603
2604 #[test]
2605 fn test_du_repeated_apparent_size() {
2606 new_ucmd!()
2607 .args(&["-s", "-A", "-A"])
2608 .succeeds()
2609 .stdout_only("6\t.\n");
2610 }
2611
2612 #[test]
2613 fn test_du_repeated_block_size() {
2614 new_ucmd!()
2615 .args(&["-s", "-B", "100", "-B", "100"])
2616 .succeeds();
2617 }
2618
2619 #[test]
2620 fn test_du_repeated_b() {
2621 new_ucmd!()
2622 .args(&["-s", "-b", "-b"])
2623 .succeeds()
2624 .stdout_only("5148\t.\n");
2625 }
2626
2627 #[test]
2628 fn test_du_repeated_c() {
2629 new_ucmd!().args(&["-s", "-c", "-c"]).succeeds();
2630 }
2631
2632 #[test]
2633 fn test_du_repeated_d() {
2634 new_ucmd!().args(&["-d", "2", "-d", "2"]).succeeds();
2635 }
2636
2637 #[test]
2638 fn test_du_repeated_inodes() {
2639 new_ucmd!()
2640 .args(&["-s", "--inodes", "--inodes"])
2641 .succeeds()
2642 .stdout_only("11\t.\n");
2643 }
2644
2645 #[test]
2646 fn test_du_repeated_k() {
2647 new_ucmd!().args(&["-s", "-k", "-k"]).succeeds();
2648 }
2649
2650 #[test]
2651 fn test_du_repeated_l() {
2652 new_ucmd!().args(&["-s", "-l", "-l"]).succeeds();
2653 }
2654
2655 #[test]
2656 fn test_du_repeated_dereference() {
2657 new_ucmd!().args(&["-s", "-L", "-L"]).succeeds();
2658 }
2659
2660 #[test]
2661 fn test_du_repeated_dereference_args() {
2662 new_ucmd!().args(&["-s", "-D", "-D"]).succeeds();
2663 }
2664
2665 #[test]
2666 fn test_du_repeated_no_dereference() {
2667 new_ucmd!().args(&["-s", "-P", "-P"]).succeeds();
2668 }
2669
2670 #[test]
2671 fn test_du_repeated_m() {
2672 new_ucmd!()
2673 .args(&["-s", "-m", "-m"])
2674 .succeeds()
2675 .stdout_only("1\t.\n");
2676 }
2677
2678 #[test]
2679 fn test_du_repeated_0() {
2680 new_ucmd!().args(&["-s", "-0", "-0"]).succeeds();
2681 }
2682
2683 #[test]
2684 fn test_du_repeated_separate_dirs() {
2685 new_ucmd!().args(&["-s", "-S", "-S"]).succeeds();
2686 }
2687
2688 #[test]
2689 fn test_du_repeated_s() {
2690 new_ucmd!().args(&["-s", "-s"]).succeeds();
2691 }
2692
2693 #[test]
2694 fn test_du_repeated_si() {
2695 new_ucmd!().args(&["-s", "--si", "--si"]).succeeds();
2696 }
2697
2698 #[test]
2699 fn test_du_repeated_x() {
2700 new_ucmd!().args(&["-s", "-x", "-x"]).succeeds();
2701 }
2702
2703 #[test]
2704 fn test_du_repeated_t() {
2705 new_ucmd!()
2706 .args(&["-s", "-t", "100", "-t", "100"])
2707 .succeeds();
2708 }
2709
2710 #[test]
2711 fn test_du_repeated_v() {
2712 new_ucmd!().args(&["-s", "-v", "-v"]).succeeds();
2713 }
2714
2715 #[test]
2716 fn test_du_repeated_files0_from() {
2717 let ts = TestScenario::new(util_name!());
2718 let at = &ts.fixtures;
2719
2720 // Set up the data for use with command 'du' and argument '--files0-from'.
2721 // File 'somefile' has to contain a list of NUL-terminated directory and
2722 // file names.
2723 at.mkdir("dir");
2724 at.write("dir/file2", "xyz");
2725 at.write("./somefile", "dir\0");
2726
2727 ts.ucmd()
2728 .args(&[
2729 "-s",
2730 "--files0-from",
2731 "somefile",
2732 "--files0-from",
2733 "somefile",
2734 ])
2735 .succeeds();
2736 }
2737
2738 #[test]
2739 #[cfg(feature = "touch")]
2740 fn test_du_repeated_time() {
2741 let ts = TestScenario::new(util_name!());
2742
2743 // du --time formats the timestamp according to the local timezone. We set the TZ
2744 // environment variable to UTC in the commands below to ensure consistent outputs
2745 // and test results regardless of the timezone of the machine this test runs in.
2746
2747 ts.ccmd("touch")
2748 .env("TZ", "UTC")
2749 .arg("-a")
2750 .arg("-t")
2751 .arg("201505150000")
2752 .arg("date_test")
2753 .succeeds();
2754
2755 ts.ccmd("touch")
2756 .env("TZ", "UTC")
2757 .arg("-m")
2758 .arg("-t")
2759 .arg("201606160000")
2760 .arg("date_test")
2761 .succeeds();
2762
2763 let result = ts
2764 .ucmd()
2765 .env("TZ", "UTC")
2766 .arg("--time")
2767 .arg("date_test")
2768 .arg("--time")
2769 .arg("date_test")
2770 .succeeds();
2771 result.stdout_only("0\t2016-06-16 00:00\tdate_test\n");
2772 }
2773
2774 #[test]
2775 #[cfg(feature = "touch")]
2776 fn test_du_repeated_time_style() {
2777 let ts = TestScenario::new(util_name!());
2778
2779 // du --time formats the timestamp according to the local timezone. We set the TZ
2780 // environment variable to UTC in the commands below to ensure consistent outputs
2781 // and test results regardless of the timezone of the machine this test runs in.
2782
2783 ts.ccmd("touch")
2784 .env("TZ", "UTC")
2785 .arg("-a")
2786 .arg("-t")
2787 .arg("201505150000")
2788 .arg("date_test")
2789 .succeeds();
2790
2791 ts.ccmd("touch")
2792 .env("TZ", "UTC")
2793 .arg("-m")
2794 .arg("-t")
2795 .arg("201606160000")
2796 .arg("date_test")
2797 .succeeds();
2798
2799 // full-iso
2800 let result = ts
2801 .ucmd()
2802 .env("TZ", "UTC")
2803 .arg("--time")
2804 .arg("--time-style=full-iso")
2805 .arg("--time-style=full-iso")
2806 .arg("date_test")
2807 .succeeds();
2808 result.stdout_only("0\t2016-06-16 00:00:00.000000000 +0000\tdate_test\n");
2809 }
2810
2811 #[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))]
2812 mod diagnostics {
2813 use super::*;
2814
2815 #[cfg(unix)]
2816 #[test]
2817 fn test_snippet_points_at_the_unknown_unit_of_block_size() {
2818 let result = new_ucmd!()
2819 .terminal_sim_stderr()
2820 .args(&["-B", "1fb"])
2821 .fails_with_code(1);
2822
2823 // The number parsed; only the unit did not.
2824 assert_eq!(
2825 result.stderr_as_displayed(),
2826 "\
2827 du: invalid suffix in --block-size argument '1fb'
2828 ╭─[ du:1:8 ]
2829 │
2830 1 │ du -B 1fb
2831 │ ─┬
2832 │ ╰── not a known unit
2833 │
2834 │ Help: a size is a number and an optional unit: K, M, G and so on for 1024, KB, MB, GB for 1000
2835 ───╯"
2836 );
2837 }
2838
2839 #[cfg(unix)]
2840 #[test]
2841 fn test_snippet_points_inside_an_attached_block_size() {
2842 let result = new_ucmd!()
2843 .terminal_sim_stderr()
2844 .arg("--block-size=1fb")
2845 .fails_with_code(1);
2846
2847 assert_eq!(result.caret_column(), Some(18));
2848 assert!(
2849 result.stderr_as_displayed().contains("not a known unit"),
2850 "{}",
2851 result.stderr_as_displayed()
2852 );
2853 }
2854
2855 #[cfg(unix)]
2856 #[test]
2857 fn test_snippet_underlines_a_zero_block_size() {
2858 let result = new_ucmd!()
2859 .terminal_sim_stderr()
2860 .args(&["-B", "0"])
2861 .fails_with_code(1);
2862 let stderr = result.stderr_as_displayed();
2863
2864 // Zero parses as a number but is not a usable block size, so the
2865 // whole value is underlined and nothing is said about the unit.
2866 assert!(
2867 stderr.starts_with("du: invalid --block-size argument '0'"),
2868 "{stderr}"
2869 );
2870 assert_eq!(result.caret_column(), Some(7));
2871 assert!(!stderr.contains("not a known unit"), "{stderr}");
2872 }
2873
2874 #[cfg(unix)]
2875 #[test]
2876 fn test_snippet_underlines_the_sign_of_a_rejected_threshold() {
2877 let result = new_ucmd!()
2878 .terminal_sim_stderr()
2879 .args(&["-t", "-0"])
2880 .fails_with_code(1);
2881 let stderr = result.stderr_as_displayed();
2882
2883 // '-0' is rejected for the sign and the zero together, so the caret
2884 // covers both.
2885 assert!(
2886 stderr.starts_with("du: invalid --threshold argument '-0'"),
2887 "{stderr}"
2888 );
2889 assert_eq!(result.caret_column(), Some(7));
2890 }
2891
2892 #[cfg(unix)]
2893 #[test]
2894 fn test_snippet_skips_the_sign_of_a_threshold_with_an_unknown_unit() {
2895 let result = new_ucmd!()
2896 .terminal_sim_stderr()
2897 .args(&["-t", "-1fb"])
2898 .fails_with_code(1);
2899
2900 // Here only the unit is at fault, so the sign is not underlined.
2901 assert_eq!(result.caret_column(), Some(9));
2902 assert!(
2903 result.stderr_as_displayed().contains("not a known unit"),
2904 "{}",
2905 result.stderr_as_displayed()
2906 );
2907 }
2908
2909 #[test]
2910 fn test_plain_message_when_stderr_is_a_pipe() {
2911 new_ucmd!()
2912 .args(&["-B", "1fb"])
2913 .fails_with_code(1)
2914 .stderr_is("du: invalid suffix in --block-size argument '1fb'\n");
2915 }
2916 }