syncable_cli/analyzer/hadolint/rules/
dl3039.rs1use 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 "DL3039",
13 Severity::Warning,
14 "Do not use `dnf update`.",
15 |instr, shell| match instr {
16 Instruction::Run(_) => {
17 if let Some(shell) = shell {
18 !shell.any_command(|cmd| {
19 cmd.name == "dnf" && cmd.has_any_arg(&["update", "upgrade"])
20 })
21 } else {
22 true
23 }
24 }
25 _ => true,
26 },
27 )
28}
29
30#[cfg(test)]
31mod tests {
32 use super::*;
33 use crate::analyzer::hadolint::config::HadolintConfig;
34 use crate::analyzer::hadolint::lint::{LintResult, lint};
35
36 fn lint_dockerfile(content: &str) -> LintResult {
37 lint(content, &HadolintConfig::default())
38 }
39
40 #[test]
41 fn test_dnf_update() {
42 let result = lint_dockerfile("FROM fedora:latest\nRUN dnf update -y");
43 assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3039"));
44 }
45
46 #[test]
47 fn test_dnf_install() {
48 let result = lint_dockerfile("FROM fedora:latest\nRUN dnf install -y nginx");
49 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3039"));
50 }
51}