mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-19 15:23:36 +00: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.
163 lines
5.6 KiB
Go
163 lines
5.6 KiB
Go
package artwork
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/consts"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/tests"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("Uploader", func() {
|
|
var ctx context.Context
|
|
var svc Uploader
|
|
var tmpDir string
|
|
var artRepo *tests.MockArtworkRepo
|
|
var queueRepo *tests.MockArtworkQueueRepo
|
|
|
|
BeforeEach(func() {
|
|
ctx = GinkgoT().Context()
|
|
tmpDir = GinkgoT().TempDir()
|
|
conf.Update(func(c *conf.Options) { c.DataFolder = conf.NewDir(tmpDir) })
|
|
artRepo = tests.CreateMockArtworkRepo()
|
|
queueRepo = tests.CreateMockArtworkQueueRepo()
|
|
ds := &tests.MockDataStore{MockedArtwork: artRepo, MockedArtworkQueue: queueRepo}
|
|
svc = NewUploader(ds)
|
|
})
|
|
|
|
Describe("SetImage", func() {
|
|
It("creates directory and saves image file", func() {
|
|
reader := strings.NewReader("fake image data")
|
|
filename, err := svc.SetImage(ctx, consts.EntityArtist, "ar-1", "Pink Floyd", "", reader, ".jpg")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(filename).To(Equal("ar-1_pink_floyd.jpg"))
|
|
|
|
absPath := filepath.Join(tmpDir, "artwork", "artist", "ar-1_pink_floyd.jpg")
|
|
data, err := os.ReadFile(absPath)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(string(data)).To(Equal("fake image data"))
|
|
})
|
|
|
|
It("falls back to ID-only filename when name cleans to empty", func() {
|
|
reader := strings.NewReader("data")
|
|
filename, err := svc.SetImage(ctx, consts.EntityPlaylist, "pl-1", "!!!", "", reader, ".png")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(filename).To(Equal("pl-1.png"))
|
|
})
|
|
|
|
It("removes old image when replacing", func() {
|
|
oldDir := filepath.Join(tmpDir, "artwork", "artist")
|
|
Expect(os.MkdirAll(oldDir, 0755)).To(Succeed())
|
|
oldFile := filepath.Join(oldDir, "ar-1_old.png")
|
|
Expect(os.WriteFile(oldFile, []byte("old"), 0600)).To(Succeed())
|
|
|
|
reader := strings.NewReader("new image")
|
|
_, err := svc.SetImage(ctx, consts.EntityArtist, "ar-1", "New Name", oldFile, reader, ".jpg")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(oldFile).ToNot(BeAnExistingFile())
|
|
|
|
newPath := filepath.Join(oldDir, "ar-1_new_name.jpg")
|
|
Expect(newPath).To(BeAnExistingFile())
|
|
})
|
|
|
|
It("ignores missing old file without error", func() {
|
|
reader := strings.NewReader("data")
|
|
_, err := svc.SetImage(ctx, consts.EntityArtist, "ar-1", "Name", "/nonexistent/path.jpg", reader, ".jpg")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
})
|
|
|
|
It("does not touch artwork state or the queue (that is EnqueueArtwork's job, post-Put)", func() {
|
|
Expect(artRepo.PutItemArtwork(ctx, &model.ItemArtwork{
|
|
ItemKind: "ar", ItemID: "ar-1", Hash: "oldhash", Source: "external",
|
|
})).To(Succeed())
|
|
|
|
_, err := svc.SetImage(ctx, consts.EntityArtist, "ar-1", "Pink Floyd", "", strings.NewReader("img"), ".jpg")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// SetImage only writes the file; the state row survives and nothing is queued until
|
|
// the caller has persisted the new filename and called EnqueueArtwork.
|
|
_, err = artRepo.GetItemArtwork(ctx, model.KindArtistArtwork, "ar-1", model.ImageTypePrimary)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(queueRepo.DequeueBatch(ctx, 1000)).To(BeEmpty())
|
|
})
|
|
})
|
|
|
|
Describe("EnqueueArtwork", func() {
|
|
It("clears artwork state and enqueues a Bump", func() {
|
|
Expect(artRepo.PutItemArtwork(ctx, &model.ItemArtwork{
|
|
ItemKind: "ar", ItemID: "ar-1", Hash: "oldhash", Source: "external",
|
|
})).To(Succeed())
|
|
|
|
svc.EnqueueArtwork(ctx, consts.EntityArtist, "ar-1")
|
|
|
|
_, err := artRepo.GetItemArtwork(ctx, model.KindArtistArtwork, "ar-1", model.ImageTypePrimary)
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
|
|
queued, err := queueRepo.DequeueBatch(ctx, 1000)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(queued).To(ContainElement(SatisfyAll(
|
|
HaveField("ItemKind", "ar"),
|
|
HaveField("ItemID", "ar-1"),
|
|
HaveField("Priority", model.ArtworkPriorityBump),
|
|
)))
|
|
})
|
|
|
|
It("is a no-op for an unknown entity type", func() {
|
|
svc.EnqueueArtwork(ctx, "unknown", "x-1")
|
|
Expect(queueRepo.DequeueBatch(ctx, 1000)).To(BeEmpty())
|
|
})
|
|
})
|
|
|
|
Describe("RemoveImage", func() {
|
|
It("removes the file at the given path", func() {
|
|
dir := filepath.Join(tmpDir, "artwork", "artist")
|
|
Expect(os.MkdirAll(dir, 0755)).To(Succeed())
|
|
path := filepath.Join(dir, "ar-1_test.jpg")
|
|
Expect(os.WriteFile(path, []byte("img"), 0600)).To(Succeed())
|
|
|
|
err := svc.RemoveImage(ctx, path)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(path).ToNot(BeAnExistingFile())
|
|
})
|
|
|
|
It("succeeds when file does not exist", func() {
|
|
err := svc.RemoveImage(ctx, "/nonexistent/file.jpg")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
})
|
|
|
|
It("succeeds with empty path", func() {
|
|
err := svc.RemoveImage(ctx, "")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
})
|
|
})
|
|
})
|
|
|
|
var _ = Describe("MaxImageUploadSize", func() {
|
|
|
|
It("returns the configured size when valid", func() {
|
|
conf.Update(func(c *conf.Options) { c.MaxImageUploadSize = "20MB" })
|
|
Expect(MaxImageUploadSize()).To(Equal(int64(20_000_000)))
|
|
})
|
|
|
|
It("returns the default size when config is empty", func() {
|
|
conf.Update(func(c *conf.Options) { c.MaxImageUploadSize = "" })
|
|
Expect(MaxImageUploadSize()).To(Equal(int64(10_000_000)))
|
|
})
|
|
|
|
It("returns the default size when config is invalid", func() {
|
|
conf.Update(func(c *conf.Options) { c.MaxImageUploadSize = "not-a-size" })
|
|
Expect(MaxImageUploadSize()).To(Equal(int64(10_000_000)))
|
|
})
|
|
|
|
It("parses raw byte values", func() {
|
|
conf.Update(func(c *conf.Options) { c.MaxImageUploadSize = "52428800" })
|
|
Expect(MaxImageUploadSize()).To(Equal(int64(52_428_800)))
|
|
})
|
|
})
|