Merge pull request #4991 from shinhs0506/du_apparent

du: directories have apparent size of 0
This commit is contained in:
Daniel Hofstetter 2023-06-26 14:58:40 +02:00 committed by GitHub
commit 9149409065
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 80 deletions

View file

@ -145,7 +145,7 @@ impl Stat {
return Ok(Self {
path: path.to_path_buf(),
is_dir: metadata.is_dir(),
size: metadata.len(),
size: if path.is_dir() { 0 } else { metadata.len() },
blocks: metadata.blocks(),
inodes: 1,
inode: Some(file_info),
@ -162,7 +162,7 @@ impl Stat {
Ok(Self {
path: path.to_path_buf(),
is_dir: metadata.is_dir(),
size: metadata.len(),
size: if path.is_dir() { 0 } else { metadata.len() },
blocks: size_on_disk / 1024 * 2,
inode: file_info,
inodes: 1,

View file

@ -580,97 +580,58 @@ fn test_du_invalid_threshold() {
#[test]
fn test_du_apparent_size() {
let ts = TestScenario::new(util_name!());
let result = ts.ucmd().arg("--apparent-size").succeeds();
let (at, mut ucmd) = at_and_ucmd!();
#[cfg(any(target_os = "linux", target_os = "android"))]
at.mkdir_all("a/b");
at.write("a/b/file1", "foo");
at.write("a/b/file2", "foobar");
let result = ucmd.args(&["--apparent-size", "--all", "a"]).succeeds();
#[cfg(not(target_os = "windows"))]
{
let result_reference = unwrap_or_return!(expected_result(&ts, &["--apparent-size"]));
assert_eq!(result.stdout_str(), result_reference.stdout_str());
result.stdout_contains_line("1\ta/b/file2");
result.stdout_contains_line("1\ta/b/file1");
result.stdout_contains_line("1\ta/b");
result.stdout_contains_line("1\ta");
}
#[cfg(not(any(target_os = "linux", target_os = "android")))]
_du_apparent_size(result.stdout_str());
}
#[cfg(target_os = "windows")]
fn _du_apparent_size(s: &str) {
assert_eq!(
s,
"1\t.\\subdir\\deeper\\deeper_dir
1\t.\\subdir\\deeper
6\t.\\subdir\\links
6\t.\\subdir
6\t.
"
);
}
#[cfg(target_vendor = "apple")]
fn _du_apparent_size(s: &str) {
assert_eq!(
s,
"1\t./subdir/deeper/deeper_dir
1\t./subdir/deeper
6\t./subdir/links
6\t./subdir
6\t.
"
);
}
#[cfg(target_os = "freebsd")]
fn _du_apparent_size(s: &str) {
assert_eq!(
s,
"1\t./subdir/deeper/deeper_dir
2\t./subdir/deeper
6\t./subdir/links
8\t./subdir
8\t.
"
);
}
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "freebsd")
))]
fn _du_apparent_size(s: &str) {
assert_eq!(
s,
"5\t./subdir/deeper/deeper_dir
9\t./subdir/deeper
10\t./subdir/links
22\t./subdir
26\t.
"
);
#[cfg(target_os = "windows")]
{
result.stdout_contains_line("1\ta\\b\\file2");
result.stdout_contains_line("1\ta\\b\\file1");
result.stdout_contains_line("1\ta\\b");
result.stdout_contains_line("1\ta");
}
}
#[test]
fn test_du_bytes() {
let ts = TestScenario::new(util_name!());
let result = ts.ucmd().arg("--bytes").succeeds();
let (at, mut ucmd) = at_and_ucmd!();
#[cfg(any(target_os = "linux", target_os = "android"))]
at.mkdir_all("a/b");
at.write("a/b/file1", "foo");
at.write("a/b/file2", "foobar");
let result = ucmd.args(&["--bytes", "--all", "a"]).succeeds();
#[cfg(not(target_os = "windows"))]
{
let result_reference = unwrap_or_return!(expected_result(&ts, &["--bytes"]));
assert_eq!(result.stdout_str(), result_reference.stdout_str());
result.stdout_contains_line("6\ta/b/file2");
result.stdout_contains_line("3\ta/b/file1");
result.stdout_contains_line("9\ta/b");
result.stdout_contains_line("9\ta");
}
#[cfg(target_os = "windows")]
result.stdout_contains("5145\t.\\subdir\n");
#[cfg(target_vendor = "apple")]
result.stdout_contains("5625\t./subdir\n");
#[cfg(target_os = "freebsd")]
result.stdout_contains("7193\t./subdir\n");
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "freebsd"),
not(target_os = "linux"),
not(target_os = "android"),
))]
result.stdout_contains("21529\t./subdir\n");
{
result.stdout_contains_line("6\ta\\b\\file2");
result.stdout_contains_line("3\ta\\b\\file1");
result.stdout_contains_line("9\ta\\b");
result.stdout_contains_line("9\ta");
}
}
#[test]

View file

@ -659,6 +659,17 @@ impl CmdResult {
self
}
#[track_caller]
pub fn stdout_contains_line<T: AsRef<str>>(&self, cmp: T) -> &Self {
assert!(
self.stdout_str().lines().any(|line| line == cmp.as_ref()),
"'{}' does not contain line '{}'",
self.stdout_str(),
cmp.as_ref()
);
self
}
#[track_caller]
pub fn stderr_contains<T: AsRef<str>>(&self, cmp: T) -> &Self {
assert!(