foundry_test_utils/
filter.rs

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
use foundry_common::TestFilter;
use regex::Regex;
use std::path::Path;

pub struct Filter {
    test_regex: Regex,
    contract_regex: Regex,
    path_regex: Regex,
    exclude_tests: Option<Regex>,
    exclude_contracts: Option<Regex>,
    exclude_paths: Option<Regex>,
}

impl Filter {
    pub fn new(test_pattern: &str, contract_pattern: &str, path_pattern: &str) -> Self {
        Self {
            test_regex: Regex::new(test_pattern)
                .unwrap_or_else(|_| panic!("Failed to parse test pattern: `{test_pattern}`")),
            contract_regex: Regex::new(contract_pattern).unwrap_or_else(|_| {
                panic!("Failed to parse contract pattern: `{contract_pattern}`")
            }),
            path_regex: Regex::new(path_pattern)
                .unwrap_or_else(|_| panic!("Failed to parse path pattern: `{path_pattern}`")),
            exclude_tests: None,
            exclude_contracts: None,
            exclude_paths: None,
        }
    }

    pub fn contract(contract_pattern: &str) -> Self {
        Self::new(".*", contract_pattern, ".*")
    }

    pub fn path(path_pattern: &str) -> Self {
        Self::new(".*", ".*", path_pattern)
    }

    /// All tests to also exclude
    ///
    /// This is a workaround since regex does not support negative look aheads
    pub fn exclude_tests(mut self, pattern: &str) -> Self {
        self.exclude_tests = Some(Regex::new(pattern).unwrap());
        self
    }

    /// All contracts to also exclude
    ///
    /// This is a workaround since regex does not support negative look aheads
    pub fn exclude_contracts(mut self, pattern: &str) -> Self {
        self.exclude_contracts = Some(Regex::new(pattern).unwrap());
        self
    }

    /// All paths to also exclude
    ///
    /// This is a workaround since regex does not support negative look aheads
    pub fn exclude_paths(mut self, pattern: &str) -> Self {
        self.exclude_paths = Some(Regex::new(pattern).unwrap());
        self
    }

    pub fn matches_all() -> Self {
        Self {
            test_regex: Regex::new(".*").unwrap(),
            contract_regex: Regex::new(".*").unwrap(),
            path_regex: Regex::new(".*").unwrap(),
            exclude_tests: None,
            exclude_contracts: None,
            exclude_paths: None,
        }
    }
}

impl TestFilter for Filter {
    fn matches_test(&self, test_name: &str) -> bool {
        if let Some(exclude) = &self.exclude_tests {
            if exclude.is_match(test_name) {
                return false;
            }
        }
        self.test_regex.is_match(test_name)
    }

    fn matches_contract(&self, contract_name: &str) -> bool {
        if let Some(exclude) = &self.exclude_contracts {
            if exclude.is_match(contract_name) {
                return false;
            }
        }

        self.contract_regex.is_match(contract_name)
    }

    fn matches_path(&self, path: &Path) -> bool {
        let Some(path) = path.to_str() else {
            return false;
        };

        if let Some(exclude) = &self.exclude_paths {
            if exclude.is_match(path) {
                return false;
            }
        }
        self.path_regex.is_match(path)
    }
}