1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

du: fix tests on linux (#2066) (#2090)

This commit is contained in:
Jan Scheer 2021-04-19 10:45:51 +02:00 committed by GitHub
parent df0304d8f4
commit 049f21a199
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 53 deletions

View file

@ -500,7 +500,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
}; };
let strs = if matches.free.is_empty() { let strs = if matches.free.is_empty() {
vec!["./".to_owned()] vec!["./".to_owned()] // TODO: gnu `du` doesn't use trailing "/" here
} else { } else {
matches.free.clone() matches.free.clone()
}; };

View file

@ -10,7 +10,7 @@ fn test_du_basics() {
new_ucmd!().succeeds().no_stderr(); new_ucmd!().succeeds().no_stderr();
} }
#[cfg(target_vendor = "apple")] #[cfg(target_vendor = "apple")]
fn _du_basics(s: String) { fn _du_basics(s: &str) {
let answer = "32\t./subdir let answer = "32\t./subdir
8\t./subdir/deeper 8\t./subdir/deeper
24\t./subdir/links 24\t./subdir/links
@ -30,11 +30,18 @@ fn _du_basics(s: &str) {
#[test] #[test]
fn test_du_basics_subdir() { fn test_du_basics_subdir() {
let (_at, mut ucmd) = at_and_ucmd!(); let scene = TestScenario::new(util_name!());
let result = ucmd.arg(SUB_DIR).run(); let result = scene.ucmd().arg(SUB_DIR).succeeds();
assert!(result.success);
assert_eq!(result.stderr, ""); #[cfg(target_os = "linux")]
{
let result_reference = scene.cmd("du").arg(SUB_DIR).run();
if result_reference.succeeded() {
assert_eq!(result.stdout_str(), result_reference.stdout_str());
return;
}
}
_du_basics_subdir(result.stdout_str()); _du_basics_subdir(result.stdout_str());
} }
@ -58,26 +65,29 @@ fn _du_basics_subdir(s: &str) {
#[test] #[test]
fn test_du_basics_bad_name() { fn test_du_basics_bad_name() {
let (_at, mut ucmd) = at_and_ucmd!(); new_ucmd!()
.arg("bad_name")
let result = ucmd.arg("bad_name").run(); .succeeds() // TODO: replace with ".fails()" once `du` is fixed
assert_eq!(result.stdout_str(), ""); .stderr_only("du: error: bad_name: No such file or directory\n");
assert_eq!(
result.stderr,
"du: error: bad_name: No such file or directory\n"
);
} }
#[test] #[test]
fn test_du_soft_link() { fn test_du_soft_link() {
let ts = TestScenario::new("du"); let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let link = ts.ccmd("ln").arg("-s").arg(SUB_FILE).arg(SUB_LINK).run(); at.symlink_file(SUB_FILE, SUB_LINK);
assert!(link.success);
let result = ts.ucmd().arg(SUB_DIR_LINKS).run(); let result = scene.ucmd().arg(SUB_DIR_LINKS).succeeds();
assert!(result.success);
assert_eq!(result.stderr, ""); #[cfg(target_os = "linux")]
{
let result_reference = scene.cmd("du").arg(SUB_DIR_LINKS).run();
if result_reference.succeeded() {
assert_eq!(result.stdout_str(), result_reference.stdout_str());
return;
}
}
_du_soft_link(result.stdout_str()); _du_soft_link(result.stdout_str());
} }
@ -102,14 +112,23 @@ fn _du_soft_link(s: &str) {
#[test] #[test]
fn test_du_hard_link() { fn test_du_hard_link() {
let ts = TestScenario::new("du"); let scene = TestScenario::new(util_name!());
let link = ts.ccmd("ln").arg(SUB_FILE).arg(SUB_LINK).run(); let result_ln = scene.cmd("ln").arg(SUB_FILE).arg(SUB_LINK).run();
assert!(link.success); if !result_ln.succeeded() {
scene.ccmd("ln").arg(SUB_FILE).arg(SUB_LINK).succeeds();
}
let result = ts.ucmd().arg(SUB_DIR_LINKS).run(); let result = scene.ucmd().arg(SUB_DIR_LINKS).succeeds();
assert!(result.success);
assert_eq!(result.stderr, ""); #[cfg(target_os = "linux")]
{
let result_reference = scene.cmd("du").arg(SUB_DIR_LINKS).run();
if result_reference.succeeded() {
assert_eq!(result.stdout_str(), result_reference.stdout_str());
return;
}
}
// We do not double count hard links as the inodes are identical // We do not double count hard links as the inodes are identical
_du_hard_link(result.stdout_str()); _du_hard_link(result.stdout_str());
} }
@ -134,11 +153,23 @@ fn _du_hard_link(s: &str) {
#[test] #[test]
fn test_du_d_flag() { fn test_du_d_flag() {
let ts = TestScenario::new("du"); let scene = TestScenario::new(util_name!());
let result = ts.ucmd().arg("-d").arg("1").run(); let result = scene.ucmd().arg("-d1").succeeds();
assert!(result.success);
assert_eq!(result.stderr, ""); #[cfg(target_os = "linux")]
{
let result_reference = scene.cmd("du").arg("-d1").run();
if result_reference.succeeded() {
assert_eq!(
// TODO: gnu `du` doesn't use trailing "/" here
// result.stdout_str(), result_reference.stdout_str()
result.stdout_str().trim_end_matches("/\n"),
result_reference.stdout_str().trim_end_matches("\n")
);
return;
}
}
_du_d_flag(result.stdout_str()); _du_d_flag(result.stdout_str());
} }
@ -162,9 +193,7 @@ fn _du_d_flag(s: &str) {
#[test] #[test]
fn test_du_h_flag_empty_file() { fn test_du_h_flag_empty_file() {
let ts = TestScenario::new("du"); new_ucmd!()
ts.ucmd()
.arg("-h") .arg("-h")
.arg("empty.txt") .arg("empty.txt")
.succeeds() .succeeds()
@ -174,54 +203,51 @@ fn test_du_h_flag_empty_file() {
#[cfg(feature = "touch")] #[cfg(feature = "touch")]
#[test] #[test]
fn test_du_time() { fn test_du_time() {
let ts = TestScenario::new("du"); let scene = TestScenario::new(util_name!());
let touch = ts scene
.ccmd("touch") .ccmd("touch")
.arg("-a") .arg("-a")
.arg("-m") .arg("-m")
.arg("-t") .arg("-t")
.arg("201505150000") .arg("201505150000")
.arg("date_test") .arg("date_test")
.run(); .succeeds();
assert!(touch.success);
let result = ts.ucmd().arg("--time").arg("date_test").run(); scene
.ucmd()
.arg("--time")
.arg("date_test")
.succeeds()
.stdout_only("0\t2015-05-15 00:00\tdate_test\n");
// cleanup by removing test file // cleanup by removing test file
ts.cmd("rm").arg("date_test").run(); scene.cmd("rm").arg("date_test").succeeds(); // TODO: is this necessary?
assert!(result.success);
assert_eq!(result.stderr, "");
assert_eq!(result.stdout, "0\t2015-05-15 00:00\tdate_test\n");
} }
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
#[cfg(feature = "chmod")] #[cfg(feature = "chmod")]
#[test] #[test]
fn test_du_no_permission() { fn test_du_no_permission() {
let ts = TestScenario::new("du"); let ts = TestScenario::new(util_name!());
let chmod = ts.ccmd("chmod").arg("-r").arg(SUB_DIR_LINKS).run(); let _chmod = ts.ccmd("chmod").arg("-r").arg(SUB_DIR_LINKS).succeeds();
println!("chmod output: {:?}", chmod); let result = ts.ucmd().arg(SUB_DIR_LINKS).succeeds();
assert!(chmod.success);
let result = ts.ucmd().arg(SUB_DIR_LINKS).run();
ts.ccmd("chmod").arg("+r").arg(SUB_DIR_LINKS).run(); ts.ccmd("chmod").arg("+r").arg(SUB_DIR_LINKS).run();
assert!(result.success);
assert_eq!( assert_eq!(
result.stderr, result.stderr_str(),
"du: cannot read directory subdir/links: Permission denied (os error 13)\n" "du: cannot read directory subdir/links: Permission denied (os error 13)\n"
); );
_du_no_permission(result.stdout); _du_no_permission(result.stdout_str());
} }
#[cfg(target_vendor = "apple")] #[cfg(target_vendor = "apple")]
fn _du_no_permission(s: String) { fn _du_no_permission(s: &str) {
assert_eq!(s, "0\tsubdir/links\n"); assert_eq!(s, "0\tsubdir/links\n");
} }
#[cfg(all(not(target_vendor = "apple"), not(target_os = "windows")))] #[cfg(all(not(target_vendor = "apple"), not(target_os = "windows")))]
fn _du_no_permission(s: String) { fn _du_no_permission(s: &str) {
assert_eq!(s, "4\tsubdir/links\n"); assert_eq!(s, "4\tsubdir/links\n");
} }