Early return if the `/crds` directory does not exist and don't silently discard the error from `os.Stat`.

Signed-off-by: Zach Burgess <zachburg@google.com>
pull/31015/head
Zach Burgess 3 months ago
parent d6ddd8e661
commit 562ff982cb

@ -40,7 +40,7 @@ const invalidChartFileDir = "rules/testdata/invalidchartfile"
func TestBadChart(t *testing.T) { func TestBadChart(t *testing.T) {
m := RunAll(badChartDir, values, namespace).Messages m := RunAll(badChartDir, values, namespace).Messages
if len(m) != 9 { if len(m) != 8 {
t.Errorf("Number of errors %v", len(m)) t.Errorf("Number of errors %v", len(m))
t.Errorf("All didn't fail with expected errors, got %#v", m) t.Errorf("All didn't fail with expected errors, got %#v", m)
} }

@ -21,6 +21,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
@ -35,13 +36,13 @@ func Crds(linter *support.Linter) {
fpath := "crds/" fpath := "crds/"
crdsPath := filepath.Join(linter.ChartDir, fpath) crdsPath := filepath.Join(linter.ChartDir, fpath)
crdsDirExist := linter.RunLinterRule(support.WarningSev, fpath, validateCrdsDir(crdsPath))
// crds directory is optional // crds directory is optional
if !crdsDirExist { if _, err := os.Stat(crdsPath); errors.Is(err, fs.ErrNotExist) {
return return
} }
linter.RunLinterRule(support.WarningSev, fpath, validateCrdsDir(crdsPath))
// Load chart and parse CRDs // Load chart and parse CRDs
chart, err := loader.Load(linter.ChartDir) chart, err := loader.Load(linter.ChartDir)
@ -81,10 +82,12 @@ func Crds(linter *support.Linter) {
// Validation functions // Validation functions
func validateCrdsDir(crdsPath string) error { func validateCrdsDir(crdsPath string) error {
if fi, err := os.Stat(crdsPath); err == nil { fi, err := os.Stat(crdsPath)
if !fi.IsDir() { if err != nil {
return errors.New("not a directory") return err
} }
if !fi.IsDir() {
return errors.New("not a directory")
} }
return nil return nil
} }

@ -23,11 +23,11 @@ import (
"helm.sh/helm/v4/pkg/lint/support" "helm.sh/helm/v4/pkg/lint/support"
) )
const crdsTestBasedir = "./testdata/withcrd" const crdsTestBaseDir = "./testdata/withcrd"
const invalidCrdsDir = "./testdata/invalidcrdsdir" const invalidCrdsDir = "./testdata/invalidcrdsdir"
func TestCrdsDir(t *testing.T) { func TestCrdsDir(t *testing.T) {
linter := support.Linter{ChartDir: crdsTestBasedir} linter := support.Linter{ChartDir: crdsTestBaseDir}
Crds(&linter) Crds(&linter)
res := linter.Messages res := linter.Messages

Loading…
Cancel
Save