1 use std::{io, io::prelude::Write};
3 use super::OutputFormatter;
5 bench::fmt_bench_samples,
6 console::{ConsoleTestState, OutputLocation},
8 test_result::TestResult,
14 // We insert a '\n' when the output hits 100 columns in quiet mode. 88 test
15 // result chars leaves 12 chars for a progress count like " 11704/12853".
16 const QUIET_MODE_MAX_COLUMN: usize = 88;
18 pub(crate) struct TerseFormatter<T> {
19 out: OutputLocation<T>,
21 is_multithreaded: bool,
22 /// Number of columns to fill when aligning names
26 total_test_count: usize,
29 impl<T: Write> TerseFormatter<T> {
31 out: OutputLocation<T>,
34 is_multithreaded: bool,
42 total_test_count: 0, // initialized later, when write_run_start is called
46 pub fn write_ok(&mut self) -> io::Result<()> {
47 self.write_short_result(".", term::color::GREEN)
50 pub fn write_failed(&mut self) -> io::Result<()> {
51 self.write_short_result("F", term::color::RED)
54 pub fn write_ignored(&mut self) -> io::Result<()> {
55 self.write_short_result("i", term::color::YELLOW)
58 pub fn write_bench(&mut self) -> io::Result<()> {
59 self.write_pretty("bench", term::color::CYAN)
62 pub fn write_short_result(
65 color: term::color::Color,
67 self.write_pretty(result, color)?;
68 if self.test_count % QUIET_MODE_MAX_COLUMN == QUIET_MODE_MAX_COLUMN - 1 {
69 // We insert a new line regularly in order to flush the
70 // screen when dealing with line-buffered output (e.g., piping to
71 // `stamp` in the rust CI).
72 let out = format!(" {}/{}\n", self.test_count + 1, self.total_test_count);
73 self.write_plain(out)?;
80 pub fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
82 OutputLocation::Pretty(ref mut term) => {
86 term.write_all(word.as_bytes())?;
92 OutputLocation::Raw(ref mut stdout) => {
93 stdout.write_all(word.as_bytes())?;
99 pub fn write_plain<S: AsRef<str>>(&mut self, s: S) -> io::Result<()> {
101 self.out.write_all(s.as_bytes())?;
105 pub fn write_outputs(&mut self, state: &ConsoleTestState) -> io::Result<()> {
106 self.write_plain("\nsuccesses:\n")?;
107 let mut successes = Vec::new();
108 let mut stdouts = String::new();
109 for (f, stdout) in &state.not_failures {
110 successes.push(f.name.to_string());
111 if !stdout.is_empty() {
112 stdouts.push_str(&format!("---- {} stdout ----\n", f.name));
113 let output = String::from_utf8_lossy(stdout);
114 stdouts.push_str(&output);
118 if !stdouts.is_empty() {
119 self.write_plain("\n")?;
120 self.write_plain(&stdouts)?;
123 self.write_plain("\nsuccesses:\n")?;
125 for name in &successes {
126 self.write_plain(&format!(" {name}\n"))?;
131 pub fn write_failures(&mut self, state: &ConsoleTestState) -> io::Result<()> {
132 self.write_plain("\nfailures:\n")?;
133 let mut failures = Vec::new();
134 let mut fail_out = String::new();
135 for (f, stdout) in &state.failures {
136 failures.push(f.name.to_string());
137 if !stdout.is_empty() {
138 fail_out.push_str(&format!("---- {} stdout ----\n", f.name));
139 let output = String::from_utf8_lossy(stdout);
140 fail_out.push_str(&output);
144 if !fail_out.is_empty() {
145 self.write_plain("\n")?;
146 self.write_plain(&fail_out)?;
149 self.write_plain("\nfailures:\n")?;
151 for name in &failures {
152 self.write_plain(&format!(" {name}\n"))?;
157 fn write_test_name(&mut self, desc: &TestDesc) -> io::Result<()> {
158 let name = desc.padded_name(self.max_name_len, desc.name.padding());
159 if let Some(test_mode) = desc.test_mode() {
160 self.write_plain(format!("test {name} - {test_mode} ... "))?;
162 self.write_plain(format!("test {name} ... "))?;
169 impl<T: Write> OutputFormatter for TerseFormatter<T> {
170 fn write_run_start(&mut self, test_count: usize, shuffle_seed: Option<u64>) -> io::Result<()> {
171 self.total_test_count = test_count;
172 let noun = if test_count != 1 { "tests" } else { "test" };
173 let shuffle_seed_msg = if let Some(shuffle_seed) = shuffle_seed {
174 format!(" (shuffle seed: {shuffle_seed})")
178 self.write_plain(format!("\nrunning {test_count} {noun}{shuffle_seed_msg}\n"))
181 fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()> {
182 // Remnants from old libtest code that used the padding value
183 // in order to indicate benchmarks.
184 // When running benchmarks, terse-mode should still print their name as if
185 // it is the Pretty formatter.
186 if !self.is_multithreaded && desc.name.padding() == NamePadding::PadOnRight {
187 self.write_test_name(desc)?;
197 _: Option<&time::TestExecTime>,
199 _: &ConsoleTestState,
200 ) -> io::Result<()> {
202 TestResult::TrOk => self.write_ok(),
203 TestResult::TrFailed | TestResult::TrFailedMsg(_) | TestResult::TrTimedFail => {
206 TestResult::TrIgnored => self.write_ignored(),
207 TestResult::TrBench(ref bs) => {
208 if self.is_multithreaded {
209 self.write_test_name(desc)?;
212 self.write_plain(format!(": {}\n", fmt_bench_samples(bs)))
217 fn write_timeout(&mut self, desc: &TestDesc) -> io::Result<()> {
218 self.write_plain(format!(
219 "test {} has been running for over {} seconds\n",
221 time::TEST_WARN_TIMEOUT_S
225 fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool> {
226 if state.options.display_output {
227 self.write_outputs(state)?;
229 let success = state.failed == 0;
231 self.write_failures(state)?;
234 self.write_plain("\ntest result: ")?;
237 // There's no parallelism at this point so it's safe to use color
238 self.write_pretty("ok", term::color::GREEN)?;
240 self.write_pretty("FAILED", term::color::RED)?;
244 ". {} passed; {} failed; {} ignored; {} measured; {} filtered out",
245 state.passed, state.failed, state.ignored, state.measured, state.filtered_out
248 self.write_plain(s)?;
250 if let Some(ref exec_time) = state.exec_time {
251 let time_str = format!("; finished in {exec_time}");
252 self.write_plain(time_str)?;
255 self.write_plain("\n\n")?;