| Conditions | 5 |
| Total Lines | 88 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | package path |
||
| 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 | } |
||
| 142 |