syncable_cli/analyzer/hadolint/rules/
dl3034.rs

1//! DL3034: Non-interactive switch missing from zypper command
2//!
3//! zypper commands should use -n or --non-interactive.
4
5use crate::analyzer::hadolint::parser::instruction::Instruction;
6use crate::analyzer::hadolint::rules::{simple_rule, SimpleRule};
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        "DL3034",
13        Severity::Warning,
14        "Non-interactive switch missing from `zypper` command: `-n`.",
15        |instr, shell| {
16            match instr {
17                Instruction::Run(_) => {
18                    if let Some(shell) = shell {
19                        !shell.any_command(|cmd| {
20                            if cmd.name == "zypper" {
21                                !cmd.has_any_flag(&["n", "non-interactive"])
22                            } else {
23                                false
24                            }
25                        })
26                    } else {
27                        true
28                    }
29                }
30                _ => true,
31            }
32        },
33    )
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use crate::analyzer::hadolint::lint::{lint, LintResult};
40    use crate::analyzer::hadolint::config::HadolintConfig;
41
42    fn lint_dockerfile(content: &str) -> LintResult {
43        lint(content, &HadolintConfig::default())
44    }
45
46    #[test]
47    fn test_zypper_without_n() {
48        let result = lint_dockerfile("FROM opensuse:latest\nRUN zypper refresh");
49        assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3034"));
50    }
51
52    #[test]
53    fn test_zypper_with_n() {
54        let result = lint_dockerfile("FROM opensuse:latest\nRUN zypper -n refresh");
55        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3034"));
56    }
57}