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.
117 lines
3.9 KiB
Go
117 lines
3.9 KiB
Go
package scanner_test
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/navidrome/navidrome/consts"
|
|
"github.com/navidrome/navidrome/core/artwork"
|
|
"github.com/navidrome/navidrome/core/metrics"
|
|
"github.com/navidrome/navidrome/core/playlists"
|
|
"github.com/navidrome/navidrome/db"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/persistence"
|
|
"github.com/navidrome/navidrome/scanner"
|
|
"github.com/navidrome/navidrome/server/events"
|
|
"github.com/navidrome/navidrome/tests"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("Controller", func() {
|
|
var ctx context.Context
|
|
var ds *tests.MockDataStore
|
|
var ctrl model.Scanner
|
|
|
|
BeforeEach(func() {
|
|
ctx = GinkgoT().Context()
|
|
})
|
|
|
|
Describe("Status", func() {
|
|
BeforeEach(func() {
|
|
db.Init(ctx)
|
|
DeferCleanup(func() { Expect(tests.ClearDB(ctx)).To(Succeed()) })
|
|
ds = &tests.MockDataStore{RealDS: persistence.New(db.Db())}
|
|
ds.MockedProperty = &tests.MockedPropertyRepo{}
|
|
ctrl = scanner.New(ctx, ds, events.NoopBroker(), playlists.NewPlaylists(ds, artwork.NewUploader(ds)), metrics.NewNoopInstance())
|
|
})
|
|
|
|
It("includes last scan error", func() {
|
|
Expect(ds.Property().Put(ctx, consts.LastScanErrorKey, "boom")).To(Succeed())
|
|
status, err := ctrl.Status(ctx)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(status.LastError).To(Equal("boom"))
|
|
})
|
|
|
|
It("includes scan type and error in status", func() {
|
|
// Set up test data in property repo
|
|
Expect(ds.Property().Put(ctx, consts.LastScanErrorKey, "test error")).To(Succeed())
|
|
Expect(ds.Property().Put(ctx, consts.LastScanTypeKey, "full")).To(Succeed())
|
|
|
|
// Get status and verify basic info
|
|
status, err := ctrl.Status(ctx)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(status.LastError).To(Equal("test error"))
|
|
Expect(status.ScanType).To(Equal("full"))
|
|
})
|
|
})
|
|
})
|
|
|
|
var _ = Describe("LockForMaintenance", func() {
|
|
It("allows only one database maintenance operation at a time", func() {
|
|
release, ok := scanner.LockForMaintenance()
|
|
Expect(ok).To(BeTrue())
|
|
DeferCleanup(release)
|
|
|
|
_, ok = scanner.LockForMaintenance()
|
|
Expect(ok).To(BeFalse())
|
|
})
|
|
})
|
|
|
|
var _ = Describe("EffectiveFullScan", func() {
|
|
var ctx context.Context
|
|
var ds *tests.MockDataStore
|
|
|
|
BeforeEach(func() {
|
|
ctx = GinkgoT().Context()
|
|
pid := model.Library{}.EffectivePID()
|
|
libraries := &tests.MockLibraryRepo{}
|
|
libraries.SetData(model.Libraries{
|
|
{ID: 1, FullScanInProgress: true, ScannedPIDAlbum: pid.Album, ScannedPIDTrack: pid.Track},
|
|
{ID: 2, ScannedPIDAlbum: pid.Album, ScannedPIDTrack: pid.Track},
|
|
{ID: 3, LastScanAt: time.Now(), PIDAlbum: "folder", ScannedPIDAlbum: pid.Album, ScannedPIDTrack: pid.Track},
|
|
})
|
|
ds = &tests.MockDataStore{MockedLibrary: libraries}
|
|
})
|
|
|
|
It("detects a library that needs a full rescan for a PID change", func() {
|
|
targets := []model.ScanTarget{{LibraryID: 3, FolderPath: "."}}
|
|
Expect(scanner.EffectiveFullScan(ctx, ds, false, targets)).To(BeTrue())
|
|
})
|
|
|
|
It("detects an interrupted full scan in a targeted library", func() {
|
|
targets := []model.ScanTarget{{LibraryID: 1, FolderPath: "."}}
|
|
Expect(scanner.EffectiveFullScan(ctx, ds, false, targets)).To(BeTrue())
|
|
})
|
|
|
|
It("detects an interrupted full scan when scanning all libraries", func() {
|
|
Expect(scanner.EffectiveFullScan(ctx, ds, false, nil)).To(BeTrue())
|
|
})
|
|
|
|
It("ignores interrupted full scans in untargeted libraries", func() {
|
|
targets := []model.ScanTarget{{LibraryID: 2, FolderPath: "."}}
|
|
Expect(scanner.EffectiveFullScan(ctx, ds, false, targets)).To(BeFalse())
|
|
})
|
|
})
|
|
|
|
var _ = Describe("GetInstance", func() {
|
|
It("returns the same controller to every caller", func() {
|
|
ds := &tests.MockDataStore{}
|
|
pls := playlists.NewPlaylists(ds, artwork.NewUploader(ds))
|
|
ctx := context.WithoutCancel(GinkgoT().Context())
|
|
a := scanner.GetInstance(ctx, ds, events.NoopBroker(), pls, metrics.NewNoopInstance())
|
|
b := scanner.GetInstance(ctx, ds, events.NoopBroker(), pls, metrics.NewNoopInstance())
|
|
Expect(a).To(BeIdenticalTo(b))
|
|
})
|
|
})
|