Improve scanner (#1054)

* Handle subdirectories without rx permission correctly
Allow ogg files w/o metadata, having taglib behave more like ffmpeg

* Fix test for walk_dir_tree, fix full reading of files in permission-
constrained directories, allow directories with leading ellipses

* Sorted directory traversal is preferred, and cleanup tests

* Small refactoring to clean-up `loadDir` function and to remove some "warnings" from IntelliJ

Co-authored-by: Deluan <deluan@navidrome.org>
This commit is contained in:
whorfin 2021-04-28 16:51:02 -07:00 committed by GitHub
parent 771c91d2dd
commit 20d2726faa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 100 additions and 57 deletions

View file

@ -10,7 +10,8 @@ import (
. "github.com/onsi/gomega/gstruct"
)
var _ = Describe("load_tree", func() {
var _ = Describe("walk_dir_tree", func() {
baseDir := filepath.Join("tests", "fixtures")
Describe("walkDirTree", func() {
It("reads all info correctly", func() {
@ -18,7 +19,7 @@ var _ = Describe("load_tree", func() {
results := make(walkResults, 5000)
var err error
go func() {
err = walkDirTree(context.TODO(), "tests/fixtures", results)
err = walkDirTree(context.TODO(), baseDir, results)
}()
for {
@ -30,49 +31,61 @@ var _ = Describe("load_tree", func() {
}
Expect(err).To(BeNil())
Expect(collected["tests/fixtures"]).To(MatchFields(IgnoreExtras, Fields{
Expect(collected[baseDir]).To(MatchFields(IgnoreExtras, Fields{
"HasImages": BeTrue(),
"HasPlaylist": BeFalse(),
"AudioFilesCount": BeNumerically("==", 4),
}))
Expect(collected["tests/fixtures/playlists"].HasPlaylist).To(BeTrue())
Expect(collected).To(HaveKey("tests/fixtures/symlink2dir"))
Expect(collected).To(HaveKey("tests/fixtures/empty_folder"))
Expect(collected[filepath.Join(baseDir, "playlists")].HasPlaylist).To(BeTrue())
Expect(collected).To(HaveKey(filepath.Join(baseDir, "symlink2dir")))
Expect(collected).To(HaveKey(filepath.Join(baseDir, "empty_folder")))
})
})
Describe("isDirOrSymlinkToDir", func() {
It("returns true for normal dirs", func() {
dir, _ := os.Stat("tests/fixtures")
Expect(isDirOrSymlinkToDir("tests", dir)).To(BeTrue())
dirEntry, _ := getDirEntry("tests", "fixtures")
Expect(isDirOrSymlinkToDir(baseDir, dirEntry)).To(BeTrue())
})
It("returns true for symlinks to dirs", func() {
dir, _ := os.Stat("tests/fixtures/symlink2dir")
Expect(isDirOrSymlinkToDir("tests/fixtures", dir)).To(BeTrue())
dirEntry, _ := getDirEntry(baseDir, "symlink2dir")
Expect(isDirOrSymlinkToDir(baseDir, dirEntry)).To(BeTrue())
})
It("returns false for files", func() {
dir, _ := os.Stat("tests/fixtures/test.mp3")
Expect(isDirOrSymlinkToDir("tests/fixtures", dir)).To(BeFalse())
dirEntry, _ := getDirEntry(baseDir, "test.mp3")
Expect(isDirOrSymlinkToDir(baseDir, dirEntry)).To(BeFalse())
})
It("returns false for symlinks to files", func() {
dir, _ := os.Stat("tests/fixtures/symlink")
Expect(isDirOrSymlinkToDir("tests/fixtures", dir)).To(BeFalse())
dirEntry, _ := getDirEntry(baseDir, "symlink")
Expect(isDirOrSymlinkToDir(baseDir, dirEntry)).To(BeFalse())
})
})
Describe("isDirIgnored", func() {
baseDir := filepath.Join("tests", "fixtures")
It("returns false for normal dirs", func() {
dir, _ := os.Stat(filepath.Join(baseDir, "empty_folder"))
Expect(isDirIgnored(baseDir, dir)).To(BeFalse())
dirEntry, _ := getDirEntry(baseDir, "empty_folder")
Expect(isDirIgnored(baseDir, dirEntry)).To(BeFalse())
})
It("returns true when folder contains .ndignore file", func() {
dir, _ := os.Stat(filepath.Join(baseDir, "ignored_folder"))
Expect(isDirIgnored(baseDir, dir)).To(BeTrue())
dirEntry, _ := getDirEntry(baseDir, "ignored_folder")
Expect(isDirIgnored(baseDir, dirEntry)).To(BeTrue())
})
It("returns true when folder name starts with a `.`", func() {
dir, _ := os.Stat(filepath.Join(baseDir, ".hidden_folder"))
Expect(isDirIgnored(baseDir, dir)).To(BeTrue())
dirEntry, _ := getDirEntry(baseDir, ".hidden_folder")
Expect(isDirIgnored(baseDir, dirEntry)).To(BeTrue())
})
It("returns false when folder name starts with ellipses", func() {
dirEntry, _ := getDirEntry(baseDir, "...unhidden_folder")
Expect(isDirIgnored(baseDir, dirEntry)).To(BeFalse())
})
})
})
func getDirEntry(baseDir, name string) (os.DirEntry, error) {
dirEntries, _ := os.ReadDir(baseDir)
for _, entry := range dirEntries {
if entry.Name() == name {
return entry, nil
}
}
return nil, os.ErrNotExist
}