update
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
This commit is contained in:
117
utils/merge/merge_fs_test.go
Normal file
117
utils/merge/merge_fs_test.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package merge_test
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/navidrome/navidrome/utils/merge"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestMergeFS(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "MergeFS Suite")
|
||||
}
|
||||
|
||||
var _ = Describe("FS", func() {
|
||||
var baseName, overlayName string
|
||||
var mergedDir fs.FS
|
||||
|
||||
BeforeEach(func() {
|
||||
baseName, _ = os.MkdirTemp("", "merge_fs_base_test")
|
||||
overlayName, _ = os.MkdirTemp("", "merge_fs_overlay_test")
|
||||
baseDir := os.DirFS(baseName)
|
||||
overlayDir := os.DirFS(overlayName)
|
||||
mergedDir = merge.FS{Base: baseDir, Overlay: overlayDir}
|
||||
})
|
||||
AfterEach(func() {
|
||||
_ = os.RemoveAll(baseName)
|
||||
_ = os.RemoveAll(overlayName)
|
||||
})
|
||||
|
||||
It("reads from Base dir if not found in Overlay", func() {
|
||||
_f(baseName, "a.json")
|
||||
file, err := mergedDir.Open("a.json")
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
stat, err := file.Stat()
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
Expect(stat.Name()).To(Equal("a.json"))
|
||||
})
|
||||
|
||||
It("reads overridden file", func() {
|
||||
_f(baseName, "b.json", "original")
|
||||
_f(baseName, "b.json", "overridden")
|
||||
|
||||
file, err := mergedDir.Open("b.json")
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
content, err := io.ReadAll(file)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(string(content)).To(Equal("overridden"))
|
||||
})
|
||||
|
||||
It("reads only files from Base if Overlay is empty", func() {
|
||||
_f(baseName, "test.txt")
|
||||
|
||||
dir, err := mergedDir.Open(".")
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
list, err := dir.(fs.ReadDirFile).ReadDir(-1)
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
Expect(list).To(HaveLen(1))
|
||||
Expect(list[0].Name()).To(Equal("test.txt"))
|
||||
})
|
||||
|
||||
It("reads merged dirs", func() {
|
||||
_f(baseName, "1111.txt")
|
||||
_f(overlayName, "2222.json")
|
||||
|
||||
dir, err := mergedDir.Open(".")
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
list, err := dir.(fs.ReadDirFile).ReadDir(-1)
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
Expect(list).To(HaveLen(2))
|
||||
Expect(list[0].Name()).To(Equal("1111.txt"))
|
||||
Expect(list[1].Name()).To(Equal("2222.json"))
|
||||
})
|
||||
|
||||
It("allows to seek to the beginning of the directory", func() {
|
||||
_f(baseName, "1111.txt")
|
||||
_f(baseName, "2222.txt")
|
||||
_f(baseName, "3333.txt")
|
||||
|
||||
dir, err := mergedDir.Open(".")
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
list, _ := dir.(fs.ReadDirFile).ReadDir(2)
|
||||
Expect(list).To(HaveLen(2))
|
||||
Expect(list[0].Name()).To(Equal("1111.txt"))
|
||||
Expect(list[1].Name()).To(Equal("2222.txt"))
|
||||
|
||||
list, _ = dir.(fs.ReadDirFile).ReadDir(2)
|
||||
Expect(list).To(HaveLen(1))
|
||||
Expect(list[0].Name()).To(Equal("3333.txt"))
|
||||
})
|
||||
})
|
||||
|
||||
func _f(dir, name string, content ...string) string {
|
||||
path := filepath.Join(dir, name)
|
||||
file, err := os.Create(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if len(content) > 0 {
|
||||
_, _ = file.WriteString(content[0])
|
||||
}
|
||||
_ = file.Close()
|
||||
return path
|
||||
}
|
||||
Reference in New Issue
Block a user