Skip to content
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

feat: add gomegacheck #3279

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,7 @@ linters:
- goheader
- goimports
- golint
- gomegacheck
- gomnd
- gomoddirectives
- gomodguard
Expand Down Expand Up @@ -2099,6 +2100,7 @@ linters:
- goheader
- goimports
- golint
- gomegacheck
- gomnd
- gomoddirectives
- gomodguard
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,5 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect
)

require github.com/gardener/gardener/hack/tools/gomegacheck v0.0.0-20221004145958-3f57fd0f4151
2 changes: 2 additions & 0 deletions go.sum

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

19 changes: 19 additions & 0 deletions pkg/golinters/gomegacheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package golinters

import (
"github.com/gardener/gardener/hack/tools/gomegacheck/pkg/gomegacheck"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewGomegaCheck() *goanalysis.Linter {
a := gomegacheck.Analyzer

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeWholeProgram)
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithURL("https://github.com/bombsimon/wsl"),

linter.NewConfig(golinters.NewGomegaCheck()).
WithSince("v1.51.0").
WithLoadForGoAnalysis().
WithPresets(linter.PresetBugs).
WithURL("https://github.com/gardener/gardener/tree/master/hack/tools/gomegacheck"),

// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
linter.NewConfig(golinters.NewNoLintLint(noLintLintCfg)).
WithSince("v1.26.0").
Expand Down
1 change: 1 addition & 0 deletions test/linters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestTypecheck(t *testing.T) {
func TestSourcesFromTestdataSubDir(t *testing.T) {
subDirs := []string{
"loggercheck",
"gomegacheck",
}

for _, dir := range subDirs {
Expand Down
12 changes: 12 additions & 0 deletions test/testdata/gomegacheck/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module gomegacheck

go 1.19

require github.com/onsi/gomega v1.20.2

require (
github.com/google/go-cmp v0.5.8 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 14 additions & 0 deletions test/testdata/gomegacheck/go.sum

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

204 changes: 204 additions & 0 deletions test/testdata/gomegacheck/gomegacheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
//golangcitest:args -Egomegacheck
package gomegacheck

import (
"time"

"github.com/onsi/gomega"
"github.com/onsi/gomega/types"
)

// Copyright (c) 2022 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This is a test file for executing unit tests for gomegacheck using golang.org/x/tools/go/analysis/analysistest.

func expect() {
gomega.Expect("foo").To(Match())
gomega.Expect("foo").Should(Match())
gomega.Expect("foo").NotTo(Match())
gomega.Expect("foo").ToNot(Match())
gomega.Expect("foo").ShouldNot(Match())
gomega.Expect("foo").WithOffset(1).To(Match())
gomega.Expect("foo").WithOffset(1).Should(Match())
gomega.Expect("foo").WithOffset(1).NotTo(Match())
gomega.Expect("foo").WithOffset(1).ToNot(Match())
gomega.Expect("foo").WithOffset(1).ShouldNot(Match())

gomega.Expect("foo") // want `gomega.Assertion is missing a call to one of Should, ShouldNot, To, ToNot, NotTo`
gomega.Expect("foo").WithOffset(1) // want `gomega.Assertion is missing a call to one of Should, ShouldNot, To, ToNot, NotTo`

gomega.Expect("foo").To(Match(), Match()) // want `GomegaMatcher passed to optionalDescription param, you can only pass one matcher to each assertion`
gomega.Expect("foo").Should(Match(), Match()) // want `GomegaMatcher passed to optionalDescription param, you can only pass one matcher to each assertion`
gomega.Expect("foo").NotTo(Match(), Match()) // want `GomegaMatcher passed to optionalDescription param, you can only pass one matcher to each assertion`
gomega.Expect("foo").ToNot(Match(), Match()) // want `GomegaMatcher passed to optionalDescription param, you can only pass one matcher to each assertion`
gomega.Expect("foo").ShouldNot(Match(), Match()) // want `GomegaMatcher passed to optionalDescription param, you can only pass one matcher to each assertion`
gomega.Expect("foo").WithOffset(1).To(Match(), Match()) // want `GomegaMatcher passed to optionalDescription param, you can only pass one matcher to each assertion`
}

func expectWithOffset() {
gomega.ExpectWithOffset(1, "foo").To(Match())
gomega.ExpectWithOffset(1, "foo").Should(Match())
gomega.ExpectWithOffset(1, "foo").NotTo(Match())
gomega.ExpectWithOffset(1, "foo").ToNot(Match())
gomega.ExpectWithOffset(1, "foo").ShouldNot(Match())
gomega.ExpectWithOffset(1, "foo").WithOffset(1).To(Match())
gomega.ExpectWithOffset(1, "foo").WithOffset(1).Should(Match())
gomega.ExpectWithOffset(1, "foo").WithOffset(1).NotTo(Match())
gomega.ExpectWithOffset(1, "foo").WithOffset(1).ToNot(Match())
gomega.ExpectWithOffset(1, "foo").WithOffset(1).ShouldNot(Match())

gomega.ExpectWithOffset(1, "foo") // want `gomega.Assertion is missing a call to one of Should, ShouldNot, To, ToNot, NotTo`
gomega.ExpectWithOffset(1, "foo").WithOffset(1) // want `gomega.Assertion is missing a call to one of Should, ShouldNot, To, ToNot, NotTo`
}

func omega() {
gomega.Ω("foo").To(Match())
gomega.Ω("foo").Should(Match())
gomega.Ω("foo").NotTo(Match())
gomega.Ω("foo").ToNot(Match())
gomega.Ω("foo").ShouldNot(Match())
gomega.Ω("foo").WithOffset(1).To(Match())
gomega.Ω("foo").WithOffset(1).Should(Match())
gomega.Ω("foo").WithOffset(1).NotTo(Match())
gomega.Ω("foo").WithOffset(1).ToNot(Match())
gomega.Ω("foo").WithOffset(1).ShouldNot(Match())

gomega.Ω("foo") // want `gomega.Assertion is missing a call to one of Should, ShouldNot, To, ToNot, NotTo`
gomega.Ω("foo").WithOffset(1) // want `gomega.Assertion is missing a call to one of Should, ShouldNot, To, ToNot, NotTo`
}

func eventually() {
c := make(chan bool)

gomega.Eventually(c).Should(Match())
gomega.Eventually(c).ShouldNot(Match())
gomega.Eventually(c).WithOffset(1).Should(Match())
gomega.Eventually(c).WithOffset(1).ShouldNot(Match())
gomega.Eventually(c).WithTimeout(time.Second).Should(Match())
gomega.Eventually(c).WithTimeout(time.Second).ShouldNot(Match())
gomega.Eventually(c).WithPolling(time.Second).Should(Match())
gomega.Eventually(c).WithPolling(time.Second).ShouldNot(Match())

gomega.Eventually(c) // want `gomega.AsyncAssertion is missing a call to one of Should, ShouldNot`
gomega.Eventually(c).WithOffset(1) // want `gomega.AsyncAssertion is missing a call to one of Should, ShouldNot`
gomega.Eventually(c).WithTimeout(time.Second) // want `gomega.AsyncAssertion is missing a call to one of Should, ShouldNot`
gomega.Eventually(c).WithPolling(time.Second) // want `gomega.AsyncAssertion is missing a call to one of Should, ShouldNot`
}

func consistently() {
c := make(chan bool)

gomega.Consistently(c).Should(Match())
gomega.Consistently(c).ShouldNot(Match())
gomega.Consistently(c).WithOffset(1).Should(Match())
gomega.Consistently(c).WithOffset(1).ShouldNot(Match())
gomega.Consistently(c).WithTimeout(time.Second).Should(Match())
gomega.Consistently(c).WithTimeout(time.Second).ShouldNot(Match())
gomega.Consistently(c).WithPolling(time.Second).Should(Match())
gomega.Consistently(c).WithPolling(time.Second).ShouldNot(Match())

gomega.Consistently(c) // want `gomega.AsyncAssertion is missing a call to one of Should, ShouldNot`
gomega.Consistently(c).WithOffset(1) // want `gomega.AsyncAssertion is missing a call to one of Should, ShouldNot`
gomega.Consistently(c).WithTimeout(time.Second) // want `gomega.AsyncAssertion is missing a call to one of Should, ShouldNot`
gomega.Consistently(c).WithPolling(time.Second) // want `gomega.AsyncAssertion is missing a call to one of Should, ShouldNot`
}

func assertionInAsync() {
gomega.Eventually(func(g gomega.Gomega) {
g.Expect("foo").To(Match())
}).Should(Match())
gomega.Eventually(func(g gomega.Gomega) {
g.Expect("foo").Should(Match())
}).Should(Match())
gomega.Eventually(func(g gomega.Gomega) {
g.Expect("foo").NotTo(Match())
}).Should(Match())
gomega.Eventually(func(g gomega.Gomega) {
g.Expect("foo").ToNot(Match())
}).Should(Match())
gomega.Eventually(func(g gomega.Gomega) {
g.Expect("foo").ShouldNot(Match())
}).Should(Match())
gomega.Eventually(func(g gomega.Gomega) {
g.Expect("foo").WithOffset(1).To(Match())
}).Should(Match())
gomega.Eventually(func(g gomega.Gomega) {
g.Expect("foo").WithOffset(1).Should(Match())
}).Should(Match())
gomega.Eventually(func(g gomega.Gomega) {
g.Expect("foo").WithOffset(1).NotTo(Match())
}).Should(Match())
gomega.Eventually(func(g gomega.Gomega) {
g.Expect("foo").WithOffset(1).ToNot(Match())
}).Should(Match())
gomega.Eventually(func(g gomega.Gomega) {
g.Expect("foo").WithOffset(1).ShouldNot(Match())
}).Should(Match())

gomega.Eventually(func(g gomega.Gomega) {
g.Expect("foo") // want `gomega.Assertion is missing a call to one of Should, ShouldNot, To, ToNot, NotTo`
}).Should(Match())
gomega.Eventually(func(g gomega.Gomega) {
g.Expect("foo").WithOffset(1) // want `gomega.Assertion is missing a call to one of Should, ShouldNot, To, ToNot, NotTo`
}).Should(Match())
}

// returning Assertion/AsyncAssertion should not yield an error.

func helper(actual interface{}) gomega.Assertion {
return gomega.Expect(actual).WithOffset(1)
}

func helperAsync(actual interface{}) gomega.AsyncAssertion {
return gomega.Eventually(actual).WithOffset(1)
}

// calling helpers should however yield an error

func callingHelper() {
helper("foo").To(Match())
helper("foo").Should(Match())
helper("foo").NotTo(Match())
helper("foo").ToNot(Match())
helper("foo").ShouldNot(Match())
helper("foo").WithOffset(1).To(Match())
helper("foo").WithOffset(1).Should(Match())
helper("foo").WithOffset(1).NotTo(Match())
helper("foo").WithOffset(1).ToNot(Match())
helper("foo").WithOffset(1).ShouldNot(Match())

helper("foo") // want `gomega.Assertion is missing a call to one of Should, ShouldNot, To, ToNot, NotTo`
helper("foo").WithOffset(1) // want `gomega.Assertion is missing a call to one of Should, ShouldNot, To, ToNot, NotTo`
}

func callingHelperAsync() {
helperAsync("foo").Should(Match())
helperAsync("foo").ShouldNot(Match())
helperAsync("foo").WithOffset(1).Should(Match())
helperAsync("foo").WithOffset(1).ShouldNot(Match())

helperAsync("foo") // want `gomega.AsyncAssertion is missing a call to one of Should, ShouldNot`
helperAsync("foo").WithOffset(1) // want `gomega.AsyncAssertion is missing a call to one of Should, ShouldNot`
}

func Match() types.GomegaMatcher {
return dummyMatcher{}
}

type dummyMatcher struct{}

func (m dummyMatcher) Match(actual interface{}) (success bool, err error) { return true, nil }
func (m dummyMatcher) FailureMessage(actual interface{}) (message string) { return "fail" }
func (m dummyMatcher) NegatedFailureMessage(actual interface{}) (message string) { return "whatever" }