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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use crate::{
config::{NextestJunitConfig, NextestProfile},
errors::{JunitError, WriteEventError},
reporter::TestEvent,
runner::{ExecuteStatus, ExecutionDescription, ExecutionResult},
test_list::TestInstance,
};
use camino::Utf8Path;
use chrono::{DateTime, FixedOffset, Utc};
use debug_ignore::DebugIgnore;
use quick_junit::{NonSuccessKind, Report, TestCase, TestCaseStatus, TestRerun, TestSuite};
use std::{collections::HashMap, fs::File, time::SystemTime};
#[derive(Clone, Debug)]
#[allow(dead_code)]
pub(crate) struct EventAggregator<'cfg> {
store_dir: &'cfg Utf8Path,
junit: Option<MetadataJunit<'cfg>>,
}
impl<'cfg> EventAggregator<'cfg> {
pub(crate) fn new(profile: &'cfg NextestProfile<'cfg>) -> Self {
Self {
store_dir: profile.store_dir(),
junit: profile.junit().map(MetadataJunit::new),
}
}
pub(crate) fn write_event(&mut self, event: TestEvent<'cfg>) -> Result<(), WriteEventError> {
if let Some(junit) = &mut self.junit {
junit.write_event(event)?;
}
Ok(())
}
}
#[derive(Clone, Debug)]
struct MetadataJunit<'cfg> {
config: NextestJunitConfig<'cfg>,
test_suites: DebugIgnore<HashMap<&'cfg str, TestSuite>>,
}
impl<'cfg> MetadataJunit<'cfg> {
fn new(config: NextestJunitConfig<'cfg>) -> Self {
Self {
config,
test_suites: DebugIgnore(HashMap::new()),
}
}
pub(crate) fn write_event(&mut self, event: TestEvent<'cfg>) -> Result<(), WriteEventError> {
match event {
TestEvent::RunStarted { .. } => {}
TestEvent::TestStarted { .. } => {}
TestEvent::TestSlow { .. } => {}
TestEvent::TestRetry { .. } => {
}
TestEvent::TestFinished {
test_instance,
run_statuses,
} => {
fn kind_ty(run_status: &ExecuteStatus) -> (NonSuccessKind, &'static str) {
match run_status.result {
ExecutionResult::Fail => (NonSuccessKind::Failure, "test failure"),
ExecutionResult::ExecFail => (NonSuccessKind::Error, "execution failure"),
ExecutionResult::Pass => unreachable!("this is a failure status"),
}
}
let testsuite = self.testsuite_for(test_instance);
let (mut testcase_status, main_status, reruns) = match run_statuses.describe() {
ExecutionDescription::Success { single_status } => {
(TestCaseStatus::success(), single_status, &[][..])
}
ExecutionDescription::Flaky {
last_status,
prior_statuses,
} => (TestCaseStatus::success(), last_status, prior_statuses),
ExecutionDescription::Failure {
first_status,
retries,
..
} => {
let (kind, ty) = kind_ty(first_status);
let mut testcase_status = TestCaseStatus::non_success(kind);
testcase_status.set_type(ty);
(testcase_status, first_status, retries)
}
};
for rerun in reruns {
let (kind, ty) = kind_ty(rerun);
let mut test_rerun = TestRerun::new(kind);
test_rerun
.set_timestamp(to_datetime(rerun.start_time))
.set_time(rerun.time_taken)
.set_type(ty)
.set_system_out_lossy(rerun.stdout())
.set_system_err_lossy(rerun.stderr());
testcase_status.add_rerun(test_rerun);
}
let mut testcase = TestCase::new(test_instance.name, testcase_status);
testcase
.set_classname(&test_instance.bin_info.binary_id)
.set_timestamp(to_datetime(main_status.start_time))
.set_time(main_status.time_taken);
if !main_status.result.is_success() {
testcase
.set_system_out_lossy(main_status.stdout())
.set_system_err_lossy(main_status.stderr());
}
testsuite.add_test_case(testcase);
}
TestEvent::TestSkipped { .. } => {
}
TestEvent::RunBeginCancel { .. } => {}
TestEvent::RunFinished {
start_time,
elapsed,
..
} => {
let mut report = Report::new(self.config.report_name());
report
.set_timestamp(to_datetime(start_time))
.set_time(elapsed)
.add_test_suites(self.test_suites.drain().map(|(_, testsuite)| testsuite));
let junit_path = self.config.path();
let junit_dir = junit_path.parent().expect("junit path must have a parent");
std::fs::create_dir_all(junit_dir).map_err(|error| WriteEventError::Fs {
file: junit_dir.to_path_buf(),
error,
})?;
let f = File::create(junit_path).map_err(|error| WriteEventError::Fs {
file: junit_path.to_path_buf(),
error,
})?;
report.serialize(f).map_err(|err| WriteEventError::Junit {
file: junit_path.to_path_buf(),
error: JunitError::new(err),
})?;
}
}
Ok(())
}
fn testsuite_for(&mut self, test_instance: TestInstance<'cfg>) -> &mut TestSuite {
self.test_suites
.entry(&test_instance.bin_info.binary_id)
.or_insert_with(|| TestSuite::new(&test_instance.bin_info.binary_id))
}
}
fn to_datetime(system_time: SystemTime) -> DateTime<FixedOffset> {
let datetime = DateTime::<Utc>::from(system_time);
datetime.into()
}