Skip to content

Commit c861293

Browse files
committed
test: Create --sort=size test cases
1 parent 61bd00a commit c861293

2 files changed

Lines changed: 113 additions & 7 deletions

File tree

tests/testenv/mod.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,46 @@ fn create_working_directory(
6868
Ok(temp_dir)
6969
}
7070

71+
/// Create the working directory and the test files.
72+
fn create_test_directory_with_sized_files(
73+
directories: &[&'static str],
74+
files: &[(&'static str, usize)],
75+
) -> Result<TempDir, io::Error> {
76+
let temp_dir = tempfile::Builder::new().prefix("fd-tests").tempdir()?;
77+
78+
{
79+
let root = temp_dir.path();
80+
81+
// Pretend that this is a Git repository in order for `.gitignore` files to be respected
82+
fs::create_dir_all(root.join(".git"))?;
83+
84+
for directory in directories {
85+
fs::create_dir_all(root.join(directory))?;
86+
}
87+
88+
for (file, size) in files {
89+
let fp = fs::File::create(root.join(file))?;
90+
91+
// Create a file with the specified size.
92+
fp.set_len(*size as u64)?;
93+
}
94+
95+
#[cfg(unix)]
96+
unix::fs::symlink(root.join("one/two"), root.join("symlink"))?;
97+
98+
// Note: creating symlinks on Windows requires the `SeCreateSymbolicLinkPrivilege` which
99+
// is by default only granted for administrators.
100+
#[cfg(windows)]
101+
windows::fs::symlink_dir(root.join("one/two"), root.join("symlink"))?;
102+
103+
fs::File::create(root.join(".fdignore"))?.write_all(b"fdignored.foo")?;
104+
105+
fs::File::create(root.join(".gitignore"))?.write_all(b"gitignored.foo")?;
106+
}
107+
108+
Ok(temp_dir)
109+
}
110+
71111
fn create_config_directory_with_global_ignore(ignore_file_content: &str) -> io::Result<TempDir> {
72112
let config_dir = tempfile::Builder::new().prefix("fd-config").tempdir()?;
73113
let fd_dir = config_dir.path().join("fd");
@@ -159,6 +199,7 @@ fn trim_lines(s: &str) -> String {
159199
}
160200

161201
impl TestEnv {
202+
/// Create a test environment with a temporary folder, empty files, directories, and symlinks.
162203
pub fn new(directories: &[&'static str], files: &[&'static str]) -> TestEnv {
163204
let temp_dir = create_working_directory(directories, files).expect("working directory");
164205
let fd_exe = find_fd_exe();
@@ -172,6 +213,23 @@ impl TestEnv {
172213
}
173214
}
174215

216+
pub fn new_with_sized_files(
217+
directories: &[&'static str],
218+
files: &[(&'static str, usize)],
219+
) -> TestEnv {
220+
let temp_dir =
221+
create_test_directory_with_sized_files(directories, files).expect("working directory");
222+
let fd_exe = find_fd_exe();
223+
224+
TestEnv {
225+
temp_dir,
226+
fd_exe,
227+
normalize_line: false,
228+
allow_random_result_order: true,
229+
config_dir: None,
230+
}
231+
}
232+
175233
/// Sets whether output lines should be normalized before comparison.
176234
///
177235
/// Normalization sorts whitespace-separated elements in each line to ensure consistent comparison.

tests/tests.rs

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ static DEFAULT_FILES: &[&str] = &[
2828
"e1 e2",
2929
];
3030

31+
static DEFAULT_FILES_WITH_SIZES: &[(&str, usize)] = &[
32+
("a.foo", 437),
33+
("one/b.foo", 823),
34+
("one/two/c.foo", 91),
35+
("one/two/C.Foo2", 614),
36+
("one/two/three/d.foo", 259),
37+
("fdignored.foo", 748),
38+
("gitignored.foo", 183),
39+
(".hidden.foo", 502),
40+
("e1 e2", 376),
41+
];
42+
3143
#[allow(clippy::let_and_return)]
3244
fn get_absolute_root_path(env: &TestEnv) -> String {
3345
let path = env
@@ -2049,6 +2061,23 @@ fn test_sort_by_path() {
20492061
);
20502062
}
20512063

2064+
#[test]
2065+
fn test_sort_by_size() {
2066+
let te = TestEnv::new_with_sized_files(DEFAULT_DIRS, DEFAULT_FILES_WITH_SIZES)
2067+
.allow_random_result_order(false);
2068+
2069+
// --exec with --sort should produce output in sorted order.
2070+
te.assert_output(
2071+
&["foo", "--sort=size"],
2072+
"one/two/three/directory_foo/
2073+
one/two/c.foo
2074+
one/two/three/d.foo
2075+
a.foo
2076+
one/two/C.Foo2
2077+
one/b.foo",
2078+
);
2079+
}
2080+
20522081
/// Shell script execution with --sort (--exec)
20532082
#[cfg(not(windows))]
20542083
#[test]
@@ -2058,13 +2087,32 @@ fn test_sort_by_path_with_exec() {
20582087

20592088
// --exec with --sort should produce output in sorted order.
20602089
te.assert_output(
2061-
&["foo", "--sort=path", "--exec", "echo", "File: {}"],
2062-
"File: ./a.foo
2063-
File: ./one/b.foo
2064-
File: ./one/two/C.Foo2
2065-
File: ./one/two/c.foo
2066-
File: ./one/two/three/d.foo
2067-
File: ./one/two/three/directory_foo",
2090+
&["foo", "--sort=path", "--exec", "echo", "Item: {}"],
2091+
"Item: ./a.foo
2092+
Item: ./one/b.foo
2093+
Item: ./one/two/C.Foo2
2094+
Item: ./one/two/c.foo
2095+
Item: ./one/two/three/d.foo
2096+
Item: ./one/two/three/directory_foo",
2097+
);
2098+
}
2099+
2100+
/// Shell script execution with --sort (--exec)
2101+
#[cfg(not(windows))]
2102+
#[test]
2103+
fn test_sort_by_size_with_exec() {
2104+
let te = TestEnv::new_with_sized_files(DEFAULT_DIRS, DEFAULT_FILES_WITH_SIZES)
2105+
.allow_random_result_order(false);
2106+
2107+
// --exec with --sort should produce output in sorted order.
2108+
te.assert_output(
2109+
&["foo", "--sort=size", "--exec", "echo", "Item: {}"],
2110+
"Item: ./one/two/three/directory_foo
2111+
Item: ./one/two/c.foo
2112+
Item: ./one/two/three/d.foo
2113+
Item: ./a.foo
2114+
Item: ./one/two/C.Foo2
2115+
Item: ./one/b.foo",
20682116
);
20692117
}
20702118

0 commit comments

Comments
 (0)