github.com/anishathalye/periscope@v0.3.5/internal/periscope/periscope_test.go (about) 1 package periscope 2 3 import ( 4 "github.com/anishathalye/periscope/internal/db" 5 6 "bytes" 7 "io/ioutil" 8 "log" 9 "path/filepath" 10 "testing" 11 12 "github.com/spf13/afero" 13 ) 14 15 func newTest(fs afero.Fs) (*Periscope, *bytes.Buffer, *bytes.Buffer) { 16 if testDebug { 17 log.SetFlags(log.LstdFlags | log.Lshortfile) 18 } else { 19 log.SetFlags(0) 20 log.SetOutput(ioutil.Discard) 21 } 22 db, err := db.NewInMemory() 23 if err != nil { 24 panic(err) 25 } 26 outStream := new(bytes.Buffer) 27 errStream := new(bytes.Buffer) 28 _, realFs := fs.(*afero.OsFs) 29 return &Periscope{ 30 fs: fs, 31 realFs: realFs, 32 db: db, 33 dbPath: "", 34 outStream: outStream, 35 errStream: errStream, 36 options: &Options{Debug: false}, 37 }, outStream, errStream 38 } 39 40 func check(t *testing.T, err error) { 41 if err != nil { 42 t.Fatal(err) 43 } 44 } 45 46 func checkErr(t *testing.T, err error) { 47 if err == nil { 48 t.Fatal("expected error") 49 } 50 } 51 52 func tempDir() string { 53 dir, err := ioutil.TempDir("", "") 54 if err != nil { 55 panic(err) 56 } 57 resolved, err := filepath.EvalSymlinks(dir) 58 if err != nil { 59 panic(err) 60 } 61 return resolved 62 } 63 64 func checkEquivalentDuplicateSet(t *testing.T, expected, got []db.DuplicateSet) { 65 if len(expected) != len(got) { 66 t.Fatalf("duplicate sets differ in length: expected %d, got %d", len(expected), len(got)) 67 } 68 for i := range expected { 69 if len(expected[i]) != len(got[i]) { 70 t.Fatalf("duplicate sets have different sizes at index %d, expected %v, got %v", i, len(expected[i]), len(got[i])) 71 } 72 for j := range expected[i] { 73 expectedInfo := expected[i][j] 74 gotInfo := got[i][j] 75 if expectedInfo.Path != gotInfo.Path { 76 t.Fatalf("duplicate set path differs at (%d, %d), expected %v, got %v", i, j, expectedInfo.Path, gotInfo.Path) 77 } 78 if expectedInfo.Size != gotInfo.Size { 79 t.Fatalf("duplicate set size differs at (%d, %d), expected %v, got %v", i, j, expectedInfo.Size, gotInfo.Size) 80 } 81 } 82 } 83 } 84 85 func checkEquivalentInfos(t *testing.T, expected, got []db.FileInfo) { 86 if len(expected) != len(got) { 87 t.Fatalf("infos differ in length: expected %d, got %d", len(expected), len(got)) 88 } 89 for i := range expected { 90 expectedInfo := expected[i] 91 gotInfo := got[i] 92 if expectedInfo.Path != gotInfo.Path { 93 t.Fatalf("info path differs at %d, expected %v, got %v", i, expectedInfo.Path, gotInfo.Path) 94 } 95 if expectedInfo.Size != gotInfo.Size { 96 t.Fatalf("info size differs at %d, expected %v, got %v", i, expectedInfo.Size, gotInfo.Size) 97 } 98 if (len(expectedInfo.ShortHash) != 0) != (len(gotInfo.ShortHash) != 0) { 99 t.Fatalf("info short hash presence differs at %d, expected %v, got %v", i, len(expectedInfo.ShortHash) != 0, len(gotInfo.ShortHash) != 0) 100 } 101 if (len(expectedInfo.FullHash) != 0) != (len(gotInfo.FullHash) != 0) { 102 t.Fatalf("info full hash presence differs at %d, expected %v, got %v", i, len(expectedInfo.FullHash) != 0, len(gotInfo.FullHash) != 0) 103 } 104 } 105 } 106 107 var dummyHash []byte = []byte{0x01, 0x03, 0x03, 0x07}