syncable_cli/analyzer/hadolint/rules/
dl3036.rs

1//! DL3036: zypper clean missing after zypper install
2//!
3//! Clean up zypper cache after installing packages.
4
5use crate::analyzer::hadolint::parser::instruction::Instruction;
6use crate::analyzer::hadolint::rules::{SimpleRule, simple_rule};
7use crate::analyzer::hadolint::shell::ParsedShell;
8use crate::analyzer::hadolint::types::Severity;
9
10pub fn rule() -> SimpleRule<impl Fn(&Instruction, Option<&ParsedShell>) -> bool + Send + Sync> {
11    simple_rule(
12        "DL3036",
13        Severity::Warning,
14        "`zypper clean` missing after zypper install.",
15        |instr, shell| match instr {
16            Instruction::Run(_) => {
17                if let Some(shell) = shell {
18                    let has_install = shell.any_command(|cmd| {
19                        cmd.name == "zypper" && cmd.has_any_arg(&["install", "in"])
20                    });
21
22                    if !has_install {
23                        return true;
24                    }
25
26                    shell.any_command(|cmd| {
27                        (cmd.name == "zypper" && cmd.has_any_arg(&["clean", "cc"]))
28                            || (cmd.name == "rm"
29                                && cmd.arguments.iter().any(|a| a.contains("/var/cache/zypp")))
30                    })
31                } else {
32                    true
33                }
34            }
35            _ => true,
36        },
37    )
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43    use crate::analyzer::hadolint::config::HadolintConfig;
44    use crate::analyzer::hadolint::lint::{LintResult, lint};
45
46    fn lint_dockerfile(content: &str) -> LintResult {
47        lint(content, &HadolintConfig::default())
48    }
49
50    #[test]
51    fn test_zypper_without_clean() {
52        let result = lint_dockerfile("FROM opensuse:latest\nRUN zypper -n install nginx");
53        assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3036"));
54    }
55
56    #[test]
57    fn test_zypper_with_clean() {
58        let result =
59            lint_dockerfile("FROM opensuse:latest\nRUN zypper -n install nginx && zypper clean");
60        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3036"));
61    }
62}