alint_rules/
file_content_matches.rs1use alint_core::{Context, Error, FixSpec, Fixer, Level, Result, Rule, RuleSpec, Scope, Violation};
4use regex::Regex;
5use serde::Deserialize;
6
7use crate::fixers::FileAppendFixer;
8
9#[derive(Debug, Deserialize)]
10struct Options {
11 pattern: String,
12}
13
14#[derive(Debug)]
15pub struct FileContentMatchesRule {
16 id: String,
17 level: Level,
18 policy_url: Option<String>,
19 message: Option<String>,
20 scope: Scope,
21 pattern_src: String,
22 pattern: Regex,
23 fixer: Option<FileAppendFixer>,
24}
25
26impl Rule for FileContentMatchesRule {
27 fn id(&self) -> &str {
28 &self.id
29 }
30 fn level(&self) -> Level {
31 self.level
32 }
33 fn policy_url(&self) -> Option<&str> {
34 self.policy_url.as_deref()
35 }
36
37 fn fixer(&self) -> Option<&dyn Fixer> {
38 self.fixer.as_ref().map(|f| f as &dyn Fixer)
39 }
40
41 fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
42 let mut violations = Vec::new();
43 for entry in ctx.index.files() {
44 if !self.scope.matches(&entry.path) {
45 continue;
46 }
47 let full = ctx.root.join(&entry.path);
48 let bytes = match std::fs::read(&full) {
49 Ok(b) => b,
50 Err(e) => {
51 violations.push(
52 Violation::new(format!("could not read file: {e}"))
53 .with_path(entry.path.clone()),
54 );
55 continue;
56 }
57 };
58 let Ok(text) = std::str::from_utf8(&bytes) else {
59 violations.push(
60 Violation::new("file is not valid UTF-8; cannot match regex")
61 .with_path(entry.path.clone()),
62 );
63 continue;
64 };
65 if !self.pattern.is_match(text) {
66 let msg = self.message.clone().unwrap_or_else(|| {
67 format!(
68 "content does not match required pattern /{}/",
69 self.pattern_src
70 )
71 });
72 violations.push(Violation::new(msg).with_path(entry.path.clone()));
73 }
74 }
75 Ok(violations)
76 }
77}
78
79pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
80 let Some(paths) = &spec.paths else {
81 return Err(Error::rule_config(
82 &spec.id,
83 "file_content_matches requires a `paths` field",
84 ));
85 };
86 let opts: Options = spec
87 .deserialize_options()
88 .map_err(|e| Error::rule_config(&spec.id, format!("invalid options: {e}")))?;
89 let pattern = Regex::new(&opts.pattern)
90 .map_err(|e| Error::rule_config(&spec.id, format!("invalid pattern: {e}")))?;
91 let fixer = match &spec.fix {
92 Some(FixSpec::FileAppend { file_append }) => {
93 let source = alint_core::resolve_content_source(
94 &spec.id,
95 "file_append",
96 &file_append.content,
97 &file_append.content_from,
98 )?;
99 Some(FileAppendFixer::new(source))
100 }
101 Some(other) => {
102 return Err(Error::rule_config(
103 &spec.id,
104 format!(
105 "fix.{} is not compatible with file_content_matches",
106 other.op_name()
107 ),
108 ));
109 }
110 None => None,
111 };
112 Ok(Box::new(FileContentMatchesRule {
113 id: spec.id.clone(),
114 level: spec.level,
115 policy_url: spec.policy_url.clone(),
116 message: spec.message.clone(),
117 scope: Scope::from_paths_spec(paths)?,
118 pattern_src: opts.pattern,
119 pattern,
120 fixer,
121 }))
122}
123
124#[cfg(test)]
125mod tests {
126 use super::*;
127 use crate::test_support::{ctx, spec_yaml, tempdir_with_files};
128
129 #[test]
130 fn build_rejects_missing_paths_field() {
131 let spec = spec_yaml(
132 "id: t\n\
133 kind: file_content_matches\n\
134 pattern: \".*\"\n\
135 level: error\n",
136 );
137 assert!(build(&spec).is_err());
138 }
139
140 #[test]
141 fn build_rejects_invalid_regex() {
142 let spec = spec_yaml(
143 "id: t\n\
144 kind: file_content_matches\n\
145 paths: \"**/*\"\n\
146 pattern: \"[unterminated\"\n\
147 level: error\n",
148 );
149 assert!(build(&spec).is_err());
150 }
151
152 #[test]
153 fn evaluate_passes_when_pattern_matches() {
154 let spec = spec_yaml(
155 "id: t\n\
156 kind: file_content_matches\n\
157 paths: \"LICENSE\"\n\
158 pattern: \"Apache License\"\n\
159 level: error\n",
160 );
161 let rule = build(&spec).unwrap();
162 let (tmp, idx) =
163 tempdir_with_files(&[("LICENSE", b"Apache License Version 2.0, January 2004\n")]);
164 let v = rule.evaluate(&ctx(tmp.path(), &idx)).unwrap();
165 assert!(v.is_empty(), "pattern should match: {v:?}");
166 }
167
168 #[test]
169 fn evaluate_fires_when_pattern_missing() {
170 let spec = spec_yaml(
171 "id: t\n\
172 kind: file_content_matches\n\
173 paths: \"LICENSE\"\n\
174 pattern: \"Apache License\"\n\
175 level: error\n",
176 );
177 let rule = build(&spec).unwrap();
178 let (tmp, idx) = tempdir_with_files(&[("LICENSE", b"MIT License\n\nCopyright ...\n")]);
179 let v = rule.evaluate(&ctx(tmp.path(), &idx)).unwrap();
180 assert_eq!(v.len(), 1);
181 }
182
183 #[test]
184 fn evaluate_skips_files_outside_scope() {
185 let spec = spec_yaml(
186 "id: t\n\
187 kind: file_content_matches\n\
188 paths: \"LICENSE\"\n\
189 pattern: \"Apache\"\n\
190 level: error\n",
191 );
192 let rule = build(&spec).unwrap();
193 let (tmp, idx) = tempdir_with_files(&[("README.md", b"no apache here")]);
194 let v = rule.evaluate(&ctx(tmp.path(), &idx)).unwrap();
195 assert!(v.is_empty(), "out-of-scope shouldn't fire: {v:?}");
196 }
197
198 #[test]
199 fn evaluate_fires_with_clear_message_on_non_utf8() {
200 let spec = spec_yaml(
205 "id: t\n\
206 kind: file_content_matches\n\
207 paths: \"img.bin\"\n\
208 pattern: \"never matches\"\n\
209 level: error\n",
210 );
211 let rule = build(&spec).unwrap();
212 let (tmp, idx) = tempdir_with_files(&[("img.bin", &[0xff, 0xfe, 0xfd])]);
213 let v = rule.evaluate(&ctx(tmp.path(), &idx)).unwrap();
214 assert_eq!(v.len(), 1, "non-UTF-8 should report one violation");
215 assert!(
216 v[0].message.contains("UTF-8"),
217 "message should mention UTF-8: {}",
218 v[0].message
219 );
220 }
221}