Skip to content

thelper: allow to disable one option #2854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
95 changes: 53 additions & 42 deletions pkg/golinters/thelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,44 @@ import (
func NewThelper(cfg *config.ThelperSettings) *goanalysis.Linter {
a := analyzer.NewAnalyzer()

cfgMap := map[string]map[string]interface{}{}
if cfg != nil {
var opts []string
opts := map[string]struct{}{
"t_name": {},
"t_begin": {},
"t_first": {},

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")
}
"f_name": {},
"f_begin": {},
"f_first": {},

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")
}
"b_name": {},
"b_begin": {},
"b_first": {},

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")
}
"tb_name": {},
"tb_begin": {},
"tb_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")
}
if cfg != nil {
applyTHelperOptions(cfg.Test, "t_", opts)
applyTHelperOptions(cfg.Fuzz, "f_", opts)
applyTHelperOptions(cfg.Benchmark, "b_", opts)
applyTHelperOptions(cfg.TB, "tb_", opts)
}

cfgMap[a.Name] = map[string]interface{}{
"checks": strings.Join(opts, ","),
}
if len(opts) == 0 {
linterLogger.Fatalf("thelper: at least one option must be enabled")
}

var args []string
for k := range opts {
args = append(args, k)
}

cfgMap := map[string]map[string]interface{}{
a.Name: {
"checks": strings.Join(args, ","),
},
}

return goanalysis.NewLinter(
Expand All @@ -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 changes: 5 additions & 0 deletions test/testdata/configs/thelper.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
linters-settings:
thelper:
test:
name: false
begin: true
18 changes: 18 additions & 0 deletions test/testdata/thelper_with_options.go
Original file line number Diff line number Diff line change
@@ -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()
}