Skip to main content

alint_rules/
file_is_text.rs

1//! `file_is_text` — every file in scope must be detected as text (not binary).
2//!
3//! Detection uses `content_inspector` on the first 8 KiB of each file
4//! (magic-byte + heuristic analysis). UTF-8, UTF-16 (with BOM), and plain
5//! 7-bit ASCII are treated as text.
6
7use alint_core::{Context, Error, Level, Result, Rule, RuleSpec, Scope, Violation};
8
9use crate::io::{Classification, classify_bytes, read_prefix};
10
11#[derive(Debug)]
12pub struct FileIsTextRule {
13    id: String,
14    level: Level,
15    policy_url: Option<String>,
16    message: Option<String>,
17    scope: Scope,
18}
19
20impl Rule for FileIsTextRule {
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
31    fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
32        let mut violations = Vec::new();
33        for entry in ctx.index.files() {
34            if !self.scope.matches(&entry.path) {
35                continue;
36            }
37            if entry.size == 0 {
38                // Empty files are text by convention.
39                continue;
40            }
41            let full = ctx.root.join(&entry.path);
42            let bytes = match read_prefix(&full) {
43                Ok(b) => b,
44                Err(e) => {
45                    violations.push(
46                        Violation::new(format!("could not read file: {e}")).with_path(&entry.path),
47                    );
48                    continue;
49                }
50            };
51            if classify_bytes(&bytes) == Classification::Binary {
52                let msg = self.message.clone().unwrap_or_else(|| {
53                    "file is detected as binary; text is required here".to_string()
54                });
55                violations.push(Violation::new(msg).with_path(&entry.path));
56            }
57        }
58        Ok(violations)
59    }
60}
61
62pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
63    let Some(paths) = &spec.paths else {
64        return Err(Error::rule_config(
65            &spec.id,
66            "file_is_text requires a `paths` field",
67        ));
68    };
69    Ok(Box::new(FileIsTextRule {
70        id: spec.id.clone(),
71        level: spec.level,
72        policy_url: spec.policy_url.clone(),
73        message: spec.message.clone(),
74        scope: Scope::from_paths_spec(paths)?,
75    }))
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81    use crate::test_support::{ctx, spec_yaml, tempdir_with_files};
82
83    #[test]
84    fn build_rejects_missing_paths_field() {
85        let spec = spec_yaml(
86            "id: t\n\
87             kind: file_is_text\n\
88             level: warning\n",
89        );
90        assert!(build(&spec).is_err());
91    }
92
93    #[test]
94    fn evaluate_passes_on_utf8_text() {
95        let spec = spec_yaml(
96            "id: t\n\
97             kind: file_is_text\n\
98             paths: \"**/*.rs\"\n\
99             level: warning\n",
100        );
101        let rule = build(&spec).unwrap();
102        let (tmp, idx) = tempdir_with_files(&[("a.rs", b"// hello\nfn main() {}\n")]);
103        let v = rule.evaluate(&ctx(tmp.path(), &idx)).unwrap();
104        assert!(v.is_empty(), "utf-8 text should pass: {v:?}");
105    }
106
107    #[test]
108    fn evaluate_fires_on_binary_content() {
109        let spec = spec_yaml(
110            "id: t\n\
111             kind: file_is_text\n\
112             paths: \"**/*\"\n\
113             level: warning\n",
114        );
115        let rule = build(&spec).unwrap();
116        // Bytes with NUL + binary tail; content_inspector
117        // should classify as Binary.
118        let mut binary = vec![0u8; 16];
119        binary.extend_from_slice(&[0xff, 0xfe, 0xfd, 0xfc]);
120        let (tmp, idx) = tempdir_with_files(&[("img.bin", &binary)]);
121        let v = rule.evaluate(&ctx(tmp.path(), &idx)).unwrap();
122        assert_eq!(v.len(), 1, "binary should fire: {v:?}");
123    }
124
125    #[test]
126    fn evaluate_silent_on_zero_byte_file() {
127        // Empty files are treated as text by convention —
128        // no read needed, no violation.
129        let spec = spec_yaml(
130            "id: t\n\
131             kind: file_is_text\n\
132             paths: \"**/*\"\n\
133             level: warning\n",
134        );
135        let rule = build(&spec).unwrap();
136        let (tmp, idx) = tempdir_with_files(&[("empty", b"")]);
137        let v = rule.evaluate(&ctx(tmp.path(), &idx)).unwrap();
138        assert!(v.is_empty());
139    }
140
141    #[test]
142    fn evaluate_skips_out_of_scope_files() {
143        let spec = spec_yaml(
144            "id: t\n\
145             kind: file_is_text\n\
146             paths: \"src/**/*.rs\"\n\
147             level: warning\n",
148        );
149        let rule = build(&spec).unwrap();
150        let (tmp, idx) = tempdir_with_files(&[("img.bin", &[0u8; 64])]);
151        let v = rule.evaluate(&ctx(tmp.path(), &idx)).unwrap();
152        assert!(v.is_empty(), "out-of-scope shouldn't fire: {v:?}");
153    }
154}