alint_rules/
file_min_size.rs1use alint_core::{Context, Error, Level, Result, Rule, RuleSpec, Scope, Violation};
15use serde::Deserialize;
16
17#[derive(Debug, Deserialize)]
18#[serde(deny_unknown_fields)]
19struct Options {
20 min_bytes: u64,
21}
22
23#[derive(Debug)]
24pub struct FileMinSizeRule {
25 id: String,
26 level: Level,
27 policy_url: Option<String>,
28 message: Option<String>,
29 scope: Scope,
30 min_bytes: u64,
31}
32
33impl Rule for FileMinSizeRule {
34 alint_core::rule_common_impl!();
35
36 fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
37 let mut violations = Vec::new();
38 for entry in ctx.index.files() {
39 if !self.scope.matches(&entry.path, ctx.index) {
40 continue;
41 }
42 if entry.size < self.min_bytes {
43 let msg = self.message.clone().unwrap_or_else(|| {
44 format!(
45 "file below {} byte(s) (actual: {})",
46 self.min_bytes, entry.size,
47 )
48 });
49 violations.push(Violation::new(msg).with_path(entry.path.clone()));
50 }
51 }
52 Ok(violations)
53 }
54}
55
56pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
57 let Some(_paths) = &spec.paths else {
58 return Err(Error::rule_config(
59 &spec.id,
60 "file_min_size requires a `paths` field",
61 ));
62 };
63 let opts: Options = spec
64 .deserialize_options()
65 .map_err(|e| Error::rule_config(&spec.id, format!("invalid options: {e}")))?;
66 Ok(Box::new(FileMinSizeRule {
67 id: spec.id.clone(),
68 level: spec.level,
69 policy_url: spec.policy_url.clone(),
70 message: spec.message.clone(),
71 scope: Scope::from_spec(spec)?,
72 min_bytes: opts.min_bytes,
73 }))
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79 use crate::test_support::{ctx, spec_yaml};
80 use alint_core::{FileEntry, FileIndex};
81 use std::path::Path;
82
83 fn idx_with_size(path: &str, size: u64) -> FileIndex {
84 FileIndex::from_entries(vec![FileEntry {
85 path: std::path::Path::new(path).into(),
86 is_dir: false,
87 size,
88 }])
89 }
90
91 #[test]
92 fn build_rejects_missing_paths_field() {
93 let spec = spec_yaml(
94 "id: t\n\
95 kind: file_min_size\n\
96 min_bytes: 100\n\
97 level: warning\n",
98 );
99 assert!(build(&spec).is_err());
100 }
101
102 #[test]
103 fn evaluate_passes_when_size_above_minimum() {
104 let spec = spec_yaml(
105 "id: t\n\
106 kind: file_min_size\n\
107 paths: \"README.md\"\n\
108 min_bytes: 100\n\
109 level: warning\n",
110 );
111 let rule = build(&spec).unwrap();
112 let idx = idx_with_size("README.md", 1024);
113 let v = rule.evaluate(&ctx(Path::new("/fake"), &idx)).unwrap();
114 assert!(v.is_empty());
115 }
116
117 #[test]
118 fn evaluate_fires_when_size_below_minimum() {
119 let spec = spec_yaml(
120 "id: t\n\
121 kind: file_min_size\n\
122 paths: \"README.md\"\n\
123 min_bytes: 100\n\
124 level: warning\n",
125 );
126 let rule = build(&spec).unwrap();
127 let idx = idx_with_size("README.md", 10);
128 let v = rule.evaluate(&ctx(Path::new("/fake"), &idx)).unwrap();
129 assert_eq!(v.len(), 1);
130 }
131
132 #[test]
133 fn evaluate_size_at_exact_minimum_passes() {
134 let spec = spec_yaml(
137 "id: t\n\
138 kind: file_min_size\n\
139 paths: \"a.bin\"\n\
140 min_bytes: 100\n\
141 level: warning\n",
142 );
143 let rule = build(&spec).unwrap();
144 let idx = idx_with_size("a.bin", 100);
145 let v = rule.evaluate(&ctx(Path::new("/fake"), &idx)).unwrap();
146 assert!(v.is_empty(), "size == min should pass: {v:?}");
147 }
148
149 #[test]
150 fn evaluate_zero_byte_file_fires_when_minimum_positive() {
151 let spec = spec_yaml(
152 "id: t\n\
153 kind: file_min_size\n\
154 paths: \"empty.txt\"\n\
155 min_bytes: 1\n\
156 level: warning\n",
157 );
158 let rule = build(&spec).unwrap();
159 let idx = idx_with_size("empty.txt", 0);
160 let v = rule.evaluate(&ctx(Path::new("/fake"), &idx)).unwrap();
161 assert_eq!(v.len(), 1);
162 }
163
164 #[test]
165 fn scope_filter_narrows() {
166 let spec = spec_yaml(
169 "id: t\n\
170 kind: file_min_size\n\
171 paths: \"**/*.txt\"\n\
172 min_bytes: 100\n\
173 scope_filter:\n \
174 has_ancestor: marker.lock\n\
175 level: warning\n",
176 );
177 let rule = build(&spec).unwrap();
178 let idx = FileIndex::from_entries(vec![
179 FileEntry {
180 path: std::path::Path::new("pkg/marker.lock").into(),
181 is_dir: false,
182 size: 1,
183 },
184 FileEntry {
185 path: std::path::Path::new("pkg/small.txt").into(),
186 is_dir: false,
187 size: 5,
188 },
189 FileEntry {
190 path: std::path::Path::new("other/small.txt").into(),
191 is_dir: false,
192 size: 5,
193 },
194 ]);
195 let v = rule.evaluate(&ctx(Path::new("/fake"), &idx)).unwrap();
196 assert_eq!(v.len(), 1, "only in-scope file should fire: {v:?}");
197 assert_eq!(v[0].path.as_deref(), Some(Path::new("pkg/small.txt")));
198 }
199}