Skip to content

Commit 0be0a8d

Browse files
committed
tests: fix flaky ls tests that depend on filesystem directory order
The tests test_f_flag_disables_sorting, test_big_u_overrides_f_sort, and test_f_overrides_sort_flags made incorrect assumptions that unsorted directory order would always differ from sorted order. However, fs::read_dir() returns entries in filesystem-dependent order which may accidentally match sorted order on some filesystems. Changes: - Removed assertions comparing unsorted vs sorted outputs - Added deterministic checks (e.g., verifying --sort after -f works) - Added explicit order verification for size-sorted outputs - Tests now verify flag precedence without relying on directory order Fixes CI failures on Windows and SELinux platforms. Quality checks passed: - cargo fmt --check: ✓ - cargo clippy --test tests: ✓ - all 3 modified tests pass: ✓
1 parent baf2dd5 commit 0be0a8d

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

tests/by-util/test_ls.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6331,43 +6331,54 @@ fn test_f_flag_enables_all() {
63316331

63326332
#[test]
63336333
fn test_f_flag_disables_sorting() {
6334-
// Test that -f disables sorting by comparing with -a (sorted) and -U (unsorted)
6335-
// We compare outputs instead of relying on filesystem directory order
6334+
// Test that -f disables sorting by verifying it matches -U behavior
6335+
// and that explicitly sorting after -f works (proving sorting was disabled)
63366336
let scene = TestScenario::new(util_name!());
63376337
let at = &scene.fixtures;
63386338
at.touch("zebra");
63396339
at.touch("apple");
63406340
at.touch("banana");
63416341

63426342
// Get outputs with different flags
6343-
let out_a = scene
6343+
let out_f = scene
63446344
.ucmd()
6345+
.arg("-f")
6346+
.arg("-1")
6347+
.succeeds()
6348+
.stdout_move_str();
6349+
let out_u_all = scene
6350+
.ucmd()
6351+
.arg("-U")
63456352
.arg("-a")
63466353
.arg("-1")
63476354
.succeeds()
63486355
.stdout_move_str();
6349-
let out_f = scene
6356+
6357+
// Test that explicit sorting after -f works (proves -f disabled sorting)
6358+
let out_f_then_sort = scene
63506359
.ucmd()
63516360
.arg("-f")
6361+
.arg("--sort=name")
63526362
.arg("-1")
63536363
.succeeds()
63546364
.stdout_move_str();
6355-
let out_u_all = scene
6365+
6366+
// Get sorted output for comparison
6367+
let out_sorted = scene
63566368
.ucmd()
6357-
.arg("-U")
63586369
.arg("-a")
63596370
.arg("-1")
63606371
.succeeds()
63616372
.stdout_move_str();
63626373

6363-
// -f output should differ from sorted -a output (proves sorting is disabled)
6364-
assert_ne!(
6365-
out_a, out_f,
6366-
"-f should produce different order than sorted -a"
6367-
);
6368-
63696374
// -f output should match -U output (both use directory order)
63706375
assert_eq!(out_f, out_u_all, "-f should match unsorted -U behavior");
6376+
6377+
// Explicit --sort after -f should enable sorting
6378+
assert_eq!(
6379+
out_f_then_sort, out_sorted,
6380+
"--sort after -f should enable sorting"
6381+
);
63716382
}
63726383

63736384
#[test]
@@ -6508,10 +6519,6 @@ fn test_f_overrides_sort_flags() {
65086519
out_s_f, out_u,
65096520
"-f should win: output should match unsorted -U"
65106521
);
6511-
assert_ne!(
6512-
out_s_f, out_s,
6513-
"-f should win: output should differ from sorted -S"
6514-
);
65156522
}
65166523

65176524
#[test]
@@ -6570,19 +6577,24 @@ fn test_big_u_overrides_f_sort() {
65706577
"-S should win: output should be size-sorted"
65716578
);
65726579

6573-
// -S then -U: -U wins, should be unsorted
6580+
// -S then -U: -U wins, should match plain -U output
65746581
let out_s_u = scene
65756582
.ucmd()
65766583
.arg("-S")
65776584
.arg("-U")
65786585
.arg("-1")
65796586
.succeeds()
65806587
.stdout_move_str();
6581-
assert_eq!(out_s_u, out_u, "-U should win: output should be unsorted");
6582-
assert_ne!(
6583-
out_s_u, out_s,
6584-
"-U should win: output should differ from sorted -S"
6588+
assert_eq!(
6589+
out_s_u, out_u,
6590+
"-U should win: output should match plain -U"
65856591
);
6592+
6593+
// Verify size-sorted output is in correct order (this is deterministic)
6594+
let lines: Vec<&str> = out_s.lines().collect();
6595+
assert_eq!(lines[0], "large.txt", "First file should be largest");
6596+
assert_eq!(lines[1], "medium.txt", "Second file should be medium");
6597+
assert_eq!(lines[2], "small.txt", "Third file should be smallest");
65866598
}
65876599

65886600
#[test]

0 commit comments

Comments
 (0)