Skip to content

Commit a7a5126

Browse files
committed
Improve the tests
1 parent 3642d5e commit a7a5126

File tree

2 files changed

+77
-34
lines changed

2 files changed

+77
-34
lines changed

src/uucore/src/lib/features/safe_traversal.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Safe directory traversal using openat() and related syscalls
22
// This module provides TOCTOU-safe filesystem operations for recursive traversal
33
// Only available on Linux
4-
// spell-checker:ignore CLOEXEC RDONLY TOCTOU closedir dirp fdopendir fstatat openat REMOVEDIR unlinkat
4+
// spell-checker:ignore CLOEXEC RDONLY TOCTOU closedir dirp fdopendir fstatat openat REMOVEDIR unlinkat smallfile
55
// spell-checker:ignore RAII dirfd
66

77
#![cfg(target_os = "linux")]
@@ -708,6 +708,7 @@ mod tests {
708708
}
709709

710710
#[test]
711+
#[allow(clippy::unnecessary_cast)]
711712
fn test_file_info() {
712713
let temp_dir = TempDir::new().unwrap();
713714
let file_path = temp_dir.path().join("test_file");
@@ -716,7 +717,6 @@ mod tests {
716717
let dir_fd = DirFd::open(temp_dir.path()).unwrap();
717718
let stat = dir_fd.stat_at(OsStr::new("test_file"), true).unwrap();
718719
let file_info = FileInfo::from_stat(&stat);
719-
720720
assert_eq!(file_info.device(), stat.st_dev as u64);
721721
assert_eq!(file_info.inode(), stat.st_ino as u64);
722722
}
@@ -756,6 +756,7 @@ mod tests {
756756
}
757757

758758
#[test]
759+
#[allow(clippy::unnecessary_cast)]
759760
fn test_metadata_wrapper() {
760761
let temp_dir = TempDir::new().unwrap();
761762
let file_path = temp_dir.path().join("test_file");

tests/by-util/test_du.rs

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// file that was distributed with this source code.
55

66
// spell-checker:ignore (paths) atim sublink subwords azerty azeaze xcwww azeaz amaz azea qzerty tazerty tsublink testfile1 testfile2 filelist fpath testdir testfile
7-
// spell-checker:ignore selfref ELOOP
7+
// spell-checker:ignore selfref ELOOP smallfile
88
#[cfg(not(windows))]
99
use regex::Regex;
1010

@@ -197,45 +197,71 @@ fn test_du_soft_link() {
197197
let ts = TestScenario::new(util_name!());
198198
let at = &ts.fixtures;
199199

200-
at.symlink_file(SUB_FILE, SUB_LINK);
200+
// Create the directory and file structure explicitly for this test
201+
at.mkdir_all("subdir/links");
202+
at.write("subdir/links/subwords.txt", &"hello world\n".repeat(100));
203+
at.symlink_file("subdir/links/subwords.txt", "subdir/links/sublink.txt");
201204

202-
let result = ts.ucmd().arg(SUB_DIR_LINKS).succeeds();
205+
let result = ts.ucmd().arg("subdir/links").succeeds();
203206

204207
#[cfg(any(target_os = "linux", target_os = "android"))]
205208
{
206-
let result_reference = unwrap_or_return!(expected_result(&ts, &[SUB_DIR_LINKS]));
209+
let result_reference = unwrap_or_return!(expected_result(&ts, &["subdir/links"]));
207210
if result_reference.succeeded() {
208211
assert_eq!(result.stdout_str(), result_reference.stdout_str());
209212
return;
210213
}
211214
}
212-
du_soft_link(result.stdout_str());
213-
}
214215

215-
#[cfg(target_vendor = "apple")]
216-
fn du_soft_link(s: &str) {
217-
// 'macos' host variants may have `du` output variation for soft links
218-
assert!((s == "12\tsubdir/links\n") || (s == "16\tsubdir/links\n"));
219-
}
220-
#[cfg(target_os = "windows")]
221-
fn du_soft_link(s: &str) {
222-
assert_eq!(s, "8\tsubdir/links\n");
223-
}
224-
#[cfg(target_os = "freebsd")]
225-
fn du_soft_link(s: &str) {
226-
assert_eq!(s, "16\tsubdir/links\n");
227-
}
228-
#[cfg(all(
229-
not(target_vendor = "apple"),
230-
not(target_os = "windows"),
231-
not(target_os = "freebsd")
232-
))]
233-
fn du_soft_link(s: &str) {
234-
// MS-WSL linux has altered expected output
235-
if uucore::os::is_wsl_1() {
236-
assert_eq!(s, "8\tsubdir/links\n");
237-
} else {
238-
assert_eq!(s, "16\tsubdir/links\n");
216+
let s = result.stdout_str();
217+
println!("Output: {s}");
218+
219+
// Helper closure to assert output matches one of the valid sizes
220+
#[cfg(any(target_vendor = "apple", target_os = "windows", target_os = "freebsd"))]
221+
let assert_valid_size = |output: &str, valid_sizes: &[&str]| {
222+
assert!(
223+
valid_sizes.contains(&output),
224+
"Expected one of {valid_sizes:?}, got {output}"
225+
);
226+
};
227+
228+
#[cfg(target_vendor = "apple")]
229+
{
230+
// 'macos' host variants may have `du` output variation for soft links
231+
let valid_sizes = [
232+
"8\tsubdir/links\n",
233+
"12\tsubdir/links\n",
234+
"16\tsubdir/links\n",
235+
];
236+
assert_valid_size(s, &valid_sizes);
237+
}
238+
239+
#[cfg(target_os = "windows")]
240+
{
241+
let valid_sizes = ["4\tsubdir/links\n", "8\tsubdir/links\n"];
242+
assert_valid_size(s, &valid_sizes);
243+
}
244+
245+
#[cfg(target_os = "freebsd")]
246+
{
247+
// FreeBSD may have different block allocations depending on filesystem
248+
// Accept both common sizes
249+
let valid_sizes = ["12\tsubdir/links\n", "16\tsubdir/links\n"];
250+
assert_valid_size(&s, &valid_sizes);
251+
}
252+
253+
#[cfg(all(
254+
not(target_vendor = "apple"),
255+
not(target_os = "windows"),
256+
not(target_os = "freebsd")
257+
))]
258+
{
259+
// MS-WSL linux has altered expected output
260+
if uucore::os::is_wsl_1() {
261+
assert_eq!(s, "8\tsubdir/links\n");
262+
} else {
263+
assert_eq!(s, "16\tsubdir/links\n");
264+
}
239265
}
240266
}
241267

