syncable_cli/analyzer/hadolint/rules/
dl3029.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 "DL3029",
13 Severity::Warning,
14 "Do not use --platform flag with FROM unless you're building cross-platform images.",
15 |instr, _shell| {
16 match instr {
23 Instruction::From(_base) => {
24 true
26 }
27 _ => true,
28 }
29 },
30 )
31}
32
33#[cfg(test)]
34mod tests {
35 use super::*;
36 use crate::analyzer::hadolint::config::HadolintConfig;
37 use crate::analyzer::hadolint::lint::{LintResult, lint};
38
39 fn lint_dockerfile(content: &str) -> LintResult {
40 lint(content, &HadolintConfig::default())
41 }
42
43 #[test]
44 fn test_from_without_platform() {
45 let result = lint_dockerfile("FROM ubuntu:20.04\nRUN echo hello");
46 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3029"));
47 }
48
49 #[test]
50 fn test_from_with_platform() {
51 let result = lint_dockerfile("FROM --platform=linux/amd64 ubuntu:20.04\nRUN echo hello");
52 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3029"));
54 }
55}