Enable the errname linter.

Signed-off-by: Mads Jensen <atombrella@users.noreply.github.com>
pull/31647/head
Mads Jensen 10 months ago
parent 5d2ab10caa
commit 4ec0b32ac7

@ -20,6 +20,7 @@ linters:
enable: enable:
- depguard - depguard
- dupl - dupl
- errname
- gomodguard - gomodguard
- govet - govet
- ineffassign - ineffassign

@ -234,7 +234,7 @@ func TestMalformedTemplate(t *testing.T) {
var values map[string]any var values map[string]any
c := time.After(3 * time.Second) c := time.After(3 * time.Second)
ch := make(chan int, 1) ch := make(chan int, 1)
var m []support.Message var m []support.MessageError
go func() { go func() {
m = RunAll(malformedTemplate, values, namespace).Messages m = RunAll(malformedTemplate, values, namespace).Messages
ch <- 1 ch <- 1

@ -35,27 +35,27 @@ var sev = []string{"UNKNOWN", "INFO", "WARNING", "ERROR"}
// Linter encapsulates a linting run of a particular chart. // Linter encapsulates a linting run of a particular chart.
type Linter struct { type Linter struct {
Messages []Message Messages []MessageError
// The highest severity of all the failing lint rules // The highest severity of all the failing lint rules
HighestSeverity int HighestSeverity int
ChartDir string ChartDir string
} }
// Message describes an error encountered while linting. // Message describes an error encountered while linting.
type Message struct { type MessageError struct {
// Severity is one of the *Sev constants // Severity is one of the *Sev constants
Severity int Severity int
Path string Path string
Err error Err error
} }
func (m Message) Error() string { func (m MessageError) Error() string {
return fmt.Sprintf("[%s] %s: %s", sev[m.Severity], m.Path, m.Err.Error()) return fmt.Sprintf("[%s] %s: %s", sev[m.Severity], m.Path, m.Err.Error())
} }
// NewMessage creates a new Message struct // NewMessage creates a new Message struct
func NewMessage(severity int, path string, err error) Message { func NewMessage(severity int, path string, err error) MessageError {
return Message{Severity: severity, Path: path, Err: err} return MessageError{Severity: severity, Path: path, Err: err}
} }
// RunLinterRule returns true if the validation passed // RunLinterRule returns true if the validation passed

@ -62,17 +62,17 @@ func TestRunLinterRule(t *testing.T) {
} }
func TestMessage(t *testing.T) { func TestMessage(t *testing.T) {
m := Message{ErrorSev, "Chart.yaml", errors.New("Foo")} m := MessageError{ErrorSev, "Chart.yaml", errors.New("Foo")}
if m.Error() != "[ERROR] Chart.yaml: Foo" { if m.Error() != "[ERROR] Chart.yaml: Foo" {
t.Errorf("Unexpected output: %s", m.Error()) t.Errorf("Unexpected output: %s", m.Error())
} }
m = Message{WarningSev, "templates/", errors.New("Bar")} m = MessageError{WarningSev, "templates/", errors.New("Bar")}
if m.Error() != "[WARNING] templates/: Bar" { if m.Error() != "[WARNING] templates/: Bar" {
t.Errorf("Unexpected output: %s", m.Error()) t.Errorf("Unexpected output: %s", m.Error())
} }
m = Message{InfoSev, "templates/rc.yaml", errors.New("FooBar")} m = MessageError{InfoSev, "templates/rc.yaml", errors.New("FooBar")}
if m.Error() != "[INFO] templates/rc.yaml: FooBar" { if m.Error() != "[INFO] templates/rc.yaml: FooBar" {
t.Errorf("Unexpected output: %s", m.Error()) t.Errorf("Unexpected output: %s", m.Error())
} }

@ -51,7 +51,7 @@ func processDependencyConditions(reqs []*chart.Dependency, cvals common.Values,
break break
} }
slog.Warn("returned non-bool value", "path", c, "chart", r.Name) slog.Warn("returned non-bool value", "path", c, "chart", r.Name)
} else if _, ok := err.(common.ErrNoValue); !ok { } else if _, ok := err.(common.NoValueError); !ok {
// this is a real error // this is a real error
slog.Warn("the method PathValue returned error", slog.Any("error", err)) slog.Warn("the method PathValue returned error", slog.Any("error", err))
} }

@ -252,10 +252,10 @@ func TestProcessDependencyImportValues(t *testing.T) {
t.Error("expect nil value not found but found it") t.Error("expect nil value not found but found it")
} }
switch xerr := err.(type) { switch xerr := err.(type) {
case common.ErrNoValue: case common.NoValueError:
// We found what we expected // We found what we expected
default: default:
t.Errorf("expected an ErrNoValue but got %q instead", xerr) t.Errorf("expected an NoValueErr but got %q instead", xerr)
} }
c = loadChart(t, "testdata/subpop") c = loadChart(t, "testdata/subpop")

