github.com/afking/bazel-gazelle@v0.0.0-20180301150245-c02bc0f529e8/internal/resolve/resolve_external_test.go (about) 1 /* Copyright 2016 The Bazel Authors. All rights reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package resolve 17 18 import ( 19 "fmt" 20 "reflect" 21 "strings" 22 "testing" 23 24 "github.com/bazelbuild/bazel-gazelle/internal/config" 25 "github.com/bazelbuild/bazel-gazelle/internal/label" 26 "github.com/bazelbuild/bazel-gazelle/internal/repos" 27 28 "golang.org/x/tools/go/vcs" 29 ) 30 31 func TestExternalResolver(t *testing.T) { 32 for _, spec := range []struct { 33 importpath string 34 repos []repos.Repo 35 want label.Label 36 }{ 37 { 38 importpath: "example.com/repo", 39 want: label.New("com_example_repo", "", config.DefaultLibName), 40 }, { 41 importpath: "example.com/repo/lib", 42 want: label.New("com_example_repo", "lib", config.DefaultLibName), 43 }, { 44 importpath: "example.com/repo/lib", 45 repos: []repos.Repo{{ 46 Name: "custom_repo_name", 47 GoPrefix: "example.com/repo", 48 }}, 49 want: label.New("custom_repo_name", "lib", config.DefaultLibName), 50 }, { 51 importpath: "example.com/repo.git/lib", 52 want: label.New("com_example_repo_git", "lib", config.DefaultLibName), 53 }, { 54 importpath: "example.com/lib", 55 want: label.New("com_example", "lib", config.DefaultLibName), 56 }, 57 } { 58 r := newStubExternalResolver(spec.repos) 59 l, err := r.resolve(spec.importpath) 60 if err != nil { 61 t.Errorf("r.ResolveGo(%q) failed with %v; want success", spec.importpath, err) 62 continue 63 } 64 if got, want := l, spec.want; !reflect.DeepEqual(got, want) { 65 t.Errorf("r.ResolveGo(%q) = %s; want %s", spec.importpath, got, want) 66 } 67 } 68 } 69 70 func newStubExternalResolver(knownRepos []repos.Repo) *externalResolver { 71 l := label.NewLabeler(&config.Config{}) 72 rc := newStubRemoteCache(knownRepos) 73 return newExternalResolver(l, rc) 74 } 75 76 func newStubRemoteCache(knownRepos []repos.Repo) *repos.RemoteCache { 77 rc := repos.NewRemoteCache(knownRepos) 78 rc.RepoRootForImportPath = stubRepoRootForImportPath 79 rc.HeadCmd = nil 80 return rc 81 } 82 83 // stubRepoRootForImportPath is a stub implementation of vcs.RepoRootForImportPath 84 func stubRepoRootForImportPath(importpath string, verbose bool) (*vcs.RepoRoot, error) { 85 if strings.HasPrefix(importpath, "example.com/repo.git") { 86 return &vcs.RepoRoot{ 87 VCS: vcs.ByCmd("git"), 88 Repo: "https://example.com/repo.git", 89 Root: "example.com/repo.git", 90 }, nil 91 } 92 93 if strings.HasPrefix(importpath, "example.com/repo") { 94 return &vcs.RepoRoot{ 95 VCS: vcs.ByCmd("git"), 96 Repo: "https://example.com/repo.git", 97 Root: "example.com/repo", 98 }, nil 99 } 100 101 if strings.HasPrefix(importpath, "example.com") { 102 return &vcs.RepoRoot{ 103 VCS: vcs.ByCmd("git"), 104 Repo: "https://example.com", 105 Root: "example.com", 106 }, nil 107 } 108 109 return nil, fmt.Errorf("could not resolve import path: %q", importpath) 110 }