alint_rules/
no_submodules.rs1use alint_core::{
21 Context, Error, FixSpec, Fixer, Level, PathsSpec, Result, Rule, RuleSpec, Scope, Violation,
22 reject_scope_filter_with_reason,
23};
24
25use crate::fixers::FileRemoveFixer;
26
27#[derive(Debug)]
28pub struct NoSubmodulesRule {
29 id: String,
30 level: Level,
31 policy_url: Option<String>,
32 message: Option<String>,
33 scope: Scope,
34 fixer: Option<FileRemoveFixer>,
35}
36
37impl Rule for NoSubmodulesRule {
38 alint_core::rule_common_impl!();
39
40 fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
41 let mut violations = Vec::new();
42 for entry in ctx.index.files() {
43 if !self.scope.matches(&entry.path, ctx.index) {
44 continue;
45 }
46 let msg = self.message.clone().unwrap_or_else(|| {
47 "`.gitmodules` present — git submodules are forbidden".to_string()
48 });
49 violations.push(Violation::new(msg).with_path(entry.path.clone()));
50 }
51 Ok(violations)
52 }
53
54 fn fixer(&self) -> Option<&dyn Fixer> {
55 self.fixer.as_ref().map(|f| f as &dyn Fixer)
56 }
57}
58
59pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
60 if spec.paths.is_some() {
61 return Err(Error::rule_config(
62 &spec.id,
63 "no_submodules does not accept a `paths` field; it always targets `.gitmodules` at \
64 the repo root. Use `file_absent` for more general patterns.",
65 ));
66 }
67 reject_scope_filter_with_reason(
68 spec,
69 "no_submodules",
70 "this rule is hardcoded to inspect `.gitmodules` at the repo root and does not iterate \
71 the file index",
72 )?;
73 let fixer = match &spec.fix {
74 Some(FixSpec::FileRemove { .. }) => Some(FileRemoveFixer),
75 Some(other) => {
76 return Err(Error::rule_config(
77 &spec.id,
78 format!(
79 "fix.{} is not compatible with no_submodules",
80 other.op_name()
81 ),
82 ));
83 }
84 None => None,
85 };
86 let pinned = PathsSpec::Single(".gitmodules".to_string());
87 Ok(Box::new(NoSubmodulesRule {
88 id: spec.id.clone(),
89 level: spec.level,
90 policy_url: spec.policy_url.clone(),
91 message: spec.message.clone(),
92 scope: Scope::from_paths_spec(&pinned)?,
93 fixer,
94 }))
95}
96
97#[cfg(test)]
98mod tests {
99 use super::*;
100 use crate::test_support::{ctx, index, spec_yaml};
101 use std::path::Path;
102
103 #[test]
104 fn build_rejects_paths_field() {
105 let spec = spec_yaml(
106 "id: t\n\
107 kind: no_submodules\n\
108 paths: \"**\"\n\
109 level: warning\n",
110 );
111 let err = build(&spec).unwrap_err().to_string();
112 assert!(err.contains("paths"), "unexpected: {err}");
113 }
114
115 #[test]
116 fn build_accepts_minimal_config() {
117 let spec = spec_yaml(
118 "id: t\n\
119 kind: no_submodules\n\
120 level: warning\n",
121 );
122 assert!(build(&spec).is_ok());
123 }
124
125 #[test]
126 fn build_accepts_file_remove_fix() {
127 let spec = spec_yaml(
128 "id: t\n\
129 kind: no_submodules\n\
130 level: warning\n\
131 fix:\n \
132 file_remove: {}\n",
133 );
134 let rule = build(&spec).unwrap();
135 assert!(rule.fixer().is_some());
136 }
137
138 #[test]
139 fn build_rejects_incompatible_fix() {
140 let spec = spec_yaml(
141 "id: t\n\
142 kind: no_submodules\n\
143 level: warning\n\
144 fix:\n \
145 file_create:\n \
146 content: \"x\"\n",
147 );
148 assert!(build(&spec).is_err());
149 }
150
151 #[test]
152 fn evaluate_passes_without_gitmodules() {
153 let spec = spec_yaml(
154 "id: t\n\
155 kind: no_submodules\n\
156 level: warning\n",
157 );
158 let rule = build(&spec).unwrap();
159 let idx = index(&["README.md", ".gitignore"]);
160 let v = rule.evaluate(&ctx(Path::new("/fake"), &idx)).unwrap();
161 assert!(v.is_empty());
162 }
163
164 #[test]
165 fn evaluate_fires_when_gitmodules_present() {
166 let spec = spec_yaml(
167 "id: t\n\
168 kind: no_submodules\n\
169 level: warning\n",
170 );
171 let rule = build(&spec).unwrap();
172 let idx = index(&[".gitmodules", "README.md"]);
173 let v = rule.evaluate(&ctx(Path::new("/fake"), &idx)).unwrap();
174 assert_eq!(v.len(), 1);
175 }
176
177 #[test]
178 fn build_rejects_scope_filter() {
179 let spec = spec_yaml(
183 "id: t\n\
184 kind: no_submodules\n\
185 scope_filter:\n \
186 has_ancestor: marker.lock\n\
187 level: warning\n",
188 );
189 let err = build(&spec).unwrap_err().to_string();
190 assert!(
191 err.contains("scope_filter is not supported on no_submodules"),
192 "unexpected error: {err}"
193 );
194 }
195}