github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/cmd/fetch_repo/fetch_repo_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 main 17 18 import ( 19 "os" 20 "reflect" 21 "testing" 22 23 "golang.org/x/tools/go/vcs" 24 ) 25 26 var root = &vcs.RepoRoot{ 27 VCS: vcs.ByCmd("git"), 28 Repo: "https://github.com/bazeltest/rules_go", 29 Root: "github.com/bazeltest/rules_go", 30 } 31 32 func TestMain(m *testing.M) { 33 // Replace vcs.RepoRootForImportPath to disable any network calls. 34 repoRootForImportPath = func(_ string, _ bool) (*vcs.RepoRoot, error) { 35 return root, nil 36 } 37 os.Exit(m.Run()) 38 } 39 40 func TestGetRepoRoot(t *testing.T) { 41 for _, tc := range []struct { 42 label string 43 remote string 44 cmd string 45 importpath string 46 r *vcs.RepoRoot 47 }{ 48 { 49 label: "all", 50 remote: "https://github.com/bazeltest/rules_go", 51 cmd: "git", 52 importpath: "github.com/bazeltest/rules_go", 53 r: root, 54 }, 55 { 56 label: "different remote", 57 remote: "https://example.com/rules_go", 58 cmd: "git", 59 importpath: "github.com/bazeltest/rules_go", 60 r: &vcs.RepoRoot{ 61 VCS: vcs.ByCmd("git"), 62 Repo: "https://example.com/rules_go", 63 Root: "github.com/bazeltest/rules_go", 64 }, 65 }, 66 { 67 label: "only importpath", 68 importpath: "github.com/bazeltest/rules_go", 69 r: root, 70 }, 71 } { 72 r, err := getRepoRoot(tc.remote, tc.cmd, tc.importpath) 73 if err != nil { 74 t.Errorf("[%s] %v", tc.label, err) 75 } 76 if !reflect.DeepEqual(r, tc.r) { 77 t.Errorf("[%s] Expected %+v, got %+v", tc.label, tc.r, r) 78 } 79 } 80 } 81 82 func TestGetRepoRoot_error(t *testing.T) { 83 for _, tc := range []struct { 84 label string 85 remote string 86 cmd string 87 importpath string 88 }{ 89 { 90 label: "importpath as remote", 91 remote: "github.com/bazeltest/rules_go", 92 }, 93 { 94 label: "missing vcs", 95 remote: "https://github.com/bazeltest/rules_go", 96 importpath: "github.com/bazeltest/rules_go", 97 }, 98 { 99 label: "missing remote", 100 cmd: "git", 101 importpath: "github.com/bazeltest/rules_go", 102 }, 103 } { 104 r, err := getRepoRoot(tc.remote, tc.cmd, tc.importpath) 105 if err == nil { 106 t.Errorf("[%s] expected error. Got %+v", tc.label, r) 107 } 108 } 109 }