|
| 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, |
| 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 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 | + match result { |
| 74 | + TestResult::TrIgnored => { /* no-op */ } |
| 75 | + TestResult::TrFailed => { |
| 76 | + self.write_message(&*format!( |
| 77 | + "<testcase classname=\"test.global\" \ |
| 78 | + name=\"{}\" time=\"{}\">", |
| 79 | + desc.name.as_slice(), |
| 80 | + duration.as_secs() |
| 81 | + ))?; |
| 82 | + self.write_message("<failure type=\"assert\"/>")?; |
| 83 | + self.write_message("</testcase>")?; |
| 84 | + } |
| 85 | + |
| 86 | + TestResult::TrFailedMsg(ref m) => { |
| 87 | + self.write_message(&*format!( |
| 88 | + "<testcase classname=\"test.global\" \ |
| 89 | + name=\"{}\" time=\"{}\">", |
| 90 | + desc.name.as_slice(), |
| 91 | + duration.as_secs() |
| 92 | + ))?; |
| 93 | + self.write_message(&*format!("<failure message=\"{}\" type=\"assert\"/>", m))?; |
| 94 | + self.write_message("</testcase>")?; |
| 95 | + } |
| 96 | + |
| 97 | + TestResult::TrTimedFail => { |
| 98 | + self.write_message(&*format!( |
| 99 | + "<testcase classname=\"test.global\" \ |
| 100 | + name=\"{}\" time=\"{}\">", |
| 101 | + desc.name.as_slice(), |
| 102 | + duration.as_secs() |
| 103 | + ))?; |
| 104 | + self.write_message("<failure type=\"timeout\"/>")?; |
| 105 | + self.write_message("</testcase>")?; |
| 106 | + } |
| 107 | + |
| 108 | + TestResult::TrBench(ref b) => { |
| 109 | + self.write_message(&*format!( |
| 110 | + "<testcase classname=\"benchmark.global\" \ |
| 111 | + name=\"{}\" time=\"{}\" />", |
| 112 | + desc.name.as_slice(), |
| 113 | + b.ns_iter_summ.sum |
| 114 | + ))?; |
| 115 | + } |
| 116 | + |
| 117 | + TestResult::TrOk | TestResult::TrAllowedFail => { |
| 118 | + self.write_message(&*format!( |
| 119 | + "<testcase classname=\"test.global\" \ |
| 120 | + name=\"{}\" time=\"{}\"/>", |
| 121 | + desc.name.as_slice(), |
| 122 | + duration.as_secs() |
| 123 | + ))?; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + self.write_message("<system-out/>")?; |
| 128 | + self.write_message("<system-err/>")?; |
| 129 | + self.write_message("</testsuite>")?; |
| 130 | + self.write_message("</testsuites>")?; |
| 131 | + |
| 132 | + Ok(state.failed == 0) |
| 133 | + } |
| 134 | +} |
0 commit comments