forked from rtk-ai/rtk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.rs
More file actions
271 lines (242 loc) · 8.28 KB
/
Copy pathrunner.rs
File metadata and controls
271 lines (242 loc) · 8.28 KB
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
use crate::tracking;
use anyhow::{Context, Result};
use regex::Regex;
use std::process::{Command, Stdio};
/// Run a command and filter output to show only errors/warnings
pub fn run_err(command: &str, verbose: u8) -> Result<()> {
let timer = tracking::TimedExecution::start();
if verbose > 0 {
eprintln!("Running: {}", command);
}
let output = if cfg!(target_os = "windows") {
Command::new("cmd")
.args(["/C", command])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
} else {
Command::new("sh")
.args(["-c", command])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
}
.context("Failed to execute command")?;
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
let raw = format!("{}\n{}", stdout, stderr);
let filtered = filter_errors(&raw);
let mut rtk = String::new();
if filtered.is_empty() {
if output.status.success() {
rtk.push_str("[ok] Command completed successfully (no errors)");
} else {
rtk.push_str(&format!(
"[FAIL] Command failed (exit code: {:?})\n",
output.status.code()
));
let lines: Vec<&str> = raw.lines().collect();
for line in lines.iter().rev().take(10).rev() {
rtk.push_str(&format!(" {}\n", line));
}
}
} else {
rtk.push_str(&filtered);
}
let exit_code = output
.status
.code()
.unwrap_or(if output.status.success() { 0 } else { 1 });
if let Some(hint) = crate::tee::tee_and_hint(&raw, "err", exit_code) {
println!("{}\n{}", rtk, hint);
} else {
println!("{}", rtk);
}
timer.track(command, "rtk run-err", &raw, &rtk);
Ok(())
}
/// Run tests and show only failures
pub fn run_test(command: &str, verbose: u8) -> Result<()> {
let timer = tracking::TimedExecution::start();
if verbose > 0 {
eprintln!("Running tests: {}", command);
}
let output = if cfg!(target_os = "windows") {
Command::new("cmd")
.args(["/C", command])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
} else {
Command::new("sh")
.args(["-c", command])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
}
.context("Failed to execute test command")?;
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
let raw = format!("{}\n{}", stdout, stderr);
let exit_code = output
.status
.code()
.unwrap_or(if output.status.success() { 0 } else { 1 });
let summary = extract_test_summary(&raw, command);
if let Some(hint) = crate::tee::tee_and_hint(&raw, "test", exit_code) {
println!("{}\n{}", summary, hint);
} else {
println!("{}", summary);
}
timer.track(command, "rtk run-test", &raw, &summary);
Ok(())
}
fn filter_errors(output: &str) -> String {
lazy_static::lazy_static! {
static ref ERROR_PATTERNS: Vec<Regex> = vec![
// Generic errors
Regex::new(r"(?i)^.*error[\s:\[].*$").unwrap(),
Regex::new(r"(?i)^.*\berr\b.*$").unwrap(),
Regex::new(r"(?i)^.*warning[\s:\[].*$").unwrap(),
Regex::new(r"(?i)^.*\bwarn\b.*$").unwrap(),
Regex::new(r"(?i)^.*failed.*$").unwrap(),
Regex::new(r"(?i)^.*failure.*$").unwrap(),
Regex::new(r"(?i)^.*exception.*$").unwrap(),
Regex::new(r"(?i)^.*panic.*$").unwrap(),
// Rust specific
Regex::new(r"^error\[E\d+\]:.*$").unwrap(),
Regex::new(r"^\s*--> .*:\d+:\d+$").unwrap(),
// Python
Regex::new(r"^Traceback.*$").unwrap(),
Regex::new(r#"^\s*File ".*", line \d+.*$"#).unwrap(),
// JavaScript/TypeScript
Regex::new(r"^\s*at .*:\d+:\d+.*$").unwrap(),
// Go
Regex::new(r"^.*\.go:\d+:.*$").unwrap(),
];
}
let mut result = Vec::new();
let mut in_error_block = false;
let mut blank_count = 0;
for line in output.lines() {
let is_error_line = ERROR_PATTERNS.iter().any(|p| p.is_match(line));
if is_error_line {
in_error_block = true;
blank_count = 0;
result.push(line.to_string());
} else if in_error_block {
if line.trim().is_empty() {
blank_count += 1;
if blank_count >= 2 {
in_error_block = false;
} else {
result.push(line.to_string());
}
} else if line.starts_with(' ') || line.starts_with('\t') {
// Continuation of error
result.push(line.to_string());
blank_count = 0;
} else {
in_error_block = false;
}
}
}
result.join("\n")
}
fn extract_test_summary(output: &str, command: &str) -> String {
let mut result = Vec::new();
let lines: Vec<&str> = output.lines().collect();
// Detect test framework
let is_cargo = command.contains("cargo test");
let is_pytest = command.contains("pytest");
let is_jest =
command.contains("jest") || command.contains("npm test") || command.contains("yarn test");
let is_go = command.contains("go test");
// Collect failures
let mut failures = Vec::new();
let mut in_failure = false;
let mut failure_lines = Vec::new();
for line in lines.iter() {
// Cargo test
if is_cargo {
if line.contains("test result:") {
result.push(line.to_string());
}
if line.contains("FAILED") && !line.contains("test result") {
failures.push(line.to_string());
}
if line.starts_with("failures:") {
in_failure = true;
}
if in_failure && line.starts_with(" ") {
failure_lines.push(line.to_string());
}
}
// Pytest
if is_pytest {
if line.contains(" passed") || line.contains(" failed") || line.contains(" error") {
result.push(line.to_string());
}
if line.contains("FAILED") {
failures.push(line.to_string());
}
}
// Jest
if is_jest {
if line.contains("Tests:") || line.contains("Test Suites:") {
result.push(line.to_string());
}
if line.contains("✕") || line.contains("FAIL") {
failures.push(line.to_string());
}
}
// Go test
if is_go {
if line.starts_with("ok") || line.starts_with("FAIL") || line.starts_with("---") {
result.push(line.to_string());
}
if line.contains("FAIL") {
failures.push(line.to_string());
}
}
}
// Build output
let mut output = String::new();
if !failures.is_empty() {
output.push_str("[FAIL] FAILURES:\n");
for f in failures.iter().take(10) {
output.push_str(&format!(" {}\n", f));
}
if failures.len() > 10 {
output.push_str(&format!(" ... +{} more failures\n", failures.len() - 10));
}
output.push('\n');
}
if !result.is_empty() {
output.push_str("SUMMARY:\n");
for r in &result {
output.push_str(&format!(" {}\n", r));
}
} else {
// Fallback: show last few lines
output.push_str("OUTPUT (last 5 lines):\n");
let start = lines.len().saturating_sub(5);
for line in &lines[start..] {
if !line.trim().is_empty() {
output.push_str(&format!(" {}\n", line));
}
}
}
output
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_filter_errors() {
let output = "info: compiling\nerror: something failed\n at line 10\ninfo: done";
let filtered = filter_errors(output);
assert!(filtered.contains("error"));
assert!(!filtered.contains("info"));
}
}