| 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::TestDesc; |
| 9 | use crate::{term, time}; |
| 10 | |
| 11 | pub(crate) struct PrettyFormatter<T> { |
| 12 | out: OutputLocation<T>, |
| 13 | use_color: bool, |
| 14 | time_options: Option<time::TestTimeOptions>, |
| 15 | |
| 16 | /// Number of columns to fill when aligning names |
| 17 | max_name_len: usize, |
| 18 | |
| 19 | is_multithreaded: bool, |
| 20 | } |
| 21 | |
| 22 | impl<T: Write> PrettyFormatter<T> { |
| 23 | pub(crate) fn new( |
| 24 | out: OutputLocation<T>, |
| 25 | use_color: bool, |
| 26 | max_name_len: usize, |
| 27 | is_multithreaded: bool, |
| 28 | time_options: Option<time::TestTimeOptions>, |
| 29 | ) -> Self { |
| 30 | PrettyFormatter { out, use_color, max_name_len, is_multithreaded, time_options } |
| 31 | } |
| 32 | |
| 33 | #[cfg (test)] |
| 34 | pub(crate) fn output_location(&self) -> &OutputLocation<T> { |
| 35 | &self.out |
| 36 | } |
| 37 | |
| 38 | pub(crate) fn write_ok(&mut self) -> io::Result<()> { |
| 39 | self.write_short_result("ok" , term::color::GREEN) |
| 40 | } |
| 41 | |
| 42 | pub(crate) fn write_failed(&mut self) -> io::Result<()> { |
| 43 | self.write_short_result("FAILED" , term::color::RED) |
| 44 | } |
| 45 | |
| 46 | pub(crate) fn write_ignored(&mut self, message: Option<&'static str>) -> io::Result<()> { |
| 47 | if let Some(message) = message { |
| 48 | self.write_short_result(&format!("ignored, {message}" ), term::color::YELLOW) |
| 49 | } else { |
| 50 | self.write_short_result("ignored" , term::color::YELLOW) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub(crate) fn write_time_failed(&mut self) -> io::Result<()> { |
| 55 | self.write_short_result("FAILED (time limit exceeded)" , term::color::RED) |
| 56 | } |
| 57 | |
| 58 | pub(crate) fn write_bench(&mut self) -> io::Result<()> { |
| 59 | self.write_pretty("bench" , term::color::CYAN) |
| 60 | } |
| 61 | |
| 62 | pub(crate) fn write_short_result( |
| 63 | &mut self, |
| 64 | result: &str, |
| 65 | color: term::color::Color, |
| 66 | ) -> io::Result<()> { |
| 67 | self.write_pretty(result, color) |
| 68 | } |
| 69 | |
| 70 | pub(crate) fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> { |
| 71 | match self.out { |
| 72 | OutputLocation::Pretty(ref mut term) => { |
| 73 | if self.use_color { |
| 74 | term.fg(color)?; |
| 75 | } |
| 76 | term.write_all(word.as_bytes())?; |
| 77 | if self.use_color { |
| 78 | term.reset()?; |
| 79 | } |
| 80 | term.flush() |
| 81 | } |
| 82 | OutputLocation::Raw(ref mut stdout) => { |
| 83 | stdout.write_all(word.as_bytes())?; |
| 84 | stdout.flush() |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | pub(crate) fn write_plain<S: AsRef<str>>(&mut self, s: S) -> io::Result<()> { |
| 90 | let s = s.as_ref(); |
| 91 | self.out.write_all(s.as_bytes())?; |
| 92 | self.out.flush() |
| 93 | } |
| 94 | |
| 95 | fn write_time( |
| 96 | &mut self, |
| 97 | desc: &TestDesc, |
| 98 | exec_time: Option<&time::TestExecTime>, |
| 99 | ) -> io::Result<()> { |
| 100 | if let (Some(opts), Some(time)) = (self.time_options, exec_time) { |
| 101 | let time_str = format!(" < {time}>" ); |
| 102 | |
| 103 | let color = if self.use_color { |
| 104 | if opts.is_critical(desc, time) { |
| 105 | Some(term::color::RED) |
| 106 | } else if opts.is_warn(desc, time) { |
| 107 | Some(term::color::YELLOW) |
| 108 | } else { |
| 109 | None |
| 110 | } |
| 111 | } else { |
| 112 | None |
| 113 | }; |
| 114 | |
| 115 | match color { |
| 116 | Some(color) => self.write_pretty(&time_str, color)?, |
| 117 | None => self.write_plain(&time_str)?, |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | Ok(()) |
| 122 | } |
| 123 | |
| 124 | fn write_results( |
| 125 | &mut self, |
| 126 | inputs: &Vec<(TestDesc, Vec<u8>)>, |
| 127 | results_type: &str, |
| 128 | ) -> io::Result<()> { |
| 129 | let results_out_str = format!(" \n{results_type}: \n" ); |
| 130 | |
| 131 | self.write_plain(&results_out_str)?; |
| 132 | |
| 133 | let mut results = Vec::new(); |
| 134 | let mut stdouts = String::new(); |
| 135 | for (f, stdout) in inputs { |
| 136 | results.push(f.name.to_string()); |
| 137 | if !stdout.is_empty() { |
| 138 | stdouts.push_str(&format!("---- {} stdout ---- \n" , f.name)); |
| 139 | let output = String::from_utf8_lossy(stdout); |
| 140 | stdouts.push_str(&output); |
| 141 | stdouts.push(' \n' ); |
| 142 | } |
| 143 | } |
| 144 | if !stdouts.is_empty() { |
| 145 | self.write_plain(" \n" )?; |
| 146 | self.write_plain(&stdouts)?; |
| 147 | } |
| 148 | |
| 149 | self.write_plain(&results_out_str)?; |
| 150 | results.sort(); |
| 151 | for name in &results { |
| 152 | self.write_plain(&format!(" {name}\n" ))?; |
| 153 | } |
| 154 | Ok(()) |
| 155 | } |
| 156 | |
| 157 | pub(crate) fn write_successes(&mut self, state: &ConsoleTestState) -> io::Result<()> { |
| 158 | self.write_results(&state.not_failures, "successes" ) |
| 159 | } |
| 160 | |
| 161 | pub(crate) fn write_failures(&mut self, state: &ConsoleTestState) -> io::Result<()> { |
| 162 | self.write_results(&state.failures, "failures" ) |
| 163 | } |
| 164 | |
| 165 | pub(crate) fn write_time_failures(&mut self, state: &ConsoleTestState) -> io::Result<()> { |
| 166 | self.write_results(&state.time_failures, "failures (time limit exceeded)" ) |
| 167 | } |
| 168 | |
| 169 | fn write_test_name(&mut self, desc: &TestDesc) -> io::Result<()> { |
| 170 | let name = desc.padded_name(self.max_name_len, desc.name.padding()); |
| 171 | if let Some(test_mode) = desc.test_mode() { |
| 172 | self.write_plain(format!("test {name} - {test_mode} ... " ))?; |
| 173 | } else { |
| 174 | self.write_plain(format!("test {name} ... " ))?; |
| 175 | } |
| 176 | |
| 177 | Ok(()) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | impl<T: Write> OutputFormatter for PrettyFormatter<T> { |
| 182 | fn write_discovery_start(&mut self) -> io::Result<()> { |
| 183 | Ok(()) |
| 184 | } |
| 185 | |
| 186 | fn write_test_discovered(&mut self, desc: &TestDesc, test_type: &str) -> io::Result<()> { |
| 187 | self.write_plain(format!(" {}: {test_type}\n" , desc.name)) |
| 188 | } |
| 189 | |
| 190 | fn write_discovery_finish(&mut self, state: &ConsoleTestDiscoveryState) -> io::Result<()> { |
| 191 | fn plural(count: usize, s: &str) -> String { |
| 192 | match count { |
| 193 | 1 => format!("1 {s}" ), |
| 194 | n => format!(" {n} {s}s" ), |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if state.tests != 0 || state.benchmarks != 0 { |
| 199 | self.write_plain(" \n" )?; |
| 200 | } |
| 201 | |
| 202 | self.write_plain(format!( |
| 203 | " {}, {}\n" , |
| 204 | plural(state.tests, "test" ), |
| 205 | plural(state.benchmarks, "benchmark" ) |
| 206 | )) |
| 207 | } |
| 208 | |
| 209 | fn write_run_start(&mut self, test_count: usize, shuffle_seed: Option<u64>) -> io::Result<()> { |
| 210 | let noun = if test_count != 1 { "tests" } else { "test" }; |
| 211 | let shuffle_seed_msg = if let Some(shuffle_seed) = shuffle_seed { |
| 212 | format!(" (shuffle seed: {shuffle_seed})" ) |
| 213 | } else { |
| 214 | String::new() |
| 215 | }; |
| 216 | self.write_plain(format!(" \nrunning {test_count} {noun}{shuffle_seed_msg}\n" )) |
| 217 | } |
| 218 | |
| 219 | fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()> { |
| 220 | // When running tests concurrently, we should not print |
| 221 | // the test's name as the result will be mis-aligned. |
| 222 | // When running the tests serially, we print the name here so |
| 223 | // that the user can see which test hangs. |
| 224 | if !self.is_multithreaded { |
| 225 | self.write_test_name(desc)?; |
| 226 | } |
| 227 | |
| 228 | Ok(()) |
| 229 | } |
| 230 | |
| 231 | fn write_result( |
| 232 | &mut self, |
| 233 | desc: &TestDesc, |
| 234 | result: &TestResult, |
| 235 | exec_time: Option<&time::TestExecTime>, |
| 236 | _: &[u8], |
| 237 | _: &ConsoleTestState, |
| 238 | ) -> io::Result<()> { |
| 239 | if self.is_multithreaded { |
| 240 | self.write_test_name(desc)?; |
| 241 | } |
| 242 | |
| 243 | match *result { |
| 244 | TestResult::TrOk => self.write_ok()?, |
| 245 | TestResult::TrFailed | TestResult::TrFailedMsg(_) => self.write_failed()?, |
| 246 | TestResult::TrIgnored => self.write_ignored(desc.ignore_message)?, |
| 247 | TestResult::TrBench(ref bs) => { |
| 248 | self.write_bench()?; |
| 249 | self.write_plain(format!(": {}" , fmt_bench_samples(bs)))?; |
| 250 | } |
| 251 | TestResult::TrTimedFail => self.write_time_failed()?, |
| 252 | } |
| 253 | |
| 254 | self.write_time(desc, exec_time)?; |
| 255 | self.write_plain(" \n" ) |
| 256 | } |
| 257 | |
| 258 | fn write_timeout(&mut self, desc: &TestDesc) -> io::Result<()> { |
| 259 | self.write_plain(format!( |
| 260 | "test {} has been running for over {} seconds \n" , |
| 261 | desc.name, |
| 262 | time::TEST_WARN_TIMEOUT_S |
| 263 | )) |
| 264 | } |
| 265 | |
| 266 | fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool> { |
| 267 | if state.options.display_output { |
| 268 | self.write_successes(state)?; |
| 269 | } |
| 270 | let success = state.failed == 0; |
| 271 | if !success { |
| 272 | if !state.failures.is_empty() { |
| 273 | self.write_failures(state)?; |
| 274 | } |
| 275 | |
| 276 | if !state.time_failures.is_empty() { |
| 277 | self.write_time_failures(state)?; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | self.write_plain(" \ntest result: " )?; |
| 282 | |
| 283 | if success { |
| 284 | // There's no parallelism at this point so it's safe to use color |
| 285 | self.write_pretty("ok" , term::color::GREEN)?; |
| 286 | } else { |
| 287 | self.write_pretty("FAILED" , term::color::RED)?; |
| 288 | } |
| 289 | |
| 290 | let s = format!( |
| 291 | ". {} passed; {} failed; {} ignored; {} measured; {} filtered out" , |
| 292 | state.passed, state.failed, state.ignored, state.measured, state.filtered_out |
| 293 | ); |
| 294 | |
| 295 | self.write_plain(s)?; |
| 296 | |
| 297 | if let Some(ref exec_time) = state.exec_time { |
| 298 | let time_str = format!("; finished in {exec_time}" ); |
| 299 | self.write_plain(time_str)?; |
| 300 | } |
| 301 | |
| 302 | self.write_plain(" \n\n" )?; |
| 303 | |
| 304 | Ok(success) |
| 305 | } |
| 306 | } |
| 307 | |