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() {
vec!["./".to_owned()]
vec!["./".to_owned()] // TODO: gnu `du` doesn't use trailing "/" here
} else {
matches.free.clone()
};

View file

@ -10,7 +10,7 @@ fn test_du_basics() {
new_ucmd!().succeeds().no_stderr();
}
#[cfg(target_vendor = "apple")]
fn _du_basics(s: String) {
fn _du_basics(s: &str) {
let answer = "32\t./subdir
8\t./subdir/deeper
24\t./subdir/links
@ -30,11 +30,18 @@ fn _du_basics(s: &str) {
#[test]
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();
assert!(result.success);
assert_eq!(result.stderr, "");
let result = scene.ucmd().arg(SUB_DIR).succeeds();
#[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());
}
@ -58,26 +65,29 @@ fn _du_basics_subdir(s: &str) {
#[test]
fn test_du_basics_bad_name() {
let (_at, mut ucmd) = at_and_ucmd!();
let result = ucmd.arg("bad_name").run();
assert_eq!(result.stdout_str(), "");
assert_eq!(
result.stderr,
"du: error: bad_name: No such file or directory\n"
);
new_ucmd!()
.arg("bad_name")
.succeeds() // TODO: replace with ".fails()" once `du` is fixed
.stderr_only("du: error: bad_name: No such file or directory\n");
}
#[test]
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();
assert!(link.success);
at.symlink_file(SUB_FILE, SUB_LINK);
let result = ts.ucmd().arg(SUB_DIR_LINKS).run();
assert!(result.success);
assert_eq!(result.stderr, "");
let result = scene.ucmd().arg(SUB_DIR_LINKS).succeeds();
#[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());
}
@ -102,14 +112,23 @@ fn _du_soft_link(s: &str) {
#[test]
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();
assert!(link.success);
let result_ln = scene.cmd("ln").arg(SUB_FILE).arg(SUB_LINK).run();
if !result_ln.succeeded() {
scene.ccmd("ln").arg(SUB_FILE).arg(SUB_LINK).succeeds();
}
let result = ts.ucmd().arg(SUB_DIR_LINKS).run();
assert!(result.success);
assert_eq!(result.stderr, "");
let result = scene.ucmd().arg(SUB_DIR_LINKS).succeeds();
#[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
_du_hard_link(result.stdout_str());
}
@ -134,11 +153,23 @@ fn _du_hard_link(s: &str) {
#[test]
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();
assert!(result.success);
assert_eq!(result.stderr, "");
let result = scene.ucmd().arg("-d1").succeeds();
#[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());
}
@ -162,9 +193,7 @@ fn _du_d_flag(s: &str) {
#[test]
fn test_du_h_flag_empty_file() {
let ts = TestScenario::new("du");
ts.ucmd()
new_ucmd!()
.arg("-h")
.arg("empty.txt")
.succeeds()
@ -174,54 +203,51 @@ fn test_du_h_flag_empty_file() {
#[cfg(feature = "touch")]
#[test]
fn test_du_time() {
let ts = TestScenario::new("du");
let scene = TestScenario::new(util_name!());
let touch = ts
scene
.ccmd("touch")
.arg("-a")
.arg("-m")
.arg("-t")
.arg("201505150000")
.arg("date_test")
.run();
assert!(touch.success);
.succeeds();
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
ts.cmd("rm").arg("date_test").run();
assert!(result.success);
assert_eq!(result.stderr, "");
assert_eq!(result.stdout, "0\t2015-05-15 00:00\tdate_test\n");
scene.cmd("rm").arg("date_test").succeeds(); // TODO: is this necessary?
}
#[cfg(not(target_os = "windows"))]
#[cfg(feature = "chmod")]
#[test]
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();
println!("chmod output: {:?}", chmod);
assert!(chmod.success);
let result = ts.ucmd().arg(SUB_DIR_LINKS).run();
let _chmod = ts.ccmd("chmod").arg("-r").arg(SUB_DIR_LINKS).succeeds();
let result = ts.ucmd().arg(SUB_DIR_LINKS).succeeds();
ts.ccmd("chmod").arg("+r").arg(SUB_DIR_LINKS).run();
assert!(result.success);
assert_eq!(
result.stderr,
result.stderr_str(),
"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")]
fn _du_no_permission(s: String) {
fn _du_no_permission(s: &str) {
assert_eq!(s, "0\tsubdir/links\n");
}
#[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");
}