diff --git a/file_test.go b/file_test.go index 8abf469..4c67a5b 100644 --- a/file_test.go +++ b/file_test.go @@ -3,13 +3,13 @@ package main import ( "io" "os" - "errors" "reflect" - "strings" "strconv" "testing" "io/ioutil" "path/filepath" + + "github.com/JamTools/goff/fsutil" ) func tmpFile(t *testing.T, input string, f func(in *os.File)) { @@ -30,45 +30,6 @@ func tmpFile(t *testing.T, input string, f func(in *os.File)) { f(in) } -type testFile struct { - name, contents string -} - -func createTestFiles(files []*testFile, t *testing.T) string { - td, err := ioutil.TempDir("", "") - if err != nil { - t.Fatal(err) - } - - for i := range files { - if len(files[i].name) == 0 { - continue - } - - pa := strings.Split(files[i].name, "/") - p := filepath.Join(td, filepath.Join(pa[:len(pa)-1]...)) - - // create parent dirs - if len(pa) > 1 { - err := os.MkdirAll(p, 0777) - if err != nil { - t.Fatal(err) - } - } - - // create file - if len(pa[len(pa)-1]) > 0 { - fullpath := filepath.Join(p, pa[len(pa)-1]) - err := ioutil.WriteFile(fullpath, []byte(files[i].contents), 0644) - if err != nil { - t.Fatal(err) - } - } - } - - return td -} - func TestPathInfo(t *testing.T) { tests := []struct { artist, base, path string @@ -140,58 +101,6 @@ func TestOnlyDir(t *testing.T) { } } -func TestFilesByExtensionImages(t *testing.T) { - files := []*testFile{ - {"file1", ""}, - {"file2.jpeg", ""}, - {"dir1/file3.JPG", ""}, - {"dir1/dir2/file4.png", ""}, - } - - result := []string{ - "dir1/dir2/file4.png", - "dir1/file3.JPG", - "file2.jpeg", - } - - dir := createTestFiles(files, t) - defer os.RemoveAll(dir) - - paths := filesByExtension(dir, imageExts) - if strings.Join(paths, "\n") != strings.Join(result, "\n") { - t.Errorf("Expected %v, got %v", result, paths) - } -} - -func TestFilesByExtensionAudio(t *testing.T) { - files := []*testFile{ - {"not audio file", ""}, - {"file1.FLAC", ""}, - {"file2.m4a", ""}, - {"dir1/file3.mp3", ""}, - {"dir1/dir2/file4.mp4", ""}, - {"dir1/dir2/file5.SHN", ""}, - {"dir1/dir2/file6.WAV", ""}, - } - - result := []string{ - "dir1/dir2/file4.mp4", - "dir1/dir2/file5.SHN", - "dir1/dir2/file6.WAV", - "dir1/file3.mp3", - "file1.FLAC", - "file2.m4a", - } - - dir := createTestFiles(files, t) - defer os.RemoveAll(dir) - - paths := filesByExtension(dir, audioExts) - if strings.Join(paths, "\n") != strings.Join(result, "\n") { - t.Errorf("Expected %v, got %v", result, paths) - } -} - func TestBundleFiles(t *testing.T) { testFiles := []string{ "artist1/file1", @@ -250,111 +159,8 @@ func TestSafeFilename(t *testing.T) { } } -func testFilesFullPath(t *testing.T, f func(dir string, files []string)) { - newFiles := []*testFile{ - {"file1", "abcde"}, - {"file2.jpeg", "a"}, - {"dir1/file3.JPG", "acddfefsefd"}, - {"dir1/dir2/file4.png", "dfadfd"}, - } - - dir := createTestFiles(newFiles, t) - defer os.RemoveAll(dir) - - files := []string{} - for i := range newFiles { - files = append(files, filepath.Join(dir, newFiles[i].name)) - } - - f(dir, files) -} - -func TestNthFileSize(t *testing.T) { - tests := []struct { - smallest bool - result string - other string - }{ - { smallest: true, result: "file2.jpeg" }, - { smallest: false, result: "dir1/file3.JPG" }, - { other: "audiocc-file-def-dne", result: "" }, - } - - testFilesFullPath(t, func(dir string, files []string) { - for i := range tests { - // test file does not exist - if len(tests[i].other) > 0 { - files = []string{ tests[i].other } - } - r, err := nthFileSize(files, tests[i].smallest) - - // test errors by setting empty result - if err != nil && tests[i].result == "" { - continue - } - - res := filepath.Join(dir, tests[i].result) - if r != res { - t.Errorf("Expected %v, got %v", res, r) - } - } - }) -} - -func TestIsLarger(t *testing.T) { - testFilesFullPath(t, func(dir string, files []string) { - tests := []struct { - src, dest string - result bool - }{ - { src: files[0], dest: files[1], result: true }, - { src: files[1], dest: files[0], result: false }, - { src: files[2], dest: files[3], result: true }, - { src: "audiocc-file-def-dne", dest: files[3], result: false }, - } - - for i := range tests { - r := isLarger(tests[i].src, tests[i].dest) - if r != tests[i].result { - t.Errorf("Expected %v, got %v", tests[i].result, r) - } - } - }) -} - -func TestCopyFile(t *testing.T) { - testFilesFullPath(t, func(dir string, files []string) { - // destination dir - td, err := ioutil.TempDir("", "") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(td) - - tests := []struct { - src, dest string - error error - }{ - { src: files[2], dest: filepath.Join(td, "file1"), error: nil }, - { src: files[3], dest: filepath.Join(td, "file3"), error: nil }, - { src: "audiocc-file-def-dne", dest: files[1], - error: errors.New("audiocc-file-def-dne") }, - } - - for i := range tests { - e := copyFile(tests[i].src, tests[i].dest) - if e == nil && tests[i].error == nil { - break - } - if e.Error() != tests[i].error.Error() { - t.Errorf("Expected %#v, got %#v", tests[i].error.Error(), e.Error()) - } - } - }) -} - func TestRenameFolder(t *testing.T) { - testFiles := []*testFile{ + testFiles := []*fsutil.TestFile{ {"dir1/file1", "abcde"}, {"dir2/file2", "a"}, {"dir3/file3", ""}, @@ -362,7 +168,7 @@ func TestRenameFolder(t *testing.T) { {"dir6/file6", ""}, } - dir := createTestFiles(testFiles, t) + dir := fsutil.CreateTestFiles(t, testFiles) defer os.RemoveAll(dir) tests := [][]string{