mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-18 02:52:36 +03:00
* 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.
82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
package request
|
|
|
|
import (
|
|
"context"
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("Token epoch holder", func() {
|
|
var ctx context.Context
|
|
BeforeEach(func() {
|
|
ctx = GinkgoT().Context()
|
|
})
|
|
|
|
It("reports nothing when unset", func() {
|
|
ctx = WithTokenEpochHolder(ctx)
|
|
_, ok := TokenEpochFrom(ctx)
|
|
Expect(ok).To(BeFalse())
|
|
})
|
|
|
|
It("round-trips a value set by the handler", func() {
|
|
ctx = WithTokenEpochHolder(ctx)
|
|
SetTokenEpoch(ctx, 7)
|
|
|
|
epoch, ok := TokenEpochFrom(ctx)
|
|
Expect(ok).To(BeTrue())
|
|
Expect(epoch).To(Equal(7))
|
|
})
|
|
|
|
It("survives being wrapped in a derived context", func() {
|
|
ctx = WithTokenEpochHolder(ctx)
|
|
SetTokenEpoch(context.WithValue(ctx, contextKey("unrelated"), 1), 3)
|
|
|
|
epoch, ok := TokenEpochFrom(ctx)
|
|
Expect(ok).To(BeTrue())
|
|
Expect(epoch).To(Equal(3))
|
|
})
|
|
|
|
It("is a no-op with no holder installed", func() {
|
|
Expect(func() { SetTokenEpoch(ctx, 5) }).ToNot(Panic())
|
|
_, ok := TokenEpochFrom(ctx)
|
|
Expect(ok).To(BeFalse())
|
|
})
|
|
})
|
|
|
|
var _ = Describe("Server address", func() {
|
|
var ctx context.Context
|
|
BeforeEach(func() {
|
|
ctx = GinkgoT().Context()
|
|
})
|
|
|
|
It("reports nothing when unset", func() {
|
|
_, _, ok := ServerAddressFrom(ctx)
|
|
Expect(ok).To(BeFalse())
|
|
})
|
|
|
|
It("round-trips the scheme and host", func() {
|
|
ctx = WithServerAddress(ctx, "https", "music.example.com")
|
|
|
|
scheme, host, ok := ServerAddressFrom(ctx)
|
|
Expect(ok).To(BeTrue())
|
|
Expect(scheme).To(Equal("https"))
|
|
Expect(host).To(Equal("music.example.com"))
|
|
})
|
|
|
|
It("reports nothing when the host is empty", func() {
|
|
ctx = WithServerAddress(ctx, "https", "")
|
|
|
|
_, _, ok := ServerAddressFrom(ctx)
|
|
Expect(ok).To(BeFalse())
|
|
})
|
|
|
|
It("is carried over to a background context by AddValues", func() {
|
|
reqCtx := WithServerAddress(ctx, "https", "music.example.com")
|
|
|
|
_, host, ok := ServerAddressFrom(AddValues(ctx, reqCtx))
|
|
Expect(ok).To(BeTrue())
|
|
Expect(host).To(Equal("music.example.com"))
|
|
})
|
|
})
|