Skip to content

Commit

Permalink
update checking target and execution condition
Browse files Browse the repository at this point in the history
  • Loading branch information
sivchari committed Sep 16, 2021
1 parent e4e65fb commit b386e5c
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,9 @@ linters-settings:
tenv:
# The following configurations enable checks prior to Go 1.17.
force: false
# The all option will run against all method in test file.
# By default, only methods that take *testing.T, *testing.B, and testing.TB as arguments are checked.
all: false

unparam:
# Inspect exported functions, default is false. Set to true if no external program/library imports your code.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ require (
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
github.com/shirou/gopsutil/v3 v3.21.8
github.com/sirupsen/logrus v1.8.1
github.com/sivchari/tenv v1.0.5
github.com/sivchari/tenv v1.1.6
github.com/sonatard/noctx v0.0.1
github.com/sourcegraph/go-diff v0.6.1
github.com/spf13/cobra v1.2.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ type ThelperSettings struct {

type TenvSettings struct {
Force bool `mapstructure:"force"`
All bool `mapstructure:"all"`
}

type UnparamSettings struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/golinters/tenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func NewTenv(settings *config.TenvSettings) *goanalysis.Linter {
cfg = map[string]map[string]interface{}{
a.Name: {
tenv.F: settings.Force,
tenv.A: settings.All,
},
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/testdata/configs/tenv_all_force.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters-settings:
tenv:
force: true
all: true
File renamed without changes.
41 changes: 41 additions & 0 deletions test/testdata/tenv_all_force_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// args: -Etenv
// config_path: testdata/configs/tenv_all_force.yml
package testdata

import (
"os"
"testing"
)

var (
e = os.Setenv("a", "b") // ERROR "variable e is not using testing.Setenv"
)

func setup() {
os.Setenv("a", "b") // ERROR "func setup is not using testing.Setenv"
err := os.Setenv("a", "b") // ERROR "func setup is not using testing.Setenv"
if err != nil {
_ = err
}
}

func TestF(t *testing.T) {
os.Setenv("a", "b") // ERROR "func TestF is not using testing.Setenv"
if err := os.Setenv("a", "b"); err != nil { // ERROR "func TestF is not using testing.Setenv"
_ = err
}
}

func BenchmarkF(b *testing.B) {
os.Setenv("a", "b") // ERROR "func BenchmarkF is not using testing.Setenv"
if err := os.Setenv("a", "b"); err != nil { // ERROR "func BenchmarkF is not using testing.Setenv"
_ = err
}
}

func testTB(tb testing.TB) {
os.Setenv("a", "b") // ERROR "func testTB is not using testing.Setenv"
if err := os.Setenv("a", "b"); err != nil { // ERROR "func testTB is not using testing.Setenv"
_ = err
}
}
22 changes: 19 additions & 3 deletions test/testdata/tenv_force_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// args: -Etenv
// config_path: testdata/configs/tenv_force.yml
package testdata

import (
"os"
"testing"
)

var (
Expand All @@ -17,9 +19,23 @@ func setup() {
}
}

func TestF() {
os.Setenv("a", "b") // OK
if err := os.Setenv("a", "b"); err != nil { // OK
func TestF(t *testing.T) {
os.Setenv("a", "b") // ERROR "func TestF is not using testing.Setenv"
if err := os.Setenv("a", "b"); err != nil { // ERROR "func TestF is not using testing.Setenv"
_ = err
}
}

func BenchmarkF(b *testing.B) {
os.Setenv("a", "b") // ERROR "func BenchmarkF is not using testing.Setenv"
if err := os.Setenv("a", "b"); err != nil { // ERROR "func BenchmarkF is not using testing.Setenv"
_ = err
}
}

func testTB(tb testing.TB) {
os.Setenv("a", "b") // ERROR "func testTB is not using testing.Setenv"
if err := os.Setenv("a", "b"); err != nil { // ERROR "func testTB is not using testing.Setenv"
_ = err
}
}
28 changes: 21 additions & 7 deletions test/testdata/tenv_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
// args: -Etenv
// config_path: testdata/configs/tenv.yml
package testdata

import (
"os"
"testing"
)

var (
e = os.Setenv("a", "b") // ERROR "variable e is not using t.Setenv"
e = os.Setenv("a", "b") // OK
)

func setup() {
os.Setenv("a", "b") // ERROR "func setup is not using t.Setenv"
err := os.Setenv("a", "b") // ERROR "func setup is not using t.Setenv"
os.Setenv("a", "b") // OK
err := os.Setenv("a", "b") // OK
if err != nil {
_ = err
}
}

func TestF() {
os.Setenv("a", "b") // ERROR "func TestF is not using t.Setenv"
if err := os.Setenv("a", "b"); err != nil { // ERROR "func TestF is not using t.Setenv"
func TestF(t *testing.T) {
os.Setenv("a", "b") // OK
if err := os.Setenv("a", "b"); err != nil { // OK
_ = err
}
}

func BenchmarkF(b *testing.B) {
os.Setenv("a", "b") // OK
if err := os.Setenv("a", "b"); err != nil { // OK
_ = err
}
}

func testTB(tb testing.TB) {
os.Setenv("a", "b") // OK
if err := os.Setenv("a", "b"); err != nil { // OK
_ = err
}
}

0 comments on commit b386e5c

Please sign in to comment.