]> git.lizzy.rs Git - rust.git/blob - src/libtest/formatters/terse.rs
Rollup merge of #65696 - varkor:nll-chalk-const-generics-issue, r=eddyb
[rust.git] / src / libtest / formatters / terse.rs
1 use std::{
2     io,
3     io::prelude::Write,
4 };
5
6 use crate::{
7     types::TestDesc,
8     time,
9     test_result::TestResult,
10     types::NamePadding,
11     console::{ConsoleTestState, OutputLocation},
12     bench::fmt_bench_samples,
13 };
14 use super::OutputFormatter;
15
16 // insert a '\n' after 100 tests in quiet mode
17 const QUIET_MODE_MAX_COLUMN: usize = 100;
18
19 pub(crate) struct TerseFormatter<T> {
20     out: OutputLocation<T>,
21     use_color: bool,
22     is_multithreaded: bool,
23     /// Number of columns to fill when aligning names
24     max_name_len: usize,
25
26     test_count: usize,
27     total_test_count: usize,
28 }
29
30 impl<T: Write> TerseFormatter<T> {
31     pub fn new(
32         out: OutputLocation<T>,
33         use_color: bool,
34         max_name_len: usize,
35         is_multithreaded: bool,
36     ) -> Self {
37         TerseFormatter {
38             out,
39             use_color,
40             max_name_len,
41             is_multithreaded,
42             test_count: 0,
43             total_test_count: 0, // initialized later, when write_run_start is called
44         }
45     }
46
47     pub fn write_ok(&mut self) -> io::Result<()> {
48         self.write_short_result(".", term::color::GREEN)
49     }
50
51     pub fn write_failed(&mut self) -> io::Result<()> {
52         self.write_short_result("F", term::color::RED)
53     }
54
55     pub fn write_ignored(&mut self) -> io::Result<()> {
56         self.write_short_result("i", term::color::YELLOW)
57     }
58
59     pub fn write_allowed_fail(&mut self) -> io::Result<()> {
60         self.write_short_result("a", term::color::YELLOW)
61     }
62
63     pub fn write_bench(&mut self) -> io::Result<()> {
64         self.write_pretty("bench", term::color::CYAN)
65     }
66
67     pub fn write_short_result(
68         &mut self,
69         result: &str,
70         color: term::color::Color,
71     ) -> io::Result<()> {
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)?;
79         }
80
81         self.test_count += 1;
82         Ok(())
83     }
84
85     pub fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
86         match self.out {
87             OutputLocation::Pretty(ref mut term) => {
88                 if self.use_color {
89                     term.fg(color)?;
90                 }
91                 term.write_all(word.as_bytes())?;
92                 if self.use_color {
93                     term.reset()?;
94                 }
95                 term.flush()
96             }
97             OutputLocation::Raw(ref mut stdout) => {
98                 stdout.write_all(word.as_bytes())?;
99                 stdout.flush()
100             }
101         }
102     }
103
104     pub fn write_plain<S: AsRef<str>>(&mut self, s: S) -> io::Result<()> {
105         let s = s.as_ref();
106         self.out.write_all(s.as_bytes())?;
107         self.out.flush()
108     }
109
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");
121             }
122         }
123         if !stdouts.is_empty() {
124             self.write_plain("\n")?;
125             self.write_plain(&stdouts)?;
126         }
127
128         self.write_plain("\nsuccesses:\n")?;
129         successes.sort();
130         for name in &successes {
131             self.write_plain(&format!("    {}\n", name))?;
132         }
133         Ok(())
134     }
135
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");
147             }
148         }
149         if !fail_out.is_empty() {
150             self.write_plain("\n")?;
151             self.write_plain(&fail_out)?;
152         }
153
154         self.write_plain("\nfailures:\n")?;
155         failures.sort();
156         for name in &failures {
157             self.write_plain(&format!("    {}\n", name))?;
158         }
159         Ok(())
160     }
161
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))?;
165
166         Ok(())
167     }
168 }
169
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))
175     }
176
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)?;
184         }
185
186         Ok(())
187     }
188
189     fn write_result(
190         &mut self,
191         desc: &TestDesc,
192         result: &TestResult,
193         _: Option<&time::TestExecTime>,
194         _: &[u8],
195         _: &ConsoleTestState,
196     ) -> io::Result<()> {
197         match *result {
198             TestResult::TrOk => self.write_ok(),
199             TestResult::TrFailed
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)?;
207                 }
208                 self.write_bench()?;
209                 self.write_plain(&format!(": {}\n", fmt_bench_samples(bs)))
210             }
211         }
212     }
213
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
218         ))
219     }
220
221     fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool> {
222         if state.options.display_output {
223             self.write_outputs(state)?;
224         }
225         let success = state.failed == 0;
226         if !success {
227             self.write_failures(state)?;
228         }
229
230         self.write_plain("\ntest result: ")?;
231
232         if success {
233             // There's no parallelism at this point so it's safe to use color
234             self.write_pretty("ok", term::color::GREEN)?;
235         } else {
236             self.write_pretty("FAILED", term::color::RED)?;
237         }
238
239         let s = if state.allowed_fail > 0 {
240             format!(
241                 ". {} passed; {} failed ({} allowed); {} ignored; {} measured; {} filtered out\n\n",
242                 state.passed,
243                 state.failed + state.allowed_fail,
244                 state.allowed_fail,
245                 state.ignored,
246                 state.measured,
247                 state.filtered_out
248             )
249         } else {
250             format!(
251                 ". {} passed; {} failed; {} ignored; {} measured; {} filtered out\n\n",
252                 state.passed, state.failed, state.ignored, state.measured, state.filtered_out
253             )
254         };
255
256         self.write_plain(&s)?;
257
258         Ok(success)
259     }
260 }