feat: lint info on template spacing failure

Signed-off-by: Matt Butcher <matt.butcher@microsoft.com>
pull/8043/head
Matt Butcher 5 years ago
parent 08e546f169
commit b6a3b5fa93
No known key found for this signature in database
GPG Key ID: DCD5F5E5EF32C345

@ -106,6 +106,7 @@ func Templates(linter *support.Linter, values map[string]interface{}, namespace
// chart is not compatible with v3
linter.RunLinterRule(support.WarningSev, path, validateNoCRDHooks(data))
linter.RunLinterRule(support.ErrorSev, path, validateNoReleaseTime(data))
linter.RunLinterRule(support.InfoSev, path, validateWhitespaceAroundTemplateDirectives(string(data)))
// We only apply the following lint rules to yaml files
if filepath.Ext(fileName) != ".yaml" || filepath.Ext(fileName) == ".yml" {
@ -185,6 +186,26 @@ func validateNoReleaseTime(manifest []byte) error {
return nil
}
// TODO: I strongly suspect that there are better regexps than these two.
var (
badTplStart = regexp.MustCompile(`{{-?[^-\s]+`)
badTplEnd = regexp.MustCompile(`[^\s-]+-?}}`)
)
func validateWhitespaceAroundTemplateDirectives(template string) error {
badMatches := []string{}
if matches := badTplStart.FindAllString(template, 10); matches != nil {
badMatches = append(badMatches, fmt.Sprintf("\nbad start: %s", strings.Join(matches, "\n\t")))
}
if matches := badTplEnd.FindAllString(template, 10); matches != nil {
badMatches = append(badMatches, fmt.Sprintf("\nbad end: %s", strings.Join(matches, "\n\t")))
}
if len(badMatches) == 0 {
return nil
}
return fmt.Errorf("whitespace required between template markers and body: %s", strings.Join(badMatches, "\n"))
}
// K8sYamlStruct stubs a Kubernetes YAML file.
//
// DEPRECATED: In Helm 4, this will be made a private type, as it is for use only within

@ -226,3 +226,31 @@ func TestStrictTemplateParsingMapError(t *testing.T) {
}
}
}
func TestValidateWhitespaceAroundTemplateDirectives(t *testing.T) {
for example, success := range map[string]bool{
"{{foo}}": false,
"{{-foo-}}": false,
"{{ foo-}}": false,
"{{ foo}}": false,
"{{-foo }}": false,
"{{foo }}": false,
"{{ foo }}": true,
"{{ default 2 .Values.foo }}": true,
"{{default 2 .Values.foo }}": false,
"{{ default 2 .Values.foo}}": false,
`{{ default "}" .Values.foo }}`: true,
`{{- foo }}`: true,
`{{ legal }}{{illegal }}`: false,
`{{ legal }}{{- legal -}}`: true,
"{{\nlegal\n}}": true,
} {
if err := validateWhitespaceAroundTemplateDirectives(example); (err == nil) != success {
st := "failure"
if success {
st = "success"
}
t.Errorf("Expected %s for %q", st, example)
}
}
}

Loading…
Cancel
Save