syncable_cli/analyzer/hadolint/rules/
dl3030.rs1use 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 "DL3030",
13 Severity::Warning,
14 "Use the `--non-interactive` switch to avoid prompts during `zypper` install.",
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" && cmd.has_any_arg(&["install", "in"]) {
21 !cmd.has_any_flag(&["n", "non-interactive", "no-confirm", "y"])
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_flag() {
48 let result = lint_dockerfile("FROM opensuse:latest\nRUN zypper install nginx");
49 assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3030"));
50 }
51
52 #[test]
53 fn test_zypper_with_n() {
54 let result = lint_dockerfile("FROM opensuse:latest\nRUN zypper -n install nginx");
55 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3030"));
56 }
57
58 #[test]
59 fn test_zypper_with_non_interactive() {
60 let result = lint_dockerfile("FROM opensuse:latest\nRUN zypper --non-interactive install nginx");
61 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3030"));
62 }
63}