syncable_cli/analyzer/hadolint/rules/
dl3038.rs

1//! DL3038: Use the -y switch to avoid prompts for dnf install
2//!
3//! dnf install should use -y 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        "DL3038",
13        Severity::Warning,
14        "Use the `-y` switch to avoid prompts during `dnf install`.",
15        |instr, shell| match instr {
16            Instruction::Run(_) => {
17                if let Some(shell) = shell {
18                    !shell.any_command(|cmd| {
19                        if cmd.name == "dnf" && cmd.has_any_arg(&["install"]) {
20                            !cmd.has_any_flag(&["y", "yes", "assumeyes"])
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_dnf_without_y() {
46        let result = lint_dockerfile("FROM fedora:latest\nRUN dnf install nginx");
47        assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3038"));
48    }
49
50    #[test]
51    fn test_dnf_with_y() {
52        let result = lint_dockerfile("FROM fedora:latest\nRUN dnf install -y nginx");
53        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3038"));
54    }
55}