]> git.lizzy.rs Git - rust.git/blob - src/libtest/formatters/terse.rs
Rollup merge of #65237 - KodrAus:fix/map-entry-err, r=sfackler
[rust.git] / src / libtest / formatters / terse.rs
1 use super::*;
2
3 pub(crate) struct TerseFormatter<T> {
4     out: OutputLocation<T>,
5     use_color: bool,
6     is_multithreaded: bool,
7     /// Number of columns to fill when aligning names
8     max_name_len: usize,
9
10     test_count: usize,
11     total_test_count: usize,
12 }
13
14 impl<T: Write> TerseFormatter<T> {
15     pub fn new(
16         out: OutputLocation<T>,
17         use_color: bool,
18         max_name_len: usize,
19         is_multithreaded: bool,
20     ) -> Self {
21         TerseFormatter {
22             out,
23             use_color,
24             max_name_len,
25             is_multithreaded,
26             test_count: 0,
27             total_test_count: 0, // initialized later, when write_run_start is called
28         }
29     }
30
31     pub fn write_ok(&mut self) -> io::Result<()> {
32         self.write_short_result(".", term::color::GREEN)
33     }
34
35     pub fn write_failed(&mut self) -> io::Result<()> {
36         self.write_short_result("F", term::color::RED)
37     }
38
39     pub fn write_ignored(&mut self) -> io::Result<()> {
40         self.write_short_result("i", term::color::YELLOW)
41     }
42
43     pub fn write_allowed_fail(&mut self) -> io::Result<()> {
44         self.write_short_result("a", term::color::YELLOW)
45     }
46
47     pub fn write_bench(&mut self) -> io::Result<()> {
48         self.write_pretty("bench", term::color::CYAN)
49     }
50
51     pub fn write_short_result(
52         &mut self,
53         result: &str,
54         color: term::color::Color,
55     ) -> io::Result<()> {
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)?;
63         }
64
65         self.test_count += 1;
66         Ok(())
67     }
68
69     pub fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
70         match self.out {
71             Pretty(ref mut term) => {
72                 if self.use_color {
73                     term.fg(color)?;
74                 }
75                 term.write_all(word.as_bytes())?;
76                 if self.use_color {
77                     term.reset()?;
78                 }
79                 term.flush()
80             }
81             Raw(ref mut stdout) => {
82                 stdout.write_all(word.as_bytes())?;
83                 stdout.flush()
84             }
85         }
86     }
87
88     pub fn write_plain<S: AsRef<str>>(&mut self, s: S) -> io::Result<()> {
89         let s = s.as_ref();
90         self.out.write_all(s.as_bytes())?;
91         self.out.flush()
92     }
93
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");
105             }
106         }
107         if !stdouts.is_empty() {
108             self.write_plain("\n")?;
109             self.write_plain(&stdouts)?;
110         }
111
112         self.write_plain("\nsuccesses:\n")?;
113         successes.sort();
114         for name in &successes {
115             self.write_plain(&format!("    {}\n", name))?;
116         }
117         Ok(())
118     }
119
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");
131             }
132         }
133         if !fail_out.is_empty() {
134             self.write_plain("\n")?;
135             self.write_plain(&fail_out)?;
136         }
137
138         self.write_plain("\nfailures:\n")?;
139         failures.sort();
140         for name in &failures {
141             self.write_plain(&format!("    {}\n", name))?;
142         }
143         Ok(())
144     }
145
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))?;
149
150         Ok(())
151     }
152 }
153
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))
159     }
160
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)?;
168         }
169
170         Ok(())
171     }
172
173     fn write_result(
174         &mut self,
175         desc: &TestDesc,
176         result: &TestResult,
177         _: Option<&TestExecTime>,
178         _: &[u8],
179         _: &ConsoleTestState,
180     ) -> io::Result<()> {
181         match *result {
182             TrOk => self.write_ok(),
183             TrFailed | TrFailedMsg(_) | TrTimedFail => self.write_failed(),
184             TrIgnored => self.write_ignored(),
185             TrAllowedFail => self.write_allowed_fail(),
186             TrBench(ref bs) => {
187                 if self.is_multithreaded {
188                     self.write_test_name(desc)?;
189                 }
190                 self.write_bench()?;
191                 self.write_plain(&format!(": {}\n", fmt_bench_samples(bs)))
192             }
193         }
194     }
195
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
200         ))
201     }
202
203     fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool> {
204         if state.options.display_output {
205             self.write_outputs(state)?;
206         }
207         let success = state.failed == 0;
208         if !success {
209             self.write_failures(state)?;
210         }
211
212         self.write_plain("\ntest result: ")?;
213
214         if success {
215             // There's no parallelism at this point so it's safe to use color
216             self.write_pretty("ok", term::color::GREEN)?;
217         } else {
218             self.write_pretty("FAILED", term::color::RED)?;
219         }
220
221         let s = if state.allowed_fail > 0 {
222             format!(
223                 ". {} passed; {} failed ({} allowed); {} ignored; {} measured; {} filtered out\n\n",
224                 state.passed,
225                 state.failed + state.allowed_fail,
226                 state.allowed_fail,
227                 state.ignored,
228                 state.measured,
229                 state.filtered_out
230             )
231         } else {
232             format!(
233                 ". {} passed; {} failed; {} ignored; {} measured; {} filtered out\n\n",
234                 state.passed, state.failed, state.ignored, state.measured, state.filtered_out
235             )
236         };
237
238         self.write_plain(&s)?;
239
240         Ok(success)
241     }
242 }