@@ -814,12 +840,18 @@ fn test_du_no_exec_permission() {
814840
#[cfg(not(target_os = "openbsd"))]
815841
fn test_du_one_file_system() {
816842
let ts = TestScenario::new(util_name!());
843+
let at = &ts.fixtures;
844+
845+
// Create the directory structure explicitly for this test
846+
at.mkdir_all("subdir/deeper/deeper_dir");
847+
at.write("subdir/deeper/deeper_dir/deeper_words.txt", "hello world");
848+
at.write("subdir/deeper/words.txt", "world");
817849

818-
let result = ts.ucmd().arg("-x").arg(SUB_DIR).succeeds();
850+
let result = ts.ucmd().arg("-x").arg("subdir/deeper").succeeds();
819851

820852
#[cfg(any(target_os = "linux", target_os = "android"))]
821853
{
822-
let result_reference = unwrap_or_return!(expected_result(&ts, &["-x", SUB_DIR]));
854+
let result_reference = unwrap_or_return!(expected_result(&ts, &["-x", "subdir/deeper"]));
823855
if result_reference.succeeded() {
824856
assert_eq!(result.stdout_str(), result_reference.stdout_str());
825857
return;
@@ -832,16 +864,26 @@ fn test_du_one_file_system() {
832864
#[cfg(not(target_os = "openbsd"))]
833865
fn test_du_threshold() {
834866
let ts = TestScenario::new(util_name!());
867+
let at = &ts.fixtures;
868+
869+
// Create the directory structure explicitly for this test
870+
at.mkdir_all("subdir/links");
871+
at.mkdir_all("subdir/deeper/deeper_dir");
872+
// Create files with specific sizes to test threshold
873+
at.write("subdir/links/bigfile.txt", &"x".repeat(10000)); // ~10K file
874+
at.write("subdir/deeper/deeper_dir/smallfile.txt", "small"); // small file
835875

836876
let threshold = if cfg!(windows) { "7K" } else { "10K" };
837877

838878
ts.ucmd()
879+
.arg("--apparent-size")
839880
.arg(format!("--threshold={threshold}"))
840881
.succeeds()
841882
.stdout_contains("links")
842883
.stdout_does_not_contain("deeper_dir");
843884

844885
ts.ucmd()
886+
.arg("--apparent-size")
845887
.arg(format!("--threshold=-{threshold}"))
846888
.succeeds()
847889
.stdout_does_not_contain("links")

0 commit comments

Comments
 (0)