thelper: allow to disable one option (#2854)
This commit is contained in:
parent
d8bf2434ec
commit
b504325dc5
@ -550,26 +550,16 @@ type TestpackageSettings struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ThelperSettings struct {
|
type ThelperSettings struct {
|
||||||
Test struct {
|
Test ThelperOptions `mapstructure:"test"`
|
||||||
First bool `mapstructure:"first"`
|
Fuzz ThelperOptions `mapstructure:"fuzz"`
|
||||||
Name bool `mapstructure:"name"`
|
Benchmark ThelperOptions `mapstructure:"benchmark"`
|
||||||
Begin bool `mapstructure:"begin"`
|
TB ThelperOptions `mapstructure:"tb"`
|
||||||
} `mapstructure:"test"`
|
}
|
||||||
Fuzz struct {
|
|
||||||
First bool `mapstructure:"first"`
|
type ThelperOptions struct {
|
||||||
Name bool `mapstructure:"name"`
|
First *bool `mapstructure:"first"`
|
||||||
Begin bool `mapstructure:"begin"`
|
Name *bool `mapstructure:"name"`
|
||||||
} `mapstructure:"fuzz"`
|
Begin *bool `mapstructure:"begin"`
|
||||||
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"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type TenvSettings struct {
|
type TenvSettings struct {
|
||||||
|
@ -13,53 +13,44 @@ import (
|
|||||||
func NewThelper(cfg *config.ThelperSettings) *goanalysis.Linter {
|
func NewThelper(cfg *config.ThelperSettings) *goanalysis.Linter {
|
||||||
a := analyzer.NewAnalyzer()
|
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 {
|
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 {
|
if len(opts) == 0 {
|
||||||
opts = append(opts, "t_name")
|
linterLogger.Fatalf("thelper: at least one option must be enabled")
|
||||||
}
|
}
|
||||||
if cfg.Test.Begin {
|
|
||||||
opts = append(opts, "t_begin")
|
|
||||||
}
|
|
||||||
if cfg.Test.First {
|
|
||||||
opts = append(opts, "t_first")
|
|
||||||
}
|
|
||||||
|
|
||||||
if cfg.Fuzz.Name {
|
var args []string
|
||||||
opts = append(opts, "f_name")
|
for k := range opts {
|
||||||
}
|
args = append(args, k)
|
||||||
if cfg.Fuzz.Begin {
|
}
|
||||||
opts = append(opts, "f_begin")
|
|
||||||
}
|
|
||||||
if cfg.Fuzz.First {
|
|
||||||
opts = append(opts, "f_first")
|
|
||||||
}
|
|
||||||
|
|
||||||
if cfg.Benchmark.Name {
|
cfgMap := map[string]map[string]interface{}{
|
||||||
opts = append(opts, "b_name")
|
a.Name: {
|
||||||
}
|
"checks": strings.Join(args, ","),
|
||||||
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, ","),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return goanalysis.NewLinter(
|
return goanalysis.NewLinter(
|
||||||
@ -69,3 +60,23 @@ func NewThelper(cfg *config.ThelperSettings) *goanalysis.Linter {
|
|||||||
cfgMap,
|
cfgMap,
|
||||||
).WithLoadMode(goanalysis.LoadModeTypesInfo)
|
).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
5
test/testdata/configs/thelper.yml
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
linters-settings:
|
||||||
|
thelper:
|
||||||
|
test:
|
||||||
|
name: false
|
||||||
|
begin: true
|
18
test/testdata/thelper_with_options.go
vendored
Normal file
18
test/testdata/thelper_with_options.go
vendored
Normal 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()
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user