Skip to content

Commit 613e9be

Browse files
committed
chore(stat): add test case for file metadata
1 parent c30593a commit 613e9be

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

tests/by-util/test_stat.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ use uutests::unwrap_or_return;
99
use uutests::util::{TestScenario, expected_result};
1010
use uutests::util_name;
1111

12+
use std::fs::metadata;
13+
use std::os::unix::fs::MetadataExt;
14+
1215
#[test]
1316
fn test_invalid_arg() {
1417
new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
@@ -578,3 +581,54 @@ fn test_percent_escaping() {
578581
.succeeds();
579582
assert_eq!(result.stdout_str(), "%/%m/%%");
580583
}
584+
585+
#[cfg(unix)]
586+
#[test]
587+
fn test_correct_metadata() {
588+
use uucore::fs::{major, minor};
589+
590+
let ts = TestScenario::new(util_name!());
591+
let parse = |(i, str): (usize, &str)| {
592+
// Some outputs (%[fDRtT]) are in hex; they're redundant, but we might
593+
// as well also test case conversion.
594+
let radix = if matches!(i, 2 | 10 | 14..) { 16 } else { 10 };
595+
i128::from_str_radix(str, radix)
596+
};
597+
for device in ["/", "/dev/null"] {
598+
let metadata = metadata(device).unwrap();
599+
// We avoid time vals because of fs race conditions, especially with
600+
// access time and status time (this previously killed an otherwise
601+
// perfect 11-hour-long CI run...). The large number of as-casts is
602+
// due to inconsistencies on some platforms (read: BSDs), and we use
603+
// i128 as a lowest-common denominator.
604+
let test_str = "%u %g %f %b %s %h %i %d %Hd %Ld %D %r %Hr %Lr %R %t %T";
605+
let expected = [
606+
metadata.uid() as _,
607+
metadata.gid() as _,
608+
metadata.mode() as _,
609+
metadata.blocks() as _,
610+
metadata.size() as _,
611+
metadata.nlink() as _,
612+
metadata.ino() as _,
613+
metadata.dev() as _,
614+
major(metadata.dev() as _) as _,
615+
minor(metadata.dev() as _) as _,
616+
metadata.dev() as _,
617+
metadata.rdev() as _,
618+
major(metadata.rdev() as _) as _,
619+
minor(metadata.rdev() as _) as _,
620+
metadata.rdev() as _,
621+
major(metadata.rdev() as _) as _,
622+
minor(metadata.rdev() as _) as _,
623+
];
624+
let result = ts.ucmd().args(&["--printf", test_str, device]).succeeds();
625+
let output = result
626+
.stdout_str()
627+
.split(' ')
628+
.enumerate()
629+
.map(parse)
630+
.collect::<Result<Vec<i128>, _>>()
631+
.unwrap();
632+
assert_eq!(output, &expected);
633+
}
634+
}

0 commit comments

Comments
 (0)