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) {
m := RunAll(badChartDir, values, namespace).Messages
if len(m) != 9 {
if len(m) != 8 {
t.Errorf("Number of errors %v", len(m))
t.Errorf("All didn't fail with expected errors, got %#v", m)
}

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

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

Loading…
Cancel
Save