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.
132 lines
4.1 KiB
Go
132 lines
4.1 KiB
Go
package plugins
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/navidrome/navidrome/core/scrobbler"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/tests"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("syncPlugins", func() {
|
|
var ctx context.Context
|
|
var m *Manager
|
|
var repo *tests.MockPluginRepo
|
|
var folder string
|
|
|
|
BeforeEach(func() {
|
|
ctx = GinkgoT().Context()
|
|
folder = GinkgoT().TempDir()
|
|
repo = tests.CreateMockPluginRepo()
|
|
repo.SetData(model.Plugins{})
|
|
m = &Manager{ds: &tests.MockDataStore{MockedPlugin: repo}}
|
|
})
|
|
|
|
writePackage := func(name string) {
|
|
GinkgoHelper()
|
|
manifest := &Manifest{Name: "Test Plugin", Author: "Test Author", Version: "1.0.0"}
|
|
wasm := []byte{0x00, 0x61, 0x73, 0x6d} // Minimal wasm header
|
|
Expect(createTestPackage(filepath.Join(folder, name), manifest, wasm)).To(Succeed())
|
|
}
|
|
|
|
It("registers a plugin with a usable ID", func() {
|
|
writePackage("my-plugin" + PackageExtension)
|
|
|
|
Expect(m.syncPlugins(ctx, folder)).To(Succeed())
|
|
|
|
_, err := repo.Get(ctx, "my-plugin")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
})
|
|
|
|
It("skips packages whose name yields a path-like ID", func() {
|
|
writePackage("." + PackageExtension)
|
|
writePackage(".." + PackageExtension)
|
|
|
|
Expect(m.syncPlugins(ctx, folder)).To(Succeed())
|
|
|
|
all, err := repo.GetAll(ctx)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(all).To(BeEmpty())
|
|
})
|
|
})
|
|
|
|
var _ = Describe("removePluginFromDB", func() {
|
|
var ctx context.Context
|
|
|
|
BeforeEach(func() {
|
|
ctx = GinkgoT().Context()
|
|
})
|
|
|
|
It("discards buffered scrobbles for the removed plugin", func() {
|
|
buffer := tests.CreateMockedScrobbleBufferRepo()
|
|
Expect(buffer.Enqueue(ctx, "my-plugin", "user1", "track1", time.Now())).To(Succeed())
|
|
Expect(buffer.Enqueue(ctx, "other-plugin", "user1", "track2", time.Now())).To(Succeed())
|
|
|
|
repo := tests.CreateMockPluginRepo()
|
|
plugin := model.Plugin{ID: "my-plugin", Enabled: false}
|
|
repo.SetData(model.Plugins{plugin})
|
|
|
|
// No broker: sendPluginRefreshEvent is nil-safe, and testBroker is
|
|
// defined in manager_test.go, which is excluded on Windows.
|
|
m := &Manager{
|
|
ds: &tests.MockDataStore{MockedScrobbleBuffer: buffer},
|
|
}
|
|
Expect(m.removePluginFromDB(ctx, repo, &plugin)).To(Succeed())
|
|
|
|
_, err := repo.Get(ctx, "my-plugin")
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
|
|
remaining, err := buffer.Length(ctx)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(remaining).To(Equal(int64(1)))
|
|
entry, err := buffer.Next(ctx, "other-plugin", "user1")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(entry).ToNot(BeNil(), "entries of other services must be kept")
|
|
})
|
|
|
|
It("keeps buffered scrobbles of a builtin scrobbler sharing the removed plugin's name", func() {
|
|
scrobbler.Register("builtin-svc", func(model.DataStore) scrobbler.Scrobbler { return nil })
|
|
buffer := tests.CreateMockedScrobbleBufferRepo()
|
|
Expect(buffer.Enqueue(ctx, "builtin-svc", "user1", "track1", time.Now())).To(Succeed())
|
|
|
|
repo := tests.CreateMockPluginRepo()
|
|
plugin := model.Plugin{ID: "builtin-svc", Enabled: false}
|
|
repo.SetData(model.Plugins{plugin})
|
|
|
|
m := &Manager{
|
|
ds: &tests.MockDataStore{MockedScrobbleBuffer: buffer},
|
|
}
|
|
Expect(m.removePluginFromDB(ctx, repo, &plugin)).To(Succeed())
|
|
|
|
remaining, err := buffer.Length(ctx)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(remaining).To(Equal(int64(1)), "builtin scrobbler queue must not be wiped")
|
|
})
|
|
})
|
|
|
|
var _ = Describe("ComputeFileSHA256", func() {
|
|
It("returns a consistent 64-char lowercase hex hash for the same file", func() {
|
|
dir := GinkgoT().TempDir()
|
|
ndpPath := filepath.Join(dir, "test.ndp")
|
|
err := createTestPackage(ndpPath, &Manifest{Name: "S", Author: "a", Version: "1.0.0"}, []byte{0x00, 0x61, 0x73, 0x6d})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
hash1, err := ComputeFileSHA256(ndpPath)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
hash2, err := ComputeFileSHA256(ndpPath)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(hash1).To(Equal(hash2))
|
|
Expect(hash1).To(MatchRegexp(`^[0-9a-f]{64}$`))
|
|
})
|
|
|
|
It("returns an error for a non-existent path", func() {
|
|
_, err := ComputeFileSHA256(filepath.Join(GinkgoT().TempDir(), "does-not-exist.ndp"))
|
|
Expect(err).To(HaveOccurred())
|
|
})
|
|
})
|