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
pub fn compress(output: &str) -> Option<String> {
if let Some(r) = try_pytest(output) {
return Some(r);
}
if let Some(r) = try_vitest(output) {
return Some(r);
}
if let Some(r) = try_jest(output) {
return Some(r);
}
if let Some(r) = try_go_test(output) {
return Some(r);
}
if let Some(r) = try_rspec(output) {
return Some(r);
}
None
}
fn try_pytest(output: &str) -> Option<String> {
if !output.contains("test session starts") && !output.contains("pytest") {
return None;
}
let mut passed = 0u32;
let mut failed = 0u32;
let mut skipped = 0u32;
let mut time = String::new();
let mut failures = Vec::new();
for line in output.lines() {
let trimmed = line.trim();
if (trimmed.contains("passed") || trimmed.contains("failed") || trimmed.contains("error"))
&& (trimmed.starts_with('=') || trimmed.starts_with('-'))
{
for word in trimmed.split_whitespace() {
if let Some(n) = word.strip_suffix("passed").or_else(|| {
if trimmed.contains(" passed") {
word.parse::<u32>().ok().map(|_| word)
} else {
None
}
}) {
if let Ok(v) = n.trim().parse::<u32>() {
passed = v;
}
}
}
if let Some(pos) = trimmed.find(" passed") {
let before = &trimmed[..pos];
if let Some(num_str) = before.split_whitespace().last() {
if let Ok(v) = num_str.parse::<u32>() {
passed = v;
}
}
}
if let Some(pos) = trimmed.find(" failed") {
let before = &trimmed[..pos];
if let Some(num_str) = before.split_whitespace().last() {
if let Ok(v) = num_str.parse::<u32>() {
failed = v;
}
}
}
if let Some(pos) = trimmed.find(" skipped") {
let before = &trimmed[..pos];
if let Some(num_str) = before.split_whitespace().last() {
if let Ok(v) = num_str.parse::<u32>() {
skipped = v;
}
}
}
if let Some(pos) = trimmed.find(" in ") {
time = trimmed[pos + 4..].trim_end_matches('=').trim().to_string();
}
}
if trimmed.starts_with("FAILED ") {
failures.push(
trimmed
.strip_prefix("FAILED ")
.unwrap_or(trimmed)
.to_string(),
);
}
}
if passed == 0 && failed == 0 {
return None;
}
let mut result = format!("pytest: {passed} passed");
if failed > 0 {
result.push_str(&format!(", {failed} failed"));
}
if skipped > 0 {
result.push_str(&format!(", {skipped} skipped"));
}
if !time.is_empty() {
result.push_str(&format!(" ({time})"));
}
for f in failures.iter().take(5) {
result.push_str(&format!("\n FAIL: {f}"));
}
Some(result)
}
fn try_jest(output: &str) -> Option<String> {
if !output.contains("Tests:") && !output.contains("Test Suites:") {
return None;
}
let mut suites_line = String::new();
let mut tests_line = String::new();
let mut time_line = String::new();
for line in output.lines() {
let trimmed = line.trim();
if trimmed.starts_with("Test Suites:") {
suites_line = trimmed.to_string();
} else if trimmed.starts_with("Tests:") {
tests_line = trimmed.to_string();
} else if trimmed.starts_with("Time:") {
time_line = trimmed.to_string();
}
}
if tests_line.is_empty() {
return None;
}
let mut result = String::new();
if !suites_line.is_empty() {
result.push_str(&suites_line);
result.push('\n');
}
result.push_str(&tests_line);
if !time_line.is_empty() {
result.push('\n');
result.push_str(&time_line);
}
Some(result)
}
fn try_go_test(output: &str) -> Option<String> {
if !output.contains("--- PASS") && !output.contains("--- FAIL") && !output.contains("PASS\n") {
return None;
}
let mut passed = 0u32;
let mut failed = 0u32;
let mut failures = Vec::new();
let mut packages = Vec::new();
for line in output.lines() {
let trimmed = line.trim();
if trimmed.starts_with("--- PASS:") {
passed += 1;
} else if trimmed.starts_with("--- FAIL:") {
failed += 1;
failures.push(
trimmed
.strip_prefix("--- FAIL: ")
.unwrap_or(trimmed)
.to_string(),
);
} else if trimmed.starts_with("ok ") || trimmed.starts_with("FAIL\t") {
packages.push(trimmed.to_string());
}
}
if passed == 0 && failed == 0 {
return None;
}
let mut result = format!("go test: {passed} passed");
if failed > 0 {
result.push_str(&format!(", {failed} failed"));
}
for pkg in &packages {
result.push_str(&format!("\n {pkg}"));
}
for f in failures.iter().take(5) {
result.push_str(&format!("\n FAIL: {f}"));
}
Some(result)
}
fn try_vitest(output: &str) -> Option<String> {
if !output.contains("PASS") && !output.contains("FAIL") {
return None;
}
if !output.contains(" Tests ") && !output.contains("Test Files") {
return None;
}
let mut test_files_line = String::new();
let mut tests_line = String::new();
let mut duration_line = String::new();
let mut failures = Vec::new();
for line in output.lines() {
let trimmed = line.trim();
let plain = strip_ansi(trimmed);
if plain.contains("Test Files") {
test_files_line = plain.clone();
} else if plain.starts_with("Tests") && plain.contains("passed") {
tests_line = plain.clone();
} else if plain.contains("Duration") || plain.contains("Time") {
if plain.contains("ms") || plain.contains("s") {
duration_line = plain.clone();
}
} else if plain.contains("FAIL")
&& (plain.contains(".test.") || plain.contains(".spec.") || plain.contains("_test."))
{
failures.push(plain.clone());
}
}
if tests_line.is_empty() && test_files_line.is_empty() {
return None;
}
let mut result = String::new();
if !test_files_line.is_empty() {
result.push_str(&test_files_line);
}
if !tests_line.is_empty() {
if !result.is_empty() {
result.push('\n');
}
result.push_str(&tests_line);
}
if !duration_line.is_empty() {
result.push('\n');
result.push_str(&duration_line);
}
for f in failures.iter().take(10) {
result.push_str(&format!("\n FAIL: {f}"));
}
Some(result)
}
fn strip_ansi(s: &str) -> String {
crate::core::compressor::strip_ansi(s)
}
fn try_rspec(output: &str) -> Option<String> {
if !output.contains("examples") || !output.contains("failures") {
return None;
}
for line in output.lines().rev() {
let trimmed = line.trim();
if trimmed.contains("example") && trimmed.contains("failure") {
return Some(format!("rspec: {trimmed}"));
}
}
None
}