-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmod.rs
More file actions
324 lines (284 loc) · 10.7 KB
/
mod.rs
File metadata and controls
324 lines (284 loc) · 10.7 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
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
use regex::Regex;
use std::sync::LazyLock;
// Public API re-exports
pub use self::builtins::builtin_patterns;
pub use self::toml::{FailureSection, PatternFile, load_user_patterns, parse_pattern_str};
/// Get a reference to the static built-in patterns.
pub fn builtins() -> &'static [Pattern] {
&BUILTINS
}
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
/// A pattern for matching and extracting information from command output.
///
/// Patterns define how to compress command output using regex matching.
/// When a command matches the `command_match` regex, the pattern's
/// success or failure logic is applied to extract compressed output.
pub struct Pattern {
/// Regex that matches the command line (e.g., `r"cargo test"`).
pub command_match: Regex,
/// Optional pattern for extracting a summary from successful command output.
pub success: Option<SuccessPattern>,
/// Optional strategy for filtering failed command output.
pub failure: Option<FailurePattern>,
}
/// Pattern for extracting a summary from successful command output.
///
/// The `pattern` field contains a regex with named capture groups.
/// The `summary` field is a template string with placeholders like `{name}`
/// that are replaced with captured values.
pub struct SuccessPattern {
/// Regex with named capture groups for extracting values.
pub pattern: Regex,
/// Template string with `{name}` placeholders for summary formatting.
pub summary: String,
}
/// Strategy for filtering failed command output.
///
/// When a command exits with a non-zero status, the failure strategy
/// extracts relevant error information (e.g., tail N lines, head N lines,
/// grep for error keywords, or extract text between delimiters).
pub struct FailurePattern {
/// The strategy to apply for extracting error information.
pub strategy: FailureStrategy,
}
/// Strategy for extracting error information from failed command output.
///
/// Each variant defines a different approach to identifying and extracting
/// the most relevant error information from command output.
pub enum FailureStrategy {
/// Keep the last N lines of output (tail).
Tail {
/// Number of lines to keep from the end.
lines: usize,
},
/// Keep the first N lines of output (head).
Head {
/// Number of lines to keep from the start.
lines: usize,
},
/// Filter lines matching a regex pattern.
Grep {
/// Regex pattern to match error lines.
pattern: Regex,
},
/// Extract text between two delimiter strings.
Between {
/// Starting delimiter string.
start: String,
/// Ending delimiter string.
end: String,
},
}
// ---------------------------------------------------------------------------
// Matching & extraction
// ---------------------------------------------------------------------------
/// Find the first pattern whose `command_match` matches `command`.
pub fn find_matching<'a>(command: &str, patterns: &'a [Pattern]) -> Option<&'a Pattern> {
patterns.iter().find(|p| p.command_match.is_match(command))
}
/// Like `find_matching` but works with a slice of references.
///
/// Useful when you have a slice of pattern references rather than values.
pub fn find_matching_ref<'a>(command: &str, patterns: &[&'a Pattern]) -> Option<&'a Pattern> {
patterns
.iter()
.find(|p| p.command_match.is_match(command))
.copied()
}
/// Apply a success pattern to output, returning the formatted summary if it matches.
pub fn extract_summary(pat: &SuccessPattern, output: &str) -> Option<String> {
let caps = pat.pattern.captures(output)?;
let mut summary = pat.summary.clone();
for name in pat.pattern.capture_names().flatten() {
if let Some(m) = caps.name(name) {
summary = summary.replace(&format!("{{{name}}}"), m.as_str());
}
}
Some(summary)
}
/// Apply a failure strategy to extract actionable output.
pub fn extract_failure(pat: &FailurePattern, output: &str) -> String {
match &pat.strategy {
FailureStrategy::Tail { lines } => {
let all: Vec<&str> = output.lines().collect();
let start = all.len().saturating_sub(*lines);
all[start..].join("\n")
}
FailureStrategy::Head { lines } => {
let all: Vec<&str> = output.lines().collect();
let end = (*lines).min(all.len());
all[..end].join("\n")
}
FailureStrategy::Grep { pattern } => output
.lines()
.filter(|l| pattern.is_match(l))
.collect::<Vec<_>>()
.join("\n"),
FailureStrategy::Between { start, end } => {
let mut capturing = false;
let mut lines = Vec::new();
for line in output.lines() {
if !capturing && line.contains(start.as_str()) {
capturing = true;
}
if capturing {
lines.push(line);
if line.contains(end.as_str()) {
break;
}
}
}
lines.join("\n")
}
}
}
// Submodules
mod builtins;
mod toml;
// Static builtin patterns
static BUILTINS: LazyLock<Vec<Pattern>> = LazyLock::new(builtin_patterns);
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_builtin_pytest_success() {
let patterns = builtins();
let pat = find_matching("pytest tests/ -x", patterns).unwrap();
let output = "collected 47 items\n\
.................\n\
47 passed in 3.2s\n";
let summary = extract_summary(pat.success.as_ref().unwrap(), output).unwrap();
assert_eq!(summary, "47 passed, 3.2s");
}
#[test]
fn test_builtin_pytest_failure_tail() {
let patterns = builtins();
let pat = find_matching("pytest -x", patterns).unwrap();
let fail_pat = pat.failure.as_ref().unwrap();
let lines: String = (0..50).map(|i| format!("line {i}\n")).collect();
let result = extract_failure(fail_pat, &lines);
// tail 30 lines from 50 → lines 20..49
assert!(result.contains("line 20"));
assert!(result.contains("line 49"));
assert!(!result.contains("line 0\n"));
}
#[test]
fn test_builtin_cargo_test_success() {
let patterns = builtins();
let pat = find_matching("cargo test --release", patterns).unwrap();
let output = "running 15 tests\n\
test result: ok. 15 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 3.45s\n";
let summary = extract_summary(pat.success.as_ref().unwrap(), output).unwrap();
assert_eq!(summary, "15 passed, 3.45s");
}
#[test]
fn test_command_matching() {
let patterns = builtins();
assert!(find_matching("pytest tests/", patterns).is_some());
assert!(find_matching("cargo test", patterns).is_some());
assert!(find_matching("cargo build", patterns).is_some());
assert!(find_matching("go test ./...", patterns).is_some());
assert!(find_matching("ruff check src/", patterns).is_some());
assert!(find_matching("eslint .", patterns).is_some());
assert!(find_matching("tsc --noEmit", patterns).is_some());
assert!(find_matching("cargo clippy", patterns).is_some());
}
#[test]
fn test_no_match_unknown_command() {
let patterns = builtins();
assert!(find_matching("curl https://example.com", patterns).is_none());
}
#[test]
fn test_summary_template_formatting() {
let pat = SuccessPattern {
pattern: Regex::new(r"(?P<a>\d+) things, (?P<b>\d+) items").unwrap(),
summary: "{a} things and {b} items".into(),
};
let result = extract_summary(&pat, "found 5 things, 3 items here").unwrap();
assert_eq!(result, "5 things and 3 items");
}
#[test]
fn test_failure_strategy_head() {
let strat = FailurePattern {
strategy: FailureStrategy::Head { lines: 3 },
};
let output = "line1\nline2\nline3\nline4\nline5\n";
let result = extract_failure(&strat, output);
assert_eq!(result, "line1\nline2\nline3");
}
#[test]
fn test_failure_strategy_grep() {
let strat = FailurePattern {
strategy: FailureStrategy::Grep {
pattern: Regex::new(r"ERROR").unwrap(),
},
};
let output = "INFO ok\nERROR bad\nINFO fine\nERROR worse\n";
let result = extract_failure(&strat, output);
assert_eq!(result, "ERROR bad\nERROR worse");
}
#[test]
fn test_failure_strategy_between() {
let strat = FailurePattern {
strategy: FailureStrategy::Between {
start: "FAILURES".into(),
end: "summary".into(),
},
};
let output = "stuff\nFAILURES\nerror 1\nerror 2\nshort test summary\nmore\n";
let result = extract_failure(&strat, output);
assert_eq!(result, "FAILURES\nerror 1\nerror 2\nshort test summary");
}
#[test]
fn test_load_pattern_from_toml() {
let toml = r#"
command_match = "^myapp test"
[success]
pattern = '(?P<count>\d+) tests passed'
summary = "{count} tests passed"
[failure]
strategy = "tail"
lines = 20
"#;
let pat = parse_pattern_str(toml).unwrap();
assert!(pat.command_match.is_match("myapp test --verbose"));
let summary = extract_summary(pat.success.as_ref().unwrap(), "42 tests passed").unwrap();
assert_eq!(summary, "42 tests passed");
}
#[test]
fn test_invalid_toml_returns_error() {
let result = parse_pattern_str("not valid toml {{{");
assert!(result.is_err());
}
#[test]
fn test_invalid_regex_returns_error() {
let toml = r#"
command_match = "[invalid"
"#;
let result = parse_pattern_str(toml);
assert!(result.is_err());
}
#[test]
fn test_user_patterns_override_builtins() {
let user_pat = parse_pattern_str(
r#"
command_match = "^pytest"
[success]
pattern = '(?P<n>\d+) ok'
summary = "{n} ok"
"#,
)
.unwrap();
// User patterns should be checked first
let mut all = vec![user_pat];
all.extend(builtin_patterns());
let pat = find_matching("pytest -x", &all).unwrap();
let summary = extract_summary(pat.success.as_ref().unwrap(), "10 ok").unwrap();
assert_eq!(summary, "10 ok");
}
}