1use std::collections::HashMap;
28use std::fs;
29use std::path::{Path, PathBuf};
30
31use regex::{Regex, RegexBuilder};
32use serde::Deserialize;
33
34use crate::compress::caps::{cap_classified_blocks_with, ClassifiedBlock, DropClass};
35use crate::compress::CompressionResult;
36
37const REGEX_SIZE_LIMIT: usize = 2 * 1024 * 1024;
40
41const MAX_PATTERNS_PER_FILTER: usize = 256;
44
45const DEFAULT_LINE_MAX: usize = usize::MAX;
49
50const DEFAULT_MAX_LINES: usize = usize::MAX;
52
53#[derive(Debug, Clone)]
55pub struct TomlFilter {
56 pub name: String,
57 pub source: FilterSource,
58 pub matches: Vec<String>,
59 pub description: Option<String>,
60 pub strip: Vec<Regex>,
61 pub line_max: usize,
62 pub max_lines: usize,
63 pub keep: KeepMode,
64 pub class_cap: Option<TomlClassCap>,
65 pub shortcircuit_when: Option<Regex>,
66 pub shortcircuit_replacement: Option<String>,
67 pub strip_ansi: bool,
68}
69
70#[derive(Debug, Clone, PartialEq, Eq)]
72pub enum FilterSource {
73 Builtin,
74 User { path: PathBuf },
75 Project { path: PathBuf },
76}
77
78#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
79pub enum KeepMode {
80 Head,
81 #[default]
82 Tail,
83 Middle,
84}
85
86#[derive(Debug, Clone)]
87pub struct TomlClassCap {
88 pub class: DropClass,
89 pub max: usize,
90 pub patterns: Vec<Regex>,
91}
92
93#[derive(Debug, Default, Clone)]
99pub struct FilterRegistry {
100 by_match: HashMap<String, TomlFilter>,
102 all: Vec<TomlFilter>,
105 warnings: Vec<String>,
107}
108
109impl FilterRegistry {
110 pub fn lookup(&self, command: &str) -> Option<&TomlFilter> {
113 let program = program_name(command)?;
114 self.by_match.get(program)
115 }
116
117 pub fn all(&self) -> &[TomlFilter] {
119 &self.all
120 }
121
122 pub fn warnings(&self) -> &[String] {
125 &self.warnings
126 }
127}
128
129pub fn build_registry(
134 builtin_inputs: &[(&'static str, &'static str)],
135 user_dir: Option<&Path>,
136 project_dir: Option<&Path>,
137) -> FilterRegistry {
138 let mut registry = FilterRegistry::default();
139
140 for (name, content) in builtin_inputs {
142 match parse_filter(name, content, FilterSource::Builtin) {
143 Ok(filter) => insert_filter(&mut registry, filter),
144 Err(e) => registry
145 .warnings
146 .push(format!("builtin filter {name}: {e}")),
147 }
148 }
149
150 if let Some(dir) = user_dir {
152 load_dir(dir, &mut registry, |path| FilterSource::User {
153 path: path.to_path_buf(),
154 });
155 }
156
157 if let Some(dir) = project_dir {
161 load_dir(dir, &mut registry, |path| FilterSource::Project {
162 path: path.to_path_buf(),
163 });
164 }
165
166 registry
167}
168
169fn load_dir<F>(dir: &Path, registry: &mut FilterRegistry, source_for: F)
170where
171 F: Fn(&Path) -> FilterSource,
172{
173 let entries = match fs::read_dir(dir) {
174 Ok(entries) => entries,
175 Err(e) => {
176 if e.kind() != std::io::ErrorKind::NotFound {
178 registry
179 .warnings
180 .push(format!("filter dir {}: {e}", dir.display()));
181 }
182 return;
183 }
184 };
185
186 let mut paths: Vec<PathBuf> = entries
187 .filter_map(|res| res.ok())
188 .map(|entry| entry.path())
189 .filter(|path| path.extension().and_then(|s| s.to_str()) == Some("toml"))
190 .collect();
191 paths.sort();
192
193 for path in paths {
194 let content = match fs::read_to_string(&path) {
195 Ok(s) => s,
196 Err(e) => {
197 registry
198 .warnings
199 .push(format!("filter {}: read failed: {e}", path.display()));
200 continue;
201 }
202 };
203 let name = path
204 .file_stem()
205 .and_then(|s| s.to_str())
206 .unwrap_or("<unknown>")
207 .to_string();
208 let source = source_for(&path);
209 match parse_filter(&name, &content, source) {
210 Ok(filter) => insert_filter(registry, filter),
211 Err(e) => registry
212 .warnings
213 .push(format!("filter {}: {e}", path.display())),
214 }
215 }
216}
217
218fn insert_filter(registry: &mut FilterRegistry, filter: TomlFilter) {
219 for keyword in &filter.matches {
223 registry.by_match.insert(keyword.clone(), filter.clone());
224 }
225 registry
228 .all
229 .retain(|existing| !(existing.name == filter.name && existing.source == filter.source));
230 registry.all.push(filter);
231}
232
233#[derive(Debug, Deserialize)]
234struct RawFilter {
235 #[serde(default)]
236 filter: RawFilterMeta,
237 #[serde(default)]
238 strip: Option<RawStrip>,
239 #[serde(default)]
240 truncate: Option<RawTruncate>,
241 #[serde(default)]
242 cap: Option<RawCap>,
243 #[serde(default)]
244 class_cap: Option<RawClassCap>,
245 #[serde(default)]
246 shortcircuit: Option<RawShortcircuit>,
247 #[serde(default)]
248 ansi: Option<RawAnsi>,
249}
250
251#[derive(Debug, Deserialize, Default)]
252struct RawFilterMeta {
253 #[serde(default)]
254 matches: Vec<String>,
255 #[serde(default)]
256 description: Option<String>,
257}
258
259#[derive(Debug, Deserialize, Default)]
260struct RawStrip {
261 #[serde(default)]
262 patterns: Vec<String>,
263}
264
265#[derive(Debug, Deserialize, Default)]
266struct RawTruncate {
267 #[serde(default)]
268 line_max: Option<usize>,
269}
270
271#[derive(Debug, Deserialize, Default)]
272struct RawCap {
273 #[serde(default)]
274 max_lines: Option<usize>,
275 #[serde(default)]
276 keep: Option<String>,
277}
278
279#[derive(Debug, Deserialize, Default)]
280struct RawClassCap {
281 #[serde(default)]
282 class: Option<String>,
283 #[serde(default)]
284 max: Option<usize>,
285 #[serde(default)]
286 patterns: Vec<String>,
287}
288
289#[derive(Debug, Deserialize, Default)]
290struct RawShortcircuit {
291 #[serde(default)]
292 when: Option<String>,
293 #[serde(default)]
294 replacement: Option<String>,
295}
296
297#[derive(Debug, Deserialize, Default)]
298struct RawAnsi {
299 #[serde(default)]
300 strip: Option<bool>,
301}
302
303pub fn parse_filter(name: &str, content: &str, source: FilterSource) -> Result<TomlFilter, String> {
306 let raw: RawFilter = toml::from_str(content).map_err(|e| format!("invalid TOML: {e}"))?;
307
308 let mut matches = raw.filter.matches;
309 if matches.is_empty() {
310 matches.push(name.to_string());
312 }
313 for keyword in &matches {
314 if keyword.is_empty() || keyword.contains(char::is_whitespace) {
315 return Err(format!("invalid match keyword {keyword:?}"));
316 }
317 }
318
319 let strip_patterns = raw.strip.unwrap_or_default().patterns;
320 if strip_patterns.len() > MAX_PATTERNS_PER_FILTER {
321 return Err(format!(
322 "too many strip patterns ({} > {MAX_PATTERNS_PER_FILTER})",
323 strip_patterns.len()
324 ));
325 }
326 let mut strip = Vec::with_capacity(strip_patterns.len());
327 for pattern in strip_patterns {
328 let regex =
329 build_regex(&pattern, true).map_err(|e| format!("strip pattern {pattern:?}: {e}"))?;
330 strip.push(regex);
331 }
332
333 let line_max = raw
334 .truncate
335 .as_ref()
336 .and_then(|t| t.line_max)
337 .unwrap_or(DEFAULT_LINE_MAX);
338
339 let cap = raw.cap.unwrap_or_default();
340 let max_lines = cap.max_lines.unwrap_or(DEFAULT_MAX_LINES);
341 let keep = match cap.keep.as_deref() {
342 None => KeepMode::default(),
343 Some("head") => KeepMode::Head,
344 Some("tail") => KeepMode::Tail,
345 Some("middle") => KeepMode::Middle,
346 Some(other) => return Err(format!("invalid cap.keep {other:?}")),
347 };
348
349 let class_cap = match raw.class_cap {
350 Some(raw_class_cap) => {
351 if raw_class_cap.patterns.len() > MAX_PATTERNS_PER_FILTER {
352 return Err(format!(
353 "too many class_cap patterns ({} > {MAX_PATTERNS_PER_FILTER})",
354 raw_class_cap.patterns.len()
355 ));
356 }
357 let class = parse_drop_class(raw_class_cap.class.as_deref().unwrap_or("list"))?;
358 let mut patterns = Vec::with_capacity(raw_class_cap.patterns.len());
359 for pattern in raw_class_cap.patterns {
360 let regex = build_regex(&pattern, true)
361 .map_err(|e| format!("class_cap pattern {pattern:?}: {e}"))?;
362 patterns.push(regex);
363 }
364 Some(TomlClassCap {
365 class,
366 max: raw_class_cap.max.unwrap_or_else(|| class.default_cap()),
367 patterns,
368 })
369 }
370 None => None,
371 };
372
373 let shortcircuit = raw.shortcircuit.unwrap_or_default();
374 let (shortcircuit_when, shortcircuit_replacement) =
375 match (shortcircuit.when, shortcircuit.replacement) {
376 (Some(when), Some(replacement)) => {
377 let regex = build_regex(&when, false)
378 .map_err(|e| format!("shortcircuit.when {when:?}: {e}"))?;
379 (Some(regex), Some(replacement))
380 }
381 (Some(_), None) => return Err("shortcircuit.when set but replacement missing".into()),
382 (None, Some(_)) => return Err("shortcircuit.replacement set but when missing".into()),
383 (None, None) => (None, None),
384 };
385
386 let strip_ansi = raw.ansi.and_then(|a| a.strip).unwrap_or(true);
387
388 Ok(TomlFilter {
389 name: name.to_string(),
390 source,
391 matches,
392 description: raw.filter.description,
393 strip,
394 line_max,
395 max_lines,
396 keep,
397 class_cap,
398 shortcircuit_when,
399 shortcircuit_replacement,
400 strip_ansi,
401 })
402}
403
404fn build_regex(pattern: &str, multiline: bool) -> Result<Regex, String> {
405 RegexBuilder::new(pattern)
406 .size_limit(REGEX_SIZE_LIMIT)
407 .multi_line(multiline)
408 .build()
409 .map_err(|e| e.to_string())
410}
411
412pub fn apply_filter(filter: &TomlFilter, output: &str) -> CompressionResult {
421 apply_filter_with_exit_code(filter, output, None)
422}
423
424pub fn apply_filter_with_exit_code(
425 filter: &TomlFilter,
426 output: &str,
427 exit_code: Option<i32>,
428) -> CompressionResult {
429 let stripped_ansi = if filter.strip_ansi {
430 crate::compress::generic::strip_ansi(output)
431 } else {
432 output.to_string()
433 };
434
435 let original_line_count = stripped_ansi.lines().count();
437 let kept: Vec<&str> = stripped_ansi
438 .lines()
439 .filter(|line| !filter.strip.iter().any(|re| re.is_match(line)))
440 .collect();
441 let strip_removed_lines = kept.len() < original_line_count;
442 let after_strip = kept.join("\n");
443
444 let shortcircuit_safe = match exit_code {
446 Some(code) => code == 0,
447 None => !super::text_has_failure_signal(&after_strip),
448 };
449 if shortcircuit_safe {
450 if let (Some(when), Some(replacement)) =
451 (&filter.shortcircuit_when, &filter.shortcircuit_replacement)
452 {
453 if when.is_match(&after_strip) {
454 return CompressionResult::new(replacement.clone());
455 }
456 }
457 }
458
459 let truncated: Vec<String> = if filter.line_max == usize::MAX {
461 kept.iter().map(|s| (*s).to_string()).collect()
462 } else {
463 kept.iter()
464 .map(|line| truncate_line(line, filter.line_max))
465 .collect()
466 };
467
468 if let Some(class_cap) = &filter.class_cap {
470 return cap_class_lines(&truncated, class_cap);
471 }
472
473 cap_lines(
475 &truncated,
476 filter.max_lines,
477 filter.keep,
478 strip_removed_lines,
479 )
480}
481
482fn truncate_line(line: &str, line_max: usize) -> String {
483 if line.chars().count() <= line_max {
484 return line.to_string();
485 }
486 let keep_each_side = line_max.saturating_sub(3) / 2;
488 let head: String = line.chars().take(keep_each_side).collect();
489 let tail: String = line
490 .chars()
491 .rev()
492 .take(keep_each_side)
493 .collect::<Vec<_>>()
494 .into_iter()
495 .rev()
496 .collect();
497 format!("{head}…{tail}")
498}
499
500fn cap_class_lines(lines: &[String], class_cap: &TomlClassCap) -> CompressionResult {
501 let blocks = lines
502 .iter()
503 .map(|line| {
504 if class_cap.patterns.is_empty()
505 || class_cap
506 .patterns
507 .iter()
508 .any(|pattern| pattern.is_match(line))
509 {
510 ClassifiedBlock::new(class_cap.class, line.clone())
511 } else {
512 ClassifiedBlock::unclassified(line.clone())
513 }
514 })
515 .collect();
516 let capped = cap_classified_blocks_with(blocks, |class| {
517 if class == class_cap.class {
518 class_cap.max
519 } else {
520 class.default_cap()
521 }
522 });
523 CompressionResult::with_class_drops(capped.text, capped.dropped_by_class)
524}
525
526fn cap_lines(
527 lines: &[String],
528 max_lines: usize,
529 keep: KeepMode,
530 had_prior_line_drop: bool,
531) -> CompressionResult {
532 if lines.len() <= max_lines || max_lines == usize::MAX {
533 return CompressionResult::new(lines.join("\n"));
534 }
535
536 if max_lines == 0 {
537 return CompressionResult::with_inner_drop(String::new(), false);
538 }
539
540 let kept = match keep {
541 KeepMode::Head => lines.iter().take(max_lines).cloned().collect::<Vec<_>>(),
542 KeepMode::Tail => lines
543 .iter()
544 .skip(lines.len().saturating_sub(max_lines))
545 .cloned()
546 .collect::<Vec<_>>(),
547 KeepMode::Middle => {
548 let head_count = max_lines / 2;
549 let tail_count = max_lines - head_count;
550 let mut kept: Vec<String> = lines.iter().take(head_count).cloned().collect();
551 kept.extend(lines.iter().skip(lines.len() - tail_count).cloned());
552 kept
553 }
554 };
555 if matches!(keep, KeepMode::Tail) && !had_prior_line_drop {
556 let dropped_prefix_lines = lines.len().saturating_sub(max_lines);
557 CompressionResult::with_prefix_drop(kept.join("\n"), dropped_prefix_lines + 1)
558 } else {
559 CompressionResult::with_inner_drop(kept.join("\n"), false)
560 }
561}
562
563fn parse_drop_class(value: &str) -> Result<DropClass, String> {
564 match value {
565 "error" | "errors" => Ok(DropClass::Error),
566 "warning" | "warnings" => Ok(DropClass::Warning),
567 "failure" | "failures" => Ok(DropClass::Failure),
568 "issue" | "issues" => Ok(DropClass::Issue),
569 "list" | "list_item" | "list-items" | "list items" => Ok(DropClass::List),
570 "inventory" | "inventory_item" | "inventory-items" | "inventory items" => {
571 Ok(DropClass::Inventory)
572 }
573 "timing" | "timing_line" | "timing-lines" | "timing lines" => Ok(DropClass::Timing),
574 other => Err(format!("invalid class_cap.class {other:?}")),
575 }
576}
577
578pub fn program_name(command: &str) -> Option<&str> {
588 for token in command.split_whitespace() {
589 if is_env_assignment(token) {
591 continue;
592 }
593 return Some(basename(token));
595 }
596 None
597}
598
599fn is_env_assignment(token: &str) -> bool {
600 let Some(eq) = token.find('=') else {
601 return false;
602 };
603 let key = &token[..eq];
604 !key.is_empty() && key.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
605}
606
607fn basename(token: &str) -> &str {
608 let last_unix = token.rfind('/');
610 let last_win = token.rfind('\\');
611 let split_at = match (last_unix, last_win) {
612 (Some(u), Some(w)) => u.max(w),
613 (Some(u), None) => u,
614 (None, Some(w)) => w,
615 (None, None) => return token,
616 };
617 &token[split_at + 1..]
618}
619
620#[cfg(test)]
621mod tests {
622 use super::*;
623
624 fn parse(content: &str) -> TomlFilter {
625 parse_filter("test", content, FilterSource::Builtin).expect("parse")
626 }
627
628 #[test]
629 fn parses_minimal_filter() {
630 let filter = parse(
631 r#"
632[filter]
633matches = ["make"]
634"#,
635 );
636 assert_eq!(filter.matches, vec!["make"]);
637 assert_eq!(filter.line_max, usize::MAX);
638 assert_eq!(filter.max_lines, usize::MAX);
639 assert!(filter.strip.is_empty());
640 assert!(filter.shortcircuit_when.is_none());
641 assert!(filter.strip_ansi);
642 }
643
644 #[test]
645 fn filename_default_match() {
646 let filter = parse_filter("ls", "", FilterSource::Builtin).expect("parse");
648 assert_eq!(filter.matches, vec!["ls"]);
649 }
650
651 #[test]
652 fn rejects_invalid_match_keyword() {
653 let err = parse_filter(
654 "bad",
655 r#"[filter]
656matches = ["has whitespace"]
657"#,
658 FilterSource::Builtin,
659 )
660 .unwrap_err();
661 assert!(err.contains("invalid match keyword"), "got: {err}");
662 }
663
664 #[test]
665 fn rejects_bad_strip_regex() {
666 let err = parse_filter(
667 "bad",
668 r#"
669[filter]
670matches = ["bad"]
671
672[strip]
673patterns = ["[unclosed"]
674"#,
675 FilterSource::Builtin,
676 )
677 .unwrap_err();
678 assert!(err.contains("strip pattern"), "got: {err}");
679 }
680
681 #[test]
682 fn strip_drops_matching_lines() {
683 let filter = parse(
684 r#"
685[filter]
686matches = ["x"]
687
688[strip]
689patterns = ['^Entering directory', '^Leaving directory']
690"#,
691 );
692 let input = "Entering directory `/tmp`\ngcc -c foo.c\nLeaving directory `/tmp`";
693 let out = apply_filter(&filter, input).text;
694 assert_eq!(out, "gcc -c foo.c");
695 }
696
697 #[test]
698 fn shortcircuit_replaces_empty_after_strip() {
699 let filter = parse(
700 r#"
701[filter]
702matches = ["x"]
703
704[strip]
705patterns = ['^make\[\d+\]:.*']
706
707[shortcircuit]
708when = '\A\z'
709replacement = "make: ok"
710"#,
711 );
712 let input = "make[1]: Entering directory `/tmp`\nmake[1]: Leaving directory `/tmp`";
713 let out = apply_filter(&filter, input).text;
714 assert_eq!(out, "make: ok");
715 }
716
717 #[test]
718 fn shortcircuit_line_anchors_do_not_match_inner_blank_lines() {
719 let filter = parse(
720 r#"
721[filter]
722matches = ["x"]
723
724[shortcircuit]
725when = '^\s*$'
726replacement = "ok"
727"#,
728 );
729 let out = apply_filter(&filter, "error\n\nhint").text;
730 assert_eq!(out, "error\n\nhint");
731 }
732
733 #[test]
734 fn cap_tail_keeps_last_n_lines() {
735 let filter = parse(
736 r#"
737[filter]
738matches = ["x"]
739
740[cap]
741max_lines = 3
742keep = "tail"
743"#,
744 );
745 let input = "1\n2\n3\n4\n5";
746 let out = apply_filter(&filter, input);
747 assert_eq!(out.text, "3\n4\n5");
748 assert!(out.had_inner_drop);
749 assert!(out.offset_hint_eligible);
750 assert_eq!(out.text.lines().count(), 3);
751 }
752
753 #[test]
754 fn cap_tail_after_strip_disables_offset_hint() {
755 let filter = parse(
756 r#"
757[filter]
758matches = ["x"]
759
760[strip]
761patterns = ["^strip-me"]
762
763[cap]
764max_lines = 2
765keep = "tail"
766"#,
767 );
768 let out = apply_filter(
769 &filter,
770 "strip-me
7711
7722
7733
7744",
775 );
776
777 assert_eq!(
778 out.text,
779 "3
7804"
781 );
782 assert!(out.had_inner_drop);
783 assert!(!out.offset_hint_eligible);
784 assert_eq!(out.offset_start_line, None);
785 }
786
787 #[test]
788 fn cap_head_keeps_first_n_lines() {
789 let filter = parse(
790 r#"
791[filter]
792matches = ["x"]
793
794[cap]
795max_lines = 2
796keep = "head"
797"#,
798 );
799 let input = "1\n2\n3\n4";
800 let out = apply_filter(&filter, input);
801 assert_eq!(out.text, "1\n2");
802 assert!(out.had_inner_drop);
803 assert!(!out.offset_hint_eligible);
804 assert_eq!(out.text.lines().count(), 2);
805 }
806
807 #[test]
808 fn cap_middle_keeps_head_and_tail() {
809 let filter = parse(
810 r#"
811[filter]
812matches = ["x"]
813
814[cap]
815max_lines = 4
816keep = "middle"
817"#,
818 );
819 let input = "1\n2\n3\n4\n5\n6\n7\n8";
820 let out = apply_filter(&filter, input);
821 assert_eq!(out.text, "1\n2\n7\n8");
822 assert!(out.had_inner_drop);
823 assert!(!out.offset_hint_eligible);
824 assert_eq!(out.text.lines().count(), 4);
825 }
826
827 #[test]
828 fn cap_zero_keeps_no_lines() {
829 let filter = parse(
830 r#"
831[filter]
832matches = ["x"]
833
834[cap]
835max_lines = 0
836keep = "head"
837"#,
838 );
839 let out = apply_filter(&filter, "1\n2\n3");
840 assert_eq!(out.text, "");
841 assert!(out.had_inner_drop);
842 }
843
844 #[test]
845 fn cap_one_keeps_one_tail_line_without_marker() {
846 let filter = parse(
847 r#"
848[filter]
849matches = ["x"]
850
851[cap]
852max_lines = 1
853keep = "tail"
854"#,
855 );
856 let out = apply_filter(&filter, "1\n2\n3");
857 assert_eq!(out.text, "3");
858 assert!(out.had_inner_drop);
859 assert!(out.offset_hint_eligible);
860 assert_eq!(out.text.lines().count(), 1);
861 }
862
863 #[test]
864 fn cap_two_keeps_two_tail_lines_without_marker() {
865 let filter = parse(
866 r#"
867[filter]
868matches = ["x"]
869
870[cap]
871max_lines = 2
872keep = "tail"
873"#,
874 );
875 let out = apply_filter(&filter, "1\n2\n3\n4");
876 assert_eq!(out.text, "3\n4");
877 assert!(out.had_inner_drop);
878 assert!(out.offset_hint_eligible);
879 assert_eq!(out.text.lines().count(), 2);
880 }
881
882 #[test]
883 fn class_cap_replaces_plain_cap_without_stacking() {
884 let filter = parse(
885 r#"
886[filter]
887matches = ["x"]
888
889[class_cap]
890class = "warning"
891max = 2
892patterns = ["^warning"]
893
894[cap]
895max_lines = 1
896keep = "head"
897"#,
898 );
899 let out = apply_filter(&filter, "warning 1\nkeep me\nwarning 2\nwarning 3");
900
901 assert!(out.text.contains("warning 1"));
902 assert!(out.text.contains("keep me"));
903 assert!(out.text.contains("warning 2"));
904 assert!(!out.text.contains("warning 3"));
905 assert_eq!(out.dropped_by_class.get(&DropClass::Warning), Some(&1));
906 assert!(out.text.lines().count() > 1, "plain [cap] must not stack");
907 }
908
909 #[test]
910 fn truncate_per_line() {
911 let filter = parse(
912 r#"
913[filter]
914matches = ["x"]
915
916[truncate]
917line_max = 10
918"#,
919 );
920 let input = "shortline\nthis is a very long line indeed";
921 let out = apply_filter(&filter, input).text;
922 assert!(out.contains("shortline"));
923 assert!(out.contains("…"));
924 assert!(out.lines().any(|l| l.chars().count() <= 10));
925 }
926
927 #[test]
928 fn ansi_strip_default_true() {
929 let filter = parse(
930 r#"
931[filter]
932matches = ["x"]
933"#,
934 );
935 let input = "\x1b[31mred\x1b[0m text";
936 let out = apply_filter(&filter, input).text;
937 assert_eq!(out, "red text");
938 }
939
940 #[test]
941 fn ansi_strip_can_be_disabled() {
942 let filter = parse(
943 r#"
944[filter]
945matches = ["x"]
946
947[ansi]
948strip = false
949"#,
950 );
951 let input = "\x1b[31mred\x1b[0m text";
952 let out = apply_filter(&filter, input).text;
953 assert_eq!(out, input);
954 }
955
956 #[test]
957 fn shortcircuit_runs_on_after_strip_body() {
958 let filter = parse(
960 r#"
961[filter]
962matches = ["x"]
963
964[strip]
965patterns = ['^.*$']
966
967[shortcircuit]
968when = '^$'
969replacement = "ok"
970"#,
971 );
972 assert_eq!(apply_filter(&filter, "anything\nat all").text, "ok");
973 }
974
975 #[test]
976 fn program_name_handles_env_and_paths() {
977 assert_eq!(program_name("make build"), Some("make"));
978 assert_eq!(program_name("FOO=1 BAR=2 make build"), Some("make"));
979 assert_eq!(program_name("/usr/bin/cargo build"), Some("cargo"));
980 assert_eq!(program_name("./node_modules/.bin/eslint ."), Some("eslint"));
981 assert_eq!(program_name("FOO=bar /opt/x/y subcmd"), Some("y"));
983 assert_eq!(program_name(""), None);
984 assert_eq!(program_name(" "), None);
985 }
986
987 #[test]
988 fn program_name_unquoted_windows_path() {
989 assert_eq!(
996 program_name(r"C:\Program Files\Git\bin\git.exe status"),
997 Some("Program")
998 );
999 }
1000
1001 #[test]
1002 fn program_name_does_not_skip_non_assignment_token_with_equals() {
1003 assert_eq!(program_name("=oops echo hi"), Some("=oops"));
1005 }
1006
1007 #[test]
1008 fn registry_lookup_by_program_name() {
1009 let registry = build_registry(
1010 &[(
1011 "make",
1012 r#"[filter]
1013matches = ["make"]
1014
1015[strip]
1016patterns = ['^Entering']
1017"#,
1018 )],
1019 None,
1020 None,
1021 );
1022 let f = registry.lookup("make build foo").unwrap();
1023 assert_eq!(f.matches, vec!["make"]);
1024 assert!(matches!(f.source, FilterSource::Builtin));
1025 }
1026
1027 #[test]
1028 fn registry_user_overrides_builtin() {
1029 let tmp = tempfile::tempdir().unwrap();
1030 let user_path = tmp.path().join("make.toml");
1031 fs::write(
1032 &user_path,
1033 r#"[filter]
1034matches = ["make"]
1035description = "user override"
1036"#,
1037 )
1038 .unwrap();
1039
1040 let registry = build_registry(
1041 &[(
1042 "make",
1043 r#"[filter]
1044matches = ["make"]
1045description = "builtin"
1046"#,
1047 )],
1048 Some(tmp.path()),
1049 None,
1050 );
1051 let f = registry.lookup("make build").unwrap();
1052 assert_eq!(f.description.as_deref(), Some("user override"));
1053 assert!(matches!(f.source, FilterSource::User { .. }));
1054 }
1055
1056 #[test]
1057 fn registry_project_overrides_user() {
1058 let user_dir = tempfile::tempdir().unwrap();
1059 let project_dir = tempfile::tempdir().unwrap();
1060 fs::write(
1061 user_dir.path().join("make.toml"),
1062 r#"[filter]
1063matches = ["make"]
1064description = "user"
1065"#,
1066 )
1067 .unwrap();
1068 fs::write(
1069 project_dir.path().join("make.toml"),
1070 r#"[filter]
1071matches = ["make"]
1072description = "project"
1073"#,
1074 )
1075 .unwrap();
1076
1077 let registry = build_registry(&[], Some(user_dir.path()), Some(project_dir.path()));
1078 let f = registry.lookup("make").unwrap();
1079 assert_eq!(f.description.as_deref(), Some("project"));
1080 assert!(matches!(f.source, FilterSource::Project { .. }));
1081 }
1082
1083 #[test]
1084 fn bad_filter_files_warn_not_panic() {
1085 let tmp = tempfile::tempdir().unwrap();
1086 fs::write(
1087 tmp.path().join("good.toml"),
1088 r#"[filter]
1089matches = ["good"]
1090"#,
1091 )
1092 .unwrap();
1093 fs::write(tmp.path().join("bad.toml"), "not valid = toml = at all =").unwrap();
1094
1095 let registry = build_registry(&[], Some(tmp.path()), None);
1096 assert!(registry.lookup("good").is_some());
1097 assert!(registry.lookup("bad").is_none());
1098 assert!(
1099 registry.warnings().iter().any(|w| w.contains("bad.toml")),
1100 "warnings: {:?}",
1101 registry.warnings()
1102 );
1103 }
1104
1105 #[test]
1106 fn missing_dir_does_not_warn() {
1107 let registry = build_registry(&[], Some(Path::new("/nonexistent/path/12345")), None);
1108 assert!(registry.warnings().is_empty());
1109 }
1110}