syncable_cli/analyzer/hadolint/rules/
dl3027.rs

1//! DL3027: Do not use apt as it is meant for interactive use
2//!
3//! apt is designed for interactive use. apt-get is more stable for scripts
4//! and Dockerfiles.
5
6use crate::analyzer::hadolint::parser::instruction::Instruction;
7use crate::analyzer::hadolint::rules::{SimpleRule, simple_rule};
8use crate::analyzer::hadolint::shell::ParsedShell;
9use crate::analyzer::hadolint::types::Severity;
10
11pub fn rule() -> SimpleRule<impl Fn(&Instruction, Option<&ParsedShell>) -> bool + Send + Sync> {
12    simple_rule(
13        "DL3027",
14        Severity::Warning,
15        "Do not use apt as it is meant to be an end-user tool, use apt-get or apt-cache instead",
16        |instr, shell| match instr {
17            Instruction::Run(_) => {
18                if let Some(shell) = shell {
19                    !shell.any_command(|cmd| cmd.name == "apt")
20                } else {
21                    true
22                }
23            }
24            _ => true,
25        },
26    )
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32    use crate::analyzer::hadolint::parser::instruction::RunArgs;
33    use crate::analyzer::hadolint::rules::{Rule, RuleState};
34
35    #[test]
36    fn test_apt_get() {
37        let rule = rule();
38        let mut state = RuleState::new();
39
40        let instr = Instruction::Run(RunArgs::shell("apt-get update"));
41        let shell = ParsedShell::parse("apt-get update");
42        rule.check(&mut state, 1, &instr, Some(&shell));
43        assert!(state.failures.is_empty());
44    }
45
46    #[test]
47    fn test_apt() {
48        let rule = rule();
49        let mut state = RuleState::new();
50
51        let instr = Instruction::Run(RunArgs::shell("apt update"));
52        let shell = ParsedShell::parse("apt update");
53        rule.check(&mut state, 1, &instr, Some(&shell));
54        assert_eq!(state.failures.len(), 1);
55        assert_eq!(state.failures[0].code.as_str(), "DL3027");
56    }
57}