|
| 1 | +use std::io::{self, prelude::Write}; |
| 2 | +use std::time::Duration; |
| 3 | + |
| 4 | +use super::OutputFormatter; |
| 5 | +use crate::{ |
| 6 | + console::{ConsoleTestState, OutputLocation}, |
| 7 | + test_result::TestResult, |
| 8 | + time, |
| 9 | + types::{TestDesc, TestType}, |
| 10 | +}; |
| 11 | + |
| 12 | +pub struct JunitFormatter<T> { |
| 13 | + out: OutputLocation<T>, |
| 14 | + results: Vec<(TestDesc, TestResult, Duration)>, |
| 15 | +} |
| 16 | + |
| 17 | +impl<T: Write> JunitFormatter<T> { |
| 18 | + pub fn new(out: OutputLocation<T>) -> Self { |
| 19 | + Self { out, results: Vec::new() } |
| 20 | + } |
| 21 | + |
| 22 | + fn write_message(&mut self, s: &str) -> io::Result<()> { |
| 23 | + assert!(!s.contains('\n')); |
| 24 | + |
| 25 | + self.out.write_all(s.as_ref()) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl<T: Write> OutputFormatter for JunitFormatter<T> { |
| 30 | + fn write_run_start(&mut self, _test_count: usize) -> io::Result<()> { |
| 31 | + // We write xml header on run start |
| 32 | + self.write_message(&"<?xml version=\"1.0\" encoding=\"UTF-8\"?>") |
| 33 | + } |
| 34 | + |
| 35 | + fn write_test_start(&mut self, _desc: &TestDesc) -> io::Result<()> { |
| 36 | + // We do not output anything on test start. |
| 37 | + Ok(()) |
| 38 | + } |
| 39 | + |
| 40 | + fn write_timeout(&mut self, _desc: &TestDesc) -> io::Result<()> { |
| 41 | + // We do not output anything on test timeout. |
| 42 | + Ok(()) |
| 43 | + } |
| 44 | + |
| 45 | + fn write_result( |
| 46 | + &mut self, |
| 47 | + desc: &TestDesc, |
| 48 | + result: &TestResult, |
| 49 | + exec_time: Option<&time::TestExecTime>, |
| 50 | + _stdout: &[u8], |
| 51 | + _state: &ConsoleTestState, |
| 52 | + ) -> io::Result<()> { |
| 53 | + // Because the testsuit node holds some of the information as attributes, we can't write it |
| 54 | + // until all of the tests has ran. Instead of writting every result as they come in, we add |
| 55 | + // them to a Vec and write them all at once when run is complete. |
| 56 | + let duration = exec_time.map(|t| t.0.clone()).unwrap_or_default(); |
| 57 | + self.results.push((desc.clone(), result.clone(), duration)); |
| 58 | + Ok(()) |
| 59 | + } |
| 60 | + fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool> { |
| 61 | + self.write_message("<testsuites>")?; |
| 62 | + |
| 63 | + self.write_message(&*format!( |
| 64 | + "<testsuite name=\"test\" package=\"test\" id=\"0\" \ |
| 65 | + errors=\"0\" \ |
| 66 | + failures=\"{}\" \ |
| 67 | + tests=\"{}\" \ |
| 68 | + skipped=\"{}\" \ |
| 69 | + >", |
| 70 | + state.failed, state.total, state.ignored |
| 71 | + ))?; |
| 72 | + for (desc, result, duration) in std::mem::replace(&mut self.results, Vec::new()) { |
| 73 | + let (class_name, test_name) = parse_class_name(&desc); |
| 74 | + match result { |
| 75 | + TestResult::TrIgnored => { /* no-op */ } |
| 76 | + TestResult::TrFailed => { |
| 77 | + self.write_message(&*format!( |
| 78 | + "<testcase classname=\"{}\" \ |
| 79 | + name=\"{}\" time=\"{}\">", |
| 80 | + class_name, |
| 81 | + test_name, |
| 82 | + duration.as_secs() |
| 83 | + ))?; |
| 84 | + self.write_message("<failure type=\"assert\"/>")?; |
| 85 | + self.write_message("</testcase>")?; |
| 86 | + } |
| 87 | + |
| 88 | + TestResult::TrFailedMsg(ref m) => { |
| 89 | + self.write_message(&*format!( |
| 90 | + "<testcase classname=\"{}\" \ |
| 91 | + name=\"{}\" time=\"{}\">", |
| 92 | + class_name, |
| 93 | + test_name, |
| 94 | + duration.as_secs() |
| 95 | + ))?; |
| 96 | + self.write_message(&*format!("<failure message=\"{}\" type=\"assert\"/>", m))?; |
| 97 | + self.write_message("</testcase>")?; |
| 98 | + } |
| 99 | + |
| 100 | + TestResult::TrTimedFail => { |
| 101 | + self.write_message(&*format!( |
| 102 | + "<testcase classname=\"{}\" \ |
| 103 | + name=\"{}\" time=\"{}\">", |
| 104 | + class_name, |
| 105 | + test_name, |
| 106 | + duration.as_secs() |
| 107 | + ))?; |
| 108 | + self.write_message("<failure type=\"timeout\"/>")?; |
| 109 | + self.write_message("</testcase>")?; |
| 110 | + } |
| 111 | + |
| 112 | + TestResult::TrBench(ref b) => { |
| 113 | + self.write_message(&*format!( |
| 114 | + "<testcase classname=\"benchmark::{}\" \ |
| 115 | + name=\"{}\" time=\"{}\" />", |
| 116 | + class_name, test_name, b.ns_iter_summ.sum |
| 117 | + ))?; |
| 118 | + } |
| 119 | + |
| 120 | + TestResult::TrOk | TestResult::TrAllowedFail => { |
| 121 | + self.write_message(&*format!( |
| 122 | + "<testcase classname=\"{}\" \ |
| 123 | + name=\"{}\" time=\"{}\"/>", |
| 124 | + class_name, |
| 125 | + test_name, |
| 126 | + duration.as_secs() |
| 127 | + ))?; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + self.write_message("<system-out/>")?; |
| 132 | + self.write_message("<system-err/>")?; |
| 133 | + self.write_message("</testsuite>")?; |
| 134 | + self.write_message("</testsuites>")?; |
| 135 | + |
| 136 | + Ok(state.failed == 0) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +fn parse_class_name(desc: &TestDesc) -> (String, String) { |
| 141 | + match desc.test_type { |
| 142 | + TestType::UnitTest => parse_class_name_unit(desc), |
| 143 | + TestType::DocTest => parse_class_name_doc(desc), |
| 144 | + TestType::IntegrationTest => parse_class_name_integration(desc), |
| 145 | + TestType::Unknown => (String::from("unknown"), String::from(desc.name.as_slice())), |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +fn parse_class_name_unit(desc: &TestDesc) -> (String, String) { |
| 150 | + // Module path => classname |
| 151 | + // Function name => name |
| 152 | + let module_segments: Vec<&str> = desc.name.as_slice().split("::").collect(); |
| 153 | + let (class_name, test_name) = match module_segments[..] { |
| 154 | + [test] => (String::from("crate"), String::from(test)), |
| 155 | + [ref path @ .., test] => (path.join("::"), String::from(test)), |
| 156 | + [..] => unreachable!(), |
| 157 | + }; |
| 158 | + (class_name, test_name) |
| 159 | +} |
| 160 | + |
| 161 | +fn parse_class_name_doc(desc: &TestDesc) -> (String, String) { |
| 162 | + // File path => classname |
| 163 | + // Line # => test name |
| 164 | + let segments: Vec<&str> = desc.name.as_slice().split(" - ").collect(); |
| 165 | + let (class_name, test_name) = match segments[..] { |
| 166 | + [file, line] => (String::from(file.trim()), String::from(line.trim())), |
| 167 | + [..] => unreachable!(), |
| 168 | + }; |
| 169 | + (class_name, test_name) |
| 170 | +} |
| 171 | + |
| 172 | +fn parse_class_name_integration(desc: &TestDesc) -> (String, String) { |
| 173 | + (String::from("integration"), String::from(desc.name.as_slice())) |
| 174 | +} |
0 commit comments