-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new optional rule: force
Expect
with To
Added new rule: should not use `Expect` with the `Should` or `ShouldNot` assertion methods. This rule is not enabled by default. To enable it, use the `--force-expect-to=true` flag. The new rule does support auto fix.
- Loading branch information
Showing
10 changed files
with
196 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package ginkgolinter | ||
|
||
const doc = `enforces standards of using ginkgo and gomega | ||
or | ||
ginkgolinter version | ||
version: %s | ||
currently, the linter searches for following: | ||
* trigger a warning when using Eventually or Constantly with a function call. This is in order to prevent the case when | ||
using a function call instead of a function. Function call returns a value only once, and so the original value | ||
is tested again and again and is never changed. [Bug] | ||
* trigger a warning when comparing a pointer to a value. [Bug] | ||
* trigger a warning for missing assertion method: [Bug] | ||
Eventually(checkSomething) | ||
* trigger a warning when a ginkgo focus container (FDescribe, FContext, FWhen or FIt) is found. [Bug] | ||
* validate the MatchError gomega matcher [Bug] | ||
* trigger a warning when using the Equal or the BeIdentical matcher with two different types, as these matchers will | ||
fail in runtime. | ||
* wrong length assertions. We want to assert the item rather than its length. [Style] | ||
For example: | ||
Expect(len(x)).Should(Equal(1)) | ||
This should be replaced with: | ||
Expect(x)).Should(HavelLen(1)) | ||
* wrong nil assertions. We want to assert the item rather than a comparison result. [Style] | ||
For example: | ||
Expect(x == nil).Should(BeTrue()) | ||
This should be replaced with: | ||
Expect(x).Should(BeNil()) | ||
* wrong error assertions. For example: [Style] | ||
Expect(err == nil).Should(BeTrue()) | ||
This should be replaced with: | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
* wrong boolean comparison, for example: [Style] | ||
Expect(x == 8).Should(BeTrue()) | ||
This should be replaced with: | ||
Expect(x).Should(BeEqual(8)) | ||
* replaces Equal(true/false) with BeTrue()/BeFalse() [Style] | ||
* replaces HaveLen(0) with BeEmpty() [Style] | ||
* replaces Expect(...).Should(...) with Expect(...).To() [stype] | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package forceExpectTo | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("gomega var", func() { | ||
It("in a valid Eventually", func() { | ||
Eventually(func(g Gomega) { | ||
g.Expect("a").Should(HaveLen(1)) // want `ginkgo-linter: must not use Expect with Should; consider using .g\.Expect\("a"\)\.To\(HaveLen\(1\)\). instead` | ||
g.Expect(len("a")).Should(Equal(1)) // want `ginkgo-linter: wrong length assertion; consider using .g\.Expect\("a"\)\.Should\(HaveLen\(1\)\). instead` `ginkgo-linter: must not use Expect with Should; consider using .g\.Expect\("a"\)\.To\(HaveLen\(1\)\). instead` | ||
}).Should(Succeed()) | ||
}) | ||
|
||
It("in an invalid Eventually", func() { | ||
Eventually(func(g Gomega) { // want `ginkgo-linter: "Eventually": missing assertion method\. Expected "Should\(\)" or "ShouldNot\(\)"` | ||
g.Expect("a").Should(HaveLen(1)) // want `ginkgo-linter: must not use Expect with Should; consider using .g\.Expect\("a"\)\.To\(HaveLen\(1\)\). instead` | ||
g.Expect(len("a")).Should(Equal(1)) // want `ginkgo-linter: wrong length assertion; consider using .g\.Expect\("a"\)\.Should\(HaveLen\(1\)\). instead` `ginkgo-linter: must not use Expect with Should; consider using .g\.Expect\("a"\)\.To\(HaveLen\(1\)\). instead` | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package forceExpectTo | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
"github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("simple case", func() { | ||
It("just replace assertion method", func() { | ||
gomega.Expect("a").Should(gomega.HaveLen(1)) // want `ginkgo-linter: must not use Expect with Should; consider using .gomega\.Expect\("a"\)\.To\(gomega\.HaveLen\(1\)\). instead` | ||
gomega.Expect("a").ShouldNot(gomega.BeEmpty()) // want `ginkgo-linter: must not use Expect with ShouldNot; consider using .gomega\.Expect\("a"\)\.ToNot\(gomega\.BeEmpty\(\)\). instead` | ||
gomega.Expect("a").Should(gomega.Not(gomega.BeEmpty())) // want `ginkgo-linter: must not use Expect with ShouldNot; consider using .gomega\.Expect\("a"\)\.ToNot\(gomega\.BeEmpty\(\)\). instead` | ||
}) | ||
|
||
It("mix with len assertion", func() { | ||
gomega.Expect(len("a")).Should(gomega.Equal(1)) // want `ginkgo-linter: wrong length assertion; consider using .gomega\.Expect\("a"\)\.Should\(gomega\.HaveLen\(1\)\). instead` `ginkgo-linter: must not use Expect with Should; consider using .gomega\.Expect\("a"\)\.To\(gomega\.HaveLen\(1\)\). instead` | ||
gomega.Expect(len("a")).ShouldNot(gomega.Equal(0)) // want `ginkgo-linter: wrong length assertion; consider using .gomega\.Expect\("a"\)\.ShouldNot\(gomega\.BeEmpty\(\)\). instead` `ginkgo-linter: must not use Expect with ShouldNot; consider using .gomega\.Expect\("a"\)\.ToNot\(gomega\.BeEmpty\(\)\). instead` | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package forceExpectTo | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("simple case", func() { | ||
It("just replace assertion method", func() { | ||
Expect("a").Should(HaveLen(1)) // want `ginkgo-linter: must not use Expect with Should; consider using .Expect\("a"\)\.To\(HaveLen\(1\)\). instead` | ||
Expect("a").ShouldNot(BeEmpty()) // want `ginkgo-linter: must not use Expect with ShouldNot; consider using .Expect\("a"\)\.ToNot\(BeEmpty\(\)\). instead` | ||
Expect("a").Should(Not(BeEmpty())) // want `ginkgo-linter: must not use Expect with ShouldNot; consider using .Expect\("a"\)\.ToNot\(BeEmpty\(\)\). instead` | ||
Ω("a").Should(HaveLen(1)) | ||
Ω("a").ShouldNot(BeEmpty()) | ||
}) | ||
|
||
It("mix with len assertion", func() { | ||
Expect(len("a")).Should(Equal(1)) // want `ginkgo-linter: wrong length assertion; consider using .Expect\("a"\)\.Should\(HaveLen\(1\)\). instead` `ginkgo-linter: must not use Expect with Should; consider using .Expect\("a"\)\.To\(HaveLen\(1\)\). instead` | ||
Expect(len("a")).ShouldNot(Equal(0)) // want `ginkgo-linter: wrong length assertion; consider using .Expect\("a"\)\.ShouldNot\(BeEmpty\(\)\). instead` `ginkgo-linter: must not use Expect with ShouldNot; consider using .Expect\("a"\)\.ToNot\(BeEmpty\(\)\). instead` | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters