navidrome/core/storage/storage_test.go
Deluan Quintão 06993a8e04
Some checks are pending
Pipeline: Test, Lint, Build / Get version info (push) Waiting to run
Pipeline: Test, Lint, Build / Lint Go code (push) Waiting to run
Pipeline: Test, Lint, Build / Test Go code (push) Waiting to run
Pipeline: Test, Lint, Build / Test Go code (Windows) (push) Waiting to run
Pipeline: Test, Lint, Build / Test JS code (push) Waiting to run
Pipeline: Test, Lint, Build / Lint i18n files (push) Waiting to run
Pipeline: Test, Lint, Build / Check Docker configuration (push) Waiting to run
Pipeline: Test, Lint, Build / Build (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-1 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-2 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-3 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-4 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-5 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Package/Release (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-6 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-7 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-8 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-9 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-10 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Push to GHCR (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build Windows installers (push) Blocked by required conditions
test(storage): re-enable local storage tests on Windows (#5654)
* refactor(storage): extract LocalPathToURL from storage.For

* test(storage): re-enable local storage tests on Windows (#5381)

* test(storage): fix Windows drive-letter path expectation

The 'should handle Windows drive letters correctly' test was gated behind
the now-removed SkipOnWindows BeforeEach, so its expectation had never run.
On Windows, newLocalStorage re-joins u.Host+u.Path via filepath.Join, which
yields a backslash path (C:\music), not C:/music. Assert against
filepath.Join so the expectation matches the OS-native result.

* test(storage): probe Windows drive-letter path in LocalPathToURL

Three review bots flagged that LocalPathToURL escapes the drive-letter
colon (C: -> C%3A), which url.Parse rejects. There are no Windows bug
reports, so add a Windows-gated test that exercises the real conversion
on a drive-letter path and let CI decide whether the bug is real before
changing production code.
2026-06-22 21:33:14 -04:00

133 lines
4.4 KiB
Go

package storage
import (
"net/url"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/navidrome/navidrome/tests"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestApp(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Storage Test Suite")
}
var _ = Describe("Storage", func() {
When("schema is not registered", func() {
BeforeEach(func() {
registry = map[string]constructor{}
})
It("should return error", func() {
_, err := For("file:///tmp")
Expect(err).To(HaveOccurred())
})
})
When("schema is registered", func() {
BeforeEach(func() {
registry = map[string]constructor{}
Register("file", func(url url.URL) Storage { return &fakeLocalStorage{u: url} })
Register("s3", func(url url.URL) Storage { return &fakeS3Storage{u: url} })
})
It("should return correct implementation", func() {
s, err := For("file:///tmp")
Expect(err).ToNot(HaveOccurred())
Expect(s).To(BeAssignableToTypeOf(&fakeLocalStorage{}))
Expect(s.(*fakeLocalStorage).u.Scheme).To(Equal("file"))
Expect(s.(*fakeLocalStorage).u.Path).To(Equal("/tmp"))
s, err = For("s3:///bucket")
Expect(err).ToNot(HaveOccurred())
Expect(s).To(BeAssignableToTypeOf(&fakeS3Storage{}))
Expect(s.(*fakeS3Storage).u.Scheme).To(Equal("s3"))
Expect(s.(*fakeS3Storage).u.Path).To(Equal("/bucket"))
})
It("should return a file implementation when schema is not specified", func() {
s, err := For("/tmp")
Expect(err).ToNot(HaveOccurred())
Expect(s).To(BeAssignableToTypeOf(&fakeLocalStorage{}))
Expect(s.(*fakeLocalStorage).u.Scheme).To(Equal("file"))
Expect(s.(*fakeLocalStorage).u.Path).To(Equal("/tmp"))
})
It("should return a file implementation for a relative folder", func() {
tests.SkipOnWindows("path separator bug (#TBD-path-sep-storage)")
s, err := For("tmp")
Expect(err).ToNot(HaveOccurred())
cwd, _ := os.Getwd()
Expect(s).To(BeAssignableToTypeOf(&fakeLocalStorage{}))
Expect(s.(*fakeLocalStorage).u.Scheme).To(Equal("file"))
Expect(s.(*fakeLocalStorage).u.Path).To(Equal(filepath.Join(cwd, "tmp")))
})
It("should return error if schema is unregistered", func() {
_, err := For("webdav:///tmp")
Expect(err).To(HaveOccurred())
})
DescribeTable("should handle paths with special characters correctly",
func(inputPath string) {
s, err := For(inputPath)
Expect(err).ToNot(HaveOccurred())
Expect(s).To(BeAssignableToTypeOf(&fakeLocalStorage{}))
Expect(s.(*fakeLocalStorage).u.Scheme).To(Equal("file"))
// The path should be exactly the same as the input - after URL parsing it gets decoded back
Expect(s.(*fakeLocalStorage).u.Path).To(Equal(inputPath))
},
Entry("hash symbols", "/tmp/test#folder/file.mp3"),
Entry("spaces", "/tmp/test folder/file with spaces.mp3"),
Entry("question marks", "/tmp/test?query/file.mp3"),
Entry("ampersands", "/tmp/test&amp/file.mp3"),
Entry("multiple special chars", "/tmp/Song #1 & More?.mp3"),
)
})
Describe("LocalPathToURL", func() {
It("builds a file:// URL from an absolute path", func() {
u, err := LocalPathToURL("/tmp/music")
Expect(err).ToNot(HaveOccurred())
Expect(u.Scheme).To(Equal("file"))
Expect(u.Path).To(Equal("/tmp/music"))
})
It("escapes special characters and decodes them back in Path", func() {
u, err := LocalPathToURL("/tmp/Song #1 & More?.mp3")
Expect(err).ToNot(HaveOccurred())
Expect(u.Path).To(Equal("/tmp/Song #1 & More?.mp3"))
})
It("produces the same url.URL that For uses for a bare path", func() {
registry = map[string]constructor{}
Register("file", func(url url.URL) Storage { return &fakeLocalStorage{u: url} })
s, err := For("/tmp/music")
Expect(err).ToNot(HaveOccurred())
direct, err := LocalPathToURL("/tmp/music")
Expect(err).ToNot(HaveOccurred())
Expect(s.(*fakeLocalStorage).u).To(Equal(direct))
})
// On Windows, library paths are drive-lettered (e.g. C:\Music). This
// exercises the real conversion to confirm a drive-letter path yields a
// usable file:// URL on the actual platform (no-op on Unix).
It("handles a Windows drive-letter path", func() {
if runtime.GOOS != "windows" {
Skip("Windows-specific path handling")
}
u, err := LocalPathToURL(`C:\Music`)
Expect(err).ToNot(HaveOccurred())
Expect(u.Scheme).To(Equal("file"))
})
})
})
type fakeLocalStorage struct {
Storage
u url.URL
}
type fakeS3Storage struct {
Storage
u url.URL
}