github.com/fawick/restic@v0.1.1-0.20171126184616-c02923fbfc79/internal/fs/path_prefix_test.go (about) 1 package fs 2 3 import ( 4 "path/filepath" 5 "runtime" 6 "testing" 7 ) 8 9 func fromSlashAbs(p string) string { 10 if runtime.GOOS == "windows" { 11 if len(p) > 0 && p[0] == '/' { 12 p = "c:" + p 13 } 14 } 15 16 return filepath.FromSlash(p) 17 } 18 19 func TestHasPathPrefix(t *testing.T) { 20 var tests = []struct { 21 base, p string 22 result bool 23 }{ 24 {"", "", false}, 25 {"/", "", false}, 26 {"/", "x", false}, 27 {"x", "/", false}, 28 {"/", "/x", true}, 29 {"/x", "/y", false}, 30 {"/home/user/foo", "/home", false}, 31 {"/home/user/foo/", "/home", false}, 32 {"/home/user/foo", "/home/", false}, 33 {"/home/user/foo/", "/home/", false}, 34 {"/home/user/foo", "/home/user/foo/bar", true}, 35 {"/home/user/foo", "/home/user/foo/bar/baz/x/y/z", true}, 36 {"/home/user/foo", "/home/user/foobar", false}, 37 {"/home/user/Foo", "/home/user/foo/bar/baz", false}, 38 {"/home/user/foo", "/home/user/Foo/bar/baz", false}, 39 } 40 41 for _, test := range tests { 42 t.Run("", func(t *testing.T) { 43 base := fromSlashAbs(test.base) 44 p := fromSlashAbs(test.p) 45 result := HasPathPrefix(base, p) 46 if result != test.result { 47 t.Fatalf("wrong result for HasPathPrefix(%q, %q): want %v, got %v", 48 base, p, test.result, result) 49 } 50 }) 51 } 52 }