github.com/wolfd/bazel-gazelle@v0.14.0/internal/repos/remote_test.go (about) 1 /* Copyright 2018 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 repos 17 18 import ( 19 "fmt" 20 "strings" 21 "testing" 22 23 "golang.org/x/tools/go/vcs" 24 ) 25 26 func TestRootSpecialCases(t *testing.T) { 27 for _, tc := range []struct { 28 in, wantRoot, wantName string 29 repos []Repo 30 wantError bool 31 }{ 32 {in: "golang.org/x/net/context", wantRoot: "golang.org/x/net", wantName: "org_golang_x_net"}, 33 {in: "golang.org/x/tools/go/vcs", wantRoot: "golang.org/x/tools", wantName: "org_golang_x_tools"}, 34 {in: "golang.org/x/goimports", wantRoot: "golang.org/x/goimports", wantName: "org_golang_x_goimports"}, 35 {in: "cloud.google.com/fashion/industry", wantRoot: "cloud.google.com/fashion", wantName: "com_google_cloud_fashion"}, 36 {in: "github.com/foo", wantError: true}, 37 {in: "github.com/foo/bar", wantRoot: "github.com/foo/bar", wantName: "com_github_foo_bar"}, 38 {in: "github.com/foo/bar/baz", wantRoot: "github.com/foo/bar", wantName: "com_github_foo_bar"}, 39 {in: "gopkg.in/yaml.v2", wantRoot: "gopkg.in/yaml.v2", wantName: "in_gopkg_yaml_v2"}, 40 {in: "gopkg.in/src-d/go-git.v4", wantRoot: "gopkg.in/src-d/go-git.v4", wantName: "in_gopkg_src_d_go_git_v4"}, 41 {in: "unsupported.org/x/net/context", wantError: true}, 42 { 43 in: "private.com/my/repo/package/path", 44 repos: []Repo{ 45 { 46 Name: "com_other_host_repo", 47 GoPrefix: "other-host.com/repo", 48 }, { 49 Name: "com_private_my_repo", 50 GoPrefix: "private.com/my/repo", 51 }, 52 }, 53 wantRoot: "private.com/my/repo", 54 wantName: "com_private_my_repo", 55 }, 56 { 57 in: "unsupported.org/x/net/context", 58 repos: []Repo{ 59 { 60 Name: "com_private_my_repo", 61 GoPrefix: "private.com/my/repo", 62 }, 63 }, 64 wantError: true, 65 }, 66 { 67 in: "github.com/foo/bar", 68 repos: []Repo{{ 69 Name: "custom_repo", 70 GoPrefix: "github.com/foo/bar", 71 }}, 72 wantRoot: "github.com/foo/bar", 73 wantName: "custom_repo", 74 }, 75 } { 76 t.Run(tc.in, func(t *testing.T) { 77 rc := newStubRemoteCache(tc.repos) 78 if gotRoot, gotName, err := rc.Root(tc.in); err != nil { 79 if !tc.wantError { 80 t.Errorf("unexpected error: %v", err) 81 } 82 } else if tc.wantError { 83 t.Errorf("unexpected success: %v", tc.in) 84 } else if gotRoot != tc.wantRoot { 85 t.Errorf("root for %q: got %q; want %q", tc.in, gotRoot, tc.wantRoot) 86 } else if gotName != tc.wantName { 87 t.Errorf("name for %q: got %q; want %q", tc.in, gotName, tc.wantName) 88 } 89 }) 90 } 91 } 92 93 func TestRemote(t *testing.T) { 94 for _, tc := range []struct { 95 desc, root string 96 repos []Repo 97 wantRemote, wantVCS string 98 wantError bool 99 }{ 100 { 101 desc: "unstubbed_remote", 102 root: "github.com/bazelbuild/bazel-gazelle", 103 wantError: true, // stub should return an error 104 }, { 105 desc: "known_repo", 106 root: "github.com/example/project", 107 repos: []Repo{{ 108 Name: "com_github_example_project", 109 GoPrefix: "github.com/example/project", 110 Remote: "https://private.com/example/project", 111 VCS: "git", 112 }}, 113 wantRemote: "https://private.com/example/project", 114 wantVCS: "git", 115 }, { 116 desc: "git_repo", 117 root: "example.com/repo", // stub knows this 118 wantRemote: "https://example.com/repo.git", 119 wantVCS: "git", 120 }, { 121 desc: "local_repo", 122 root: "github.com/example/project", 123 repos: []Repo{{ 124 Name: "com_github_example_project", 125 GoPrefix: "github.com/example/project", 126 Remote: "/home/joebob/go/src/github.com/example/project", 127 VCS: "local", 128 }}, 129 wantRemote: "/home/joebob/go/src/github.com/example/project", 130 wantVCS: "local", 131 }, 132 } { 133 t.Run(tc.desc, func(t *testing.T) { 134 rc := newStubRemoteCache(tc.repos) 135 if gotRemote, gotVCS, err := rc.Remote(tc.root); err != nil { 136 if !tc.wantError { 137 t.Errorf("unexpected error: %v", err) 138 } 139 } else if tc.wantError { 140 t.Errorf("unexpected success") 141 } else if gotRemote != tc.wantRemote { 142 t.Errorf("remote for %q: got %q ; want %q", tc.root, gotRemote, tc.wantRemote) 143 } else if gotVCS != tc.wantVCS { 144 t.Errorf("vcs for %q: got %q ; want %q", tc.root, gotVCS, tc.wantVCS) 145 } 146 }) 147 } 148 } 149 150 func TestHead(t *testing.T) { 151 for _, tc := range []struct { 152 desc, remote, vcs string 153 wantCommit, wantTag string 154 wantError bool 155 }{ 156 { 157 desc: "unstubbed_remote", 158 remote: "https://github.com/bazelbuild/bazel-gazelle", 159 vcs: "git", 160 wantError: true, // stub should return an error 161 }, 162 } { 163 t.Run(tc.desc, func(t *testing.T) { 164 rc := newStubRemoteCache(nil) 165 if gotCommit, gotTag, err := rc.Head(tc.remote, tc.vcs); err != nil { 166 if !tc.wantError { 167 t.Errorf("unexpected error: %v", err) 168 } 169 } else if tc.wantError { 170 t.Errorf("unexpected success") 171 } else if gotCommit != tc.wantCommit { 172 t.Errorf("commit for %q: got %q ; want %q", tc.remote, gotCommit, tc.wantCommit) 173 } else if gotTag != tc.wantTag { 174 t.Errorf("tag for %q: got %q ; want %q", tc.remote, gotTag, tc.wantTag) 175 } 176 }) 177 } 178 } 179 180 func newStubRemoteCache(rs []Repo) *RemoteCache { 181 rc := NewRemoteCache(rs) 182 rc.RepoRootForImportPath = stubRepoRootForImportPath 183 rc.HeadCmd = stubHeadCmd 184 return rc 185 } 186 187 // stubRepoRootForImportPath is a stub implementation of vcs.RepoRootForImportPath 188 func stubRepoRootForImportPath(importpath string, verbose bool) (*vcs.RepoRoot, error) { 189 if strings.HasPrefix(importpath, "example.com/repo.git") { 190 return &vcs.RepoRoot{ 191 VCS: vcs.ByCmd("git"), 192 Repo: "https://example.com/repo.git", 193 Root: "example.com/repo.git", 194 }, nil 195 } 196 197 if strings.HasPrefix(importpath, "example.com/repo") { 198 return &vcs.RepoRoot{ 199 VCS: vcs.ByCmd("git"), 200 Repo: "https://example.com/repo.git", 201 Root: "example.com/repo", 202 }, nil 203 } 204 205 if strings.HasPrefix(importpath, "example.com") { 206 return &vcs.RepoRoot{ 207 VCS: vcs.ByCmd("git"), 208 Repo: "https://example.com", 209 Root: "example.com", 210 }, nil 211 } 212 213 return nil, fmt.Errorf("could not resolve import path: %q", importpath) 214 } 215 216 func stubHeadCmd(remote, vcs string) (string, error) { 217 if vcs == "git" && remote == "https://example.com/repo" { 218 return "abcdef", nil 219 } 220 return "", fmt.Errorf("could not resolve remote: %q", remote) 221 }