github.com/andrewlunde/glide@v0.20.1/path/strip_test.go (about) 1 package path 2 3 import ( 4 "errors" 5 "os" 6 "path/filepath" 7 "reflect" 8 "testing" 9 "time" 10 ) 11 12 type mockFileInfo struct { 13 name string 14 isDir bool 15 } 16 17 func (mfi *mockFileInfo) Name() string { 18 return mfi.name 19 } 20 21 func (mfi *mockFileInfo) Size() int64 { 22 panic("not implemented") 23 } 24 25 func (mfi *mockFileInfo) Mode() os.FileMode { 26 panic("not implemented") 27 } 28 29 func (mfi *mockFileInfo) ModTime() time.Time { 30 panic("not implemented") 31 } 32 33 func (mfi *mockFileInfo) IsDir() bool { 34 return mfi.isDir 35 } 36 37 func (mfi *mockFileInfo) Sys() interface{} { 38 panic("not implemented") 39 } 40 41 type removeAll struct { 42 calledWith string 43 err error 44 } 45 46 func (rah *removeAll) removeAll(p string) error { 47 rah.calledWith = p 48 return rah.err 49 } 50 51 func TestWalkFunction(t *testing.T) { 52 type args struct { 53 searchPath string 54 removeAll *removeAll 55 path string 56 info os.FileInfo 57 err error 58 } 59 tests := []struct { 60 name string 61 args args 62 want error 63 wantCalledWith string 64 }{ 65 { 66 name: "WalkFunctionSkipsNonVendor", 67 args: args{searchPath: "foo", 68 removeAll: &removeAll{}, 69 path: "foo/bar", 70 info: &mockFileInfo{name: "bar", isDir: true}, 71 err: nil, 72 }, 73 want: nil, 74 wantCalledWith: "", 75 }, 76 { 77 name: "WalkFunctionSkipsNonDir", 78 args: args{searchPath: "foo", 79 removeAll: &removeAll{}, 80 path: "foo/vendor", 81 info: &mockFileInfo{name: "vendor", isDir: false}, 82 err: nil, 83 }, 84 want: nil, 85 wantCalledWith: "", 86 }, 87 { 88 name: "WalkFunctionDeletesVendor", 89 args: args{searchPath: "foo", 90 removeAll: &removeAll{}, 91 path: "foo/vendor", 92 info: &mockFileInfo{name: "vendor", isDir: true}, 93 err: nil, 94 }, 95 want: filepath.SkipDir, 96 wantCalledWith: "foo/vendor", 97 }, 98 { 99 name: "WalkFunctionReturnsPassedError", 100 args: args{searchPath: "foo", 101 removeAll: &removeAll{}, 102 path: "foo/vendor", 103 info: &mockFileInfo{name: "vendor", isDir: true}, 104 err: errors.New("expected"), 105 }, 106 want: errors.New("expected"), 107 wantCalledWith: "", 108 }, 109 { 110 name: "WalkFunctionReturnsRemoveAllError", 111 args: args{searchPath: "foo", 112 removeAll: &removeAll{err: errors.New("expected")}, 113 path: "foo/vendor", 114 info: &mockFileInfo{name: "vendor", isDir: true}, 115 err: nil, 116 }, 117 want: errors.New("expected"), 118 wantCalledWith: "foo/vendor", 119 }, 120 { 121 name: "WalkFunctionSkipsBaseDir", 122 args: args{searchPath: "vendor", 123 removeAll: &removeAll{}, 124 path: "vendor", 125 info: &mockFileInfo{name: "vendor", isDir: true}, 126 err: nil, 127 }, 128 want: nil, 129 wantCalledWith: "", 130 }, 131 } 132 for _, test := range tests { 133 walkFunction := getWalkFunction(test.args.searchPath, test.args.removeAll.removeAll) 134 if actual := walkFunction(test.args.path, test.args.info, test.args.err); !reflect.DeepEqual(actual, test.want) { 135 t.Errorf("walkFunction() = %v, want %v", actual, test.want) 136 } 137 if test.args.removeAll.calledWith != test.wantCalledWith { 138 t.Errorf("removeAll argument = \"%s\", want \"%s\"", test.args.removeAll.calledWith, test.wantCalledWith) 139 } 140 } 141 }