Skip to content

Commit 477eff6

Browse files
committed
Fix filtered test reporting
1 parent 750637c commit 477eff6

File tree

4 files changed

+40
-30
lines changed

4 files changed

+40
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
### Fixed
1818

1919
* report an error instead of panicking when encountering a suggestion that does not belong to the main file.
20+
* number of filtered tests is now > 0 when things actually got filtered.
2021

2122
### Changed
2223

src/lib.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@ pub fn run_tests(mut config: Config) -> Result<()> {
139139

140140
/// The filter used by `run_tests` to only run on `.rs` files that are
141141
/// specified by [`Config::filter_files`] and [`Config::skip_files`].
142-
pub fn default_file_filter(path: &Path, config: &Config) -> bool {
143-
path.extension().is_some_and(|ext| ext == "rs") && default_any_file_filter(path, config)
142+
/// Returns `None` if there is no extension or the extension is not `.rs`.
143+
pub fn default_file_filter(path: &Path, config: &Config) -> Option<bool> {
144+
path.extension().filter(|&ext| ext == "rs")?;
145+
Some(default_any_file_filter(path, config))
144146
}
145147

146148
/// Run on all files that are specified by [`Config::filter_files`] and
@@ -203,8 +205,6 @@ pub enum TestOk {
203205
Ok,
204206
/// The test was ignored due to a rule (`//@only-*` or `//@ignore-*`)
205207
Ignored,
206-
/// The test was filtered with the `file_filter` argument.
207-
Filtered,
208208
}
209209

210210
/// The possible results a single test can have.
@@ -233,9 +233,12 @@ struct TestRun {
233233
/// All `configs` are being run in parallel.
234234
/// If multiple configs are provided, the [`Config::threads`] value of the first one is used;
235235
/// the thread count of all other configs is ignored.
236+
/// The file filter is supposed to return `None` if it was filtered because of file extensions
237+
/// and `Some(false)` if the file was rejected out of other reasons like the file path not matching
238+
/// a user defined filter.
236239
pub fn run_tests_generic(
237240
mut configs: Vec<Config>,
238-
file_filter: impl Fn(&Path, &Config) -> bool + Sync,
241+
file_filter: impl Fn(&Path, &Config) -> Option<bool> + Sync,
239242
per_file_config: impl Fn(&mut Config, &Path, &[u8]) + Sync,
240243
status_emitter: impl StatusEmitter + Send,
241244
) -> Result<()> {
@@ -275,6 +278,7 @@ pub fn run_tests_generic(
275278
},
276279
};
277280

281+
let mut filtered = 0;
278282
run_and_collect(
279283
num_threads,
280284
|submit| {
@@ -297,10 +301,14 @@ pub fn run_tests_generic(
297301
for entry in entries {
298302
todo.push_back((entry, config));
299303
}
300-
} else if file_filter(&path, config) {
301-
let status = status_emitter.register_test(path);
302-
// Forward .rs files to the test workers.
303-
submit.send((status, config)).unwrap();
304+
} else if let Some(matched) = file_filter(&path, config) {
305+
if matched {
306+
let status = status_emitter.register_test(path);
307+
// Forward .rs files to the test workers.
308+
submit.send((status, config)).unwrap();
309+
} else {
310+
filtered += 1;
311+
}
304312
}
305313
}
306314
},
@@ -357,13 +365,11 @@ pub fn run_tests_generic(
357365
let mut failures = vec![];
358366
let mut succeeded = 0;
359367
let mut ignored = 0;
360-
let mut filtered = 0;
361368

362369
for run in results {
363370
match run.result {
364371
Ok(TestOk::Ok) => succeeded += 1,
365372
Ok(TestOk::Ignored) => ignored += 1,
366-
Ok(TestOk::Filtered) => filtered += 1,
367373
Err(errored) => failures.push((run.status, errored)),
368374
}
369375
}

src/status_emitter.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ impl TestStatus for TextTest {
217217
Ok(TestOk::Ok) => "ok".green(),
218218
Err(Errored { .. }) => "FAILED".bright_red().bold(),
219219
Ok(TestOk::Ignored) => "ignored (in-test comment)".yellow(),
220-
Ok(TestOk::Filtered) => return,
221220
};
222221
let old_msg = self.msg();
223222
let msg = format!("... {result}");

tests/integration.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,29 @@ fn main() -> Result<()> {
9898
.ends_with("-fail");
9999
if cfg!(windows) && path.components().any(|c| c.as_os_str() == "basic-bin") {
100100
// on windows there's also a .pdb file, so we get additional errors that aren't there on other platforms
101-
return false;
101+
return Some(false);
102102
}
103-
path.ends_with("Cargo.toml")
104-
&& path.parent().unwrap().parent().unwrap() == root_dir
105-
&& match config
106-
.comment_defaults
107-
.base_immut()
108-
.mode
109-
.as_deref()
110-
.unwrap()
111-
{
112-
Mode::Pass => !fail,
113-
// This is weird, but `cargo test` returns 101 instead of 1 when
114-
// multiple [[test]]s exist. If there's only one test, it returns
115-
// 1 on failure.
116-
Mode::Panic => fail,
117-
Mode::Run { .. } | Mode::Yolo { .. } | Mode::Fail { .. } => unreachable!(),
118-
}
119-
&& default_any_file_filter(path, config)
103+
if !path.ends_with("Cargo.toml") {
104+
return None;
105+
}
106+
Some(
107+
path.parent().unwrap().parent().unwrap() == root_dir
108+
&& match config
109+
.comment_defaults
110+
.base_immut()
111+
.mode
112+
.as_deref()
113+
.unwrap()
114+
{
115+
Mode::Pass => !fail,
116+
// This is weird, but `cargo test` returns 101 instead of 1 when
117+
// multiple [[test]]s exist. If there's only one test, it returns
118+
// 1 on failure.
119+
Mode::Panic => fail,
120+
Mode::Run { .. } | Mode::Yolo { .. } | Mode::Fail { .. } => unreachable!(),
121+
}
122+
&& default_any_file_filter(path, config),
123+
)
120124
},
121125
|_, _, _| {},
122126
(

0 commit comments

Comments
 (0)