9 test_result::TestResult,
11 console::{ConsoleTestState, OutputLocation},
12 bench::fmt_bench_samples,
14 use super::OutputFormatter;
16 // insert a '\n' after 100 tests in quiet mode
17 const QUIET_MODE_MAX_COLUMN: usize = 100;
19 pub(crate) struct TerseFormatter<T> {
20 out: OutputLocation<T>,
22 is_multithreaded: bool,
23 /// Number of columns to fill when aligning names
27 total_test_count: usize,
30 impl<T: Write> TerseFormatter<T> {
32 out: OutputLocation<T>,
35 is_multithreaded: bool,
43 total_test_count: 0, // initialized later, when write_run_start is called
47 pub fn write_ok(&mut self) -> io::Result<()> {
48 self.write_short_result(".", term::color::GREEN)
51 pub fn write_failed(&mut self) -> io::Result<()> {
52 self.write_short_result("F", term::color::RED)
55 pub fn write_ignored(&mut self) -> io::Result<()> {
56 self.write_short_result("i", term::color::YELLOW)
59 pub fn write_allowed_fail(&mut self) -> io::Result<()> {
60 self.write_short_result("a", term::color::YELLOW)
63 pub fn write_bench(&mut self) -> io::Result<()> {
64 self.write_pretty("bench", term::color::CYAN)
67 pub fn write_short_result(
70 color: term::color::Color,
72 self.write_pretty(result, color)?;
73 if self.test_count % QUIET_MODE_MAX_COLUMN == QUIET_MODE_MAX_COLUMN - 1 {
74 // we insert a new line every 100 dots in order to flush the
75 // screen when dealing with line-buffered output (e.g., piping to
76 // `stamp` in the rust CI).
77 let out = format!(" {}/{}\n", self.test_count+1, self.total_test_count);
78 self.write_plain(&out)?;
85 pub fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
87 OutputLocation::Pretty(ref mut term) => {
91 term.write_all(word.as_bytes())?;
97 OutputLocation::Raw(ref mut stdout) => {
98 stdout.write_all(word.as_bytes())?;
104 pub fn write_plain<S: AsRef<str>>(&mut self, s: S) -> io::Result<()> {
106 self.out.write_all(s.as_bytes())?;
110 pub fn write_outputs(&mut self, state: &ConsoleTestState) -> io::Result<()> {
111 self.write_plain("\nsuccesses:\n")?;
112 let mut successes = Vec::new();
113 let mut stdouts = String::new();
114 for &(ref f, ref stdout) in &state.not_failures {
115 successes.push(f.name.to_string());
116 if !stdout.is_empty() {
117 stdouts.push_str(&format!("---- {} stdout ----\n", f.name));
118 let output = String::from_utf8_lossy(stdout);
119 stdouts.push_str(&output);
120 stdouts.push_str("\n");
123 if !stdouts.is_empty() {
124 self.write_plain("\n")?;
125 self.write_plain(&stdouts)?;
128 self.write_plain("\nsuccesses:\n")?;
130 for name in &successes {
131 self.write_plain(&format!(" {}\n", name))?;
136 pub fn write_failures(&mut self, state: &ConsoleTestState) -> io::Result<()> {
137 self.write_plain("\nfailures:\n")?;
138 let mut failures = Vec::new();
139 let mut fail_out = String::new();
140 for &(ref f, ref stdout) in &state.failures {
141 failures.push(f.name.to_string());
142 if !stdout.is_empty() {
143 fail_out.push_str(&format!("---- {} stdout ----\n", f.name));
144 let output = String::from_utf8_lossy(stdout);
145 fail_out.push_str(&output);
146 fail_out.push_str("\n");
149 if !fail_out.is_empty() {
150 self.write_plain("\n")?;
151 self.write_plain(&fail_out)?;
154 self.write_plain("\nfailures:\n")?;
156 for name in &failures {
157 self.write_plain(&format!(" {}\n", name))?;
162 fn write_test_name(&mut self, desc: &TestDesc) -> io::Result<()> {
163 let name = desc.padded_name(self.max_name_len, desc.name.padding());
164 self.write_plain(&format!("test {} ... ", name))?;
170 impl<T: Write> OutputFormatter for TerseFormatter<T> {
171 fn write_run_start(&mut self, test_count: usize) -> io::Result<()> {
172 self.total_test_count = test_count;
173 let noun = if test_count != 1 { "tests" } else { "test" };
174 self.write_plain(&format!("\nrunning {} {}\n", test_count, noun))
177 fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()> {
178 // Remnants from old libtest code that used the padding value
179 // in order to indicate benchmarks.
180 // When running benchmarks, terse-mode should still print their name as if
181 // it is the Pretty formatter.
182 if !self.is_multithreaded && desc.name.padding() == NamePadding::PadOnRight {
183 self.write_test_name(desc)?;
193 _: Option<&time::TestExecTime>,
195 _: &ConsoleTestState,
196 ) -> io::Result<()> {
198 TestResult::TrOk => self.write_ok(),
200 | TestResult::TrFailedMsg(_)
201 | TestResult::TrTimedFail => self.write_failed(),
202 TestResult::TrIgnored => self.write_ignored(),
203 TestResult::TrAllowedFail => self.write_allowed_fail(),
204 TestResult::TrBench(ref bs) => {
205 if self.is_multithreaded {
206 self.write_test_name(desc)?;
209 self.write_plain(&format!(": {}\n", fmt_bench_samples(bs)))
214 fn write_timeout(&mut self, desc: &TestDesc) -> io::Result<()> {
215 self.write_plain(&format!(
216 "test {} has been running for over {} seconds\n",
217 desc.name, time::TEST_WARN_TIMEOUT_S
221 fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool> {
222 if state.options.display_output {
223 self.write_outputs(state)?;
225 let success = state.failed == 0;
227 self.write_failures(state)?;
230 self.write_plain("\ntest result: ")?;
233 // There's no parallelism at this point so it's safe to use color
234 self.write_pretty("ok", term::color::GREEN)?;
236 self.write_pretty("FAILED", term::color::RED)?;
239 let s = if state.allowed_fail > 0 {
241 ". {} passed; {} failed ({} allowed); {} ignored; {} measured; {} filtered out\n\n",
243 state.failed + state.allowed_fail,
251 ". {} passed; {} failed; {} ignored; {} measured; {} filtered out\n\n",
252 state.passed, state.failed, state.ignored, state.measured, state.filtered_out
256 self.write_plain(&s)?;