syncable_cli/analyzer/hadolint/rules/
dl3030.rs

1//! DL3030: Use the --yes switch to avoid prompts for zypper install
2//!
3//! zypper install should use --non-interactive or -n to avoid prompts.
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        "DL3030",
13        Severity::Warning,
14        "Use the `--non-interactive` switch to avoid prompts during `zypper` install.",
15        |instr, shell| match instr {
16            Instruction::Run(_) => {
17                if let Some(shell) = shell {
18                    !shell.any_command(|cmd| {
19                        if cmd.name == "zypper" && cmd.has_any_arg(&["install", "in"]) {
20                            !cmd.has_any_flag(&["n", "non-interactive", "no-confirm", "y"])
21                        } else {
22                            false
23                        }
24                    })
25                } else {
26                    true
27                }
28            }
29            _ => true,
30        },
31    )
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37    use crate::analyzer::hadolint::config::HadolintConfig;
38    use crate::analyzer::hadolint::lint::{LintResult, lint};
39
40    fn lint_dockerfile(content: &str) -> LintResult {
41        lint(content, &HadolintConfig::default())
42    }
43
44    #[test]
45    fn test_zypper_without_flag() {
46        let result = lint_dockerfile("FROM opensuse:latest\nRUN zypper install nginx");
47        assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3030"));
48    }
49
50    #[test]
51    fn test_zypper_with_n() {
52        let result = lint_dockerfile("FROM opensuse:latest\nRUN zypper -n install nginx");
53        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3030"));
54    }
55
56    #[test]
57    fn test_zypper_with_non_interactive() {
58        let result =
59            lint_dockerfile("FROM opensuse:latest\nRUN zypper --non-interactive install nginx");
60        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3030"));
61    }
62}