alint_rules/
dir_exists.rs1use alint_core::{Context, Error, Level, PathsSpec, Result, Rule, RuleSpec, Scope, Violation};
4
5#[derive(Debug)]
6pub struct DirExistsRule {
7 id: String,
8 level: Level,
9 policy_url: Option<String>,
10 message: Option<String>,
11 scope: Scope,
12 patterns: Vec<String>,
13 git_tracked_only: bool,
18}
19
20impl Rule for DirExistsRule {
21 fn id(&self) -> &str {
22 &self.id
23 }
24 fn level(&self) -> Level {
25 self.level
26 }
27 fn policy_url(&self) -> Option<&str> {
28 self.policy_url.as_deref()
29 }
30 fn wants_git_tracked(&self) -> bool {
31 self.git_tracked_only
32 }
33
34 fn requires_full_index(&self) -> bool {
35 true
42 }
43
44 fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
45 let found = ctx.index.dirs().any(|entry| {
46 if !self.scope.matches(&entry.path) {
47 return false;
48 }
49 if self.git_tracked_only && !ctx.dir_has_tracked_files(&entry.path) {
50 return false;
51 }
52 true
53 });
54 if found {
55 Ok(Vec::new())
56 } else {
57 let msg = self.message.clone().unwrap_or_else(|| {
58 let tracked = if self.git_tracked_only {
59 " (with tracked content)"
60 } else {
61 ""
62 };
63 format!(
64 "expected a directory matching [{}]{tracked}",
65 self.patterns.join(", ")
66 )
67 });
68 Ok(vec![Violation::new(msg)])
69 }
70 }
71}
72
73pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
74 alint_core::reject_scope_filter_on_cross_file(spec, "dir_exists")?;
75 let Some(paths) = &spec.paths else {
76 return Err(Error::rule_config(
77 &spec.id,
78 "dir_exists requires a `paths` field",
79 ));
80 };
81 Ok(Box::new(DirExistsRule {
82 id: spec.id.clone(),
83 level: spec.level,
84 policy_url: spec.policy_url.clone(),
85 message: spec.message.clone(),
86 scope: Scope::from_paths_spec(paths)?,
87 patterns: patterns_of(paths),
88 git_tracked_only: spec.git_tracked_only,
89 }))
90}
91
92fn patterns_of(spec: &PathsSpec) -> Vec<String> {
93 match spec {
94 PathsSpec::Single(s) => vec![s.clone()],
95 PathsSpec::Many(v) => v.clone(),
96 PathsSpec::IncludeExclude { include, .. } => include.clone(),
97 }
98}
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103 use crate::test_support::{ctx, index_with_dirs, spec_yaml};
104 use std::path::Path;
105
106 #[test]
107 fn build_rejects_missing_paths_field() {
108 let spec = spec_yaml(
109 "id: t\n\
110 kind: dir_exists\n\
111 level: error\n",
112 );
113 let err = build(&spec).unwrap_err().to_string();
114 assert!(err.contains("paths"), "unexpected: {err}");
115 }
116
117 #[test]
118 fn evaluate_passes_when_matching_dir_present() {
119 let spec = spec_yaml(
120 "id: t\n\
121 kind: dir_exists\n\
122 paths: \"docs\"\n\
123 level: error\n",
124 );
125 let rule = build(&spec).unwrap();
126 let idx = index_with_dirs(&[("docs", true), ("docs/README.md", false)]);
127 let v = rule.evaluate(&ctx(Path::new("/fake"), &idx)).unwrap();
128 assert!(v.is_empty(), "unexpected: {v:?}");
129 }
130
131 #[test]
132 fn evaluate_fires_when_directory_missing() {
133 let spec = spec_yaml(
134 "id: t\n\
135 kind: dir_exists\n\
136 paths: \"docs\"\n\
137 level: error\n",
138 );
139 let rule = build(&spec).unwrap();
140 let idx = index_with_dirs(&[("README.md", false), ("src", true)]);
141 let v = rule.evaluate(&ctx(Path::new("/fake"), &idx)).unwrap();
142 assert_eq!(v.len(), 1, "missing dir should fire one violation");
143 }
144
145 #[test]
146 fn evaluate_skips_files_when_dir_glob_only_matches_dirs() {
147 let spec = spec_yaml(
150 "id: t\n\
151 kind: dir_exists\n\
152 paths: \"docs\"\n\
153 level: error\n",
154 );
155 let rule = build(&spec).unwrap();
156 let idx = index_with_dirs(&[("docs", false)]); let v = rule.evaluate(&ctx(Path::new("/fake"), &idx)).unwrap();
158 assert_eq!(v.len(), 1);
159 }
160
161 #[test]
162 fn rule_advertises_full_index_requirement() {
163 let spec = spec_yaml(
164 "id: t\n\
165 kind: dir_exists\n\
166 paths: \"docs\"\n\
167 level: error\n",
168 );
169 let rule = build(&spec).unwrap();
170 assert!(rule.requires_full_index());
171 }
172
173 #[test]
174 fn git_tracked_only_propagates_to_wants_git_tracked() {
175 let spec = spec_yaml(
176 "id: t\n\
177 kind: dir_exists\n\
178 paths: \"src\"\n\
179 level: error\n\
180 git_tracked_only: true\n",
181 );
182 let rule = build(&spec).unwrap();
183 assert!(rule.wants_git_tracked());
184 }
185
186 #[test]
187 fn build_rejects_scope_filter_on_cross_file_rule() {
188 let yaml = r#"
193id: t
194kind: dir_exists
195paths: "docs"
196level: error
197scope_filter:
198 has_ancestor: Cargo.toml
199"#;
200 let spec = spec_yaml(yaml);
201 let err = build(&spec).unwrap_err().to_string();
202 assert!(
203 err.contains("scope_filter is supported on per-file rules only"),
204 "expected per-file-only message, got: {err}",
205 );
206 assert!(
207 err.contains("dir_exists"),
208 "expected message to name the cross-file kind, got: {err}",
209 );
210 }
211}