@ -250,7 +250,7 @@ func validateName(name string) error {
nname := filepath.Base(name) nname := filepath.Base(name)
if nname != name { if nname != name {
return common.ErrInvalidChartName{Name: name} return common.InvalidChartNameError{Name: name}
} }
return nil return nil

@ -43,7 +43,7 @@ type Lint struct {
// LintResult is the result of Lint // LintResult is the result of Lint
type LintResult struct { type LintResult struct {
TotalChartsLinted int TotalChartsLinted int
Messages []support.Message Messages []support.MessageError
Errors []error Errors []error
} }

@ -222,19 +222,19 @@ func (u *Uninstall) purgeReleases(rels ...*release.Release) error {
return nil return nil
} }
type joinedErrors struct { type joinedListError struct {
errs []error errs []error
sep string sep string
} }
func joinErrors(errs []error, sep string) error { func joinErrors(errs []error, sep string) error {
return &joinedErrors{ return &joinedListError{
errs: errs, errs: errs,
sep: sep, sep: sep,
} }
} }
func (e *joinedErrors) Error() string { func (e *joinedListError) Error() string {
errs := make([]string, 0, len(e.errs)) errs := make([]string, 0, len(e.errs))
for _, err := range e.errs { for _, err := range e.errs {
errs = append(errs, err.Error()) errs = append(errs, err.Error())
@ -242,7 +242,7 @@ func (e *joinedErrors) Error() string {
return strings.Join(errs, e.sep) return strings.Join(errs, e.sep)
} }
func (e *joinedErrors) Unwrap() []error { func (e *joinedListError) Unwrap() []error {
return e.errs return e.errs
} }

@ -20,24 +20,24 @@ import (
"fmt" "fmt"
) )
// ErrNoTable indicates that a chart does not have a matching table. // NoTableError indicates that a chart does not have a matching table.
type ErrNoTable struct { type NoTableError struct {
Key string Key string
} }
func (e ErrNoTable) Error() string { return fmt.Sprintf("%q is not a table", e.Key) } func (e NoTableError) Error() string { return fmt.Sprintf("%q is not a table", e.Key) }
// ErrNoValue indicates that Values does not contain a key with a value // NoValueError indicates that Values does not contain a key with a value
type ErrNoValue struct { type NoValueError struct {
Key string Key string
} }
func (e ErrNoValue) Error() string { return fmt.Sprintf("%q is not a value", e.Key) } func (e NoValueError) Error() string { return fmt.Sprintf("%q is not a value", e.Key) }
type ErrInvalidChartName struct { type InvalidChartNameError struct {
Name string Name string
} }
func (e ErrInvalidChartName) Error() string { func (e InvalidChartNameError) Error() string {
return fmt.Sprintf("%q is not a valid chart name", e.Name) return fmt.Sprintf("%q is not a valid chart name", e.Name)
} }

@ -23,7 +23,7 @@ import (
func TestErrorNoTableDoesNotPanic(t *testing.T) { func TestErrorNoTableDoesNotPanic(t *testing.T) {
x := "empty" x := "empty"
y := ErrNoTable{x} y := NoTableError{x}
t.Logf("error is: %s", y) t.Logf("error is: %s", y)
} }
@ -31,7 +31,7 @@ func TestErrorNoTableDoesNotPanic(t *testing.T) {
func TestErrorNoValueDoesNotPanic(t *testing.T) { func TestErrorNoValueDoesNotPanic(t *testing.T) {
x := "empty" x := "empty"
y := ErrNoValue{x} y := NoValueError{x}
t.Logf("error is: %s", y) t.Logf("error is: %s", y)
} }

@ -48,7 +48,7 @@ func (v Values) YAML() (string, error) {
// The above will be evaluated as "The table bar inside the table // The above will be evaluated as "The table bar inside the table
// foo". // foo".
// //
// An ErrNoTable is returned if the table does not exist. // An NoTableError is returned if the table does not exist.
func (v Values) Table(name string) (Values, error) { func (v Values) Table(name string) (Values, error) {
table := v table := v
var err error var err error
@ -84,7 +84,7 @@ func (v Values) Encode(w io.Writer) error {
func tableLookup(v Values, simple string) (Values, error) { func tableLookup(v Values, simple string) (Values, error) {
v2, ok := v[simple] v2, ok := v[simple]
if !ok { if !ok {
return v, ErrNoTable{simple} return v, NoTableError{simple}
} }
if vv, ok := v2.(map[string]interface{}); ok { if vv, ok := v2.(map[string]interface{}); ok {
return vv, nil return vv, nil
@ -97,7 +97,7 @@ func tableLookup(v Values, simple string) (Values, error) {
return vv, nil return vv, nil
} }
return Values{}, ErrNoTable{simple} return Values{}, NoTableError{simple}
} }
// ReadValues will parse YAML byte data into a Values. // ReadValues will parse YAML byte data into a Values.
@ -154,20 +154,20 @@ func (v Values) pathValue(path []string) (interface{}, error) {
if _, ok := v[path[0]]; ok && !istable(v[path[0]]) { if _, ok := v[path[0]]; ok && !istable(v[path[0]]) {
return v[path[0]], nil return v[path[0]], nil
} }
return nil, ErrNoValue{path[0]} return nil, NoValueError{path[0]}
} }
key, path := path[len(path)-1], path[:len(path)-1] key, path := path[len(path)-1], path[:len(path)-1]
// get our table for table path // get our table for table path
t, err := v.Table(joinPath(path...)) t, err := v.Table(joinPath(path...))
if err != nil { if err != nil {
return nil, ErrNoValue{key} return nil, NoValueError{key}
} }
// check table for key and ensure value is not a table // check table for key and ensure value is not a table
if k, ok := t[key]; ok && !istable(k) { if k, ok := t[key]; ok && !istable(k) {
return k, nil return k, nil
} }
return nil, ErrNoValue{key} return nil, NoValueError{key}
} }
func parsePath(key string) []string { return strings.Split(key, ".") } func parsePath(key string) []string { return strings.Split(key, ".") }

@ -238,7 +238,7 @@ func TestMalformedTemplate(t *testing.T) {
var values map[string]any var values map[string]any
c := time.After(3 * time.Second) c := time.After(3 * time.Second)
ch := make(chan int, 1) ch := make(chan int, 1)
var m []support.Message var m []support.MessageError
go func() { go func() {
m = RunAll(malformedTemplate, values, namespace).Messages m = RunAll(malformedTemplate, values, namespace).Messages
ch <- 1 ch <- 1

@ -35,27 +35,27 @@ var sev = []string{"UNKNOWN", "INFO", "WARNING", "ERROR"}
// Linter encapsulates a linting run of a particular chart. // Linter encapsulates a linting run of a particular chart.
type Linter struct { type Linter struct {
Messages []Message Messages []MessageError
// The highest severity of all the failing lint rules // The highest severity of all the failing lint rules
HighestSeverity int HighestSeverity int
ChartDir string ChartDir string
} }
// Message describes an error encountered while linting. // Message describes an error encountered while linting.
type Message struct { type MessageError struct {
// Severity is one of the *Sev constants // Severity is one of the *Sev constants
Severity int Severity int
Path string Path string
Err error Err error
} }
func (m Message) Error() string { func (m MessageError) Error() string {
return fmt.Sprintf("[%s] %s: %s", sev[m.Severity], m.Path, m.Err.Error()) return fmt.Sprintf("[%s] %s: %s", sev[m.Severity], m.Path, m.Err.Error())
} }
// NewMessage creates a new Message struct // NewMessage creates a new Message struct
func NewMessage(severity int, path string, err error) Message { func NewMessage(severity int, path string, err error) MessageError {
return Message{Severity: severity, Path: path, Err: err} return MessageError{Severity: severity, Path: path, Err: err}
} }
// RunLinterRule returns true if the validation passed // RunLinterRule returns true if the validation passed

@ -62,17 +62,17 @@ func TestRunLinterRule(t *testing.T) {
} }
func TestMessage(t *testing.T) { func TestMessage(t *testing.T) {
m := Message{ErrorSev, "Chart.yaml", errors.New("Foo")} m := MessageError{ErrorSev, "Chart.yaml", errors.New("Foo")}
if m.Error() != "[ERROR] Chart.yaml: Foo" { if m.Error() != "[ERROR] Chart.yaml: Foo" {
t.Errorf("Unexpected output: %s", m.Error()) t.Errorf("Unexpected output: %s", m.Error())
} }
m = Message{WarningSev, "templates/", errors.New("Bar")} m = MessageError{WarningSev, "templates/", errors.New("Bar")}
if m.Error() != "[WARNING] templates/: Bar" { if m.Error() != "[WARNING] templates/: Bar" {
t.Errorf("Unexpected output: %s", m.Error()) t.Errorf("Unexpected output: %s", m.Error())
} }
m = Message{InfoSev, "templates/rc.yaml", errors.New("FooBar")} m = MessageError{InfoSev, "templates/rc.yaml", errors.New("FooBar")}
if m.Error() != "[INFO] templates/rc.yaml: FooBar" { if m.Error() != "[INFO] templates/rc.yaml: FooBar" {
t.Errorf("Unexpected output: %s", m.Error()) t.Errorf("Unexpected output: %s", m.Error())
} }

@ -51,7 +51,7 @@ func processDependencyConditions(reqs []*chart.Dependency, cvals common.Values,
break break
} }
slog.Warn("returned non-bool value", "path", c, "chart", r.Name) slog.Warn("returned non-bool value", "path", c, "chart", r.Name)
} else if _, ok := err.(common.ErrNoValue); !ok { } else if _, ok := err.(common.NoValueError); !ok {
// this is a real error // this is a real error
slog.Warn("the method PathValue returned error", slog.Any("error", err)) slog.Warn("the method PathValue returned error", slog.Any("error", err))
} }

@ -252,10 +252,10 @@ func TestProcessDependencyImportValues(t *testing.T) {
t.Error("expect nil value not found but found it") t.Error("expect nil value not found but found it")
} }
switch xerr := err.(type) { switch xerr := err.(type) {
case common.ErrNoValue: case common.NoValueError:
// We found what we expected // We found what we expected
default: default:
t.Errorf("expected an ErrNoValue but got %q instead", xerr) t.Errorf("expected an NoValueErr but got %q instead", xerr)
} }
c = loadChart(t, "testdata/subpop") c = loadChart(t, "testdata/subpop")

@ -262,7 +262,7 @@ func validateName(name string) error {
nname := filepath.Base(name) nname := filepath.Base(name)
if nname != name { if nname != name {
return common.ErrInvalidChartName{Name: name} return common.InvalidChartNameError{Name: name}
} }
return nil return nil

@ -76,7 +76,7 @@ func newDependencyBuildCmd(out io.Writer) *cobra.Command {
man.Verify = downloader.VerifyIfPossible man.Verify = downloader.VerifyIfPossible
} }
err = man.Build() err = man.Build()
if e, ok := err.(downloader.ErrRepoNotFound); ok { if e, ok := err.(downloader.RepoNotFoundError); ok {
return fmt.Errorf("%s. Please add the missing repos via 'helm repo add'", e.Error()) return fmt.Errorf("%s. Please add the missing repos via 'helm repo add'", e.Error())
} }
return err return err

@ -45,14 +45,14 @@ import (
"helm.sh/helm/v4/pkg/repo/v1" "helm.sh/helm/v4/pkg/repo/v1"
) )
// ErrRepoNotFound indicates that chart repositories can't be found in local repo cache. // RepoNotFoundError indicates that chart repositories can't be found in local repo cache.
// The value of Repos is missing repos. // The value of Repos is missing repos.
type ErrRepoNotFound struct { type RepoNotFoundError struct {
Repos []string Repos []string
} }
// Error implements the error interface. // Error implements the error interface.
func (e ErrRepoNotFound) Error() string { func (e RepoNotFoundError) Error() string {
return fmt.Sprintf("no repository definition for %s", strings.Join(e.Repos, ", ")) return fmt.Sprintf("no repository definition for %s", strings.Join(e.Repos, ", "))
} }
@ -491,7 +491,7 @@ Loop:
missing = append(missing, dd.Repository) missing = append(missing, dd.Repository)
} }
if len(missing) > 0 { if len(missing) > 0 {
return ErrRepoNotFound{missing} return RepoNotFoundError{missing}
} }
return nil return nil
} }

@ -569,7 +569,7 @@ func TestErrRepoNotFound_Error(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
e := ErrRepoNotFound{ e := RepoNotFoundError{
Repos: tt.fields.Repos, Repos: tt.fields.Repos,
} }
if got := e.Error(); got != tt.want { if got := e.Error(); got != tt.want {

@ -1247,19 +1247,19 @@ func scrubValidationError(err error) error {
return err return err
} }
type joinedErrors struct { type joinedListError struct {
errs []error errs []error
sep string sep string
} }
func joinErrors(errs []error, sep string) error { func joinErrors(errs []error, sep string) error {
return &joinedErrors{ return &joinedListError{
errs: errs, errs: errs,
sep: sep, sep: sep,
} }
} }
func (e *joinedErrors) Error() string { func (e *joinedListError) Error() string {
errs := make([]string, 0, len(e.errs)) errs := make([]string, 0, len(e.errs))
for _, err := range e.errs { for _, err := range e.errs {
errs = append(errs, err.Error()) errs = append(errs, err.Error())
@ -1267,6 +1267,6 @@ func (e *joinedErrors) Error() string {
return strings.Join(errs, e.sep) return strings.Join(errs, e.sep)
} }
func (e *joinedErrors) Unwrap() []error { func (e *joinedListError) Unwrap() []error {
return e.errs return e.errs
} }

Loading…
Cancel
Save