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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
use crate::serialize::serialize_report;
use chrono::{DateTime, FixedOffset};
use indexmap::map::IndexMap;
use std::{io, iter, time::Duration};
#[derive(Clone, Debug)]
pub struct Report {
pub name: String,
pub timestamp: Option<DateTime<FixedOffset>>,
pub time: Option<Duration>,
pub tests: usize,
pub failures: usize,
pub errors: usize,
pub test_suites: Vec<TestSuite>,
}
impl Report {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
timestamp: None,
time: None,
tests: 0,
failures: 0,
errors: 0,
test_suites: vec![],
}
}
pub fn set_timestamp(&mut self, timestamp: impl Into<DateTime<FixedOffset>>) -> &mut Self {
self.timestamp = Some(timestamp.into());
self
}
pub fn set_time(&mut self, time: Duration) -> &mut Self {
self.time = Some(time);
self
}
pub fn add_test_suite(&mut self, test_suite: TestSuite) -> &mut Self {
self.tests += test_suite.tests;
self.failures += test_suite.failures;
self.errors += test_suite.errors;
self.test_suites.push(test_suite);
self
}
pub fn add_test_suites(
&mut self,
test_suites: impl IntoIterator<Item = TestSuite>,
) -> &mut Self {
for test_suite in test_suites {
self.add_test_suite(test_suite);
}
self
}
pub fn serialize(&self, writer: impl io::Write) -> quick_xml::Result<()> {
serialize_report(self, writer)
}
pub fn to_string(&self) -> quick_xml::Result<String> {
let mut buf: Vec<u8> = vec![];
self.serialize(&mut buf)?;
String::from_utf8(buf).map_err(|utf8_err| quick_xml::Error::Utf8(utf8_err.utf8_error()))
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct TestSuite {
pub name: String,
pub tests: usize,
pub disabled: usize,
pub errors: usize,
pub failures: usize,
pub timestamp: Option<DateTime<FixedOffset>>,
pub time: Option<Duration>,
pub test_cases: Vec<TestCase>,
pub properties: Vec<Property>,
pub system_out: Option<Output>,
pub system_err: Option<Output>,
pub extra: IndexMap<String, String>,
}
impl TestSuite {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
time: None,
timestamp: None,
tests: 0,
disabled: 0,
errors: 0,
failures: 0,
test_cases: vec![],
properties: vec![],
system_out: None,
system_err: None,
extra: IndexMap::new(),
}
}
pub fn set_timestamp(&mut self, timestamp: impl Into<DateTime<FixedOffset>>) -> &mut Self {
self.timestamp = Some(timestamp.into());
self
}
pub fn set_time(&mut self, time: Duration) -> &mut Self {
self.time = Some(time);
self
}
pub fn add_property(&mut self, property: impl Into<Property>) -> &mut Self {
self.properties.push(property.into());
self
}
pub fn add_properties(
&mut self,
properties: impl IntoIterator<Item = impl Into<Property>>,
) -> &mut Self {
for property in properties {
self.add_property(property);
}
self
}
pub fn add_test_case(&mut self, test_case: TestCase) -> &mut Self {
self.tests += 1;
match &test_case.status {
TestCaseStatus::Success { .. } => {}
TestCaseStatus::NonSuccess { kind, .. } => match kind {
NonSuccessKind::Failure => self.failures += 1,
NonSuccessKind::Error => self.errors += 1,
},
TestCaseStatus::Skipped { .. } => self.disabled += 1,
}
self.test_cases.push(test_case);
self
}
pub fn add_test_cases(&mut self, test_cases: impl IntoIterator<Item = TestCase>) -> &mut Self {
for test_case in test_cases {
self.add_test_case(test_case);
}
self
}
pub fn set_system_out(&mut self, system_out: impl AsRef<str>) -> &mut Self {
self.system_out = Some(Output::new(system_out.as_ref()));
self
}
pub fn set_system_out_lossy(&mut self, system_out: impl AsRef<[u8]>) -> &mut Self {
self.set_system_out(String::from_utf8_lossy(system_out.as_ref()))
}
pub fn set_system_err(&mut self, system_err: impl AsRef<str>) -> &mut Self {
self.system_err = Some(Output::new(system_err.as_ref()));
self
}
pub fn set_system_err_lossy(&mut self, system_err: impl AsRef<[u8]>) -> &mut Self {
self.set_system_err(String::from_utf8_lossy(system_err.as_ref()))
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct TestCase {
pub name: String,
pub classname: Option<String>,
pub assertions: Option<usize>,
pub timestamp: Option<DateTime<FixedOffset>>,
pub time: Option<Duration>,
pub status: TestCaseStatus,
pub system_out: Option<Output>,
pub system_err: Option<Output>,
pub extra: IndexMap<String, String>,
}
impl TestCase {
pub fn new(name: impl Into<String>, status: TestCaseStatus) -> Self {
Self {
name: name.into(),
classname: None,
assertions: None,
timestamp: None,
time: None,
status,
system_out: None,
system_err: None,
extra: IndexMap::new(),
}
}
pub fn set_classname(&mut self, classname: impl Into<String>) -> &mut Self {
self.classname = Some(classname.into());
self
}
pub fn set_assertions(&mut self, assertions: usize) -> &mut Self {
self.assertions = Some(assertions);
self
}
pub fn set_timestamp(&mut self, timestamp: impl Into<DateTime<FixedOffset>>) -> &mut Self {
self.timestamp = Some(timestamp.into());
self
}
pub fn set_time(&mut self, time: Duration) -> &mut Self {
self.time = Some(time);
self
}
pub fn set_system_out(&mut self, system_out: impl AsRef<str>) -> &mut Self {
self.system_out = Some(Output::new(system_out.as_ref()));
self
}
pub fn set_system_out_lossy(&mut self, system_out: impl AsRef<[u8]>) -> &mut Self {
self.set_system_out(String::from_utf8_lossy(system_out.as_ref()))
}
pub fn set_system_err(&mut self, system_err: impl AsRef<str>) -> &mut Self {
self.system_err = Some(Output::new(system_err.as_ref()));
self
}
pub fn set_system_err_lossy(&mut self, system_err: impl AsRef<[u8]>) -> &mut Self {
self.set_system_err(String::from_utf8_lossy(system_err.as_ref()))
}
}
#[derive(Clone, Debug)]
pub enum TestCaseStatus {
Success {
flaky_runs: Vec<TestRerun>,
},
NonSuccess {
kind: NonSuccessKind,
message: Option<String>,
ty: Option<String>,
description: Option<String>,
reruns: Vec<TestRerun>,
},
Skipped {
message: Option<String>,
ty: Option<String>,
description: Option<String>,
},
}
impl TestCaseStatus {
pub fn success() -> Self {
TestCaseStatus::Success { flaky_runs: vec![] }
}
pub fn non_success(kind: NonSuccessKind) -> Self {
TestCaseStatus::NonSuccess {
kind,
message: None,
ty: None,
description: None,
reruns: vec![],
}
}
pub fn skipped() -> Self {
TestCaseStatus::Skipped {
message: None,
ty: None,
description: None,
}
}
pub fn set_message(&mut self, message: impl Into<String>) -> &mut Self {
let message_mut = match self {
TestCaseStatus::Success { .. } => return self,
TestCaseStatus::NonSuccess { message, .. } => message,
TestCaseStatus::Skipped { message, .. } => message,
};
*message_mut = Some(message.into());
self
}
pub fn set_type(&mut self, ty: impl Into<String>) -> &mut Self {
let ty_mut = match self {
TestCaseStatus::Success { .. } => return self,
TestCaseStatus::NonSuccess { ty, .. } => ty,
TestCaseStatus::Skipped { ty, .. } => ty,
};
*ty_mut = Some(ty.into());
self
}
pub fn set_description(&mut self, description: impl Into<String>) -> &mut Self {
let description_mut = match self {
TestCaseStatus::Success { .. } => return self,
TestCaseStatus::NonSuccess { description, .. } => description,
TestCaseStatus::Skipped { description, .. } => description,
};
*description_mut = Some(description.into());
self
}
pub fn add_rerun(&mut self, rerun: TestRerun) -> &mut Self {
self.add_reruns(iter::once(rerun))
}
pub fn add_reruns(&mut self, reruns: impl IntoIterator<Item = TestRerun>) -> &mut Self {
let reruns_mut = match self {
TestCaseStatus::Success { flaky_runs } => flaky_runs,
TestCaseStatus::NonSuccess { reruns, .. } => reruns,
TestCaseStatus::Skipped { .. } => return self,
};
reruns_mut.extend(reruns);
self
}
}
#[derive(Clone, Debug)]
pub struct TestRerun {
pub kind: NonSuccessKind,
pub timestamp: Option<DateTime<FixedOffset>>,
pub time: Option<Duration>,
pub message: Option<String>,
pub ty: Option<String>,
pub stack_trace: Option<String>,
pub system_out: Option<Output>,
pub system_err: Option<Output>,
pub description: Option<String>,
}
impl TestRerun {
pub fn new(kind: NonSuccessKind) -> Self {
TestRerun {
kind,
timestamp: None,
time: None,
message: None,
ty: None,
stack_trace: None,
system_out: None,
system_err: None,
description: None,
}
}
pub fn set_timestamp(&mut self, timestamp: impl Into<DateTime<FixedOffset>>) -> &mut Self {
self.timestamp = Some(timestamp.into());
self
}
pub fn set_time(&mut self, time: Duration) -> &mut Self {
self.time = Some(time);
self
}
pub fn set_message(&mut self, message: impl Into<String>) -> &mut Self {
self.message = Some(message.into());
self
}
pub fn set_type(&mut self, ty: impl Into<String>) -> &mut Self {
self.ty = Some(ty.into());
self
}
pub fn set_stack_trace(&mut self, stack_trace: impl Into<String>) -> &mut Self {
self.stack_trace = Some(stack_trace.into());
self
}
pub fn set_system_out(&mut self, system_out: impl AsRef<str>) -> &mut Self {
self.system_out = Some(Output::new(system_out.as_ref()));
self
}
pub fn set_system_out_lossy(&mut self, system_out: impl AsRef<[u8]>) -> &mut Self {
self.set_system_out(String::from_utf8_lossy(system_out.as_ref()))
}
pub fn set_system_err(&mut self, system_err: impl AsRef<str>) -> &mut Self {
self.system_err = Some(Output::new(system_err.as_ref()));
self
}
pub fn set_system_err_lossy(&mut self, system_err: impl AsRef<[u8]>) -> &mut Self {
self.set_system_err(String::from_utf8_lossy(system_err.as_ref()))
}
pub fn set_description(&mut self, description: impl Into<String>) -> &mut Self {
self.description = Some(description.into());
self
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum NonSuccessKind {
Failure,
Error,
}
#[derive(Clone, Debug)]
pub struct Property {
pub name: String,
pub value: String,
}
impl Property {
pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
Self {
name: name.into(),
value: value.into(),
}
}
}
impl<T> From<(T, T)> for Property
where
T: Into<String>,
{
fn from((k, v): (T, T)) -> Self {
Property::new(k, v)
}
}
#[derive(Clone, Debug)]
pub struct Output {
output: Box<str>,
}
impl Output {
pub fn new(output: impl AsRef<str>) -> Self {
let output = output.as_ref();
let output = output
.replace(
|c| matches!(c, '\x00'..='\x08' | '\x0b' | '\x0c' | '\x0e'..='\x1f'),
"",
)
.into_boxed_str();
Self { output }
}
pub fn as_str(&self) -> &str {
&self.output
}
pub fn into_string(self) -> String {
self.output.into_string()
}
}
impl AsRef<str> for Output {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl From<Output> for String {
fn from(output: Output) -> Self {
output.into_string()
}
}