Fixing linting of templates on Windows

When the engine stored templates in the map the keys were generated
based on path and not filepath. filepath was being used in the linter
when retrieving content from the keys. On Windows the keys ended up
being different.

This change is to use path joins to create the lookup key. Since the
name path was used in the code it needed to be changed in order to
import the package.

Tests already exist and were failing on windows. This got in because
CI is not run on Windows.

Closes #6418

Signed-off-by: Matt Farina <matt@mattfarina.com>
pull/8627/head
Matt Farina 4 years ago
parent 198f403688
commit 11f658e223
No known key found for this signature in database
GPG Key ID: 9436E80BFBA46909

@ -19,6 +19,7 @@ package rules
import ( import (
"fmt" "fmt"
"os" "os"
"path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -47,10 +48,10 @@ var validName = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-
// Templates lints the templates in the Linter. // Templates lints the templates in the Linter.
func Templates(linter *support.Linter, values map[string]interface{}, namespace string, strict bool) { func Templates(linter *support.Linter, values map[string]interface{}, namespace string, strict bool) {
path := "templates/" fpath := "templates/"
templatesPath := filepath.Join(linter.ChartDir, path) templatesPath := filepath.Join(linter.ChartDir, fpath)
templatesDirExist := linter.RunLinterRule(support.WarningSev, path, validateTemplatesDir(templatesPath)) templatesDirExist := linter.RunLinterRule(support.WarningSev, fpath, validateTemplatesDir(templatesPath))
// Templates directory is optional for now // Templates directory is optional for now
if !templatesDirExist { if !templatesDirExist {
@ -60,7 +61,7 @@ func Templates(linter *support.Linter, values map[string]interface{}, namespace
// Load chart and parse templates // Load chart and parse templates
chart, err := loader.Load(linter.ChartDir) chart, err := loader.Load(linter.ChartDir)
chartLoaded := linter.RunLinterRule(support.ErrorSev, path, err) chartLoaded := linter.RunLinterRule(support.ErrorSev, fpath, err)
if !chartLoaded { if !chartLoaded {
return return
@ -77,14 +78,14 @@ func Templates(linter *support.Linter, values map[string]interface{}, namespace
} }
valuesToRender, err := chartutil.ToRenderValues(chart, cvals, options, nil) valuesToRender, err := chartutil.ToRenderValues(chart, cvals, options, nil)
if err != nil { if err != nil {
linter.RunLinterRule(support.ErrorSev, path, err) linter.RunLinterRule(support.ErrorSev, fpath, err)
return return
} }
var e engine.Engine var e engine.Engine
e.LintMode = true e.LintMode = true
renderedContentMap, err := e.Render(chart, valuesToRender) renderedContentMap, err := e.Render(chart, valuesToRender)
renderOk := linter.RunLinterRule(support.ErrorSev, path, err) renderOk := linter.RunLinterRule(support.ErrorSev, fpath, err)
if !renderOk { if !renderOk {
return return
@ -99,13 +100,13 @@ func Templates(linter *support.Linter, values map[string]interface{}, namespace
*/ */
for _, template := range chart.Templates { for _, template := range chart.Templates {
fileName, data := template.Name, template.Data fileName, data := template.Name, template.Data
path = fileName fpath = fileName
linter.RunLinterRule(support.ErrorSev, path, validateAllowedExtension(fileName)) linter.RunLinterRule(support.ErrorSev, fpath, validateAllowedExtension(fileName))
// These are v3 specific checks to make sure and warn people if their // These are v3 specific checks to make sure and warn people if their
// chart is not compatible with v3 // chart is not compatible with v3
linter.RunLinterRule(support.WarningSev, path, validateNoCRDHooks(data)) linter.RunLinterRule(support.WarningSev, fpath, validateNoCRDHooks(data))
linter.RunLinterRule(support.ErrorSev, path, validateNoReleaseTime(data)) linter.RunLinterRule(support.ErrorSev, fpath, validateNoReleaseTime(data))
// We only apply the following lint rules to yaml files // We only apply the following lint rules to yaml files
if filepath.Ext(fileName) != ".yaml" || filepath.Ext(fileName) == ".yml" { if filepath.Ext(fileName) != ".yaml" || filepath.Ext(fileName) == ".yml" {
@ -114,12 +115,12 @@ func Templates(linter *support.Linter, values map[string]interface{}, namespace
// NOTE: disabled for now, Refs https://github.com/helm/helm/issues/1463 // NOTE: disabled for now, Refs https://github.com/helm/helm/issues/1463
// Check that all the templates have a matching value // Check that all the templates have a matching value
//linter.RunLinterRule(support.WarningSev, path, validateNoMissingValues(templatesPath, valuesToRender, preExecutedTemplate)) //linter.RunLinterRule(support.WarningSev, fpath, validateNoMissingValues(templatesPath, valuesToRender, preExecutedTemplate))
// NOTE: disabled for now, Refs https://github.com/helm/helm/issues/1037 // NOTE: disabled for now, Refs https://github.com/helm/helm/issues/1037
// linter.RunLinterRule(support.WarningSev, path, validateQuotes(string(preExecutedTemplate))) // linter.RunLinterRule(support.WarningSev, fpath, validateQuotes(string(preExecutedTemplate)))
renderedContent := renderedContentMap[filepath.Join(chart.Name(), fileName)] renderedContent := renderedContentMap[path.Join(chart.Name(), fileName)]
if strings.TrimSpace(renderedContent) != "" { if strings.TrimSpace(renderedContent) != "" {
var yamlStruct K8sYamlStruct var yamlStruct K8sYamlStruct
// Even though K8sYamlStruct only defines a few fields, an error in any other // Even though K8sYamlStruct only defines a few fields, an error in any other
@ -128,10 +129,10 @@ func Templates(linter *support.Linter, values map[string]interface{}, namespace
// If YAML linting fails, we sill progress. So we don't capture the returned state // If YAML linting fails, we sill progress. So we don't capture the returned state
// on this linter run. // on this linter run.
linter.RunLinterRule(support.ErrorSev, path, validateYamlContent(err)) linter.RunLinterRule(support.ErrorSev, fpath, validateYamlContent(err))
linter.RunLinterRule(support.ErrorSev, path, validateMetadataName(&yamlStruct)) linter.RunLinterRule(support.ErrorSev, fpath, validateMetadataName(&yamlStruct))
linter.RunLinterRule(support.ErrorSev, path, validateNoDeprecations(&yamlStruct)) linter.RunLinterRule(support.ErrorSev, fpath, validateNoDeprecations(&yamlStruct))
linter.RunLinterRule(support.ErrorSev, path, validateMatchSelector(&yamlStruct, renderedContent)) linter.RunLinterRule(support.ErrorSev, fpath, validateMatchSelector(&yamlStruct, renderedContent))
} }
} }
} }

Loading…
Cancel
Save