use crate::analyzer::hadolint::parser::instruction::Instruction;
use crate::analyzer::hadolint::rules::{CustomRule, RuleState, custom_rule};
use crate::analyzer::hadolint::shell::ParsedShell;
use crate::analyzer::hadolint::types::Severity;
pub fn rule()
-> CustomRule<impl Fn(&mut RuleState, u32, &Instruction, Option<&ParsedShell>) + Send + Sync> {
custom_rule(
"DL3059",
Severity::Info,
"Multiple consecutive `RUN` instructions. Consider consolidation.",
|state, line, instr, _shell| {
match instr {
Instruction::From(_) => {
state.data.set_int("consecutive_runs", 0);
state.data.set_int("last_run_line", 0);
}
Instruction::Run(_) => {
let consecutive = state.data.get_int("consecutive_runs");
state.data.set_int("consecutive_runs", consecutive + 1);
state.data.set_int("last_run_line", line as i64);
if consecutive >= 1 {
state.add_failure(
"DL3059",
Severity::Info,
"Multiple consecutive `RUN` instructions. Consider consolidation.",
line,
);
}
}
_ => {
state.data.set_int("consecutive_runs", 0);
}
}
},
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::analyzer::hadolint::config::HadolintConfig;
use crate::analyzer::hadolint::lint::{LintResult, lint};
fn lint_dockerfile(content: &str) -> LintResult {
lint(content, &HadolintConfig::default())
}
#[test]
fn test_consecutive_runs() {
let result =
lint_dockerfile("FROM ubuntu:20.04\nRUN apt-get update\nRUN apt-get install -y nginx");
assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3059"));
}
#[test]
fn test_single_run() {
let result =
lint_dockerfile("FROM ubuntu:20.04\nRUN apt-get update && apt-get install -y nginx");
assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3059"));
}
#[test]
fn test_runs_separated_by_other() {
let result = lint_dockerfile(
"FROM ubuntu:20.04\nRUN apt-get update\nENV DEBIAN_FRONTEND=noninteractive\nRUN apt-get install -y nginx",
);
assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3059"));
}
#[test]
fn test_three_consecutive_runs() {
let result = lint_dockerfile("FROM ubuntu:20.04\nRUN echo 1\nRUN echo 2\nRUN echo 3");
let count = result
.failures
.iter()
.filter(|f| f.code.as_str() == "DL3059")
.count();
assert_eq!(count, 2);
}
#[test]
fn test_different_stages() {
let result = lint_dockerfile(
"FROM ubuntu:20.04 AS stage1\nRUN echo 1\nFROM ubuntu:20.04 AS stage2\nRUN echo 2",
);
assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3059"));
}
}