1
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
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 }
110
View as plain text