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
140 lines
4.0 KiB
Go
140 lines
4.0 KiB
Go
package local_test
|
|
|
|
import (
|
|
"context"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/core/storage"
|
|
"github.com/navidrome/navidrome/core/storage/local"
|
|
_ "github.com/navidrome/navidrome/core/storage/local"
|
|
"github.com/navidrome/navidrome/model/metadata"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = XDescribe("Watcher", func() {
|
|
var lsw storage.Watcher
|
|
var tmpFolder string
|
|
|
|
BeforeEach(func() {
|
|
tmpFolder = GinkgoT().TempDir()
|
|
|
|
local.RegisterExtractor("noop", func(fs fs.FS, path string) local.Extractor { return noopExtractor{} })
|
|
conf.Server.Scanner.Extractor = "noop"
|
|
|
|
ls, err := storage.For(tmpFolder)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// It should implement Watcher
|
|
var ok bool
|
|
lsw, ok = ls.(storage.Watcher)
|
|
Expect(ok).To(BeTrue())
|
|
|
|
// Make sure temp folder is created
|
|
Eventually(func() error {
|
|
_, err := os.Stat(tmpFolder)
|
|
return err
|
|
}).Should(Succeed())
|
|
})
|
|
|
|
It("should start and stop watcher", func() {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
w, err := lsw.Start(ctx)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
cancel()
|
|
Eventually(w).Should(BeClosed())
|
|
})
|
|
|
|
It("should return error if watcher is already started", func() {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
_, err := lsw.Start(ctx)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
_, err = lsw.Start(ctx)
|
|
Expect(err).To(HaveOccurred())
|
|
})
|
|
|
|
It("should detect new files", func() {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
changes, err := lsw.Start(ctx)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
_, err = os.Create(filepath.Join(tmpFolder, "test.txt"))
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Eventually(changes).WithTimeout(2 * time.Second).Should(Receive(Equal(tmpFolder)))
|
|
})
|
|
|
|
It("should detect new subfolders", func() {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
changes, err := lsw.Start(ctx)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(os.Mkdir(filepath.Join(tmpFolder, "subfolder"), 0755)).To(Succeed())
|
|
|
|
Eventually(changes).WithTimeout(2 * time.Second).Should(Receive(Equal(filepath.Join(tmpFolder, "subfolder"))))
|
|
})
|
|
|
|
It("should detect changes in subfolders recursively", func() {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
subfolder := filepath.Join(tmpFolder, "subfolder1/subfolder2")
|
|
Expect(os.MkdirAll(subfolder, 0755)).To(Succeed())
|
|
|
|
changes, err := lsw.Start(ctx)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
filePath := filepath.Join(subfolder, "test.txt")
|
|
Expect(os.WriteFile(filePath, []byte("test"), 0600)).To(Succeed())
|
|
|
|
Eventually(changes).WithTimeout(2 * time.Second).Should(Receive(Equal(filePath)))
|
|
})
|
|
|
|
It("should detect removed in files", func() {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
changes, err := lsw.Start(ctx)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
filePath := filepath.Join(tmpFolder, "test.txt")
|
|
Expect(os.WriteFile(filePath, []byte("test"), 0600)).To(Succeed())
|
|
|
|
Eventually(changes).WithTimeout(2 * time.Second).Should(Receive(Equal(filePath)))
|
|
|
|
Expect(os.Remove(filePath)).To(Succeed())
|
|
Eventually(changes).WithTimeout(2 * time.Second).Should(Receive(Equal(filePath)))
|
|
})
|
|
|
|
It("should detect file moves", func() {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
filePath := filepath.Join(tmpFolder, "test.txt")
|
|
Expect(os.WriteFile(filePath, []byte("test"), 0600)).To(Succeed())
|
|
|
|
changes, err := lsw.Start(ctx)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
newPath := filepath.Join(tmpFolder, "test2.txt")
|
|
Expect(os.Rename(filePath, newPath)).To(Succeed())
|
|
Eventually(changes).WithTimeout(2 * time.Second).Should(Receive(Equal(newPath)))
|
|
})
|
|
})
|
|
|
|
type noopExtractor struct{}
|
|
|
|
func (s noopExtractor) Parse(files ...string) (map[string]metadata.Info, error) { return nil, nil }
|
|
func (s noopExtractor) Version() string { return "0" }
|