From 8285587fcd65992104ade13679f9f7c3e7c5c8ed Mon Sep 17 00:00:00 2001 From: Deluan Date: Sat, 11 Apr 2026 19:42:47 -0400 Subject: [PATCH] refactor(artwork): trim disc art matching comments and table-drive tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Doc comment on fromExternalFile is trimmed to the one non-obvious contract (caller must pre-lowercase the pattern) plus the headline behavior; the bulleted restatement of the branch logic went away. Two inline comments that narrated what the code already shows are also gone. Hoisting a `hasWildcard := strings.ContainsRune(pattern, '*')` check out of the loop avoids per-iteration extractDiscNumber calls for literal patterns (e.g. `shellac.png`) and lets the loop break as soon as a viable fallback is found, since literal patterns can never be beaten by a numbered match. Wildcard patterns keep the original scan-to-end-for-numbered-match behavior. The two regression tests added in the previous commit were structurally identical apart from discNumber/expected, so they are collapsed into a DescribeTable with two entries — matching the existing table style used for extractDiscNumber tests in the same file. --- core/artwork/reader_disc.go | 42 ++++++++++------------ core/artwork/reader_disc_test.go | 61 ++++++++++++-------------------- 2 files changed, 41 insertions(+), 62 deletions(-) diff --git a/core/artwork/reader_disc.go b/core/artwork/reader_disc.go index 445984bbb..91f76b747 100644 --- a/core/artwork/reader_disc.go +++ b/core/artwork/reader_disc.go @@ -215,18 +215,10 @@ func extractDiscNumber(pattern, filename string) (int, bool) { } // fromExternalFile returns a sourceFunc that matches image files against a glob -// pattern with disc-number-aware filtering. -// -// Within a single pattern, a numbered filename whose number equals the target -// disc always wins over an unnumbered candidate. An unnumbered candidate is -// returned only as a fallback, and only when: -// - the album has multiple folders and the file lives in a folder containing -// tracks for this disc (the image belongs to a specific disc); or -// - the album has a single folder (the file is shared across every disc). -// -// The caller (fromDiscArtPriority) already lowercases the pattern, so this -// function assumes pattern is lowercase and only lowercases each filename. +// pattern. A numbered filename whose number equals the target disc wins over +// any unnumbered candidate; callers must pass a lowercase pattern. func (d *discArtworkReader) fromExternalFile(ctx context.Context, pattern string) sourceFunc { + hasWildcard := strings.ContainsRune(pattern, '*') return func() (io.ReadCloser, string, error) { var fallback string for _, file := range d.imgFiles { @@ -241,21 +233,20 @@ func (d *discArtworkReader) fromExternalFile(ctx context.Context, pattern string continue } - if num, hasNum := extractDiscNumber(pattern, name); hasNum { - // Numbered filename wins immediately when its number matches. - if num != d.discNumber { - continue + if hasWildcard { + if num, hasNum := extractDiscNumber(pattern, name); hasNum { + if num != d.discNumber { + continue + } + f, err := os.Open(file) + if err != nil { + log.Warn(ctx, "Could not open disc art file", "file", file, err) + continue + } + return f, file, nil } - f, err := os.Open(file) - if err != nil { - log.Warn(ctx, "Could not open disc art file", "file", file, err) - continue - } - return f, file, nil } - // Unnumbered: remember the first viable candidate as a fallback, - // but keep scanning for a numbered match that should take precedence. if fallback != "" { continue } @@ -263,6 +254,11 @@ func (d *discArtworkReader) fromExternalFile(ctx context.Context, pattern string continue } fallback = file + // Literal patterns have no numbered variants to prefer, so + // stop as soon as we have a viable fallback. + if !hasWildcard { + break + } } if fallback != "" { diff --git a/core/artwork/reader_disc_test.go b/core/artwork/reader_disc_test.go index 7bdf88d87..180bd8a6a 100644 --- a/core/artwork/reader_disc_test.go +++ b/core/artwork/reader_disc_test.go @@ -170,46 +170,29 @@ var _ = Describe("Disc Artwork Reader", func() { r.Close() }) - It("prefers disc-numbered match over shared unnumbered match within the same pattern", func() { - // Natural sort of basenames puts disc < disc1 < disc2, so - // disc.jpg appears first in imgFiles. The loop must not return - // disc.jpg for disc 2 when disc2.jpg also matches the pattern. - f1 := createFile("album/disc.jpg") - f2 := createFile("album/disc1.jpg") - f3 := createFile("album/disc2.jpg") - reader := &discArtworkReader{ - discNumber: 2, - imgFiles: []string{f1, f2, f3}, - discFolders: map[string]bool{filepath.Join(tmpDir, "album"): true}, - } + DescribeTable("numbered match wins over shared fallback within a pattern", + func(discNumber, expectedIdx int) { + files := []string{ + createFile("album/disc.jpg"), + createFile("album/disc1.jpg"), + createFile("album/disc2.jpg"), + } + reader := &discArtworkReader{ + discNumber: discNumber, + imgFiles: files, + discFolders: map[string]bool{filepath.Join(tmpDir, "album"): true}, + } - sf := reader.fromExternalFile(ctx, "disc*.*") - r, path, err := sf() - Expect(err).ToNot(HaveOccurred()) - Expect(r).ToNot(BeNil()) - r.Close() - Expect(path).To(Equal(f3)) - }) - - It("falls back to shared unnumbered match when no numbered file matches the target disc", func() { - // Disc 3 has no disc3.jpg; disc.jpg should be used as the shared - // fallback so a disc without dedicated art still gets something. - f1 := createFile("album/disc.jpg") - f2 := createFile("album/disc1.jpg") - f3 := createFile("album/disc2.jpg") - reader := &discArtworkReader{ - discNumber: 3, - imgFiles: []string{f1, f2, f3}, - discFolders: map[string]bool{filepath.Join(tmpDir, "album"): true}, - } - - sf := reader.fromExternalFile(ctx, "disc*.*") - r, path, err := sf() - Expect(err).ToNot(HaveOccurred()) - Expect(r).ToNot(BeNil()) - r.Close() - Expect(path).To(Equal(f1)) - }) + sf := reader.fromExternalFile(ctx, "disc*.*") + r, path, err := sf() + Expect(err).ToNot(HaveOccurred()) + Expect(r).ToNot(BeNil()) + r.Close() + Expect(path).To(Equal(files[expectedIdx])) + }, + Entry("disc 2 picks disc2.jpg over the shared disc.jpg", 2, 2), + Entry("disc 3 falls back to disc.jpg when no numbered match exists", 3, 0), + ) It("matches file without number in multi-folder album by folder", func() { f1 := createFile("album/cd1/disc.jpg")