1
0
mirror of https://github.com/mgechev/revive.git synced 2025-11-25 22:12:38 +02:00

fix: error-strings custom funcs overwrites defaults (#1249)

This commit is contained in:
Oleksandr Redko
2025-02-26 23:16:06 +02:00
committed by GitHub
parent 9177f5044a
commit 67d0a61a1b
4 changed files with 98 additions and 7 deletions

View File

@@ -37,14 +37,19 @@ func (r *ErrorStringsRule) Configure(arguments lint.Arguments) error {
var invalidCustomFunctions []string
for _, argument := range arguments {
if functionName, ok := argument.(string); ok {
fields := strings.Split(strings.TrimSpace(functionName), ".")
if len(fields) != 2 || len(fields[0]) == 0 || len(fields[1]) == 0 {
invalidCustomFunctions = append(invalidCustomFunctions, functionName)
continue
}
r.errorFunctions[fields[0]] = map[string]struct{}{fields[1]: {}}
pkgFunction, ok := argument.(string)
if !ok {
continue
}
pkg, function, ok := strings.Cut(strings.TrimSpace(pkgFunction), ".")
if !ok || pkg == "" || function == "" {
invalidCustomFunctions = append(invalidCustomFunctions, pkgFunction)
continue
}
if _, ok := r.errorFunctions[pkg]; !ok {
r.errorFunctions[pkg] = map[string]struct{}{}
}
r.errorFunctions[pkg][function] = struct{}{}
}
if len(invalidCustomFunctions) != 0 {
return fmt.Errorf("found invalid custom function: %s", strings.Join(invalidCustomFunctions, ","))

View File

@@ -0,0 +1,68 @@
package rule_test
import (
"errors"
"testing"
"github.com/mgechev/revive/lint"
"github.com/mgechev/revive/rule"
)
func TestErrorStringsRule_Configure(t *testing.T) {
tests := []struct {
name string
arguments lint.Arguments
wantErr error
}{
{
name: "Default configuration",
arguments: lint.Arguments{},
},
{
name: "Valid custom functions",
arguments: lint.Arguments{"mypkg.MyErrorFunc", "errors.New"},
},
{
name: "Argument not a string",
arguments: lint.Arguments{123},
},
{
name: "Invalid package",
arguments: lint.Arguments{".MyErrorFunc"},
wantErr: errors.New("found invalid custom function: .MyErrorFunc"),
},
{
name: "Invalid function",
arguments: lint.Arguments{"errors."},
wantErr: errors.New("found invalid custom function: errors."),
},
{
name: "Invalid custom function",
arguments: lint.Arguments{"invalidFunction"},
wantErr: errors.New("found invalid custom function: invalidFunction"),
},
{
name: "Mixed valid and invalid custom functions",
arguments: lint.Arguments{"mypkg.MyErrorFunc", "invalidFunction", "invalidFunction2"},
wantErr: errors.New("found invalid custom function: invalidFunction,invalidFunction2"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var r rule.ErrorStringsRule
err := r.Configure(tt.arguments)
if tt.wantErr == nil {
if err != nil {
t.Errorf("Configure() unexpected non-nil error %q", err)
}
return
}
if err == nil || err.Error() != tt.wantErr.Error() {
t.Errorf("Configure() unexpected error: got %q, want %q", err, tt.wantErr)
}
})
}
}

View File

@@ -13,3 +13,10 @@ func TestErrorStringsWithCustomFunctions(t *testing.T) {
Arguments: args,
})
}
func TestErrorStringsIssue1243(t *testing.T) {
args := []any{"errors.Wrap"}
testRule(t, "error_strings_issue_1243", &rule.ErrorStringsRule{}, &lint.RuleConfig{
Arguments: args,
})
}

11
testdata/error_strings_issue_1243.go vendored Normal file
View File

@@ -0,0 +1,11 @@
package fixtures
import (
"errors"
"fmt"
)
func issue1243() {
err := errors.New("An error occurred!") // MATCH /error strings should not be capitalized or end with punctuation or a newline/
fmt.Println(err)
}