Skip to content

Commit b5f453a

Browse files
authored
Merge pull request #8417 from drinkcat/du-ls-time-test
ls: -u/-c/--time control sorting when -l is not used
2 parents 2a80c83 + ec11b68 commit b5f453a

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/uu/ls/src/ls.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,13 @@ fn extract_sort(options: &clap::ArgMatches) -> Sort {
472472
Sort::Version
473473
} else if options.get_flag(options::sort::EXTENSION) {
474474
Sort::Extension
475+
} else if !options.get_flag(options::format::LONG)
476+
&& (options.get_flag(options::time::ACCESS)
477+
|| options.get_flag(options::time::CHANGE)
478+
|| options.get_one::<String>(options::TIME).is_some())
479+
{
480+
// If -l is not specified, -u/-c/--time controls sorting.
481+
Sort::Time
475482
} else {
476483
Sort::Name
477484
}

tests/by-util/test_ls.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,24 +2254,29 @@ fn test_ls_order_time() {
22542254
let result = scene.ucmd().arg("--sort=time").arg("-r").succeeds();
22552255
result.stdout_only("test-1\ntest-2\ntest-3\ntest-4\n");
22562256

2257+
let args: [&[&str]; 10] = [
2258+
&["-t", "-u"],
2259+
&["-u"], //-t is optional: when -l is not set -u/--time controls sorting
2260+
&["-t", "--time=atime"],
2261+
&["--time=atime"],
2262+
&["--time=atim"], // spell-checker:disable-line
2263+
&["--time=a"],
2264+
&["-t", "--time=access"],
2265+
&["--time=access"],
2266+
&["-t", "--time=use"],
2267+
&["--time=use"],
2268+
];
22572269
// 3 was accessed last in the read
22582270
// So the order should be 2 3 4 1
2259-
for arg in [
2260-
"-u",
2261-
"--time=atime",
2262-
"--time=atim", // spell-checker:disable-line
2263-
"--time=a",
2264-
"--time=access",
2265-
"--time=use",
2266-
] {
2267-
let result = scene.ucmd().arg("-t").arg(arg).succeeds();
2271+
for args in args {
2272+
let result = scene.ucmd().args(args).succeeds();
22682273
at.open("test-3").metadata().unwrap().accessed().unwrap();
22692274
at.open("test-4").metadata().unwrap().accessed().unwrap();
22702275

22712276
// It seems to be dependent on the platform whether the access time is actually set
22722277
#[cfg(unix)]
22732278
{
2274-
let expected = unwrap_or_return!(expected_result(&scene, &["-t", arg]));
2279+
let expected = unwrap_or_return!(expected_result(&scene, args));
22752280
at.open("test-3").metadata().unwrap().accessed().unwrap();
22762281
at.open("test-4").metadata().unwrap().accessed().unwrap();
22772282

@@ -2287,6 +2292,10 @@ fn test_ls_order_time() {
22872292
{
22882293
let result = scene.ucmd().arg("-tc").succeeds();
22892294
result.stdout_only("test-2\ntest-4\ntest-3\ntest-1\n");
2295+
2296+
// When -l is not set, -c also controls sorting
2297+
let result = scene.ucmd().arg("-c").succeeds();
2298+
result.stdout_only("test-2\ntest-4\ntest-3\ntest-1\n");
22902299
}
22912300
}
22922301

0 commit comments

Comments
 (0)