| 1 | use std::io; |
| 2 | use std::io::prelude::Write; |
| 3 | |
| 4 | use super::OutputFormatter; |
| 5 | use crate::bench::fmt_bench_samples; |
| 6 | use crate::console::{ConsoleTestDiscoveryState, ConsoleTestState, OutputLocation}; |
| 7 | use crate::test_result::TestResult; |
| 8 | use crate::types::{NamePadding, TestDesc}; |
| 9 | use crate::{term, time}; |
| 10 | |
| 11 | // We insert a '\n' when the output hits 100 columns in quiet mode. 88 test |
| 12 | // result chars leaves 12 chars for a progress count like " 11704/12853". |
| 13 | const QUIET_MODE_MAX_COLUMN: usize = 88; |
| 14 | |
| 15 | pub(crate) struct TerseFormatter<T> { |
| 16 | out: OutputLocation<T>, |
| 17 | use_color: bool, |
| 18 | is_multithreaded: bool, |
| 19 | /// Number of columns to fill when aligning names |
| 20 | max_name_len: usize, |
| 21 | |
| 22 | test_count: usize, |
| 23 | test_column: usize, |
| 24 | total_test_count: usize, |
| 25 | } |
| 26 | |
| 27 | impl<T: Write> TerseFormatter<T> { |
| 28 | pub(crate) fn new( |
| 29 | out: OutputLocation<T>, |
| 30 | use_color: bool, |
| 31 | max_name_len: usize, |
| 32 | is_multithreaded: bool, |
| 33 | ) -> Self { |
| 34 | TerseFormatter { |
| 35 | out, |
| 36 | use_color, |
| 37 | max_name_len, |
| 38 | is_multithreaded, |
| 39 | test_count: 0, |
| 40 | test_column: 0, |
| 41 | total_test_count: 0, // initialized later, when write_run_start is called |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | pub(crate) fn write_ok(&mut self) -> io::Result<()> { |
| 46 | self.write_short_result("." , term::color::GREEN) |
| 47 | } |
| 48 | |
| 49 | pub(crate) fn write_failed(&mut self, name: &str) -> io::Result<()> { |
| 50 | // Put failed tests on their own line and include the test name, so that it's faster |
| 51 | // to see which test failed without having to wait for them all to run. |
| 52 | |
| 53 | // normally, we write the progress unconditionally, even if the previous line was cut short. |
| 54 | // but if this is the very first column, no short results will have been printed and we'll end up with *only* the progress on the line. |
| 55 | // avoid this. |
| 56 | if self.test_column != 0 { |
| 57 | self.write_progress()?; |
| 58 | } |
| 59 | self.test_count += 1; |
| 60 | self.write_plain(format!(" {name} --- " ))?; |
| 61 | self.write_pretty("FAILED" , term::color::RED)?; |
| 62 | self.write_plain(" \n" ) |
| 63 | } |
| 64 | |
| 65 | pub(crate) fn write_ignored(&mut self) -> io::Result<()> { |
| 66 | self.write_short_result("i" , term::color::YELLOW) |
| 67 | } |
| 68 | |
| 69 | pub(crate) fn write_bench(&mut self) -> io::Result<()> { |
| 70 | self.write_pretty("bench" , term::color::CYAN) |
| 71 | } |
| 72 | |
| 73 | pub(crate) fn write_short_result( |
| 74 | &mut self, |
| 75 | result: &str, |
| 76 | color: term::color::Color, |
| 77 | ) -> io::Result<()> { |
| 78 | self.write_pretty(result, color)?; |
| 79 | self.test_count += 1; |
| 80 | self.test_column += 1; |
| 81 | if self.test_column % QUIET_MODE_MAX_COLUMN == QUIET_MODE_MAX_COLUMN - 1 { |
| 82 | // We insert a new line regularly in order to flush the |
| 83 | // screen when dealing with line-buffered output (e.g., piping to |
| 84 | // `stamp` in the Rust CI). |
| 85 | self.write_progress()?; |
| 86 | } |
| 87 | |
| 88 | Ok(()) |
| 89 | } |
| 90 | |
| 91 | fn write_progress(&mut self) -> io::Result<()> { |
| 92 | let out = format!(" {}/ {}\n" , self.test_count, self.total_test_count); |
| 93 | self.write_plain(out)?; |
| 94 | self.test_column = 0; |
| 95 | Ok(()) |
| 96 | } |
| 97 | |
| 98 | pub(crate) fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> { |
| 99 | match self.out { |
| 100 | OutputLocation::Pretty(ref mut term) => { |
| 101 | if self.use_color { |
| 102 | term.fg(color)?; |
| 103 | } |
| 104 | term.write_all(word.as_bytes())?; |
| 105 | if self.use_color { |
| 106 | term.reset()?; |
| 107 | } |
| 108 | term.flush() |
| 109 | } |
| 110 | OutputLocation::Raw(ref mut stdout) => { |
| 111 | stdout.write_all(word.as_bytes())?; |
| 112 | stdout.flush() |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | pub(crate) fn write_plain<S: AsRef<str>>(&mut self, s: S) -> io::Result<()> { |
| 118 | let s = s.as_ref(); |
| 119 | self.out.write_all(s.as_bytes())?; |
| 120 | self.out.flush() |
| 121 | } |
| 122 | |
| 123 | pub(crate) fn write_outputs(&mut self, state: &ConsoleTestState) -> io::Result<()> { |
| 124 | self.write_plain(" \nsuccesses: \n" )?; |
| 125 | let mut successes = Vec::new(); |
| 126 | let mut stdouts = String::new(); |
| 127 | for (f, stdout) in &state.not_failures { |
| 128 | successes.push(f.name.to_string()); |
| 129 | if !stdout.is_empty() { |
| 130 | stdouts.push_str(&format!("---- {} stdout ---- \n" , f.name)); |
| 131 | let output = String::from_utf8_lossy(stdout); |
| 132 | stdouts.push_str(&output); |
| 133 | stdouts.push(' \n' ); |
| 134 | } |
| 135 | } |
| 136 | if !stdouts.is_empty() { |
| 137 | self.write_plain(" \n" )?; |
| 138 | self.write_plain(&stdouts)?; |
| 139 | } |
| 140 | |
| 141 | self.write_plain(" \nsuccesses: \n" )?; |
| 142 | successes.sort(); |
| 143 | for name in &successes { |
| 144 | self.write_plain(&format!(" {name}\n" ))?; |
| 145 | } |
| 146 | Ok(()) |
| 147 | } |
| 148 | |
| 149 | pub(crate) fn write_failures(&mut self, state: &ConsoleTestState) -> io::Result<()> { |
| 150 | self.write_plain(" \nfailures: \n" )?; |
| 151 | let mut failures = Vec::new(); |
| 152 | let mut fail_out = String::new(); |
| 153 | for (f, stdout) in &state.failures { |
| 154 | failures.push(f.name.to_string()); |
| 155 | if !stdout.is_empty() { |
| 156 | fail_out.push_str(&format!("---- {} stdout ---- \n" , f.name)); |
| 157 | let output = String::from_utf8_lossy(stdout); |
| 158 | fail_out.push_str(&output); |
| 159 | fail_out.push(' \n' ); |
| 160 | } |
| 161 | } |
| 162 | if !fail_out.is_empty() { |
| 163 | self.write_plain(" \n" )?; |
| 164 | self.write_plain(&fail_out)?; |
| 165 | } |
| 166 | |
| 167 | self.write_plain(" \nfailures: \n" )?; |
| 168 | failures.sort(); |
| 169 | for name in &failures { |
| 170 | self.write_plain(&format!(" {name}\n" ))?; |
| 171 | } |
| 172 | Ok(()) |
| 173 | } |
| 174 | |
| 175 | fn write_test_name(&mut self, desc: &TestDesc) -> io::Result<()> { |
| 176 | let name = desc.padded_name(self.max_name_len, desc.name.padding()); |
| 177 | if let Some(test_mode) = desc.test_mode() { |
| 178 | self.write_plain(format!("test {name} - {test_mode} ... " ))?; |
| 179 | } else { |
| 180 | self.write_plain(format!("test {name} ... " ))?; |
| 181 | } |
| 182 | |
| 183 | Ok(()) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | impl<T: Write> OutputFormatter for TerseFormatter<T> { |
| 188 | fn write_discovery_start(&mut self) -> io::Result<()> { |
| 189 | Ok(()) |
| 190 | } |
| 191 | |
| 192 | fn write_test_discovered(&mut self, desc: &TestDesc, test_type: &str) -> io::Result<()> { |
| 193 | self.write_plain(format!(" {}: {test_type}\n" , desc.name)) |
| 194 | } |
| 195 | |
| 196 | fn write_discovery_finish(&mut self, _state: &ConsoleTestDiscoveryState) -> io::Result<()> { |
| 197 | Ok(()) |
| 198 | } |
| 199 | |
| 200 | fn write_run_start(&mut self, test_count: usize, shuffle_seed: Option<u64>) -> io::Result<()> { |
| 201 | self.total_test_count = test_count; |
| 202 | let noun = if test_count != 1 { "tests" } else { "test" }; |
| 203 | let shuffle_seed_msg = if let Some(shuffle_seed) = shuffle_seed { |
| 204 | format!(" (shuffle seed: {shuffle_seed})" ) |
| 205 | } else { |
| 206 | String::new() |
| 207 | }; |
| 208 | self.write_plain(format!(" \nrunning {test_count} {noun}{shuffle_seed_msg}\n" )) |
| 209 | } |
| 210 | |
| 211 | fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()> { |
| 212 | // Remnants from old libtest code that used the padding value |
| 213 | // in order to indicate benchmarks. |
| 214 | // When running benchmarks, terse-mode should still print their name as if |
| 215 | // it is the Pretty formatter. |
| 216 | if !self.is_multithreaded && desc.name.padding() == NamePadding::PadOnRight { |
| 217 | self.write_test_name(desc)?; |
| 218 | } |
| 219 | |
| 220 | Ok(()) |
| 221 | } |
| 222 | |
| 223 | fn write_result( |
| 224 | &mut self, |
| 225 | desc: &TestDesc, |
| 226 | result: &TestResult, |
| 227 | _: Option<&time::TestExecTime>, |
| 228 | _: &[u8], |
| 229 | _: &ConsoleTestState, |
| 230 | ) -> io::Result<()> { |
| 231 | match *result { |
| 232 | TestResult::TrOk => self.write_ok(), |
| 233 | TestResult::TrFailed | TestResult::TrFailedMsg(_) | TestResult::TrTimedFail => { |
| 234 | self.write_failed(desc.name.as_slice()) |
| 235 | } |
| 236 | TestResult::TrIgnored => self.write_ignored(), |
| 237 | TestResult::TrBench(ref bs) => { |
| 238 | if self.is_multithreaded { |
| 239 | self.write_test_name(desc)?; |
| 240 | } |
| 241 | self.write_bench()?; |
| 242 | self.write_plain(format!(": {}\n" , fmt_bench_samples(bs))) |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | fn write_timeout(&mut self, desc: &TestDesc) -> io::Result<()> { |
| 248 | self.write_plain(format!( |
| 249 | "test {} has been running for over {} seconds \n" , |
| 250 | desc.name, |
| 251 | time::TEST_WARN_TIMEOUT_S |
| 252 | )) |
| 253 | } |
| 254 | |
| 255 | fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool> { |
| 256 | if state.options.display_output { |
| 257 | self.write_outputs(state)?; |
| 258 | } |
| 259 | let success = state.failed == 0; |
| 260 | if !success { |
| 261 | self.write_failures(state)?; |
| 262 | } |
| 263 | |
| 264 | self.write_plain(" \ntest result: " )?; |
| 265 | |
| 266 | if success { |
| 267 | // There's no parallelism at this point so it's safe to use color |
| 268 | self.write_pretty("ok" , term::color::GREEN)?; |
| 269 | } else { |
| 270 | self.write_pretty("FAILED" , term::color::RED)?; |
| 271 | } |
| 272 | |
| 273 | let s = format!( |
| 274 | ". {} passed; {} failed; {} ignored; {} measured; {} filtered out" , |
| 275 | state.passed, state.failed, state.ignored, state.measured, state.filtered_out |
| 276 | ); |
| 277 | |
| 278 | self.write_plain(s)?; |
| 279 | |
| 280 | if let Some(ref exec_time) = state.exec_time { |
| 281 | let time_str = format!("; finished in {exec_time}" ); |
| 282 | self.write_plain(time_str)?; |
| 283 | } |
| 284 | |
| 285 | self.write_plain(" \n\n" )?; |
| 286 | |
| 287 | // Custom handling of cases where there is only 1 test to execute and that test was ignored. |
| 288 | // We want to show more detailed information(why was the test ignored) for investigation purposes. |
| 289 | if self.total_test_count == 1 && state.ignores.len() == 1 { |
| 290 | let test_desc = &state.ignores[0].0; |
| 291 | if let Some(im) = test_desc.ignore_message { |
| 292 | self.write_plain(format!("test: {}, ignore_message: {}\n\n" , test_desc.name, im))?; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | Ok(success) |
| 297 | } |
| 298 | } |
| 299 | |