Skip to content

Commit 1b633dc

Browse files
committed
Fix issue: failed to recognize ginkgo.SpecContext in Eventually
ginkgolinter fails to recognized ginkgo.SpecContext as a valid context in Eventually, and so wrongly identify it as the actual value, instead of the following function parameter. This PR fixes the issue and now this scenarion works ```go It("should work", func(ctx SpecContext) { Eventually(func() error { return fmt.Errorf("foo") }).WithContext(ctx).Should(MatchError("foo")) }, SpecTimeout(time.Second)) ```
1 parent d302fe7 commit 1b633dc

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

analyzer_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ func TestAllUseCases(t *testing.T) {
109109
testName: "matchError with func return error-func",
110110
testData: "a/issue-174",
111111
},
112+
{
113+
testName: "matchError with func with SpecContext",
114+
testData: "a/issue-190",
115+
},
112116
} {
113117
t.Run(tc.testName, func(tt *testing.T) {
114118
analysistest.Run(tt, analysistest.TestData(), ginkgolinter.NewAnalyzer(), tc.testData)

internal/expression/actual/actualarg.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"golang.org/x/tools/go/analysis"
99

1010
"github.com/nunnatsa/ginkgolinter/internal/expression/value"
11+
"github.com/nunnatsa/ginkgolinter/internal/ginkgoinfo"
1112
"github.com/nunnatsa/ginkgolinter/internal/gomegahandler"
1213
"github.com/nunnatsa/ginkgolinter/internal/gomegainfo"
1314
"github.com/nunnatsa/ginkgolinter/internal/reverseassertion"
@@ -98,7 +99,7 @@ func getActualArg(origActualExpr *ast.CallExpr, actualExprClone *ast.CallExpr, a
9899
argExprClone = actualExprClone.Args[funcOffset]
99100

100101
if gomegainfo.IsAsyncActualMethod(actualMethodName) {
101-
if pass.TypesInfo.TypeOf(origArgExpr).String() == "context.Context" {
102+
if ginkgoinfo.IsGinkgoContext(pass.TypesInfo.TypeOf(origArgExpr)) {
102103
funcOffset++
103104
if len(origActualExpr.Args) <= funcOffset {
104105
return nil, nil, 0, false

internal/ginkgoinfo/ginkgoinfo.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ginkgoinfo
2+
3+
import (
4+
gotypes "go/types"
5+
"strings"
6+
)
7+
8+
const (
9+
ctxTypeName = "context.Context"
10+
ginkgoCtxSuffix = "github.com/onsi/ginkgo/v2/internal.SpecContext"
11+
)
12+
13+
func IsGinkgoContext(t gotypes.Type) bool {
14+
maybeCtx := gotypes.Unalias(t)
15+
16+
typeName := maybeCtx.String()
17+
if typeName == ctxTypeName {
18+
return true
19+
}
20+
21+
if strings.HasSuffix(typeName, ginkgoCtxSuffix) {
22+
return true
23+
}
24+
25+
return false
26+
}

testdata/src/a/issue-190/issue190.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package issue_190
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
. "github.com/onsi/ginkgo/v2"
9+
10+
. "github.com/onsi/gomega"
11+
)
12+
13+
var _ = Describe("make sure issue 190 is fixed", func() {
14+
It("should work", func(ctx context.Context) {
15+
Eventually(ctx, func() error {
16+
return fmt.Errorf("foo")
17+
}).Should(MatchError("foo"))
18+
}, SpecTimeout(time.Second))
19+
20+
It("should work", func(ctx SpecContext) {
21+
Eventually(ctx, func() error {
22+
return fmt.Errorf("foo")
23+
}).Should(MatchError("foo"))
24+
}, SpecTimeout(time.Second))
25+
})
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package issue_190
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
. "github.com/onsi/ginkgo/v2"
9+
10+
"github.com/onsi/gomega"
11+
)
12+
13+
var _ = Describe("", func() {
14+
It("should work", func(ctx SpecContext) {
15+
gomega.Eventually(ctx, func() error {
16+
return fmt.Errorf("foo")
17+
}).Should(gomega.MatchError("foo"))
18+
}, SpecTimeout(time.Second))
19+
It("should work", func(ctx context.Context) {
20+
gomega.Eventually(ctx, func() error {
21+
return fmt.Errorf("foo")
22+
}).Should(gomega.MatchError("foo"))
23+
}, SpecTimeout(time.Second))
24+
})

0 commit comments

Comments
 (0)