Skip to content

Commit 8127250

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.
1 parent baf2dd5 commit 8127250

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

tests/by-util/test_ls.rs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6331,43 +6331,51 @@ 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
6344+
.ucmd()
6345+
.arg("-f")
6346+
.arg("-1")
6347+
.succeeds()
6348+
.stdout_move_str();
6349+
let out_u_all = scene
63446350
.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!(out_f_then_sort, out_sorted, "--sort after -f should enable sorting");
63716379
}
63726380

63736381
#[test]
@@ -6508,10 +6516,6 @@ fn test_f_overrides_sort_flags() {
65086516
out_s_f, out_u,
65096517
"-f should win: output should match unsorted -U"
65106518
);
6511-
assert_ne!(
6512-
out_s_f, out_s,
6513-
"-f should win: output should differ from sorted -S"
6514-
);
65156519
}
65166520

65176521
#[test]
@@ -6570,19 +6574,21 @@ fn test_big_u_overrides_f_sort() {
65706574
"-S should win: output should be size-sorted"
65716575
);
65726576

6573-
// -S then -U: -U wins, should be unsorted
6577+
// -S then -U: -U wins, should match plain -U output
65746578
let out_s_u = scene
65756579
.ucmd()
65766580
.arg("-S")
65776581
.arg("-U")
65786582
.arg("-1")
65796583
.succeeds()
65806584
.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"
6585-
);
6585+
assert_eq!(out_s_u, out_u, "-U should win: output should match plain -U");
6586+
6587+
// Verify size-sorted output is in correct order (this is deterministic)
6588+
let lines: Vec<&str> = out_s.lines().collect();
6589+
assert_eq!(lines[0], "large.txt", "First file should be largest");
6590+
assert_eq!(lines[1], "medium.txt", "Second file should be medium");
6591+
assert_eq!(lines[2], "small.txt", "Third file should be smallest");
65866592
}
65876593

65886594
#[test]

0 commit comments

Comments
 (0)