use super::capture;
use crate::cli::CliCommand;
use crate::cli::testing::{FlagRow, build_error, check, check_flags};
use crate::command::{Lint, LintReport, LintSummary, Severity};
use crate::error::Error;
use crate::values::Values;
#[test]
fn lint_flags_map_to_argv() {
let table: [FlagRow<Lint>; 7] = [
("none", |_| {}, &[]),
(
"values",
|c| c.values = Values::new().file("v.yaml").set("a", "b"),
&["-f", "v.yaml", "--set", "a=b"],
),
(
"kube-version",
|c| c.kube_version = Some("1.30.0".into()),
&["--kube-version", "1.30.0"],
),
("quiet", |c| c.quiet = true, &["--quiet"]),
(
"skip-schema-validation",
|c| c.skip_schema_validation = true,
&["--skip-schema-validation"],
),
("strict", |c| c.strict = true, &["--strict"]),
(
"with-subcharts",
|c| c.with_subcharts = true,
&["--with-subcharts"],
),
];
check_flags(&Lint::new("./a"), &["lint", "./a"], &[], &table);
check(vec![(
Lint {
paths: vec!["./a".into(), "./b".into()],
..Lint::new("x")
},
&["lint", "./a", "./b"],
None,
)]);
}
#[test]
fn lint_needs_a_path() {
let command = Lint {
paths: Vec::new(),
..Lint::new("x")
};
assert!(matches!(build_error(&command), Error::InvalidCommand(_)));
}
type Expected = (&'static str, bool, &'static [(Severity, &'static str)]);
fn report(command: &Lint, name: &str, exit_zero: bool) -> LintReport {
let stdout = capture(&format!("lint-{name}.stdout"));
if exit_zero {
return command.parse(stdout).expect("parses");
}
command
.parse_failure(&stdout, &capture(&format!("lint-{name}.stderr")))
.expect("a lint failure with findings")
.expect("parses")
}
fn strict() -> Lint {
Lint {
strict: true,
..Lint::new("x")
}
}
type Row = (
&'static str,
Lint,
bool,
Vec<Expected>,
Option<(usize, usize)>,
);
fn rows() -> Vec<Row> {
use Severity::{Error, Info, Warning};
vec![
(
"clean",
Lint::new("x"),
true,
vec![("clean", false, &[])],
Some((1, 0)),
),
(
"info",
Lint::new("x"),
true,
vec![("info", false, &[(Info, "Chart.yaml")])],
Some((1, 0)),
),
(
"warning",
Lint::new("x"),
true,
vec![("warning", false, &[(Warning, "templates/unnamed.yaml")])],
Some((1, 0)),
),
(
"error",
Lint::new("x"),
false,
vec![(
"error",
true,
&[
(Error, "Chart.yaml"),
(Warning, "Chart.yaml"),
(Error, "templates/"),
(Error, ""),
],
)],
Some((1, 1)),
),
(
"broken",
Lint::new("x"),
false,
vec![("broken", true, &[(Error, "templates/")])],
Some((1, 1)),
),
(
"quiet-clean",
Lint {
quiet: true,
..Lint::new("x")
},
true,
vec![],
None,
),
(
"quiet-mixed",
Lint {
quiet: true,
..Lint::new("x")
},
true,
vec![("warning", false, &[(Warning, "templates/unnamed.yaml")])],
Some((2, 0)),
),
(
"subcharts",
Lint {
with_subcharts: true,
..Lint::new("x")
},
true,
vec![
("parent", false, &[]),
(
"parent/charts/child",
false,
&[(Warning, "templates/unnamed.yaml")],
),
],
Some((2, 0)),
),
]
}
#[test]
fn lint_output_parses_into_typed_findings() {
for (name, command, exit_zero, charts, summary) in rows() {
let report = report(&command, name, exit_zero);
let seen: Vec<_> = report
.charts
.iter()
.map(|chart| {
let findings: Vec<_> = chart
.messages
.iter()
.map(|m| (m.severity, m.path.as_str()))
.collect();
(chart.path.as_str(), chart.failed, findings)
})
.collect();
let expected: Vec<_> = charts
.into_iter()
.map(|(path, failed, findings)| (path, failed, findings.to_vec()))
.collect();
assert_eq!(seen, expected, "{name}");
assert_eq!(
report.summary,
summary.map(|(linted, failed)| LintSummary { linted, failed }),
"{name}"
);
assert_eq!(report.passed(), exit_zero, "{name}");
}
}
#[test]
fn lint_keeps_multi_line_messages_and_empty_paths() {
let report = report(&Lint::new("x"), "broken", false);
let message = &report.charts[0].messages[0].message;
assert!(message.starts_with("broken/templates/configmap.yaml:6:15\n"));
assert!(message.ends_with("nil pointer evaluating interface {}.deep"));
let report = self::report(&Lint::new("x"), "error", false);
let last = report.charts[0].messages.last().expect("findings");
assert_eq!(last.path, "");
assert_eq!(
last.message,
"unable to load chart\n\tvalidation: chart.metadata.version is required"
);
}
#[test]
fn lint_strict_fails_on_warnings_only_with_the_flag() {
let strict_run = report(&strict(), "strict-warning", false);
let lenient = report(&Lint::new("x"), "warning", true);
let info_only = report(&strict(), "strict-info", true);
let subcharts = report(&strict(), "strict-subcharts", false);
assert!(!strict_run.passed());
assert_eq!(strict_run.charts[0].messages[0].severity, Severity::Warning);
assert_eq!(
strict_run.summary,
Some(LintSummary {
linted: 1,
failed: 1
})
);
assert!(lenient.passed());
assert!(info_only.passed());
assert_eq!(
subcharts
.charts
.iter()
.map(|c| c.failed)
.collect::<Vec<_>>(),
[false, true]
);
}
#[test]
fn lint_reports_chart_failures_that_are_not_findings() {
let report = report(&Lint::new("x"), "missing", false);
assert_eq!(report.charts[0].messages.len(), 0);
assert_eq!(
report.charts[0].errors,
[
"unable to check Chart.yaml file in chart: stat nonexistent/Chart.yaml: no such file or directory"
]
);
assert!(!report.passed());
}
#[test]
fn lint_failure_without_findings_stays_an_error() {
assert!(
Lint::new("x")
.parse_failure("", "Error: bad values\n")
.is_none()
);
}
#[test]
fn lint_reads_several_charts_in_order() {
let report = report(&Lint::new("x"), "multi", false);
assert_eq!(
report
.charts
.iter()
.map(|c| (c.path.as_str(), c.failed))
.collect::<Vec<_>>(),
[("info", false), ("error", true), ("warning", false)]
);
assert_eq!(
report.summary,
Some(LintSummary {
linted: 3,
failed: 1
})
);
}