Skip to content

[BUG] False Positive for the MatchError matcher used to assert a non error type in Eventually cases with SpecContext arguments #190

@timebertt

Description

@timebertt

Describe the bug

The linter raises false positives for the correct usage of MatchError on Eventually statements with a SpecContext argument:

It("should work", func(ctx SpecContext) {
	Eventually(ctx, func() error {
		return fmt.Errorf("foo")
	}).Should(MatchError("foo"))
}, SpecTimeout(time.Second))
ginkgo-linter: the MatchError matcher used to assert a non error type (ctx)

This pattern is the recommended way for implementing interruptible specs when using Eventually (docs).

To Reproduce

package main_test

import (
	"fmt"
	"testing"
	"time"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
)

func TestE2E(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "demo")
}

var _ = Describe("the MatchError matcher used to assert a non error type", func() {
	It("should work", func(ctx SpecContext) {
		Eventually(func() error {
			return fmt.Errorf("foo")
		}).WithContext(ctx).Should(MatchError("foo"))
	}, SpecTimeout(time.Second))
})

Expected behavior

No linter error

Environment:

  • OS: macOS
  • Version: v0.19.0

Additional context

Note that passing the context as the first Eventually arg is convenience over passing it to WithContext.
I.e., this is semantically equivalent but does not raise a false positive:

It("should work", func(ctx SpecContext) {
	Eventually(func() error {
		return fmt.Errorf("foo")
	}).WithContext(ctx).Should(MatchError("foo"))
}, SpecTimeout(time.Second))

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions