|
|
@@ -10,6 +10,7 @@ import (
|
|
|
"github.com/felamaslen/gmus-backend/pkg/database"
|
|
|
"github.com/felamaslen/gmus-backend/pkg/read"
|
|
|
setup "github.com/felamaslen/gmus-backend/pkg/testing"
|
|
|
+ "github.com/felamaslen/gmus-backend/pkg/types"
|
|
|
)
|
|
|
|
|
|
var _ = Describe("reading files", func() {
|
|
|
@@ -21,49 +22,90 @@ var _ = Describe("reading files", func() {
|
|
|
})
|
|
|
|
|
|
Describe("reading file info", func() {
|
|
|
- var results []*read.Song
|
|
|
+ var results []*types.Song
|
|
|
+ var files chan *types.File
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
results = nil
|
|
|
- files := make(chan *read.File, 1)
|
|
|
+ files = make(chan *types.File, 1)
|
|
|
+ })
|
|
|
|
|
|
- go func() {
|
|
|
- defer close(files)
|
|
|
- files <- &read.File{
|
|
|
- RelativePath: read.TestSong.RelativePath,
|
|
|
- ModifiedDate: 100123,
|
|
|
- }
|
|
|
- }()
|
|
|
+ Context("when all the files are readable", func() {
|
|
|
+ BeforeEach(func() {
|
|
|
+ go func() {
|
|
|
+ defer close(files)
|
|
|
+ files <- &types.File{
|
|
|
+ RelativePath: read.TestSong.RelativePath,
|
|
|
+ ModifiedDate: 100123,
|
|
|
+ }
|
|
|
+ }()
|
|
|
|
|
|
- outputChan := read.ReadMultipleFiles(read.TestDirectory, files)
|
|
|
+ outputChan := read.ReadMultipleFiles(read.TestDirectory, files)
|
|
|
|
|
|
- outputDone := false
|
|
|
+ outputDone := false
|
|
|
|
|
|
- for !outputDone {
|
|
|
- select {
|
|
|
- case result, more := <-outputChan:
|
|
|
- if more {
|
|
|
- results = append(results, result)
|
|
|
+ for !outputDone {
|
|
|
+ select {
|
|
|
+ case result, more := <-outputChan:
|
|
|
+ if more {
|
|
|
+ results = append(results, result)
|
|
|
+ }
|
|
|
+ outputDone = !more
|
|
|
}
|
|
|
- outputDone = !more
|
|
|
}
|
|
|
- }
|
|
|
- })
|
|
|
+ })
|
|
|
+
|
|
|
+ It("should return the correct number of results", func() {
|
|
|
+ Expect(results).To(HaveLen(1))
|
|
|
+ })
|
|
|
+
|
|
|
+ It("should get the correct info from the file", func() {
|
|
|
+ var expectedResult = read.TestSong
|
|
|
+ expectedResult.ModifiedDate = 100123
|
|
|
|
|
|
- It("should return the correct number of results", func() {
|
|
|
- Expect(results).To(HaveLen(1))
|
|
|
+ Expect(*results[0]).To(Equal(expectedResult))
|
|
|
+ })
|
|
|
})
|
|
|
|
|
|
- It("should get the correct info from the file", func() {
|
|
|
- var expectedResult = read.TestSong
|
|
|
- expectedResult.ModifiedDate = 100123
|
|
|
+ Context("when an error occurs reading a file", func() {
|
|
|
+ BeforeEach(func() {
|
|
|
+ go func() {
|
|
|
+ defer close(files)
|
|
|
+ files <- &types.File{
|
|
|
+ RelativePath: "test/file/does/not/exist.ogg",
|
|
|
+ ModifiedDate: 123,
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ outputChan := read.ReadMultipleFiles(read.TestDirectory, files)
|
|
|
|
|
|
- Expect(*results[0]).To(Equal(expectedResult))
|
|
|
+ outputDone := false
|
|
|
+
|
|
|
+ for !outputDone {
|
|
|
+ select {
|
|
|
+ case _, more := <-outputChan:
|
|
|
+ outputDone = !more
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ It("should add the file to the scan_errors table", func() {
|
|
|
+ var scanError []*types.ScanError
|
|
|
+ db.Select(&scanError, "select relative_path, base_path, error from scan_errors")
|
|
|
+
|
|
|
+ Expect(scanError).To(HaveLen(1))
|
|
|
+
|
|
|
+ Expect(scanError[0]).To(Equal(&types.ScanError{
|
|
|
+ RelativePath: "test/file/does/not/exist.ogg",
|
|
|
+ BasePath: read.TestDirectory,
|
|
|
+ Error: "open pkg/read/testdata/test/file/does/not/exist.ogg: no such file or directory",
|
|
|
+ }))
|
|
|
+ })
|
|
|
})
|
|
|
})
|
|
|
|
|
|
Describe("scanning a directory recursively", func() {
|
|
|
- var results []*read.File
|
|
|
+ var results []*types.File
|
|
|
|
|
|
var testScanDirectory = func() {
|
|
|
results = nil
|
|
|
@@ -104,9 +146,9 @@ var _ = Describe("reading files", func() {
|
|
|
|
|
|
db.MustExec(
|
|
|
`
|
|
|
- insert into songs (title, artist, album, base_path, relative_path, modified_date)
|
|
|
- values ($1, $2, $3, $4, $5, $6)
|
|
|
- `,
|
|
|
+ insert into songs (title, artist, album, base_path, relative_path, modified_date)
|
|
|
+ values ($1, $2, $3, $4, $5, $6)
|
|
|
+ `,
|
|
|
"old title",
|
|
|
"old artist",
|
|
|
"old album",
|
|
|
@@ -123,5 +165,21 @@ var _ = Describe("reading files", func() {
|
|
|
Expect(results[0].RelativePath).To(Equal(read.TestSongNested.RelativePath))
|
|
|
})
|
|
|
})
|
|
|
+
|
|
|
+ Context("when an error previously occurred scanning one of the files", func() {
|
|
|
+ BeforeEach(func() {
|
|
|
+ db.MustExec(`
|
|
|
+ insert into scan_errors (base_path, relative_path, error)
|
|
|
+ values ($1, $2, $3)
|
|
|
+ `, read.TestSong.BasePath, read.TestSong.RelativePath, "A bad thing happened")
|
|
|
+
|
|
|
+ testScanDirectory()
|
|
|
+ })
|
|
|
+
|
|
|
+ It("should only return those files which did not have errors marked against them", func() {
|
|
|
+ Expect(results).To(HaveLen(1))
|
|
|
+ Expect(results[0].RelativePath).To(Equal(read.TestSongNested.RelativePath))
|
|
|
+ })
|
|
|
+ })
|
|
|
})
|
|
|
})
|