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
193
194
195
196
197
198
199
200
201
202
203
204
205
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use super::*;
pub struct JunitTestReporter {
path: String,
// Stores TestCases (i.e. Tests) by the Test ID
cases: IndexMap<usize, quick_junit::TestCase>,
}
impl JunitTestReporter {
pub fn new(path: String) -> Self {
Self {
path,
cases: IndexMap::new(),
}
}
fn convert_status(status: &TestResult) -> quick_junit::TestCaseStatus {
match status {
TestResult::Ok => quick_junit::TestCaseStatus::success(),
TestResult::Ignored => quick_junit::TestCaseStatus::skipped(),
TestResult::Failed(failure) => quick_junit::TestCaseStatus::NonSuccess {
kind: quick_junit::NonSuccessKind::Failure,
message: Some(failure.to_string()),
ty: None,
description: None,
reruns: vec![],
},
TestResult::Cancelled => quick_junit::TestCaseStatus::NonSuccess {
kind: quick_junit::NonSuccessKind::Error,
message: Some("Cancelled".to_string()),
ty: None,
description: None,
reruns: vec![],
},
}
}
}
impl TestReporter for JunitTestReporter {
fn report_register(&mut self, description: &TestDescription) {
let mut case = quick_junit::TestCase::new(
description.name.clone(),
quick_junit::TestCaseStatus::skipped(),
);
let file_name = description.location.file_name.clone();
let file_name = file_name.strip_prefix("file://").unwrap_or(&file_name);
case
.extra
.insert(String::from("filename"), String::from(file_name));
case.extra.insert(
String::from("line"),
description.location.line_number.to_string(),
);
case.extra.insert(
String::from("col"),
description.location.column_number.to_string(),
);
self.cases.insert(description.id, case);
}
fn report_plan(&mut self, _plan: &TestPlan) {}
fn report_wait(&mut self, _description: &TestDescription) {}
fn report_output(&mut self, _output: &[u8]) {
/*
TODO(skycoop): Right now I can't include stdout/stderr in the report because
we have a global pair of output streams that don't differentiate between the
output of different tests. This is a nice to have feature, so we can come
back to it later
*/
}
fn report_result(
&mut self,
description: &TestDescription,
result: &TestResult,
elapsed: u64,
) {
if let Some(case) = self.cases.get_mut(&description.id) {
case.status = Self::convert_status(result);
case.set_time(Duration::from_millis(elapsed));
}
}
fn report_uncaught_error(&mut self, _origin: &str, _error: Box<JsError>) {}
fn report_step_register(&mut self, _description: &TestStepDescription) {}
fn report_step_wait(&mut self, _description: &TestStepDescription) {}
fn report_step_result(
&mut self,
description: &TestStepDescription,
result: &TestStepResult,
_elapsed: u64,
_tests: &IndexMap<usize, TestDescription>,
test_steps: &IndexMap<usize, TestStepDescription>,
) {
let status = match result {
TestStepResult::Ok => "passed",
TestStepResult::Ignored => "skipped",
TestStepResult::Failed(_) => "failure",
};
let root_id: usize;
let mut name = String::new();
{
let mut ancestors = vec![];
let mut current_desc = description;
loop {
if let Some(d) = test_steps.get(¤t_desc.parent_id) {
ancestors.push(&d.name);
current_desc = d;
} else {
root_id = current_desc.parent_id;
break;
}
}
ancestors.reverse();
for n in ancestors {
name.push_str(n);
name.push_str(" ... ");
}
name.push_str(&description.name);
}
if let Some(case) = self.cases.get_mut(&root_id) {
case.add_property(quick_junit::Property::new(
format!("step[{}]", status),
name,
));
}
}
fn report_summary(
&mut self,
_elapsed: &Duration,
_tests: &IndexMap<usize, TestDescription>,
_test_steps: &IndexMap<usize, TestStepDescription>,
) {
}
fn report_sigint(
&mut self,
tests_pending: &HashSet<usize>,
tests: &IndexMap<usize, TestDescription>,
_test_steps: &IndexMap<usize, TestStepDescription>,
) {
for id in tests_pending {
if let Some(description) = tests.get(id) {
self.report_result(description, &TestResult::Cancelled, 0)
}
}
}
fn flush_report(
&mut self,
elapsed: &Duration,
tests: &IndexMap<usize, TestDescription>,
_test_steps: &IndexMap<usize, TestStepDescription>,
) -> anyhow::Result<()> {
let mut suites: IndexMap<String, quick_junit::TestSuite> = IndexMap::new();
for (id, case) in &self.cases {
if let Some(test) = tests.get(id) {
suites
.entry(test.location.file_name.clone())
.and_modify(|s| {
s.add_test_case(case.clone());
})
.or_insert_with(|| {
quick_junit::TestSuite::new(test.location.file_name.clone())
.add_test_case(case.clone())
.to_owned()
});
}
}
let mut report = quick_junit::Report::new("deno test");
report.set_time(*elapsed).add_test_suites(
suites
.values()
.cloned()
.collect::<Vec<quick_junit::TestSuite>>(),
);
if self.path == "-" {
report
.serialize(std::io::stdout())
.with_context(|| "Failed to write JUnit report to stdout")?;
} else {
let file =
std::fs::File::create(self.path.clone()).with_context(|| {
format!("Failed to open JUnit report file {}", self.path)
})?;
report.serialize(file).with_context(|| {
format!("Failed to write JUnit report to {}", self.path)
})?;
}
Ok(())
}
}
|