3 pub(crate) struct TerseFormatter<T> {
4 out: OutputLocation<T>,
6 is_multithreaded: bool,
7 /// Number of columns to fill when aligning names
11 total_test_count: usize,
14 impl<T: Write> TerseFormatter<T> {
16 out: OutputLocation<T>,
19 is_multithreaded: bool,
27 total_test_count: 0, // initialized later, when write_run_start is called
31 pub fn write_ok(&mut self) -> io::Result<()> {
32 self.write_short_result(".", term::color::GREEN)
35 pub fn write_failed(&mut self) -> io::Result<()> {
36 self.write_short_result("F", term::color::RED)
39 pub fn write_ignored(&mut self) -> io::Result<()> {
40 self.write_short_result("i", term::color::YELLOW)
43 pub fn write_allowed_fail(&mut self) -> io::Result<()> {
44 self.write_short_result("a", term::color::YELLOW)
47 pub fn write_bench(&mut self) -> io::Result<()> {
48 self.write_pretty("bench", term::color::CYAN)
51 pub fn write_short_result(
54 color: term::color::Color,
56 self.write_pretty(result, color)?;
57 if self.test_count % QUIET_MODE_MAX_COLUMN == QUIET_MODE_MAX_COLUMN - 1 {
58 // we insert a new line every 100 dots in order to flush the
59 // screen when dealing with line-buffered output (e.g., piping to
60 // `stamp` in the rust CI).
61 let out = format!(" {}/{}\n", self.test_count+1, self.total_test_count);
62 self.write_plain(&out)?;
69 pub fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
71 Pretty(ref mut term) => {
75 term.write_all(word.as_bytes())?;
81 Raw(ref mut stdout) => {
82 stdout.write_all(word.as_bytes())?;
88 pub fn write_plain<S: AsRef<str>>(&mut self, s: S) -> io::Result<()> {
90 self.out.write_all(s.as_bytes())?;
94 pub fn write_outputs(&mut self, state: &ConsoleTestState) -> io::Result<()> {
95 self.write_plain("\nsuccesses:\n")?;
96 let mut successes = Vec::new();
97 let mut stdouts = String::new();
98 for &(ref f, ref stdout) in &state.not_failures {
99 successes.push(f.name.to_string());
100 if !stdout.is_empty() {
101 stdouts.push_str(&format!("---- {} stdout ----\n", f.name));
102 let output = String::from_utf8_lossy(stdout);
103 stdouts.push_str(&output);
104 stdouts.push_str("\n");
107 if !stdouts.is_empty() {
108 self.write_plain("\n")?;
109 self.write_plain(&stdouts)?;
112 self.write_plain("\nsuccesses:\n")?;
114 for name in &successes {
115 self.write_plain(&format!(" {}\n", name))?;
120 pub fn write_failures(&mut self, state: &ConsoleTestState) -> io::Result<()> {
121 self.write_plain("\nfailures:\n")?;
122 let mut failures = Vec::new();
123 let mut fail_out = String::new();
124 for &(ref f, ref stdout) in &state.failures {
125 failures.push(f.name.to_string());
126 if !stdout.is_empty() {
127 fail_out.push_str(&format!("---- {} stdout ----\n", f.name));
128 let output = String::from_utf8_lossy(stdout);
129 fail_out.push_str(&output);
130 fail_out.push_str("\n");
133 if !fail_out.is_empty() {
134 self.write_plain("\n")?;
135 self.write_plain(&fail_out)?;
138 self.write_plain("\nfailures:\n")?;
140 for name in &failures {
141 self.write_plain(&format!(" {}\n", name))?;
146 fn write_test_name(&mut self, desc: &TestDesc) -> io::Result<()> {
147 let name = desc.padded_name(self.max_name_len, desc.name.padding());
148 self.write_plain(&format!("test {} ... ", name))?;
154 impl<T: Write> OutputFormatter for TerseFormatter<T> {
155 fn write_run_start(&mut self, test_count: usize) -> io::Result<()> {
156 self.total_test_count = test_count;
157 let noun = if test_count != 1 { "tests" } else { "test" };
158 self.write_plain(&format!("\nrunning {} {}\n", test_count, noun))
161 fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()> {
162 // Remnants from old libtest code that used the padding value
163 // in order to indicate benchmarks.
164 // When running benchmarks, terse-mode should still print their name as if
165 // it is the Pretty formatter.
166 if !self.is_multithreaded && desc.name.padding() == PadOnRight {
167 self.write_test_name(desc)?;
177 _: Option<&TestExecTime>,
179 _: &ConsoleTestState,
180 ) -> io::Result<()> {
182 TrOk => self.write_ok(),
183 TrFailed | TrFailedMsg(_) => self.write_failed(),
184 TrIgnored => self.write_ignored(),
185 TrAllowedFail => self.write_allowed_fail(),
187 if self.is_multithreaded {
188 self.write_test_name(desc)?;
191 self.write_plain(&format!(": {}\n", fmt_bench_samples(bs)))
196 fn write_timeout(&mut self, desc: &TestDesc) -> io::Result<()> {
197 self.write_plain(&format!(
198 "test {} has been running for over {} seconds\n",
199 desc.name, TEST_WARN_TIMEOUT_S
203 fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool> {
204 if state.options.display_output {
205 self.write_outputs(state)?;
207 let success = state.failed == 0;
209 self.write_failures(state)?;
212 self.write_plain("\ntest result: ")?;
215 // There's no parallelism at this point so it's safe to use color
216 self.write_pretty("ok", term::color::GREEN)?;
218 self.write_pretty("FAILED", term::color::RED)?;
221 let s = if state.allowed_fail > 0 {
223 ". {} passed; {} failed ({} allowed); {} ignored; {} measured; {} filtered out\n\n",
225 state.failed + state.allowed_fail,
233 ". {} passed; {} failed; {} ignored; {} measured; {} filtered out\n\n",
234 state.passed, state.failed, state.ignored, state.measured, state.filtered_out
238 self.write_plain(&s)?;