package library import "testing" // A file whose stat failed for a transient reason is unknown, not gone. func TestPlanDoesNotFlagFilesWithStatErrors(t *testing.T) { existing := []ExistingFile{ {ID: 1, FileInfo: FileInfo{RelPath: "flaky.mp3", Size: 10, MTimeUnix: 5}}, {ID: 2, FileInfo: FileInfo{RelPath: "gone.mp3", Size: 20, MTimeUnix: 6}}, } w := walkResult{ files: []FileInfo{{RelPath: "new.mp3", Size: 10, MTimeUnix: 5}}, // same size+mtime as flaky.mp3 failedFiles: map[string]bool{"flaky.mp3": true}, } p := makePlan(w, existing, scanOptions{}) for _, id := range p.missing { if id == 1 { t.Error("flaky.mp3 was marked missing after a stat error") } } if len(p.renames) != 0 { t.Errorf("renames = %+v: a file with a stat error must not be a rename source", p.renames) } if len(p.missing) != 1 || p.missing[0] != 2 { t.Errorf("missing = %v, want only gone.mp3", p.missing) } if len(p.parse) != 1 || p.parse[0].file.RelPath != "new.mp3" { t.Errorf("parse = %+v, want new.mp3 as a new file", p.parse) } } // A full rescan reads every file again, whether or not it changed, so what the // reader learned since (ids, genres) reaches files that are already indexed. func TestFullPlanReadsEveryPresentFileAgain(t *testing.T) { existing := []ExistingFile{ {ID: 1, FileInfo: FileInfo{RelPath: "same.mp3", Size: 10, MTimeUnix: 5}}, {ID: 2, FileInfo: FileInfo{RelPath: "changed.mp3", Size: 20, MTimeUnix: 6}}, {ID: 3, FileInfo: FileInfo{RelPath: "back.mp3", Size: 30, MTimeUnix: 7}, Missing: true}, {ID: 4, FileInfo: FileInfo{RelPath: "gone.mp3", Size: 40, MTimeUnix: 8}}, } w := walkResult{files: []FileInfo{ {RelPath: "same.mp3", Size: 10, MTimeUnix: 5}, {RelPath: "changed.mp3", Size: 21, MTimeUnix: 6}, {RelPath: "back.mp3", Size: 30, MTimeUnix: 7}, {RelPath: "new.mp3", Size: 50, MTimeUnix: 9}, }} normal := makePlan(w, existing, scanOptions{}) if len(normal.parse) != 2 || normal.unchanged != 1 || len(normal.restore) != 1 { t.Fatalf("normal plan: parse=%d unchanged=%d restore=%d", len(normal.parse), normal.unchanged, len(normal.restore)) } full := makePlan(w, existing, scanOptions{full: true}) ids := map[int64]bool{} for _, j := range full.parse { ids[j.existingID] = true } if len(full.parse) != 4 || !ids[1] || !ids[2] || !ids[3] || !ids[0] { t.Errorf("full plan parses %+v, want the four present files (three known, one new)", full.parse) } // A file that came back is restored even if reading it fails this time, as in a // normal scan, and it is read as well. if full.unchanged != 0 || len(full.restore) != 1 || full.restore[0] != 3 { t.Errorf("full plan: unchanged=%d restore=%v, want only back.mp3 restored", full.unchanged, full.restore) } // What is gone is still gone. if len(full.missing) != 1 || full.missing[0] != 4 { t.Errorf("missing = %v, want only gone.mp3", full.missing) } }