2020-05-25 17:05:26 +00:00
|
|
|
|
use crate::common::util::*;
|
2018-03-20 16:10:05 +00:00
|
|
|
|
|
2018-03-22 16:05:33 +00:00
|
|
|
|
const SUB_DIR: &str = "subdir/deeper";
|
|
|
|
|
const SUB_DIR_LINKS: &str = "subdir/links";
|
|
|
|
|
const SUB_FILE: &str = "subdir/links/subwords.txt";
|
|
|
|
|
const SUB_LINK: &str = "subdir/links/sublink.txt";
|
2018-03-20 16:10:05 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_du_basics() {
|
2021-04-17 09:22:49 +00:00
|
|
|
|
new_ucmd!().succeeds().no_stderr();
|
2018-03-21 14:14:18 +00:00
|
|
|
|
}
|
2021-03-18 01:32:34 +00:00
|
|
|
|
#[cfg(target_vendor = "apple")]
|
2018-03-21 14:14:18 +00:00
|
|
|
|
fn _du_basics(s: String) {
|
2018-03-20 21:55:31 +00:00
|
|
|
|
let answer = "32\t./subdir
|
|
|
|
|
8\t./subdir/deeper
|
|
|
|
|
24\t./subdir/links
|
|
|
|
|
40\t./
|
|
|
|
|
";
|
2018-03-21 14:14:18 +00:00
|
|
|
|
assert_eq!(s, answer);
|
|
|
|
|
}
|
2021-03-18 01:32:34 +00:00
|
|
|
|
#[cfg(not(target_vendor = "apple"))]
|
2021-04-05 20:03:43 +00:00
|
|
|
|
fn _du_basics(s: &str) {
|
2018-03-21 14:14:18 +00:00
|
|
|
|
let answer = "28\t./subdir
|
|
|
|
|
8\t./subdir/deeper
|
|
|
|
|
16\t./subdir/links
|
|
|
|
|
36\t./
|
|
|
|
|
";
|
|
|
|
|
assert_eq!(s, answer);
|
2018-03-20 16:10:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_du_basics_subdir() {
|
|
|
|
|
let (_at, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
|
|
|
|
|
let result = ucmd.arg(SUB_DIR).run();
|
|
|
|
|
assert!(result.success);
|
|
|
|
|
assert_eq!(result.stderr, "");
|
2021-04-05 20:03:43 +00:00
|
|
|
|
_du_basics_subdir(result.stdout_str());
|
2018-03-21 14:14:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 01:32:34 +00:00
|
|
|
|
#[cfg(target_vendor = "apple")]
|
2021-04-05 20:03:43 +00:00
|
|
|
|
fn _du_basics_subdir(s: &str) {
|
2018-03-22 16:05:33 +00:00
|
|
|
|
assert_eq!(s, "4\tsubdir/deeper\n");
|
2018-03-21 14:14:18 +00:00
|
|
|
|
}
|
2021-04-01 21:16:47 +00:00
|
|
|
|
#[cfg(target_os = "windows")]
|
2021-04-05 20:03:43 +00:00
|
|
|
|
fn _du_basics_subdir(s: &str) {
|
2021-04-01 21:16:47 +00:00
|
|
|
|
assert_eq!(s, "0\tsubdir/deeper\n");
|
|
|
|
|
}
|
|
|
|
|
#[cfg(all(not(target_vendor = "apple"), not(target_os = "windows")))]
|
2021-04-05 20:03:43 +00:00
|
|
|
|
fn _du_basics_subdir(s: &str) {
|
2019-12-25 06:42:11 +00:00
|
|
|
|
// MS-WSL linux has altered expected output
|
|
|
|
|
if !is_wsl() {
|
|
|
|
|
assert_eq!(s, "8\tsubdir/deeper\n");
|
|
|
|
|
} else {
|
|
|
|
|
assert_eq!(s, "0\tsubdir/deeper\n");
|
|
|
|
|
}
|
2018-03-20 16:10:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_du_basics_bad_name() {
|
|
|
|
|
let (_at, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
|
|
|
|
|
let result = ucmd.arg("bad_name").run();
|
2021-04-05 20:03:43 +00:00
|
|
|
|
assert_eq!(result.stdout_str(), "");
|
2018-03-22 16:05:33 +00:00
|
|
|
|
assert_eq!(
|
|
|
|
|
result.stderr,
|
|
|
|
|
"du: error: bad_name: No such file or directory\n"
|
|
|
|
|
);
|
2018-03-20 16:10:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_du_soft_link() {
|
|
|
|
|
let ts = TestScenario::new("du");
|
|
|
|
|
|
2021-04-01 21:16:47 +00:00
|
|
|
|
let link = ts.ccmd("ln").arg("-s").arg(SUB_FILE).arg(SUB_LINK).run();
|
2018-03-20 16:10:05 +00:00
|
|
|
|
assert!(link.success);
|
|
|
|
|
|
2018-03-20 21:55:31 +00:00
|
|
|
|
let result = ts.ucmd().arg(SUB_DIR_LINKS).run();
|
2018-03-20 16:10:05 +00:00
|
|
|
|
assert!(result.success);
|
|
|
|
|
assert_eq!(result.stderr, "");
|
2021-04-05 20:03:43 +00:00
|
|
|
|
_du_soft_link(result.stdout_str());
|
2018-03-21 14:14:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 01:32:34 +00:00
|
|
|
|
#[cfg(target_vendor = "apple")]
|
2021-04-05 20:03:43 +00:00
|
|
|
|
fn _du_soft_link(s: &str) {
|
2020-01-01 23:06:56 +00:00
|
|
|
|
// 'macos' host variants may have `du` output variation for soft links
|
|
|
|
|
assert!((s == "12\tsubdir/links\n") || (s == "16\tsubdir/links\n"));
|
2018-03-21 14:14:18 +00:00
|
|
|
|
}
|
2021-04-01 21:16:47 +00:00
|
|
|
|
#[cfg(target_os = "windows")]
|
2021-04-05 20:03:43 +00:00
|
|
|
|
fn _du_soft_link(s: &str) {
|
2021-04-01 21:16:47 +00:00
|
|
|
|
assert_eq!(s, "8\tsubdir/links\n");
|
|
|
|
|
}
|
|
|
|
|
#[cfg(all(not(target_vendor = "apple"), not(target_os = "windows")))]
|
2021-04-05 20:03:43 +00:00
|
|
|
|
fn _du_soft_link(s: &str) {
|
2019-12-25 06:42:11 +00:00
|
|
|
|
// MS-WSL linux has altered expected output
|
|
|
|
|
if !is_wsl() {
|
|
|
|
|
assert_eq!(s, "16\tsubdir/links\n");
|
|
|
|
|
} else {
|
|
|
|
|
assert_eq!(s, "8\tsubdir/links\n");
|
|
|
|
|
}
|
2018-03-20 16:10:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-20 20:40:27 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_du_hard_link() {
|
|
|
|
|
let ts = TestScenario::new("du");
|
|
|
|
|
|
2021-04-01 21:16:47 +00:00
|
|
|
|
let link = ts.ccmd("ln").arg(SUB_FILE).arg(SUB_LINK).run();
|
2018-03-20 20:40:27 +00:00
|
|
|
|
assert!(link.success);
|
|
|
|
|
|
2018-03-20 21:55:31 +00:00
|
|
|
|
let result = ts.ucmd().arg(SUB_DIR_LINKS).run();
|
2018-03-20 20:40:27 +00:00
|
|
|
|
assert!(result.success);
|
|
|
|
|
assert_eq!(result.stderr, "");
|
2018-03-22 16:05:33 +00:00
|
|
|
|
// We do not double count hard links as the inodes are identical
|
2021-04-05 20:03:43 +00:00
|
|
|
|
_du_hard_link(result.stdout_str());
|
2018-03-21 14:14:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 01:32:34 +00:00
|
|
|
|
#[cfg(target_vendor = "apple")]
|
2021-04-05 20:03:43 +00:00
|
|
|
|
fn _du_hard_link(s: &str) {
|
2018-03-22 16:05:33 +00:00
|
|
|
|
assert_eq!(s, "12\tsubdir/links\n")
|
2018-03-21 14:14:18 +00:00
|
|
|
|
}
|
2021-04-01 21:16:47 +00:00
|
|
|
|
#[cfg(target_os = "windows")]
|
2021-04-05 20:03:43 +00:00
|
|
|
|
fn _du_hard_link(s: &str) {
|
2021-04-01 21:16:47 +00:00
|
|
|
|
assert_eq!(s, "8\tsubdir/links\n")
|
|
|
|
|
}
|
|
|
|
|
#[cfg(all(not(target_vendor = "apple"), not(target_os = "windows")))]
|
2021-04-05 20:03:43 +00:00
|
|
|
|
fn _du_hard_link(s: &str) {
|
2019-12-25 06:42:11 +00:00
|
|
|
|
// MS-WSL linux has altered expected output
|
|
|
|
|
if !is_wsl() {
|
|
|
|
|
assert_eq!(s, "16\tsubdir/links\n");
|
|
|
|
|
} else {
|
|
|
|
|
assert_eq!(s, "8\tsubdir/links\n");
|
|
|
|
|
}
|
2018-03-20 20:40:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-20 21:55:31 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_du_d_flag() {
|
|
|
|
|
let ts = TestScenario::new("du");
|
|
|
|
|
|
|
|
|
|
let result = ts.ucmd().arg("-d").arg("1").run();
|
|
|
|
|
assert!(result.success);
|
|
|
|
|
assert_eq!(result.stderr, "");
|
2021-04-05 20:03:43 +00:00
|
|
|
|
_du_d_flag(result.stdout_str());
|
2018-03-21 14:14:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 01:32:34 +00:00
|
|
|
|
#[cfg(target_vendor = "apple")]
|
2021-04-05 20:03:43 +00:00
|
|
|
|
fn _du_d_flag(s: &str) {
|
2018-03-22 16:05:33 +00:00
|
|
|
|
assert_eq!(s, "16\t./subdir\n20\t./\n");
|
2018-03-21 14:14:18 +00:00
|
|
|
|
}
|
2021-04-01 21:16:47 +00:00
|
|
|
|
#[cfg(target_os = "windows")]
|
2021-04-05 20:03:43 +00:00
|
|
|
|
fn _du_d_flag(s: &str) {
|
2021-04-01 21:16:47 +00:00
|
|
|
|
assert_eq!(s, "8\t./subdir\n8\t./\n");
|
|
|
|
|
}
|
|
|
|
|
#[cfg(all(not(target_vendor = "apple"), not(target_os = "windows")))]
|
2021-04-05 20:03:43 +00:00
|
|
|
|
fn _du_d_flag(s: &str) {
|
2019-12-25 06:42:11 +00:00
|
|
|
|
// MS-WSL linux has altered expected output
|
|
|
|
|
if !is_wsl() {
|
|
|
|
|
assert_eq!(s, "28\t./subdir\n36\t./\n");
|
|
|
|
|
} else {
|
|
|
|
|
assert_eq!(s, "8\t./subdir\n8\t./\n");
|
|
|
|
|
}
|
2018-03-20 21:55:31 +00:00
|
|
|
|
}
|
2021-04-01 23:42:43 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_du_h_flag_empty_file() {
|
|
|
|
|
let ts = TestScenario::new("du");
|
|
|
|
|
|
2021-04-05 20:03:43 +00:00
|
|
|
|
ts.ucmd()
|
|
|
|
|
.arg("-h")
|
|
|
|
|
.arg("empty.txt")
|
|
|
|
|
.succeeds()
|
|
|
|
|
.stdout_only("0\tempty.txt\n");
|
2021-04-01 23:42:43 +00:00
|
|
|
|
}
|
2021-04-07 06:41:04 +00:00
|
|
|
|
|
|
|
|
|
#[cfg(feature = "touch")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_du_time() {
|
|
|
|
|
let ts = TestScenario::new("du");
|
|
|
|
|
|
2021-04-17 09:22:49 +00:00
|
|
|
|
let touch = ts
|
|
|
|
|
.ccmd("touch")
|
|
|
|
|
.arg("-a")
|
|
|
|
|
.arg("-m")
|
|
|
|
|
.arg("-t")
|
|
|
|
|
.arg("201505150000")
|
|
|
|
|
.arg("date_test")
|
|
|
|
|
.run();
|
2021-04-07 06:41:04 +00:00
|
|
|
|
assert!(touch.success);
|
|
|
|
|
|
|
|
|
|
let result = ts.ucmd().arg("--time").arg("date_test").run();
|
|
|
|
|
|
|
|
|
|
// 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");
|
|
|
|
|
}
|
2021-04-17 08:26:52 +00:00
|
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "windows"))]
|
|
|
|
|
#[cfg(feature = "chmod")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_du_no_permission() {
|
|
|
|
|
let ts = TestScenario::new("du");
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
ts.ccmd("chmod").arg("+r").arg(SUB_DIR_LINKS).run();
|
|
|
|
|
|
|
|
|
|
assert!(result.success);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
result.stderr,
|
|
|
|
|
"du: cannot read directory ‘subdir/links‘: Permission denied (os error 13)\n"
|
|
|
|
|
);
|
|
|
|
|
_du_no_permission(result.stdout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_vendor = "apple")]
|
|
|
|
|
fn _du_no_permission(s: String) {
|
|
|
|
|
assert_eq!(s, "0\tsubdir/links\n");
|
|
|
|
|
}
|
|
|
|
|
#[cfg(all(not(target_vendor = "apple"), not(target_os = "windows")))]
|
|
|
|
|
fn _du_no_permission(s: String) {
|
|
|
|
|
assert_eq!(s, "4\tsubdir/links\n");
|
|
|
|
|
}
|