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