github.com/kekek/gb@v0.4.5-0.20170222120241-d4ba64b0b297/cmd/gb/internal/match/match_test.go (about) 1 package match 2 3 import ( 4 "path/filepath" 5 "reflect" 6 "testing" 7 ) 8 9 func TestImportPaths(t *testing.T) { 10 tests := []struct { 11 cwd string 12 args []string 13 want []string 14 }{{ 15 "_testdata/a", 16 nil, 17 []string{"cmd", "cmd/main", "github.com", "github.com/foo", "github.com/foo/bar", "github.com/quxx", "github.com/quxx/bar"}, 18 }, { 19 "_testdata/a", 20 []string{}, 21 []string{"cmd", "cmd/main", "github.com", "github.com/foo", "github.com/foo/bar", "github.com/quxx", "github.com/quxx/bar"}, 22 }, { 23 "_testdata/a", 24 []string{"."}, 25 []string{"."}, 26 }, { 27 "_testdata/a", 28 []string{".."}, 29 []string{".."}, 30 }, { 31 "_testdata/a", 32 []string{"./."}, 33 []string{"."}, 34 }, { 35 "_testdata/a", 36 []string{"..."}, 37 []string{"cmd", "cmd/main", "github.com", "github.com/foo", "github.com/foo/bar", "github.com/quxx", "github.com/quxx/bar"}, 38 }, { 39 "_testdata/a", 40 []string{".../bar"}, 41 []string{"github.com/foo/bar", "github.com/quxx/bar"}, 42 }, { 43 "_testdata/a", 44 []string{"cmd"}, 45 []string{"cmd"}, 46 }, { 47 "_testdata/a", 48 []string{"cmd/go"}, 49 []string{"cmd/go"}, 50 }, { 51 "_testdata/a", 52 []string{"cmd/main"}, 53 []string{"cmd/main"}, 54 }, { 55 "_testdata/a", 56 []string{"cmd/..."}, 57 []string{"cmd", "cmd/main"}, 58 }, { 59 "_testdata/a", 60 []string{"nonexist"}, 61 []string{"nonexist"}, // passed through because there is no wildcard 62 }, { 63 "_testdata/a/src", 64 nil, 65 []string{"cmd", "cmd/main", "github.com", "github.com/foo", "github.com/foo/bar", "github.com/quxx", "github.com/quxx/bar"}, 66 }, { 67 "_testdata/a/src", 68 []string{}, 69 []string{"cmd", "cmd/main", "github.com", "github.com/foo", "github.com/foo/bar", "github.com/quxx", "github.com/quxx/bar"}, 70 }, { 71 "_testdata/a/src", 72 []string{"."}, 73 []string{"."}, 74 }, { 75 "_testdata/a/src", 76 []string{".."}, 77 []string{".."}, 78 }, { 79 "_testdata/a/src", 80 []string{"./."}, 81 []string{"."}, 82 }, { 83 "_testdata/a/src", 84 []string{"..."}, 85 []string{"cmd", "cmd/main", "github.com", "github.com/foo", "github.com/foo/bar", "github.com/quxx", "github.com/quxx/bar"}, 86 }, { 87 "_testdata/a/src", 88 []string{".../bar"}, 89 []string{"github.com/foo/bar", "github.com/quxx/bar"}, 90 }, { 91 "_testdata/a/src", 92 []string{"cmd"}, 93 []string{"cmd"}, 94 }, { 95 "_testdata/a/src", 96 []string{"cmd/go"}, 97 []string{"cmd/go"}, 98 }, { 99 "_testdata/a/src", 100 []string{"cmd/main"}, 101 []string{"cmd/main"}, 102 }, { 103 "_testdata/a/src", 104 []string{"cmd/..."}, 105 []string{"cmd", "cmd/main"}, 106 }, { 107 "_testdata/a/src", 108 []string{"nonexist"}, 109 []string{"nonexist"}, // passed through because there is no wildcard 110 }, { 111 "_testdata/a/src/github.com/", 112 nil, 113 []string{"github.com", "github.com/foo", "github.com/foo/bar", "github.com/quxx", "github.com/quxx/bar"}, 114 }, { 115 "_testdata/a/src/github.com/", 116 []string{}, 117 []string{"github.com", "github.com/foo", "github.com/foo/bar", "github.com/quxx", "github.com/quxx/bar"}, 118 }, { 119 "_testdata/a/src/github.com/", 120 []string{"."}, 121 []string{"github.com"}, 122 }, { 123 "_testdata/a/src/github.com/", 124 []string{".."}, 125 []string{"."}, 126 }, { 127 "_testdata/a/src/github.com/", 128 []string{"./."}, 129 []string{"github.com"}, 130 }, { 131 "_testdata/a/src/github.com/", 132 []string{"..."}, 133 []string{"github.com", "github.com/foo", "github.com/foo/bar", "github.com/quxx", "github.com/quxx/bar"}, 134 }, { 135 "_testdata/a/src/github.com/", 136 []string{".../bar"}, 137 []string{"github.com/foo/bar", "github.com/quxx/bar"}, 138 }, { 139 "_testdata/a/src/github.com/", 140 []string{"cmd"}, 141 []string{"github.com/cmd"}, 142 }, { 143 "_testdata/a/src/github.com/", 144 []string{"cmd/go"}, 145 []string{"github.com/cmd/go"}, 146 }, { 147 "_testdata/a/src/github.com/", 148 []string{"cmd/main"}, 149 []string{"github.com/cmd/main"}, 150 }, { 151 "_testdata/a/src/github.com/", 152 []string{"cmd/..."}, 153 nil, 154 }} 155 156 for _, tt := range tests { 157 const srcdir = "_testdata/a/src" 158 got := ImportPaths(srcdir, tt.cwd, tt.args) 159 if !reflect.DeepEqual(tt.want, got) { 160 t.Errorf("ImportPaths(%q, %q): got %q, want %q", tt.cwd, tt.args, got, tt.want) 161 } 162 } 163 } 164 165 func TestMatchPackages(t *testing.T) { 166 tests := []struct { 167 pattern string 168 want []string 169 }{{ 170 "", 171 nil, 172 }, { 173 "github.com/foo", 174 []string{ 175 "github.com/foo", 176 }, 177 }, { 178 "github.com/foo/...", 179 []string{ 180 "github.com/foo", 181 "github.com/foo/bar", 182 }, 183 }, { 184 "github.com", 185 []string{ 186 "github.com", 187 }, 188 }, { 189 "github.com/...", 190 []string{ 191 "github.com", 192 "github.com/foo", 193 "github.com/foo/bar", 194 "github.com/quxx", 195 "github.com/quxx/bar", 196 }, 197 }} 198 199 for _, tt := range tests { 200 srcdir := "_testdata/a/src" 201 got, err := matchPackages(srcdir, tt.pattern) 202 if err != nil { 203 t.Error(err) 204 continue 205 } 206 if !reflect.DeepEqual(tt.want, got) { 207 t.Errorf("matchPackagesInSrcDir(%q, ..., %q): got %q, want %q", srcdir, tt.pattern, got, tt.want) 208 } 209 } 210 } 211 212 func TestIsLocalImport(t *testing.T) { 213 tests := []struct { 214 path string 215 want bool 216 }{ 217 {".", true}, 218 {"", false}, 219 {"..", true}, 220 {"a/..", false}, 221 {"../a", true}, 222 {"./a", true}, 223 } 224 225 for _, tt := range tests { 226 got := isLocalImport(tt.path) 227 if got != tt.want { 228 t.Errorf("isLocalImportPath(%q): got: %v, want: %v", tt.path, got, tt.want) 229 } 230 } 231 } 232 233 func TestSkipElem(t *testing.T) { 234 tests := []struct { 235 elem string 236 want bool 237 }{ 238 {"", false}, 239 {".", true}, 240 {"..", true}, 241 {"a", false}, 242 {".a", true}, 243 {"a.", false}, 244 {"_", true}, 245 {"_a", true}, 246 {"a_", false}, 247 {"a", false}, 248 {"testdata", true}, 249 {"_testdata", true}, 250 {".testdata", true}, 251 {"testdata_", false}, 252 } 253 254 for _, tt := range tests { 255 got := skipElem(tt.elem) 256 if got != tt.want { 257 t.Errorf("skipElem(%q): got: %v, want: %v", tt.elem, got, tt.want) 258 } 259 } 260 } 261 262 func abs(t *testing.T, path string) string { 263 path, err := filepath.Abs(path) 264 if err != nil { 265 t.Fatal(err) 266 } 267 return path 268 } 269 270 func absv(t *testing.T, paths ...string) []string { 271 for i := range paths { 272 paths[i] = abs(t, paths[i]) 273 } 274 return paths 275 }