syncable_cli/analyzer/hadolint/rules/
dl3053.rs

1//! DL3053: Label `org.opencontainers.image.title` is empty
2//!
3//! The title label should not be empty.
4
5use crate::analyzer::hadolint::parser::instruction::Instruction;
6use crate::analyzer::hadolint::rules::{simple_rule, SimpleRule};
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        "DL3053",
13        Severity::Warning,
14        "Label `org.opencontainers.image.title` is empty.",
15        |instr, _shell| {
16            match instr {
17                Instruction::Label(pairs) => {
18                    for (key, value) in pairs {
19                        if key == "org.opencontainers.image.title" && value.trim().is_empty() {
20                            return false;
21                        }
22                    }
23                    true
24                }
25                _ => true,
26            }
27        },
28    )
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34    use crate::analyzer::hadolint::lint::{lint, LintResult};
35    use crate::analyzer::hadolint::config::HadolintConfig;
36
37    fn lint_dockerfile(content: &str) -> LintResult {
38        lint(content, &HadolintConfig::default())
39    }
40
41    #[test]
42    fn test_valid_title() {
43        let result = lint_dockerfile("FROM ubuntu:20.04\nLABEL org.opencontainers.image.title=\"My App\"");
44        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3053"));
45    }
46
47    #[test]
48    fn test_empty_title() {
49        let result = lint_dockerfile("FROM ubuntu:20.04\nLABEL org.opencontainers.image.title=\"\"");
50        assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3053"));
51    }
52}