Skip to content

Commit

Permalink
don't need to specify =true in commandline flags anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
nunnatsa committed Dec 2, 2024
1 parent 6b3a585 commit 6f3281e
Show file tree
Hide file tree
Showing 27 changed files with 71 additions and 190 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ var _ = Describe("checking something", Focus, func() {
These container, or the `Focus` spec, must not be part of the final source code, and should only be used locally by the
developer.

***This rule is disabled by default***. Use the `--forbid-focus-container=true` command line flag to enable it.
***This rule is disabled by default***. Use the `--forbid-focus-container` command line flag to enable it.

### Comparing values from different types [BUG]

Expand All @@ -202,7 +202,7 @@ using casting, or use the `BeEquivalentTo` matcher.

The linter can't guess what is the best solution in each case, and so it won't auto-fix this warning.

To suppress this warning entirely, use the `--suppress-type-compare-assertion=true` command line parameter.
To suppress this warning entirely, use the `--suppress-type-compare-assertion` command line parameter.

To suppress a specific file or line, use the `// ginkgo-linter:ignore-type-compare-warning` comment (see [below](#suppress-warning-from-the-code))

Expand Down Expand Up @@ -274,7 +274,7 @@ a Gomega object as their first parameter, and returns nothing, e.g. this is a va
***Note***: This rule **does not** support auto-fix.

### Avoid Spec Pollution: Don't Initialize Variables in Container Nodes [BUG/STYLE]:
***Note***: Only applied when the `--forbid-spec-pollution=true` flag is set (disabled by default).
***Note***: Only applied when the `--forbid-spec-pollution` flag is set (disabled by default).

According to [ginkgo documentation](https://onsi.github.io/ginkgo/#avoid-spec-pollution-dont-initialize-variables-in-container-nodes),
no variable should be assigned within a container node (`Describe`, `Context`, `When` or their `F`, `P` or `X` forms)
Expand Down Expand Up @@ -451,7 +451,7 @@ Expect("abc").ShouldNot(BeEmpty()) // => Expect("abc").ToNot(BeEmpty())
```
This rule support auto fixing.

***This rule is disabled by default***. Use the `--force-expect-to=true` command line flag to enable it.
***This rule is disabled by default***. Use the `--force-expect-to` command line flag to enable it.

### Async timing interval: multiple timeout or polling intervals [STYLE]
***Note***: Only applied when the `suppress-async-assertion` flag is **not set** *and* the `validate-async-intervals`
Expand Down Expand Up @@ -522,20 +522,20 @@ will trigger a warning with a suggestion to replace the mather to
```go
Expect(myErrorFunc()).To(Succeed())
```
***This rule is disabled by default***. Use the `--force-succeed=true` command line flag to enable it.
***This rule is disabled by default***. Use the `--force-succeed` command line flag to enable it.

***Note***: This rule **does** support auto-fix, when the `--fix` command line parameter is used.

## Suppress the linter
### Suppress warning from command line
* Use the `--suppress-len-assertion=true` flag to suppress the wrong length and cap assertions warning
* Use the `--suppress-nil-assertion=true` flag to suppress the wrong nil assertion warning
* Use the `--suppress-err-assertion=true` flag to suppress the wrong error assertion warning
* Use the `--suppress-compare-assertion=true` flag to suppress the wrong comparison assertion warning
* Use the `--suppress-async-assertion=true` flag to suppress the function call in async assertion warning
* Use the `--forbid-focus-container=true` flag to activate the focused container assertion (deactivated by default)
* Use the `--suppress-type-compare-assertion=true` to suppress the type compare assertion warning
* Use the `--allow-havelen-0=true` flag to avoid warnings about `HaveLen(0)`; Note: this parameter is only supported from
* Use the `--suppress-len-assertion` flag to suppress the wrong length and cap assertions warning
* Use the `--suppress-nil-assertion` flag to suppress the wrong nil assertion warning
* Use the `--suppress-err-assertion` flag to suppress the wrong error assertion warning
* Use the `--suppress-compare-assertion` flag to suppress the wrong comparison assertion warning
* Use the `--suppress-async-assertion` flag to suppress the function call in async assertion warning
* Use the `--forbid-focus-container` flag to activate the focused container assertion (deactivated by default)
* Use the `--suppress-type-compare-assertion` to suppress the type compare assertion warning
* Use the `--allow-havelen-0` flag to avoid warnings about `HaveLen(0)`; Note: this parameter is only supported from
command line, and not from a comment.

### Suppress warning from the code
Expand Down
26 changes: 12 additions & 14 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,19 @@ func NewAnalyzer() *analysis.Analyzer {

a := NewAnalyzerWithConfig(config)

var ignored bool
a.Flags.Init("ginkgolinter", flag.ExitOnError)
a.Flags.Var(&config.SuppressLen, "suppress-len-assertion", "Suppress warning for wrong length assertions")
a.Flags.Var(&config.SuppressNil, "suppress-nil-assertion", "Suppress warning for wrong nil assertions")
a.Flags.Var(&config.SuppressErr, "suppress-err-assertion", "Suppress warning for wrong error assertions")
a.Flags.Var(&config.SuppressCompare, "suppress-compare-assertion", "Suppress warning for wrong comparison assertions")
a.Flags.Var(&config.SuppressAsync, "suppress-async-assertion", "Suppress warning for function call in async assertion, like Eventually")
a.Flags.Var(&config.ValidateAsyncIntervals, "validate-async-intervals", "best effort validation of async intervals (timeout and polling); ignored the suppress-async-assertion flag is true")
a.Flags.Var(&config.SuppressTypeCompare, "suppress-type-compare-assertion", "Suppress warning for comparing values from different types, like int32 and uint32")
a.Flags.Var(&config.AllowHaveLen0, "allow-havelen-0", "Do not warn for HaveLen(0); default = false")
a.Flags.Var(&config.ForceExpectTo, "force-expect-to", "force using `Expect` with `To`, `ToNot` or `NotTo`. reject using `Expect` with `Should` or `ShouldNot`; default = false (not forced)")
a.Flags.BoolVar(&ignored, "suppress-focus-container", true, "Suppress warning for ginkgo focus containers like FDescribe, FContext, FWhen or FIt. Deprecated and ignored: use --forbid-focus-container instead")
a.Flags.Var(&config.ForbidFocus, "forbid-focus-container", "trigger a warning for ginkgo focus containers like FDescribe, FContext, FWhen or FIt; default = false.")
a.Flags.Var(&config.ForbidSpecPollution, "forbid-spec-pollution", "trigger a warning for variable assignments in ginkgo containers like Describe, Context and When, instead of in BeforeEach(); default = false.")
a.Flags.Var(&config.ForceSucceedForFuncs, "force-succeed", "force using the Succeed matcher for error functions, and the HaveOccurred matcher for non-function error values")
a.Flags.BoolVar(&config.SuppressLen, "suppress-len-assertion", config.SuppressLen, "Suppress warning for wrong length assertions")
a.Flags.BoolVar(&config.SuppressNil, "suppress-nil-assertion", config.SuppressNil, "Suppress warning for wrong nil assertions")
a.Flags.BoolVar(&config.SuppressErr, "suppress-err-assertion", config.SuppressErr, "Suppress warning for wrong error assertions")
a.Flags.BoolVar(&config.SuppressCompare, "suppress-compare-assertion", config.SuppressCompare, "Suppress warning for wrong comparison assertions")
a.Flags.BoolVar(&config.SuppressAsync, "suppress-async-assertion", config.SuppressAsync, "Suppress warning for function call in async assertion, like Eventually")
a.Flags.BoolVar(&config.ValidateAsyncIntervals, "validate-async-intervals", config.ValidateAsyncIntervals, "best effort validation of async intervals (timeout and polling); ignored the suppress-async-assertion flag is true")
a.Flags.BoolVar(&config.SuppressTypeCompare, "suppress-type-compare-assertion", config.SuppressTypeCompare, "Suppress warning for comparing values from different types, like int32 and uint32")
a.Flags.BoolVar(&config.AllowHaveLen0, "allow-havelen-0", config.AllowHaveLen0, "Do not warn for HaveLen(0); default = false")
a.Flags.BoolVar(&config.ForceExpectTo, "force-expect-to", config.ForceExpectTo, "force using `Expect` with `To`, `ToNot` or `NotTo`. reject using `Expect` with `Should` or `ShouldNot`; default = false (not forced)")
a.Flags.BoolVar(&config.ForbidFocus, "forbid-focus-container", config.ForbidFocus, "trigger a warning for ginkgo focus containers like FDescribe, FContext, FWhen or FIt; default = false.")
a.Flags.BoolVar(&config.ForbidSpecPollution, "forbid-spec-pollution", config.ForbidSpecPollution, "trigger a warning for variable assignments in ginkgo containers like Describe, Context and When, instead of in BeforeEach(); default = false.")
a.Flags.BoolVar(&config.ForceSucceedForFuncs, "force-succeed", config.ForceSucceedForFuncs, "force using the Succeed matcher for error functions, and the HaveOccurred matcher for non-function error values")

return a
}
Expand Down
2 changes: 1 addition & 1 deletion internal/expression/actual/actualarg.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package actual

import (
"github.com/nunnatsa/ginkgolinter/internal/gomegahandler"
"go/ast"
"go/token"
gotypes "go/types"

"golang.org/x/tools/go/analysis"

"github.com/nunnatsa/ginkgolinter/internal/expression/value"
"github.com/nunnatsa/ginkgolinter/internal/gomegahandler"
"github.com/nunnatsa/ginkgolinter/internal/gomegainfo"
"github.com/nunnatsa/ginkgolinter/internal/reverseassertion"
)
Expand Down
4 changes: 2 additions & 2 deletions internal/ginkgohandler/handling.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ const (
func handleGinkgoSpecs(expr ast.Expr, config types.Config, pass *analysis.Pass, ginkgoHndlr Handler) bool {
goDeeper := false
if exp, ok := expr.(*ast.CallExpr); ok {
if bool(config.ForbidFocus) && checkFocusContainer(pass, ginkgoHndlr, exp) {
if config.ForbidFocus && checkFocusContainer(pass, ginkgoHndlr, exp) {
goDeeper = true
}

if bool(config.ForbidSpecPollution) && checkAssignmentsInContainer(pass, ginkgoHndlr, exp) {
if config.ForbidSpecPollution && checkAssignmentsInContainer(pass, ginkgoHndlr, exp) {
goDeeper = true
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/rules/asyncfunccallrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const valueInEventually = "use a function call in %[1]s. This actually checks no
type AsyncFuncCallRule struct{}

func (r AsyncFuncCallRule) isApplied(gexp *expression.GomegaExpression, config types.Config) bool {
if bool(config.SuppressAsync) || !gexp.IsAsync() {
if config.SuppressAsync || !gexp.IsAsync() {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion internal/rules/asynctimeintervalsrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
type AsyncTimeIntervalsRule struct{}

func (r AsyncTimeIntervalsRule) isApplied(gexp *expression.GomegaExpression, config types.Config) bool {
return !bool(config.SuppressAsync) && bool(config.ValidateAsyncIntervals) && gexp.IsAsync()
return !config.SuppressAsync && config.ValidateAsyncIntervals && gexp.IsAsync()
}

func (r AsyncTimeIntervalsRule) Apply(gexp *expression.GomegaExpression, config types.Config, reportBuilder *reports.Builder) bool {
Expand Down
2 changes: 1 addition & 1 deletion internal/rules/equaldifferenttypesrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const compareDifferentTypes = "use %[1]s with different types: Comparing %[2]s w
type EqualDifferentTypesRule struct{}

func (r EqualDifferentTypesRule) isApplied(config types.Config) bool {
return !bool(config.SuppressTypeCompare)
return !config.SuppressTypeCompare
}

func (r EqualDifferentTypesRule) Apply(gexp *expression.GomegaExpression, config types.Config, reportBuilder *reports.Builder) bool {
Expand Down
2 changes: 1 addition & 1 deletion internal/rules/equalnilrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
type EqualNilRule struct{}

func (r EqualNilRule) isApplied(gexp *expression.GomegaExpression, config types.Config) bool {
return !bool(config.SuppressNil) &&
return !config.SuppressNil &&
gexp.MatcherTypeIs(matcher.EqualValueMatcherType)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/rules/havelen0.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type HaveLen0 struct{}

func (r *HaveLen0) isApplied(gexp *expression.GomegaExpression, config types.Config) bool {
return gexp.MatcherTypeIs(matcher.HaveLenZeroMatcherType) && !bool(config.AllowHaveLen0)
return gexp.MatcherTypeIs(matcher.HaveLenZeroMatcherType) && !config.AllowHaveLen0
}

func (r *HaveLen0) Apply(gexp *expression.GomegaExpression, config types.Config, reportBuilder *reports.Builder) bool {
Expand Down
2 changes: 1 addition & 1 deletion internal/rules/haveoccurredrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (r HaveOccurredRule) Apply(gexp *expression.GomegaExpression, config types.
return true
}

if bool(config.ForceSucceedForFuncs) && gexp.GetActualArg().(*actual.ErrPayload).IsFunc() {
if config.ForceSucceedForFuncs && gexp.GetActualArg().(*actual.ErrPayload).IsFunc() {
gexp.ReverseAssertionFuncLogic()
gexp.SetMatcherSucceed()
reportBuilder.AddIssue(true, "prefer using the Succeed matcher for error function, instead of HaveOccurred")
Expand Down
4 changes: 2 additions & 2 deletions internal/rules/nilcomparerule.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func (r NilCompareRule) isApplied(gexp *expression.GomegaExpression, config type
return false, false
}

isErr := actl.IsError() && !bool(config.SuppressErr)
isErr := actl.IsError() && !config.SuppressErr

if !isErr && bool(config.SuppressNil) {
if !isErr && config.SuppressNil {
return isErr, false
}

Expand Down
2 changes: 1 addition & 1 deletion internal/rules/succeedrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (r SucceedRule) Apply(gexp *expression.GomegaExpression, config types.Confi
return true
}

if bool(config.ForceSucceedForFuncs) && !gexp.GetActualArg().(*actual.ErrPayload).IsFunc() {
if config.ForceSucceedForFuncs && !gexp.GetActualArg().(*actual.ErrPayload).IsFunc() {
gexp.ReverseAssertionFuncLogic()
gexp.SetMatcherHaveOccurred()

Expand Down
4 changes: 2 additions & 2 deletions tests/testdata/asyncerr.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ stderr -count=1 'wrong error assertion.'
stderr -count=1 'Success matcher does not support multiple values'

# also enable the force succeed rule
! exec ginkgolinter --force-succeed=true errassertion
! exec ginkgolinter --force-succeed errassertion
! stdout .
stderr -count=1 'Success matcher only support a single error value, or function with Gomega as its first parameter'
stderr -count=1 'wrong error assertion.'
Expand All @@ -15,7 +15,7 @@ stderr -count=1 'prefer using the HaveOccurred matcher for non-function error va
stderr -count=1 'prefer using the Succeed matcher for error function, instead of HaveOccurred.'

# run with -fix, expect wrong error assertion errors
! exec ginkgolinter --fix --force-succeed=true errassertion
! exec ginkgolinter --fix --force-succeed errassertion
! stdout .
stderr -count=1 'Success matcher only support a single error value, or function with Gomega as its first parameter'
stderr -count=1 'wrong error assertion.'
Expand Down
2 changes: 1 addition & 1 deletion tests/testdata/asynfunc.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ stderr -count=1 'Eventually\(withoutArguments\).Should\(Equal\(42\)\)'
stderr -count=1 'Eventually\(withArguments\).WithArguments\(4, 2\).Should\(Equal\(42\)\)'

# suppress async assersion checks - expect no errors
exec ginkgolinter --suppress-async-assertion=true async
exec ginkgolinter --suppress-async-assertion async
! stdout .
! stderr .

Expand Down
2 changes: 1 addition & 1 deletion tests/testdata/compare.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ stderr -count=1 'Expect\(exampleInt\).ToNot\(BeZero\(\)\)'
stderr -count=1 'Expect\(exampleInt\).To\(BeNumerically\(">", 4\)\)'

# suppress compare assertion, still get some matcher-only errors
! exec ginkgolinter --suppress-compare-assertion=true comparison
! exec ginkgolinter --suppress-compare-assertion comparison
! stdout .
stderr -count=1 'Expect\(exampleInt == 42\).To\(BeTrue\(\)\)'
stderr -count=1 'Expect\(exampleInt != 0\).To\(BeTrue\(\)\)'
Expand Down
2 changes: 1 addition & 1 deletion tests/testdata/comparetypes.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
stderr -count=1 'use Equal with different types: Comparing uint64 with int; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)'

# suppress compare-type assertion, expect no additional assertion errors
exec ginkgolinter --suppress-type-compare-assertion=true comparetypes
exec ginkgolinter --suppress-type-compare-assertion comparetypes
! stdout .
! stderr .

Expand Down
4 changes: 2 additions & 2 deletions tests/testdata/err.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ stderr -count=1 'asserting a non-error type with Succeed matcher'
stderr -count=1 'the Success matcher does not support multiple values'

# suppress error assertion, still get some comparison assertion errors
! exec ginkgolinter --suppress-err-assertion=true errassertion
! exec ginkgolinter --suppress-err-assertion errassertion
! stdout .
stderr -count=2 'wrong nil assertion. Consider using `Expect\(err\).ToNot\(BeNil\(\)\)` instead'
stderr -count=1 'asserting a non-error type with Succeed matcher'
stderr -count=1 'the Success matcher does not support multiple values'

# suppress both error and nil assertions. still get some comparison assertion errors
! exec ginkgolinter --suppress-err-assertion=true --suppress-nil-assertion=true errassertion
! exec ginkgolinter --suppress-err-assertion --suppress-nil-assertion errassertion
! stdout .
stderr -count=1 'wrong boolean assertion. Consider using `Expect\(err == nil\).To\(BeFalse\(\)\)` instead'
stderr -count=1 'asserting a non-error type with Succeed matcher'
Expand Down
6 changes: 3 additions & 3 deletions tests/testdata/focus.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
stderr -count=1 'wrong length assertion\. Consider using .Expect\("1234"\)\.To\(HaveLen\(4\)\). instead'

# run ginkgolinter to find ginkgo's focused containers; enable the focus check; expect errors
! exec ginkgolinter --forbid-focus-container=true focus
! exec ginkgolinter --forbid-focus-container focus
! stdout .
stderr -count=1 'Focus container found. This is used only for local debug and should not be part of the actual source code. Consider to replace with "Context"'
stderr -count=3 'Focus spec found. This is used only for local debug and should not be part of the actual source code. Consider to remove it'
stderr -count=1 'wrong length assertion\. Consider using .Expect\("1234"\)\.To\(HaveLen\(4\)\). instead'

# run ginkgolinter with -fix
! exec ginkgolinter --forbid-focus-container=true -fix focus
! exec ginkgolinter --forbid-focus-container -fix focus
! stdout .
stderr -count=1 'Focus container found. This is used only for local debug and should not be part of the actual source code. Consider to replace with "Context"'
stderr -count=3 'Focus spec found. This is used only for local debug and should not be part of the actual source code. Consider to remove it'
stderr -count=1 'wrong length assertion\. Consider using .Expect\("1234"\)\.To\(HaveLen\(4\)\). instead'

# run ginkgolinter to find wrong async focus, after fix. expect only the focus decorator error
! exec ginkgolinter --forbid-focus-container=true focus
! exec ginkgolinter --forbid-focus-container focus
! stdout .
stderr -count=3 'Focus spec found. This is used only for local debug and should not be part of the actual source code. Consider to remove it'

Expand Down
6 changes: 3 additions & 3 deletions tests/testdata/forceexpectto.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ exec ginkgolinter expectto
! stderr .

# enable force expect to, expect one error
! exec ginkgolinter --force-expect-to=true expectto
! exec ginkgolinter --force-expect-to expectto
! stdout .
stderr -count=1 'must not use Expect with Should'
stderr -count=1 'Expect\(true\).To\(BeTrue\(\)\)'

# run with -fix, expect error
! exec ginkgolinter --fix --force-expect-to=true expectto
! exec ginkgolinter --fix --force-expect-to expectto
! stdout .
stderr -count=1 'must not use Expect with Should'
stderr -count=1 'Expect\(true\).To\(BeTrue\(\)\)'

# run again after fix, expect no error
exec ginkgolinter --force-expect-to=true expectto
exec ginkgolinter --force-expect-to expectto
! stdout .
! stderr .

Expand Down
4 changes: 2 additions & 2 deletions tests/testdata/intervals.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ exec ginkgolinter intervals
! stderr .

# run ginkgolinter to find wrong async intervals; enable interval check; expect errors
! exec ginkgolinter --validate-async-intervals=true intervals
! exec ginkgolinter --validate-async-intervals intervals
! stdout .
stderr -count=1 'multiple issues: timeout defined more than once; polling defined more than once'
stderr -count=2 'only use time\.Duration for timeout and polling in Eventually\(\) or Consistently\(\)'
stderr -count=1 'Consistently\(func\(\) bool \{ return true \}, time\.Second\*10, time\.Second\).Should\(BeTrue\(\)\)'
stderr -count=1 'timeout must not be shorter than the polling interval'

# run ginkgolinter to find wrong async intervals; enable interval check, but suppress async checks; expect no errors
exec ginkgolinter --validate-async-intervals=true --suppress-async-assertion=true intervals
exec ginkgolinter --validate-async-intervals --suppress-async-assertion intervals
! stdout .
! stderr .

Expand Down
Loading

0 comments on commit 6f3281e

Please sign in to comment.