From 958ac72113c5e86763a8bffd82cd84bc5acac20e Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 28 Dec 2024 22:50:49 +0100 Subject: [PATCH] ls: finish the plug of mtime Will help with tests/ls/ls-time --- src/uu/ls/src/ls.rs | 3 +++ tests/by-util/test_ls.rs | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index 9a22006e097..b9da2c97720 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -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"), @@ -1442,12 +1443,14 @@ pub fn uu_app() -> Command { "Show time in :\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) diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index f65078a0d5a..1a5c11194f0 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -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();