thelper: allow to disable one option (#2854)

This commit is contained in:
Ludovic Fernandez 2022-05-16 21:16:25 +02:00 committed by GitHub
parent d8bf2434ec
commit b504325dc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 63 deletions

View File

@ -550,26 +550,16 @@ type TestpackageSettings struct {
}
type ThelperSettings struct {
Test struct {
First bool `mapstructure:"first"`
Name bool `mapstructure:"name"`
Begin bool `mapstructure:"begin"`
} `mapstructure:"test"`
Fuzz struct {
First bool `mapstructure:"first"`
Name bool `mapstructure:"name"`
Begin bool `mapstructure:"begin"`
} `mapstructure:"fuzz"`
Benchmark struct {
First bool `mapstructure:"first"`
Name bool `mapstructure:"name"`
Begin bool `mapstructure:"begin"`
} `mapstructure:"benchmark"`
TB struct {
First bool `mapstructure:"first"`
Name bool `mapstructure:"name"`
Begin bool `mapstructure:"begin"`
} `mapstructure:"tb"`
Test ThelperOptions `mapstructure:"test"`
Fuzz ThelperOptions `mapstructure:"fuzz"`
Benchmark ThelperOptions `mapstructure:"benchmark"`
TB ThelperOptions `mapstructure:"tb"`
}
type ThelperOptions struct {
First *bool `mapstructure:"first"`
Name *bool `mapstructure:"name"`
Begin *bool `mapstructure:"begin"`
}
type TenvSettings struct {

View File

@ -13,53 +13,44 @@ import (
func NewThelper(cfg *config.ThelperSettings) *goanalysis.Linter {
a := analyzer.NewAnalyzer()
cfgMap := map[string]map[string]interface{}{}
opts := map[string]struct{}{
"t_name": {},
"t_begin": {},
"t_first": {},
"f_name": {},
"f_begin": {},
"f_first": {},
"b_name": {},
"b_begin": {},
"b_first": {},
"tb_name": {},
"tb_begin": {},
"tb_first": {},
}
if cfg != nil {
var opts []string
applyTHelperOptions(cfg.Test, "t_", opts)
applyTHelperOptions(cfg.Fuzz, "f_", opts)
applyTHelperOptions(cfg.Benchmark, "b_", opts)
applyTHelperOptions(cfg.TB, "tb_", opts)
}
if cfg.Test.Name {
opts = append(opts, "t_name")
}
if cfg.Test.Begin {
opts = append(opts, "t_begin")
}
if cfg.Test.First {
opts = append(opts, "t_first")
}
if len(opts) == 0 {
linterLogger.Fatalf("thelper: at least one option must be enabled")
}
if cfg.Fuzz.Name {
opts = append(opts, "f_name")
}
if cfg.Fuzz.Begin {
opts = append(opts, "f_begin")
}
if cfg.Fuzz.First {
opts = append(opts, "f_first")
}
var args []string
for k := range opts {
args = append(args, k)
}
if cfg.Benchmark.Name {
opts = append(opts, "b_name")
}
if cfg.Benchmark.Begin {
opts = append(opts, "b_begin")
}
if cfg.Benchmark.First {
opts = append(opts, "b_first")
}
if cfg.TB.Name {
opts = append(opts, "tb_name")
}
if cfg.TB.Begin {
opts = append(opts, "tb_begin")
}
if cfg.TB.First {
opts = append(opts, "tb_first")
}
cfgMap[a.Name] = map[string]interface{}{
"checks": strings.Join(opts, ","),
}
cfgMap := map[string]map[string]interface{}{
a.Name: {
"checks": strings.Join(args, ","),
},
}
return goanalysis.NewLinter(
@ -69,3 +60,23 @@ func NewThelper(cfg *config.ThelperSettings) *goanalysis.Linter {
cfgMap,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
func applyTHelperOptions(o config.ThelperOptions, prefix string, opts map[string]struct{}) {
if o.Name != nil {
if !*o.Name {
delete(opts, prefix+"name")
}
}
if o.Begin != nil {
if !*o.Begin {
delete(opts, prefix+"begin")
}
}
if o.First != nil {
if !*o.First {
delete(opts, prefix+"first")
}
}
}

5
test/testdata/configs/thelper.yml vendored Normal file
View File

@ -0,0 +1,5 @@
linters-settings:
thelper:
test:
name: false
begin: true

18
test/testdata/thelper_with_options.go vendored Normal file
View File

@ -0,0 +1,18 @@
// args: -Ethelper
// config_path: testdata/configs/thelper.yml
package testdata
import "testing"
func thelperWithHelperAfterAssignmentWO(t *testing.T) { // ERROR "test helper function should start from t.Helper()"
_ = 0
t.Helper()
}
func thelperWithNotFirstWO(s string, t *testing.T, i int) { // ERROR `parameter \*testing.T should be the first`
t.Helper()
}
func thelperWithIncorrectNameWO(o *testing.T) {
o.Helper()
}