Skip to content

Commit 0d7dc2a

Browse files
committed
Fix tidy tests
1 parent 50b8696 commit 0d7dc2a

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

src/tools/tidy/src/alphabetical/tests.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
use std::io::Write;
2-
use std::str::from_utf8;
1+
use std::path::Path;
32

4-
use super::*;
3+
use crate::alphabetical::check_lines;
4+
use crate::diagnostics::DiagCtx;
55

66
#[track_caller]
77
fn test(lines: &str, name: &str, expected_msg: &str, expected_bad: bool) {
8-
let mut actual_msg = Vec::new();
9-
let mut actual_bad = false;
10-
let mut err = |args: &_| {
11-
write!(&mut actual_msg, "{args}")?;
12-
Ok(())
13-
};
14-
check_lines(&name, lines.lines().enumerate(), &mut err, &mut actual_bad);
15-
assert_eq!(expected_msg, from_utf8(&actual_msg).unwrap());
16-
assert_eq!(expected_bad, actual_bad);
8+
let diag_ctx = DiagCtx::new(Path::new("/"), false);
9+
let mut check = diag_ctx.start_check("alphabetical-test");
10+
check_lines(&name, lines.lines().enumerate(), &mut check);
11+
12+
assert_eq!(expected_bad, check.is_bad());
13+
let errors = check.get_errors();
14+
if expected_bad {
15+
assert_eq!(errors.len(), 1);
16+
assert_eq!(expected_msg, errors[0]);
17+
} else {
18+
assert!(errors.is_empty());
19+
}
1720
}
1821

1922
#[track_caller]

src/tools/tidy/src/diagnostics.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ impl DiagCtx {
3535
};
3636

3737
ctx.start_check(id.clone());
38-
RunningCheck { id, bad: false, ctx: self.0.clone() }
38+
RunningCheck {
39+
id,
40+
bad: false,
41+
ctx: self.0.clone(),
42+
#[cfg(test)]
43+
errors: vec![],
44+
}
3945
}
4046

4147
pub fn into_failed_checks(self) -> Vec<FinishedCheck> {
@@ -135,13 +141,18 @@ pub struct RunningCheck {
135141
id: CheckId,
136142
bad: bool,
137143
ctx: Arc<Mutex<DiagCtxInner>>,
144+
#[cfg(test)]
145+
errors: Vec<String>,
138146
}
139147

140148
impl RunningCheck {
141149
/// Immediately output an error and mark the check as failed.
142150
pub fn error<T: Display>(&mut self, msg: T) {
143151
self.mark_as_bad();
144-
output_message(&msg.to_string(), Some(&self.id), Some(COLOR_ERROR));
152+
let msg = msg.to_string();
153+
output_message(&msg, Some(&self.id), Some(COLOR_ERROR));
154+
#[cfg(test)]
155+
self.errors.push(msg);
145156
}
146157

147158
/// Immediately output a warning.
@@ -171,6 +182,11 @@ impl RunningCheck {
171182
self.ctx.lock().unwrap().verbose
172183
}
173184

185+
#[cfg(test)]
186+
pub fn get_errors(&self) -> Vec<String> {
187+
self.errors.clone()
188+
}
189+
174190
fn mark_as_bad(&mut self) {
175191
self.bad = true;
176192
}

0 commit comments

Comments
 (0)