Skip to content

Commit 5e5135c

Browse files
authored
Unrolled build for #148724
Rollup merge of #148724 - Zalathar:tidyselftest, r=jieyouxu tidy: Don't bypass stderr output capture in unit tests In unit tests, writes to stderr that don't use `eprint!` or `eprintln!` will not be captured, and instead interfere with test harness output, making it unreadable. <details> <summary><b>Detailed before/after</b></summary> ## Before ```text $ x test tidyselftest --no-doc Building bootstrap Finished `dev` profile [unoptimized] target(s) in 0.03s Testing stage1 tidy (aarch64-apple-darwin) Compiling tidy v0.1.0 (/Users/stuart/Dev/rust/rust/src/tools/tidy) Finished `release` profile [optimized + debuginfo] target(s) in 0.23s Running unittests src/lib.rs (build/aarch64-apple-darwin/bootstrap-tools/aarch64-apple-darwin/release/deps/tidy-c33a0cc08cf46c66) running 20 tests tidy [alphabetical-test]: bad:3 found `tidy-alphabetical-start` expecting `tidy-alphabetical-end` tidy [alphabetical-test]: FAIL tidy [alphabetical-test]: bad: reached end of file expecting `tidy-alphabetical-end`tidy [alphabetical-test]: bad:4: line not in alphabetical order tidy [alphabetical-test]: bad:5 found `tidy-alphabetical-end` expecting `tidy-alphabetical-start` tidy [alphabetical-test]: FAIL tidy [alphabetical-test]: FAIL tidy [alphabetical-test].: tidy [alphabetical-test...bad:4: line not in alphabetical order..]: tidy [alphabetical-test]: .. bad:7: line not in alphabetical order tidy [tidy [bad:2 found `tidy-alphabetical-end` expecting `tidy-alphabetical-start` alphabetical-testtidy [tidy [alphabetical-test]: bad:4: line not in alphabetical order .tidy [alphabetical-test]..]alphabetical-testtidy []: bad:4: line not in alphabetical orderalphabetical-testalphabetical-test]: tidy [FAILalphabetical-test ]: FAIL ]: FAIL : FAIL : bad:4: line not in alphabetical order tidy [alphabetical-test]: FAIL tidy [.alphabetical-test]: FAIL .tidy [alphabetical-test]: FAIL ..tidy [alphabetical-test]: bad:3: line not in alphabetical order tidy [alphabetical-test]: FAIL tidy [alphabetical-test]: bad:3: line not in alphabetical order tidy [alphabetical-test]: FAIL ..... test result: ok. 20 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 1.01ms Running unittests src/main.rs (build/aarch64-apple-darwin/bootstrap-tools/aarch64-apple-darwin/release/deps/rust_tidy-25232a69af4dd751) running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 26.88µs finished in 0.255 seconds Build completed successfully in 0:00:00 ``` ## After ```text $ x test tidyselftest --no-doc Building bootstrap Finished `dev` profile [unoptimized] target(s) in 0.03s Testing stage1 tidy (aarch64-apple-darwin) Compiling tidy v0.1.0 (/Users/stuart/Dev/rust/rust/src/tools/tidy) Finished `release` profile [optimized + debuginfo] target(s) in 1.74s Running unittests src/lib.rs (build/aarch64-apple-darwin/bootstrap-tools/aarch64-apple-darwin/release/deps/tidy-c33a0cc08cf46c66) running 20 tests .................... test result: ok. 20 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 1.35ms Running unittests src/main.rs (build/aarch64-apple-darwin/bootstrap-tools/aarch64-apple-darwin/release/deps/rust_tidy-25232a69af4dd751) running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 27.17µs finished in 1.764 seconds Build completed successfully in 0:00:02 ``` </details>
2 parents ab67c37 + 38ae449 commit 5e5135c

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

src/tools/tidy/src/diagnostics.rs

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::collections::HashSet;
22
use std::fmt::{Display, Formatter};
3+
use std::io;
34
use std::path::{Path, PathBuf};
45
use std::sync::{Arc, Mutex};
56

6-
use termcolor::{Color, WriteColor};
7+
use termcolor::Color;
78

89
#[derive(Clone, Default)]
910
///CLI flags used by tidy.
@@ -245,30 +246,63 @@ pub const COLOR_WARNING: Color = Color::Yellow;
245246
/// Output a message to stderr.
246247
/// The message can be optionally scoped to a certain check, and it can also have a certain color.
247248
pub fn output_message(msg: &str, id: Option<&CheckId>, color: Option<Color>) {
248-
use std::io::Write;
249+
use termcolor::{ColorChoice, ColorSpec};
249250

250-
use termcolor::{ColorChoice, ColorSpec, StandardStream};
251+
let stderr: &mut dyn termcolor::WriteColor = if cfg!(test) {
252+
&mut StderrForUnitTests
253+
} else {
254+
&mut termcolor::StandardStream::stderr(ColorChoice::Auto)
255+
};
251256

252-
let mut stderr = StandardStream::stderr(ColorChoice::Auto);
253257
if let Some(color) = &color {
254258
stderr.set_color(ColorSpec::new().set_fg(Some(*color))).unwrap();
255259
}
256260

257261
match id {
258262
Some(id) => {
259-
write!(&mut stderr, "tidy [{}", id.name).unwrap();
263+
write!(stderr, "tidy [{}", id.name).unwrap();
260264
if let Some(path) = &id.path {
261-
write!(&mut stderr, " ({})", path.display()).unwrap();
265+
write!(stderr, " ({})", path.display()).unwrap();
262266
}
263-
write!(&mut stderr, "]").unwrap();
267+
write!(stderr, "]").unwrap();
264268
}
265269
None => {
266-
write!(&mut stderr, "tidy").unwrap();
270+
write!(stderr, "tidy").unwrap();
267271
}
268272
}
269273
if color.is_some() {
270274
stderr.set_color(&ColorSpec::new()).unwrap();
271275
}
272276

273-
writeln!(&mut stderr, ": {msg}").unwrap();
277+
writeln!(stderr, ": {msg}").unwrap();
278+
}
279+
280+
/// An implementation of `io::Write` and `termcolor::WriteColor` that writes
281+
/// to stderr via `eprint!`, so that the output can be properly captured when
282+
/// running tidy's unit tests.
283+
struct StderrForUnitTests;
284+
285+
impl io::Write for StderrForUnitTests {
286+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
287+
eprint!("{}", String::from_utf8_lossy(buf));
288+
Ok(buf.len())
289+
}
290+
291+
fn flush(&mut self) -> io::Result<()> {
292+
Ok(())
293+
}
294+
}
295+
296+
impl termcolor::WriteColor for StderrForUnitTests {
297+
fn supports_color(&self) -> bool {
298+
false
299+
}
300+
301+
fn set_color(&mut self, _spec: &termcolor::ColorSpec) -> io::Result<()> {
302+
Ok(())
303+
}
304+
305+
fn reset(&mut self) -> io::Result<()> {
306+
Ok(())
307+
}
274308
}

0 commit comments

Comments
 (0)