Skip to content

Commit 216b771

Browse files
committed
refactor,fix,test: Allow TestEnv configured to validate output order
1 parent bf2435c commit 216b771

2 files changed

Lines changed: 51 additions & 6 deletions

File tree

tests/testenv/mod.rs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ pub struct TestEnv {
1818
/// Path to the *fd* executable.
1919
fd_exe: PathBuf,
2020

21-
/// Normalize each line by sorting the whitespace-separated words
21+
/// Normalize each line by sorting the whitespace-separated words.
2222
normalize_line: bool,
2323

24+
/// When `true`, the order of lines in the result is allowed to be arbitrary
25+
/// (i.e., a sort is performed before comparison).
26+
///
27+
/// When `false`, the order of lines in the result is checked (e.g., when testing the `--sort` CLI option).
28+
allow_random_result_order: bool,
29+
2430
/// Temporary directory for storing test config (global ignore file)
2531
config_dir: Option<TempDir>,
2632
}
@@ -112,7 +118,13 @@ fn format_output_error(args: &[&str], expected: &str, actual: &str) -> String {
112118
}
113119

114120
/// Normalize the output for comparison.
115-
fn normalize_output(s: &str, trim_start: bool, normalize_line: bool) -> String {
121+
///
122+
/// Args:
123+
/// - `s`: The output string to normalize.
124+
/// - `trim_start`: Whether to trim whitespace from the start of each line.
125+
/// - `normalize_line`: Whether to sort the whitespace-separated words in each line.
126+
/// - `sort_lines`: Whether to sort the lines alphabetically.
127+
fn normalize_output(s: &str, trim_start: bool, normalize_line: bool, sort_lines: bool) -> String {
116128
// Split into lines and normalize separators.
117129
let mut lines = s
118130
.replace('\0', "NULL\n")
@@ -129,7 +141,9 @@ fn normalize_output(s: &str, trim_start: bool, normalize_line: bool) -> String {
129141
})
130142
.collect::<Vec<_>>();
131143

132-
lines.sort();
144+
if sort_lines {
145+
lines.sort();
146+
}
133147
lines.join("\n")
134148
}
135149

@@ -153,15 +167,34 @@ impl TestEnv {
153167
temp_dir,
154168
fd_exe,
155169
normalize_line: false,
170+
allow_random_result_order: true,
156171
config_dir: None,
157172
}
158173
}
159174

175+
/// Sets whether output lines should be normalized before comparison.
176+
///
177+
/// Normalization sorts whitespace-separated elements in each line to ensure consistent comparison.
160178
pub fn normalize_line(self, normalize: bool) -> TestEnv {
161179
TestEnv {
162180
temp_dir: self.temp_dir,
163181
fd_exe: self.fd_exe,
164182
normalize_line: normalize,
183+
allow_random_result_order: self.allow_random_result_order,
184+
config_dir: self.config_dir,
185+
}
186+
}
187+
188+
/// Sets whether test results are allowed to appear in any order.
189+
///
190+
/// When `true`, assertions will pass regardless of result ordering,
191+
/// useful for tests where output order is non-deterministic.
192+
pub fn allow_random_result_order(self, allow_random_result_order: bool) -> TestEnv {
193+
TestEnv {
194+
temp_dir: self.temp_dir,
195+
fd_exe: self.fd_exe,
196+
normalize_line: self.normalize_line,
197+
allow_random_result_order,
165198
config_dir: self.config_dir,
166199
}
167200
}
@@ -241,10 +274,13 @@ impl TestEnv {
241274
&String::from_utf8_lossy(&output.stdout),
242275
false,
243276
self.normalize_line,
277+
self.allow_random_result_order,
244278
)
245279
}
246280

247281
/// Assert that calling *fd* with the specified arguments produces the expected output.
282+
///
283+
/// Does not compare ordering.
248284
pub fn assert_output(&self, args: &[&str], expected: &str) {
249285
self.assert_output_subdirectory(".", args, expected)
250286
}
@@ -259,14 +295,21 @@ impl TestEnv {
259295

260296
/// Assert that calling *fd* in the specified path under the root working directory,
261297
/// and with the specified arguments produces the expected output.
298+
///
299+
/// Performs normalization to
262300
pub fn assert_output_subdirectory<P: AsRef<Path>>(
263301
&self,
264302
path: P,
265303
args: &[&str],
266304
expected: &str,
267305
) {
268306
// Normalize both expected and actual output.
269-
let expected = normalize_output(expected, true, self.normalize_line);
307+
let expected = normalize_output(
308+
expected,
309+
true,
310+
self.normalize_line,
311+
self.allow_random_result_order,
312+
);
270313
let actual = self.assert_success_and_get_normalized_output(path, args);
271314

272315
// Compare actual output to expected output.

tests/tests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,7 +2034,8 @@ fn shuffle_files(files: &[&'static str], seed: u64) -> Vec<&'static str> {
20342034

20352035
#[test]
20362036
fn test_sort_by_path() {
2037-
let te = TestEnv::new(DEFAULT_DIRS, &shuffle_files(DEFAULT_FILES, 42));
2037+
let te = TestEnv::new(DEFAULT_DIRS, &shuffle_files(DEFAULT_FILES, 42))
2038+
.allow_random_result_order(false);
20382039

20392040
// --sort=path should produce deterministic alphabetical output
20402041
te.assert_output(
@@ -2052,7 +2053,8 @@ fn test_sort_by_path() {
20522053
#[cfg(not(windows))]
20532054
#[test]
20542055
fn test_sort_by_path_with_exec() {
2055-
let te = TestEnv::new(DEFAULT_DIRS, &shuffle_files(DEFAULT_FILES, 42));
2056+
let te = TestEnv::new(DEFAULT_DIRS, &shuffle_files(DEFAULT_FILES, 42))
2057+
.allow_random_result_order(false);
20562058

20572059
// --exec with --sort should produce output in sorted order
20582060
te.assert_output(

0 commit comments

Comments
 (0)