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
|
use std::io::{self, IsTerminal, StderrLock, Write};
use std::time::{Duration, Instant};
use crate::collect::Test;
use crate::run::TestResult;
/// Receives status updates by individual test runs.
pub struct Logger<'a> {
filtered: usize,
passed: usize,
failed: usize,
skipped: usize,
mismatched_image: bool,
active: Vec<&'a Test>,
last_change: Instant,
temp_lines: usize,
terminal: bool,
}
impl<'a> Logger<'a> {
/// Create a new logger.
pub fn new(filtered: usize, skipped: usize) -> Self {
Self {
filtered,
passed: 0,
failed: 0,
skipped,
mismatched_image: false,
active: vec![],
temp_lines: 0,
last_change: Instant::now(),
terminal: std::io::stderr().is_terminal(),
}
}
/// Register the start of a test.
pub fn start(&mut self, test: &'a Test) {
self.active.push(test);
self.last_change = Instant::now();
self.refresh();
}
/// Register a finished test.
pub fn end(&mut self, test: &'a Test, result: std::thread::Result<TestResult>) {
self.active.retain(|t| t.name != test.name);
let result = match result {
Ok(result) => result,
Err(_) => {
self.failed += 1;
self.temp_lines = 0;
self.print(move |out| {
writeln!(out, "❌ {test} panicked")?;
Ok(())
})
.unwrap();
return;
}
};
if result.is_ok() {
self.passed += 1;
} else {
self.failed += 1;
}
self.mismatched_image |= result.mismatched_image;
self.last_change = Instant::now();
self.print(move |out| {
if !result.errors.is_empty() {
writeln!(out, "❌ {test}")?;
for line in result.errors.lines() {
writeln!(out, " {line}")?;
}
} else if crate::ARGS.verbose || !result.infos.is_empty() {
writeln!(out, "✅ {test}")?;
}
for line in result.infos.lines() {
writeln!(out, " {line}")?;
}
Ok(())
})
.unwrap();
}
/// Prints a summary and returns whether the test suite passed.
pub fn finish(&self) -> bool {
let Self { filtered, passed, failed, skipped, .. } = *self;
eprintln!("{passed} passed, {failed} failed, {skipped} skipped");
assert_eq!(filtered, passed + failed, "not all tests were executed succesfully");
if self.mismatched_image {
eprintln!(" pass the --update flag to update the reference images");
}
self.failed == 0
}
/// Refresh the status.
pub fn refresh(&mut self) {
self.print(|_| Ok(())).unwrap();
}
/// Refresh the status print.
fn print(
&mut self,
inner: impl FnOnce(&mut StderrLock<'_>) -> io::Result<()>,
) -> io::Result<()> {
let mut out = std::io::stderr().lock();
// Clear the status lines.
for _ in 0..self.temp_lines {
write!(out, "\x1B[1F\x1B[0J")?;
self.temp_lines = 0;
}
// Print the result of a finished test.
inner(&mut out)?;
// Print the status line.
let done = self.failed + self.passed;
if done < self.filtered {
if self.last_change.elapsed() > Duration::from_secs(2) {
for test in &self.active {
writeln!(out, "⏰ {test} is taking a long time ...")?;
if self.terminal {
self.temp_lines += 1;
}
}
}
if self.terminal {
writeln!(out, "💨 {done} / {}", self.filtered)?;
self.temp_lines += 1;
}
}
Ok(())
}
}
|