syncable_cli/analyzer/hadolint/rules/
dl3043.rs

1//! DL3043: ONBUILD ONBUILD is not allowed
2//!
3//! Nested ONBUILD instructions are not allowed.
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        "DL3043",
13        Severity::Error,
14        "`ONBUILD` combined with `ONBUILD` is not allowed.",
15        |instr, _shell| match instr {
16            Instruction::OnBuild(inner) => !matches!(inner.as_ref(), Instruction::OnBuild(_)),
17            _ => true,
18        },
19    )
20}
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25    use crate::analyzer::hadolint::parser::instruction::{Arguments, RunArgs, RunFlags};
26    use crate::analyzer::hadolint::rules::{Rule, RuleState};
27
28    #[test]
29    fn test_nested_onbuild() {
30        let rule = rule();
31        let mut state = RuleState::new();
32
33        // ONBUILD ONBUILD RUN echo hello
34        let inner_run = Instruction::Run(RunArgs {
35            arguments: Arguments::Text("echo hello".to_string()),
36            flags: RunFlags::default(),
37        });
38        let inner_onbuild = Instruction::OnBuild(Box::new(inner_run));
39        let instr = Instruction::OnBuild(Box::new(inner_onbuild));
40
41        rule.check(&mut state, 1, &instr, None);
42        assert_eq!(state.failures.len(), 1);
43        assert_eq!(state.failures[0].code.as_str(), "DL3043");
44    }
45
46    #[test]
47    fn test_valid_onbuild() {
48        let rule = rule();
49        let mut state = RuleState::new();
50
51        // ONBUILD RUN echo hello
52        let inner = Instruction::Run(RunArgs {
53            arguments: Arguments::Text("echo hello".to_string()),
54            flags: RunFlags::default(),
55        });
56        let instr = Instruction::OnBuild(Box::new(inner));
57
58        rule.check(&mut state, 1, &instr, None);
59        assert!(state.failures.is_empty());
60    }
61}