diff options
author | nguyenvukhang <brew4k@gmail.com> | 2022-11-28 12:36:15 +0800 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2023-07-09 10:14:03 -0400 |
commit | 6abb962f0d655a186972462d88d2de4e5caf4a33 (patch) | |
tree | 3a3970ac6cf20c6600c7517b8bb72b56ed660462 /tests | |
parent | 6d95c130d5fb56a8641092a4808f33640bfa935c (diff) |
cli: fix non-path sorting behavior
Previously, sorting worked by sorting the parents and then sorting the
children within each parent. This was done during traversal, but it only
works when sorting parents preserves the overall order. This generally
only works for '--sort path' in ascending order.
This commit fixes the rest of the sorting behavior by collecting all of
the paths to search and then sorting them before searching. We only
collect all of the paths when sorting was requested.
Fixes #2243, Closes #2361
Diffstat (limited to 'tests')
-rw-r--r-- | tests/feature.rs | 22 | ||||
-rw-r--r-- | tests/misc.rs | 45 |
2 files changed, 67 insertions, 0 deletions
diff --git a/tests/feature.rs b/tests/feature.rs index 6d4d1947..94bb62be 100644 --- a/tests/feature.rs +++ b/tests/feature.rs @@ -787,6 +787,28 @@ rgtest!(f1466_no_ignore_files, |dir: Dir, mut cmd: TestCommand| { eqnice!("foo\n", cmd.arg("-u").stdout()); }); +// See: https://github.com/BurntSushi/ripgrep/pull/2361 +rgtest!(f2361_sort_nested_files, |dir: Dir, mut cmd: TestCommand| { + use std::{thread::sleep, time::Duration}; + + dir.create("foo", "1"); + sleep(Duration::from_millis(100)); + dir.create_dir("dir"); + sleep(Duration::from_millis(100)); + dir.create(dir.path().join("dir").join("bar"), "1"); + + cmd.arg("--sort").arg("accessed").arg("--files"); + eqnice!("foo\ndir/bar\n", cmd.stdout()); + + dir.create("foo", "2"); + sleep(Duration::from_millis(100)); + dir.create(dir.path().join("dir").join("bar"), "2"); + sleep(Duration::from_millis(100)); + + cmd.arg("--sort").arg("accessed").arg("--files"); + eqnice!("foo\ndir/bar\n", cmd.stdout()); +}); + // See: https://github.com/BurntSushi/ripgrep/issues/1404 rgtest!(f1404_nothing_searched_warning, |dir: Dir, mut cmd: TestCommand| { dir.create(".ignore", "ignored-dir/**"); diff --git a/tests/misc.rs b/tests/misc.rs index c892da5b..40f900dd 100644 --- a/tests/misc.rs +++ b/tests/misc.rs @@ -1065,3 +1065,48 @@ rgtest!(type_list, |_: Dir, mut cmd: TestCommand| { // This can change over time, so just make sure we print something. assert!(!cmd.stdout().is_empty()); }); + +// The following series of tests seeks to test all permutations of ripgrep's +// sorted queries. +// +// They all rely on this setup function, which sets up this particular file +// structure with a particular creation order: +// ├── a # 1 +// ├── b # 4 +// └── dir # 2 +// ├── c # 3 +// └── d # 5 +// +// This order is important when sorting them by system time-stamps. +fn sort_setup(dir: Dir) { + use std::{thread::sleep, time::Duration}; + + let sub_dir = dir.path().join("dir"); + dir.create("a", "test"); + sleep(Duration::from_millis(100)); + dir.create_dir(&sub_dir); + sleep(Duration::from_millis(100)); + dir.create(sub_dir.join("c"), "test"); + sleep(Duration::from_millis(100)); + dir.create("b", "test"); + sleep(Duration::from_millis(100)); + dir.create(sub_dir.join("d"), "test"); +} + +rgtest!(sort_files, |dir: Dir, mut cmd: TestCommand| { + sort_setup(dir); + let expected = "a:test\nb:test\ndir/c:test\ndir/d:test\n"; + eqnice!(expected, cmd.args(["--sort", "path", "test"]).stdout()); +}); + +rgtest!(sort_accessed, |dir: Dir, mut cmd: TestCommand| { + sort_setup(dir); + let expected = "a:test\ndir/c:test\nb:test\ndir/d:test\n"; + eqnice!(expected, cmd.args(["--sort", "accessed", "test"]).stdout()); +}); + +rgtest!(sortr_accessed, |dir: Dir, mut cmd: TestCommand| { + sort_setup(dir); + let expected = "dir/d:test\nb:test\ndir/c:test\na:test\n"; + eqnice!(expected, cmd.args(["--sortr", "accessed", "test"]).stdout()); +}); |