Skip to content

ls: finish the plug of mtime #7016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ fn extract_time(options: &clap::ArgMatches) -> Time {
match field.as_str() {
"ctime" | "status" => Time::Change,
"access" | "atime" | "use" => Time::Access,
"mtime" | "modification" => Time::Modification,
"birth" | "creation" => Time::Birth,
// below should never happen as clap already restricts the values.
_ => unreachable!("Invalid field for --time"),
Expand Down Expand Up @@ -1442,12 +1443,14 @@ pub fn uu_app() -> Command {
"Show time in <field>:\n\
\taccess time (-u): atime, access, use;\n\
\tchange time (-t): ctime, status.\n\
\tmodification time: mtime, modification.\n\
\tbirth time: birth, creation;",
)
.value_name("field")
.value_parser(ShortcutValueParser::new([
PossibleValue::new("atime").alias("access").alias("use"),
PossibleValue::new("ctime").alias("status"),
PossibleValue::new("mtime").alias("modification"),
PossibleValue::new("birth").alias("creation"),
]))
.hide_possible_values(true)
Expand Down
24 changes: 24 additions & 0 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2099,6 +2099,30 @@ fn test_ls_order_time() {
}
}

#[test]
fn test_ls_order_mtime() {
use std::time::SystemTime;
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

let f3 = at.make_file("test-3");
f3.set_modified(SystemTime::now()).unwrap();
let f4 = at.make_file("test-4");
f4.set_modified(SystemTime::now()).unwrap();
let f1 = at.make_file("test-1");
f1.set_modified(SystemTime::now()).unwrap();
let f2 = at.make_file("test-2");
f2.set_modified(SystemTime::now()).unwrap();

let result = scene.ucmd().arg("-t").arg("--time=mtime").succeeds();
result.stdout_only("test-2\ntest-1\ntest-4\ntest-3\n");
f3.set_modified(SystemTime::now()).unwrap();

f4.set_modified(SystemTime::now()).unwrap();
let result = scene.ucmd().arg("-t").arg("--time=mtime").succeeds();
result.stdout_only("test-4\ntest-3\ntest-2\ntest-1\n");
}

#[test]
fn test_ls_non_existing() {
new_ucmd!().arg("doesntexist").fails();
Expand Down
Loading