Skip to content

ginkgolinter: add suppress-async-assertion option #3735

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
Mar 26, 2023
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
4 changes: 4 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ linters-settings:
# Default: false
suppress-compare-assertion: true

# Suppress the function all in async assertion warning.
# Default: false
suppress-async-assertion: true

# Don't trigger warnings for HaveLen(0)
# Default: false
allow-havelen-zero: true
Expand Down
1 change: 1 addition & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ type GinkgoLinterSettings struct {
SuppressNilAssertion bool `mapstructure:"suppress-nil-assertion"`
SuppressErrAssertion bool `mapstructure:"suppress-err-assertion"`
SuppressCompareAssertion bool `mapstructure:"suppress-compare-assertion"`
SuppressAsyncAssertion bool `mapstructure:"suppress-async-assertion"`
AllowHaveLenZero bool `mapstructure:"allow-havelen-zero"`
}

Expand Down
1 change: 1 addition & 0 deletions pkg/golinters/ginkgolinter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func NewGinkgoLinter(cfg *config.GinkgoLinterSettings) *goanalysis.Linter {
"suppress-nil-assertion": cfg.SuppressNilAssertion,
"suppress-err-assertion": cfg.SuppressErrAssertion,
"suppress-compare-assertion": cfg.SuppressCompareAssertion,
"suppress-async-assertion": cfg.SuppressAsyncAssertion,
"allow-havelen-0": cfg.AllowHaveLenZero,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters-settings:
ginkgolinter:
suppress-async-assertion: true
12 changes: 12 additions & 0 deletions test/testdata/ginkgolinter/ginkgolinter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package ginkgolinter

import (
"errors"
"time"

. "github.com/onsi/gomega"
)

Expand Down Expand Up @@ -64,3 +66,13 @@ func WrongComparisonUsecase() {
p1, p2 := &x, &x
Expect(p1 == p2).To(Equal(true)) // want "ginkgo-linter: wrong comparison assertion; consider using .Expect\\(p1\\).To\\(BeIdenticalTo\\(p2\\)\\). instead"
}

func slowInt() int {
time.Sleep(time.Second)
return 42
}

func WrongEventuallyWithFunction() {
Eventually(slowInt).Should(Equal(42)) // valid
Eventually(slowInt()).Should(Equal(42)) // want "ginkgo-linter: use a function call in Eventually. This actually checks nothing, because Eventually receives the function returned value, instead of function itself, and this value is never changed; consider using .Eventually\\(slowInt\\)\\.Should\\(Equal\\(42\\)\\). instead"
}
12 changes: 12 additions & 0 deletions test/testdata/ginkgolinter/ginkgolinter_havelen0.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package ginkgolinter

import (
"errors"
"time"

. "github.com/onsi/gomega"
)

Expand Down Expand Up @@ -65,3 +67,13 @@ func WrongComparisonUsecase_havelen0() {
p1, p2 := &x, &x
Expect(p1 == p2).To(Equal(true)) // want "ginkgo-linter: wrong comparison assertion; consider using .Expect\\(p1\\).To\\(BeIdenticalTo\\(p2\\)\\). instead"
}

func slowInt_havelen0() int {
time.Sleep(time.Second)
return 42
}

func WrongEventuallyWithFunction_havelen0() {
Eventually(slowInt_havelen0).Should(Equal(42)) // valid
Eventually(slowInt_havelen0()).Should(Equal(42)) // want "ginkgo-linter: use a function call in Eventually. This actually checks nothing, because Eventually receives the function returned value, instead of function itself, and this value is never changed; consider using .Eventually\\(slowInt_havelen0\\)\\.Should\\(Equal\\(42\\)\\). instead"
}
80 changes: 80 additions & 0 deletions test/testdata/ginkgolinter/ginkgolinter_suppress_async.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//golangcitest:config_path configs/ginkgolinter_suppress_async.yml
//golangcitest:args --disable-all -Eginkgolinter
package ginkgolinter

import (
"errors"
"time"

. "github.com/onsi/gomega"
)

func LenUsecase_async() {
var fakeVarUnderTest []int
Expect(fakeVarUnderTest).Should(BeEmpty()) // valid
Expect(fakeVarUnderTest).ShouldNot(HaveLen(5)) // valid

Expect(len(fakeVarUnderTest)).Should(Equal(0)) // want "ginkgo-linter: wrong length assertion; consider using .Expect\\(fakeVarUnderTest\\)\\.Should\\(BeEmpty\\(\\)\\). instead"
Expect(len(fakeVarUnderTest)).ShouldNot(Equal(2)) // want "ginkgo-linter: wrong length assertion; consider using .Expect\\(fakeVarUnderTest\\)\\.ShouldNot\\(HaveLen\\(2\\)\\). instead"
Expect(len(fakeVarUnderTest)).To(BeNumerically("==", 0)) // // want "ginkgo-linter: wrong length assertion; consider using .Expect\\(fakeVarUnderTest\\)\\.To\\(BeEmpty\\(\\)\\). instead"

fakeVarUnderTest = append(fakeVarUnderTest, 3)
Expect(len(fakeVarUnderTest)).ShouldNot(Equal(0)) // want "ginkgo-linter: wrong length assertion; consider using .Expect\\(fakeVarUnderTest\\)\\.ShouldNot\\(BeEmpty\\(\\)\\). instead"
Expect(len(fakeVarUnderTest)).Should(Equal(1)) // want "ginkgo-linter: wrong length assertion; consider using .Expect\\(fakeVarUnderTest\\)\\.Should\\(HaveLen\\(1\\)\\). instead"
Expect(len(fakeVarUnderTest)).To(BeNumerically(">", 0)) // want "ginkgo-linter: wrong length assertion; consider using .Expect\\(fakeVarUnderTest\\)\\.ToNot\\(BeEmpty\\(\\)\\). instead"
Expect(len(fakeVarUnderTest)).To(BeNumerically(">=", 1)) // want "ginkgo-linter: wrong length assertion; consider using .Expect\\(fakeVarUnderTest\\)\\.ToNot\\(BeEmpty\\(\\)\\). instead"
Expect(len(fakeVarUnderTest)).To(BeNumerically("!=", 0)) // want "ginkgo-linter: wrong length assertion; consider using .Expect\\(fakeVarUnderTest\\)\\.ToNot\\(BeEmpty\\(\\)\\). instead"
}

func NilUsecase_async() {
y := 5
x := &y
Expect(x == nil).To(Equal(true)) // want "ginkgo-linter: wrong nil assertion; consider using .Expect\\(x\\)\\.To\\(BeNil\\(\\)\\). instead"
Expect(nil == x).To(Equal(true)) // want "ginkgo-linter: wrong nil assertion; consider using .Expect\\(x\\)\\.To\\(BeNil\\(\\)\\). instead"
Expect(x != nil).To(Equal(true)) // want "ginkgo-linter: wrong nil assertion; consider using .Expect\\(x\\)\\.ToNot\\(BeNil\\(\\)\\). instead"
Expect(x == nil).To(BeTrue()) // want "ginkgo-linter: wrong nil assertion; consider using .Expect\\(x\\)\\.To\\(BeNil\\(\\)\\). instead"
Expect(x == nil).To(BeFalse()) // want "ginkgo-linter: wrong nil assertion; consider using .Expect\\(x\\)\\.ToNot\\(BeNil\\(\\)\\). instead"
}
func BooleanUsecase_async() {
x := true
Expect(x).To(Equal(true)) // want "ginkgo-linter: wrong boolean assertion; consider using .Expect\\(x\\)\\.To\\(BeTrue\\(\\)\\). instead"
x = false
Expect(x).To(Equal(false)) // want "ginkgo-linter: wrong boolean assertion; consider using .Expect\\(x\\)\\.To\\(BeFalse\\(\\)\\). instead"
}

func ErrorUsecase_async() {
err := errors.New("fake error")
funcReturnsErr := func() error { return err }

Expect(err).To(BeNil()) // want "ginkgo-linter: wrong error assertion; consider using .Expect\\(err\\)\\.ToNot\\(HaveOccurred\\(\\)\\). instead"
Expect(err == nil).To(Equal(true)) // want "ginkgo-linter: wrong error assertion; consider using .Expect\\(err\\)\\.ToNot\\(HaveOccurred\\(\\)\\). instead"
Expect(err == nil).To(BeFalse()) // want "ginkgo-linter: wrong error assertion; consider using .Expect\\(err\\)\\.To\\(HaveOccurred\\(\\)\\). instead"
Expect(err != nil).To(BeTrue()) // want "ginkgo-linter: wrong error assertion; consider using .Expect\\(err\\)\\.To\\(HaveOccurred\\(\\)\\). instead"
Expect(funcReturnsErr()).To(BeNil()) // want "ginkgo-linter: wrong error assertion; consider using .Expect\\(funcReturnsErr\\(\\)\\)\\.To\\(Succeed\\(\\)\\). instead"
}

func HaveLen0Usecase_async() {
x := make([]string, 0)
Expect(x).To(HaveLen(0)) // want "ginkgo-linter: wrong length assertion; consider using .Expect\\(x\\)\\.To\\(BeEmpty\\(\\)\\). instead"
}

func WrongComparisonUsecase_async() {
x := 8
Expect(x == 8).To(BeTrue()) // want "ginkgo-linter: wrong comparison assertion; consider using .Expect\\(x\\)\\.To\\(Equal\\(8\\)\\). instead"
Expect(x < 9).To(BeTrue()) // want "ginkgo-linter: wrong comparison assertion; consider using .Expect\\(x\\)\\.To\\(BeNumerically\\(\"<\", 9\\)\\). instead"
Expect(x < 7).To(Equal(false)) // want "ginkgo-linter: wrong comparison assertion; consider using .Expect\\(x\\)\\.ToNot\\(BeNumerically\\(\"<\", 7\\)\\). instead"

p1, p2 := &x, &x
Expect(p1 == p2).To(Equal(true)) // want "ginkgo-linter: wrong comparison assertion; consider using .Expect\\(p1\\).To\\(BeIdenticalTo\\(p2\\)\\). instead"
}

func slowInt_async() int {
time.Sleep(time.Second)
return 42
}

// WrongEventuallyWithFunction_async Should trigger no warning
func WrongEventuallyWithFunction_async() {
Eventually(slowInt_async).Should(Equal(42)) // valid
Eventually(slowInt_async()).Should(Equal(42)) // suppressed
}
12 changes: 12 additions & 0 deletions test/testdata/ginkgolinter/ginkgolinter_suppress_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package ginkgolinter

import (
"errors"
"time"

. "github.com/onsi/gomega"
)

Expand Down Expand Up @@ -66,3 +68,13 @@ func WrongComparisonUsecase_compare() {
p1, p2 := &x, &x
Expect(p1 == p2).To(Equal(true))
}

func slowInt_compare() int {
time.Sleep(time.Second)
return 42
}

func WrongEventuallyWithFunction_compare() {
Eventually(slowInt_compare).Should(Equal(42)) // valid
Eventually(slowInt_compare()).Should(Equal(42)) // want "ginkgo-linter: use a function call in Eventually. This actually checks nothing, because Eventually receives the function returned value, instead of function itself, and this value is never changed; consider using .Eventually\\(slowInt_compare\\)\\.Should\\(Equal\\(42\\)\\). instead"
}
12 changes: 12 additions & 0 deletions test/testdata/ginkgolinter/ginkgolinter_suppress_err.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package ginkgolinter

import (
"errors"
"time"

. "github.com/onsi/gomega"
)

Expand Down Expand Up @@ -66,3 +68,13 @@ func WrongComparisonUsecase_err() {
p1, p2 := &x, &x
Expect(p1 == p2).To(Equal(true)) // want "ginkgo-linter: wrong comparison assertion; consider using .Expect\\(p1\\).To\\(BeIdenticalTo\\(p2\\)\\). instead"
}

func slowInt_err() int {
time.Sleep(time.Second)
return 42
}

func WrongEventuallyWithFunction_err() {
Eventually(slowInt_err).Should(Equal(42)) // valid
Eventually(slowInt_err()).Should(Equal(42)) // want "ginkgo-linter: use a function call in Eventually. This actually checks nothing, because Eventually receives the function returned value, instead of function itself, and this value is never changed; consider using .Eventually\\(slowInt_err\\)\\.Should\\(Equal\\(42\\)\\). instead"
}
12 changes: 12 additions & 0 deletions test/testdata/ginkgolinter/ginkgolinter_suppress_len.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package ginkgolinter

import (
"errors"
"time"

. "github.com/onsi/gomega"
)

Expand Down Expand Up @@ -66,3 +68,13 @@ func WrongComparisonUsecase_len() {
p1, p2 := &x, &x
Expect(p1 == p2).To(Equal(true)) // want "ginkgo-linter: wrong comparison assertion; consider using .Expect\\(p1\\).To\\(BeIdenticalTo\\(p2\\)\\). instead"
}

func slowInt_len() int {
time.Sleep(time.Second)
return 42
}

func WrongEventuallyWithFunction_len() {
Eventually(slowInt_len).Should(Equal(42)) // valid
Eventually(slowInt_len()).Should(Equal(42)) // want "ginkgo-linter: use a function call in Eventually. This actually checks nothing, because Eventually receives the function returned value, instead of function itself, and this value is never changed; consider using .Eventually\\(slowInt_len\\)\\.Should\\(Equal\\(42\\)\\). instead"
}
12 changes: 12 additions & 0 deletions test/testdata/ginkgolinter/ginkgolinter_suppress_nil.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package ginkgolinter

import (
"errors"
"time"

. "github.com/onsi/gomega"
)

Expand Down Expand Up @@ -66,3 +68,13 @@ func WrongComparisonUsecase_nil() {
p1, p2 := &x, &x
Expect(p1 == p2).To(Equal(true)) // want "ginkgo-linter: wrong comparison assertion; consider using .Expect\\(p1\\).To\\(BeIdenticalTo\\(p2\\)\\). instead"
}

func slowInt_nil() int {
time.Sleep(time.Second)
return 42
}

func WrongEventuallyWithFunction_nil() {
Eventually(slowInt_nil).Should(Equal(42)) // valid
Eventually(slowInt_nil()).Should(Equal(42)) // want "ginkgo-linter: use a function call in Eventually. This actually checks nothing, because Eventually receives the function returned value, instead of function itself, and this value is never changed; consider using .Eventually\\(slowInt_nil\\)\\.Should\\(Equal\\(42\\)\\). instead"
}
6 changes: 3 additions & 3 deletions test/testdata/ginkgolinter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module ginkgolinter

go 1.19

require github.com/onsi/gomega v1.24.1
require github.com/onsi/gomega v1.27.4

require (
github.com/google/go-cmp v0.5.9 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/text v0.8.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
19 changes: 11 additions & 8 deletions test/testdata/ginkgolinter/go.sum

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