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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use std::collections::VecDeque;
use std::path::PathBuf;
use super::fmt::to_relative_path_or_remote_url;
use super::*;
pub struct JunitTestReporter {
cwd: Url,
output_path: String,
// Stores TestCases (i.e. Tests) by the Test ID
cases: IndexMap<usize, quick_junit::TestCase>,
// Stores nodes representing test cases in such a way that can be traversed
// from child to parent to build the full test name that reflects the test
// hierarchy.
test_name_tree: TestNameTree,
failure_format_options: TestFailureFormatOptions,
}
impl JunitTestReporter {
pub fn new(
cwd: Url,
output_path: String,
failure_format_options: TestFailureFormatOptions,
) -> Self {
Self {
cwd,
output_path,
cases: IndexMap::new(),
test_name_tree: TestNameTree::new(),
failure_format_options,
}
}
fn convert_status(
status: &TestResult,
failure_format_options: &TestFailureFormatOptions,
) -> 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.overview()),
ty: None,
description: Some(failure.format(failure_format_options).into_owned()),
reruns: vec![],
},
TestResult::Cancelled => quick_junit::TestCaseStatus::NonSuccess {
kind: quick_junit::NonSuccessKind::Error,
message: Some("Cancelled".to_string()),
ty: None,
description: None,
reruns: vec![],
},
}
}
fn convert_step_status(
status: &TestStepResult,
failure_format_options: &TestFailureFormatOptions,
) -> quick_junit::TestCaseStatus {
match status {
TestStepResult::Ok => quick_junit::TestCaseStatus::success(),
TestStepResult::Ignored => quick_junit::TestCaseStatus::skipped(),
TestStepResult::Failed(failure) => {
quick_junit::TestCaseStatus::NonSuccess {
kind: quick_junit::NonSuccessKind::Failure,
message: Some(failure.overview()),
ty: None,
description: Some(
failure.format(failure_format_options).into_owned(),
),
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(),
);
case.classname = Some(to_relative_path_or_remote_url(
&self.cwd,
&description.location.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);
self.test_name_tree.add_node(description.clone().into());
}
fn report_plan(&mut self, _plan: &TestPlan) {}
fn report_slow(&mut self, _description: &TestDescription, _elapsed: u64) {}
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, &self.failure_format_options);
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) {
self.test_name_tree.add_node(description.clone().into());
let test_case_name =
self.test_name_tree.construct_full_test_name(description.id);
let mut case = quick_junit::TestCase::new(
test_case_name,
quick_junit::TestCaseStatus::skipped(),
);
case.classname = Some(to_relative_path_or_remote_url(
&self.cwd,
&description.location.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_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>,
) {
if let Some(case) = self.cases.get_mut(&description.id) {
case.status =
Self::convert_step_status(result, &self.failure_format_options);
case.set_time(Duration::from_millis(elapsed));
}
}
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 report_completed(&mut self) {
// TODO(mmastrac): This reporter does not handle stdout/stderr yet, and when we do, we may need to redirect
// pre-and-post-test output somewhere.
}
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 {
let abs_filename = match (tests.get(id), test_steps.get(id)) {
(Some(test), _) => &test.location.file_name,
(_, Some(step)) => &step.location.file_name,
(None, None) => {
unreachable!("Unknown test ID '{id}' provided");
}
};
let filename = to_relative_path_or_remote_url(&self.cwd, abs_filename);
suites
.entry(filename.clone())
.and_modify(|s| {
s.add_test_case(case.clone());
})
.or_insert_with(|| {
let mut suite = quick_junit::TestSuite::new(filename);
suite.add_test_case(case.clone());
suite
});
}
let mut report = quick_junit::Report::new("deno test");
report
.set_time(*elapsed)
.add_test_suites(suites.into_values());
if self.output_path == "-" {
report
.serialize(std::io::stdout())
.with_context(|| "Failed to write JUnit report to stdout")?;
} else {
let file =
crate::util::fs::create_file(&PathBuf::from(&self.output_path))
.context("Failed to open JUnit report file.")?;
report.serialize(file).with_context(|| {
format!("Failed to write JUnit report to {}", self.output_path)
})?;
}
Ok(())
}
}
#[derive(Debug, Default)]
struct TestNameTree(IndexMap<usize, TestNameTreeNode>);
impl TestNameTree {
fn new() -> Self {
// Pre-allocate some space to avoid excessive reallocations.
Self(IndexMap::with_capacity(256))
}
fn add_node(&mut self, node: TestNameTreeNode) {
self.0.insert(node.id, node);
}
/// Constructs the full test name by traversing the tree from the specified
/// node as a child to its parent nodes.
/// If the provided ID is not found in the tree, or the tree is broken (e.g.
/// a child node refers to a parent node that doesn't exist), this method
/// just panics.
fn construct_full_test_name(&self, id: usize) -> String {
let mut current_id = Some(id);
let mut name_pieces = VecDeque::new();
loop {
let Some(id) = current_id else {
break;
};
let Some(node) = self.0.get(&id) else {
// The ID specified as a parent node by the child node should exist in
// the tree, but it doesn't. In this case we give up constructing the
// full test name.
unreachable!("Unregistered test ID '{id}' provided");
};
name_pieces.push_front(node.test_name.as_str());
current_id = node.parent_id;
}
if name_pieces.is_empty() {
unreachable!("Unregistered test ID '{id}' provided");
}
let v: Vec<_> = name_pieces.into();
v.join(" > ")
}
}
#[derive(Debug)]
struct TestNameTreeNode {
id: usize,
parent_id: Option<usize>,
test_name: String,
}
impl From<TestDescription> for TestNameTreeNode {
fn from(description: TestDescription) -> Self {
Self {
id: description.id,
parent_id: None,
test_name: description.name,
}
}
}
impl From<TestStepDescription> for TestNameTreeNode {
fn from(description: TestStepDescription) -> Self {
Self {
id: description.id,
parent_id: Some(description.parent_id),
test_name: description.name,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn construct_full_test_name_one_node() {
let mut tree = TestNameTree::new();
tree.add_node(TestNameTreeNode {
id: 0,
parent_id: None,
test_name: "root".to_string(),
});
assert_eq!(tree.construct_full_test_name(0), "root".to_string());
}
#[test]
fn construct_full_test_name_two_level_hierarchy() {
let mut tree = TestNameTree::new();
tree.add_node(TestNameTreeNode {
id: 0,
parent_id: None,
test_name: "root".to_string(),
});
tree.add_node(TestNameTreeNode {
id: 1,
parent_id: Some(0),
test_name: "child".to_string(),
});
assert_eq!(tree.construct_full_test_name(0), "root".to_string());
assert_eq!(tree.construct_full_test_name(1), "root > child".to_string());
}
#[test]
fn construct_full_test_name_three_level_hierarchy() {
let mut tree = TestNameTree::new();
tree.add_node(TestNameTreeNode {
id: 0,
parent_id: None,
test_name: "root".to_string(),
});
tree.add_node(TestNameTreeNode {
id: 1,
parent_id: Some(0),
test_name: "child".to_string(),
});
tree.add_node(TestNameTreeNode {
id: 2,
parent_id: Some(1),
test_name: "grandchild".to_string(),
});
assert_eq!(tree.construct_full_test_name(0), "root".to_string());
assert_eq!(tree.construct_full_test_name(1), "root > child".to_string());
assert_eq!(
tree.construct_full_test_name(2),
"root > child > grandchild".to_string()
);
}
#[test]
fn construct_full_test_name_one_root_two_chains() {
// 0
// / \
// 1 2
// / \
// 3 4
let mut tree = TestNameTree::new();
tree.add_node(TestNameTreeNode {
id: 0,
parent_id: None,
test_name: "root".to_string(),
});
tree.add_node(TestNameTreeNode {
id: 1,
parent_id: Some(0),
test_name: "child 1".to_string(),
});
tree.add_node(TestNameTreeNode {
id: 2,
parent_id: Some(0),
test_name: "child 2".to_string(),
});
tree.add_node(TestNameTreeNode {
id: 3,
parent_id: Some(1),
test_name: "grandchild 1".to_string(),
});
tree.add_node(TestNameTreeNode {
id: 4,
parent_id: Some(1),
test_name: "grandchild 2".to_string(),
});
assert_eq!(tree.construct_full_test_name(0), "root".to_string());
assert_eq!(
tree.construct_full_test_name(1),
"root > child 1".to_string(),
);
assert_eq!(
tree.construct_full_test_name(2),
"root > child 2".to_string(),
);
assert_eq!(
tree.construct_full_test_name(3),
"root > child 1 > grandchild 1".to_string(),
);
assert_eq!(
tree.construct_full_test_name(4),
"root > child 1 > grandchild 2".to_string(),
);
}
}
|