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::{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        "DL3034",
13        Severity::Warning,
14        "Non-interactive switch missing from `zypper` command: `-n`.",
15        |instr, shell| match instr {
16            Instruction::Run(_) => {
17                if let Some(shell) = shell {
18                    !shell.any_command(|cmd| {
19                        if cmd.name == "zypper" {
20                            !cmd.has_any_flag(&["n", "non-interactive"])
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_n() {
46        let result = lint_dockerfile("FROM opensuse:latest\nRUN zypper refresh");
47        assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3034"));
48    }
49
50    #[test]
51    fn test_zypper_with_n() {
52        let result = lint_dockerfile("FROM opensuse:latest\nRUN zypper -n refresh");
53        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3034"));
54    }
55}