github.com/fawick/restic@v0.1.1-0.20171126184616-c02923fbfc79/cmd/restic/exclude_test.go (about) 1 package main 2 3 import ( 4 "io/ioutil" 5 "path/filepath" 6 "testing" 7 8 "github.com/restic/restic/internal/test" 9 ) 10 11 func TestRejectByPattern(t *testing.T) { 12 var tests = []struct { 13 filename string 14 reject bool 15 }{ 16 {filename: "/home/user/foo.go", reject: true}, 17 {filename: "/home/user/foo.c", reject: false}, 18 {filename: "/home/user/foobar", reject: false}, 19 {filename: "/home/user/foobar/x", reject: true}, 20 {filename: "/home/user/README", reject: false}, 21 {filename: "/home/user/README.md", reject: true}, 22 } 23 24 patterns := []string{"*.go", "README.md", "/home/user/foobar/*"} 25 26 for _, tc := range tests { 27 t.Run("", func(t *testing.T) { 28 reject := rejectByPattern(patterns) 29 res := reject(tc.filename, nil) 30 if res != tc.reject { 31 t.Fatalf("wrong result for filename %v: want %v, got %v", 32 tc.filename, tc.reject, res) 33 } 34 }) 35 } 36 } 37 38 func TestIsExcludedByFile(t *testing.T) { 39 const ( 40 tagFilename = "CACHEDIR.TAG" 41 header = "Signature: 8a477f597d28d172789f06886806bc55" 42 ) 43 tests := []struct { 44 name string 45 tagFile string 46 content string 47 want bool 48 }{ 49 {"NoTagfile", "", "", false}, 50 {"EmptyTagfile", tagFilename, "", true}, 51 {"UnnamedTagFile", "", header, false}, 52 {"WrongTagFile", "notatagfile", header, false}, 53 {"IncorrectSig", tagFilename, header[1:], false}, 54 {"ValidSig", tagFilename, header, true}, 55 {"ValidPlusStuff", tagFilename, header + "foo", true}, 56 {"ValidPlusNewlineAndStuff", tagFilename, header + "\nbar", true}, 57 } 58 for _, tc := range tests { 59 t.Run(tc.name, func(t *testing.T) { 60 tempDir, cleanup := test.TempDir(t) 61 defer cleanup() 62 63 foo := filepath.Join(tempDir, "foo") 64 err := ioutil.WriteFile(foo, []byte("foo"), 0666) 65 if err != nil { 66 t.Fatalf("could not write file: %v", err) 67 } 68 if tc.tagFile != "" { 69 tagFile := filepath.Join(tempDir, tc.tagFile) 70 err = ioutil.WriteFile(tagFile, []byte(tc.content), 0666) 71 if err != nil { 72 t.Fatalf("could not write tagfile: %v", err) 73 } 74 } 75 h := header 76 if tc.content == "" { 77 h = "" 78 } 79 if got := isExcludedByFile(foo, tagFilename, h, nil); tc.want != got { 80 t.Fatalf("expected %v, got %v", tc.want, got) 81 } 82 }) 83 } 84 }