1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use std::io::Write;
use crate::collections::{TestResult, TestSuite};
use derive_getters::Getters;
use xml::writer::{EmitterConfig, XmlEvent};
pub use chrono::{DateTime, Duration, TimeZone, Utc};
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ReportError {
#[error("unable to write report")]
Write(#[from] xml::writer::Error),
}
fn decimal_seconds(d: &Duration) -> f64 {
if let Some(n) = d.num_nanoseconds() {
n as f64 / 1_000_000_000.0
} else if let Some(n) = d.num_microseconds() {
n as f64 / 1_000_000.0
} else {
d.num_milliseconds() as f64 / 1_000.0
}
}
#[derive(Default, Debug, Clone, Getters)]
pub struct Report {
testsuites: Vec<TestSuite>,
}
impl Report {
pub fn new() -> Report {
Report {
testsuites: Vec::new(),
}
}
pub fn add_testsuite(mut self, testsuite: TestSuite) -> Self {
self.testsuites.push(testsuite);
self
}
pub fn add_testsuites(mut self, testsuites: impl IntoIterator<Item = TestSuite>) -> Self {
self.testsuites.extend(testsuites);
self
}
pub fn write_xml<W: Write>(&self, sink: W) -> Result<(), ReportError> {
let mut ew = EmitterConfig::new()
.perform_indent(true)
.create_writer(sink);
ew.write(XmlEvent::start_element("testsuites"))?;
for (id, ts) in self.testsuites.iter().enumerate() {
ew.write(
XmlEvent::start_element("testsuite")
.attr("id", &format!("{}", id))
.attr("name", &ts.name)
.attr("package", &ts.package)
.attr("tests", &format!("{}", &ts.tests()))
.attr("errors", &format!("{}", &ts.errors()))
.attr("failures", &format!("{}", &ts.failures()))
.attr("hostname", &ts.hostname)
.attr("timestamp", &ts.timestamp.to_rfc3339())
.attr("time", &format!("{}", decimal_seconds(&ts.time()))),
)?;
for tc in &ts.testcases {
if let Some(classname) = &tc.classname {
ew.write(
XmlEvent::start_element("testcase")
.attr("name", &tc.name)
.attr("classname", classname)
.attr("time", &format!("{}", decimal_seconds(&tc.time))),
)?;
} else {
ew.write(
XmlEvent::start_element("testcase")
.attr("name", &tc.name)
.attr("time", &format!("{}", decimal_seconds(&tc.time))),
)?;
}
if let Some(system_out) = &tc.system_out {
ew.write(XmlEvent::start_element("system-out"))?;
ew.write(XmlEvent::CData(system_out.as_str()))?;
ew.write(XmlEvent::end_element())?;
}
if let Some(system_err) = &tc.system_err {
ew.write(XmlEvent::start_element("system-err"))?;
ew.write(XmlEvent::CData(system_err.as_str()))?;
ew.write(XmlEvent::end_element())?;
}
match tc.result {
TestResult::Success => {}
TestResult::Error {
ref type_,
ref message,
} => {
ew.write(
XmlEvent::start_element("error")
.attr("type", &type_)
.attr("message", &message),
)?;
ew.write(XmlEvent::end_element())?;
}
TestResult::Failure {
ref type_,
ref message,
} => {
ew.write(
XmlEvent::start_element("failure")
.attr("type", &type_)
.attr("message", &message),
)?;
ew.write(XmlEvent::end_element())?;
}
};
ew.write(XmlEvent::end_element())?;
}
if let Some(system_out) = &ts.system_out {
ew.write(XmlEvent::start_element("system-out"))?;
ew.write(XmlEvent::CData(system_out.as_str()))?;
ew.write(XmlEvent::end_element())?;
}
if let Some(system_err) = &ts.system_err {
ew.write(XmlEvent::start_element("system-err"))?;
ew.write(XmlEvent::CData(system_err.as_str()))?;
ew.write(XmlEvent::end_element())?;
}
ew.write(XmlEvent::end_element())?;
}
ew.write(XmlEvent::end_element())?;
Ok(())
}
}