Files
Deluan Quintão dfe90ba384 chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints

Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.

Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.

* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx

The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.

* test: pass the caller's ctx to tests.ClearDB

ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00

71 lines
1.6 KiB
Go

package backgrounds
import (
"context"
"io"
"net/http"
"strings"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
type recordingBody struct {
io.Reader
closed *bool
}
func (b recordingBody) Close() error {
*b.closed = true
return nil
}
type stubTransport struct {
statusCode int
closed *bool
}
func (t stubTransport) RoundTrip(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: t.statusCode,
Header: make(http.Header),
Body: recordingBody{Reader: strings.NewReader("image-bytes"), closed: t.closed},
}, nil
}
var _ = Describe("serveImage", func() {
var ctx context.Context
var closed bool
BeforeEach(func() {
ctx = GinkgoT().Context()
closed = false
})
stubStatus := func(statusCode int) {
original := http.DefaultTransport
http.DefaultTransport = stubTransport{statusCode: statusCode, closed: &closed}
DeferCleanup(func() { http.DefaultTransport = original })
}
It("closes the response body when the hosting service returns an error", func() {
stubStatus(http.StatusNotFound)
_, err := (&Handler{}).serveImage(ctx, cacheKey("some-image.webp"))
Expect(err).To(MatchError(ContainSubstring("unexpected status code")))
Expect(closed).To(BeTrue(), "response body was left open")
})
It("hands the still-open body to the caller on success", func() {
stubStatus(http.StatusOK)
reader, err := (&Handler{}).serveImage(ctx, cacheKey("some-image.webp"))
Expect(err).ToNot(HaveOccurred())
Expect(closed).To(BeFalse(), "response body must stay open for the CachedStream wrapper")
body, _ := io.ReadAll(reader)
Expect(string(body)).To(Equal("image-bytes"))
})
})