Skip to content

Commit d8a7be0

Browse files
committed
tests(ls): use set_modified instead of touch; simplify assertions
- Replace #[cfg(feature = "touch")] with set_modified(UNIX_EPOCH) in test_ls_time_style_iso_recent_and_older - Remove conditional blocks in test_ls_time_sort_without_long and set mtimes via set_modified - Simplify assertion to assert_ne!(def, t) to compare full outputs Rationale: Improves determinism, portability, and reduces branching as suggested by maintainer cakebaker in PR review comments.
1 parent f59eff2 commit d8a7be0

File tree

1 file changed

+20
-45
lines changed

1 file changed

+20
-45
lines changed

tests/by-util/test_ls.rs

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6211,28 +6211,21 @@ fn test_ls_time_style_iso_recent_and_older() {
62116211
);
62126212

62136213
// Older format appends a full ISO date padded (year present)
6214-
// Only run this part when the test binary includes `touch`
6215-
#[cfg(feature = "touch")]
6216-
{
6217-
// Older file: set mtime to 1970-01-01 using uutils touch
6218-
scene
6219-
.ccmd("touch")
6220-
.args(&["-d", "1970-01-01", "older"]) // RFC3339-ish date understood by GNU and uutils touch
6221-
.succeeds();
6214+
let f = at.make_file("older");
6215+
f.set_modified(std::time::UNIX_EPOCH).unwrap();
62226216

6223-
let older = scene
6224-
.ucmd()
6225-
.arg("-l")
6226-
.arg("--time-style=iso")
6227-
.arg("older")
6228-
.succeeds();
6229-
let re_older = Regex::new(r"(^|\n).*\d{4}-\d{2}-\d{2} +").unwrap();
6230-
assert!(
6231-
re_older.is_match(older.stdout_str()),
6232-
"older not matched: {}",
6233-
older.stdout_str()
6234-
);
6235-
}
6217+
let older = scene
6218+
.ucmd()
6219+
.arg("-l")
6220+
.arg("--time-style=iso")
6221+
.arg("older")
6222+
.succeeds();
6223+
let re_older = Regex::new(r"(^|\n).*\d{4}-\d{2}-\d{2} +").unwrap();
6224+
assert!(
6225+
re_older.is_match(older.stdout_str()),
6226+
"older not matched: {}",
6227+
older.stdout_str()
6228+
);
62366229
}
62376230

62386231
#[test]
@@ -6304,35 +6297,17 @@ fn test_ls_time_style_precedence_last_wins() {
63046297
fn test_ls_time_sort_without_long() {
63056298
let scene = TestScenario::new(util_name!());
63066299

6307-
// Create two files with deterministic, distinct modification times using touch -d
6308-
#[cfg(feature = "touch")]
6309-
{
6310-
scene
6311-
.ccmd("touch")
6312-
.args(&["-d", "1970-01-01 00:00:00 UTC", "a"])
6313-
.succeeds();
6314-
scene
6315-
.ccmd("touch")
6316-
.args(&["-d", "1970-01-02 00:00:00 UTC", "b"])
6317-
.succeeds();
6318-
}
6319-
#[cfg(not(feature = "touch"))]
6320-
{
6321-
let at = &scene.fixtures;
6322-
at.touch("a");
6323-
// Fallback: sleep long enough to ensure FS timestamp resolution differences
6324-
std::thread::sleep(Duration::from_secs(2));
6325-
at.touch("b");
6326-
}
6300+
// Create two files with deterministic, distinct modification times
6301+
let at = &scene.fixtures;
6302+
let f = at.make_file("a");
6303+
f.set_modified(std::time::UNIX_EPOCH).unwrap();
6304+
at.touch("b");
63276305

63286306
// Compare default (name order) vs time-sorted (-t) order; they should differ
63296307
let default_out = scene.ucmd().succeeds();
63306308
let t_out = scene.ucmd().arg("-t").succeeds();
63316309

63326310
let def = default_out.stdout_str();
63336311
let t = t_out.stdout_str();
6334-
assert_ne!(
6335-
def.lines().next().unwrap_or(""),
6336-
t.lines().next().unwrap_or("")
6337-
);
6312+
assert_ne!(def, t);
63386313
}

0 commit comments

Comments
 (0)