-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit_remote_test.go
239 lines (215 loc) · 5.14 KB
/
git_remote_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"errors"
"io/ioutil"
"log"
"os"
"path/filepath"
"testing"
)
func mkTempDir() string {
tmpDir := os.TempDir()
testDir, err := ioutil.TempDir(tmpDir, "tests-*")
if err != nil {
log.Fatal(err)
}
return testDir
}
func TestRemoteUrl(t *testing.T) {
cases := map[string]struct {
path string
branch string
line1 int
line2 int
want string
}{
"root-dir": {
path: "./",
branch: "",
line1: 0,
line2: 0,
want: "https://github.com/inouet/gh-open",
},
"root-dir-master": {
path: "./",
branch: "master",
line1: 0,
line2: 0,
want: "https://github.com/inouet/gh-open/tree/master/",
},
"readme.md": {
path: "./README.md",
branch: "master",
line1: 10,
line2: 0,
want: "https://github.com/inouet/gh-open/tree/master/README.md#L10",
},
}
for name, c := range cases {
gr, err := newGitRemote(c.path)
if err != nil {
t.Fatal(err)
}
got, _ := gr.remoteURL(c.branch, c.line1, c.line2)
if got != c.want {
t.Errorf("%s want '%s', got '%s'\n", name, c.want, got)
}
}
}
func TestRemoteUrlFunctional(t *testing.T) {
t.Parallel()
cloneDir := mkTempDir()
defer os.RemoveAll(cloneDir)
cases := map[string]struct {
repo string
repoDir string
path string
branch string
line1 int
line2 int
want string
}{
"github-cheat-sheet": {
repo: "https://github.com/githubtraining/github-cheat-sheet.git",
repoDir: "github-cheat-sheet",
path: "LICENSE",
branch: "master",
line1: 3,
line2: 4,
want: "https://github.com/githubtraining/github-cheat-sheet/tree/master/LICENSE#L3-L4",
},
"bitbucket-test": {
repo: "https://bitbucket.org/atn13/bitbucketstationlocations.git",
repoDir: "bitbucketstationlocations",
path: "README.txt",
branch: "master",
line1: 2,
line2: 4,
want: "https://bitbucket.org/atn13/bitbucketstationlocations/src/master/README.txt#lines-2:4",
},
"gitlab-gitlab-examples-docker": {
repo: "https://gitlab.com/gitlab-examples/docker.git",
repoDir: "docker",
path: "Dockerfile",
branch: "master",
line1: 1,
line2: 2,
want: "https://gitlab.com/gitlab-examples/docker/-/blob/master/Dockerfile#L1-2",
},
}
git, _ := newGit(cloneDir)
for name, c := range cases {
c := c
git.clone(c.repo)
gitDir := filepath.Join(cloneDir, c.repoDir)
path := filepath.Join(gitDir, c.path)
gr, err := newGitRemote(path)
if err != nil {
t.Fatal(err)
}
got, err := gr.remoteURL(c.branch, c.line1, c.line2)
if err != nil {
t.Fatal(err)
}
if got != c.want {
t.Errorf("%s want '%s', got '%s'\n", name, c.want, got)
}
}
}
func TestConfig(t *testing.T) {
testDir := mkTempDir()
defer os.RemoveAll(testDir)
git, _ := newGit(testDir)
git.clone("https://github.com/githubtraining/github-cheat-sheet.git")
path := filepath.Join(testDir, "/github-cheat-sheet")
git, _ = newGit(path)
// set config
git.exec("config", gitConfigUrlTypeName, "bitbucket.org")
git.exec("config", gitConfigProtocolName, "http")
gr, err := newGitRemote(filepath.Join(path, "LICENSE"))
if err != nil {
t.Fatal(err)
}
got, _ := gr.remoteURL("master", 3, 4)
// Expect bitbucket style url and http protocol
want := "https://github.com/githubtraining/github-cheat-sheet/src/master/LICENSE#lines-3:4"
if got != want {
t.Errorf("want '%s', got '%s'\n", want, got)
}
}
func TestNewGitRemote(t *testing.T) {
emptyDir := mkTempDir()
defer os.RemoveAll(emptyDir)
cases := []struct {
path string
want string
wantErr error
}{
{
path: emptyDir,
wantErr: errors.New("not a git repository (or any of the parent directories)"),
},
}
for _, c := range cases {
_, err := newGitRemote(c.path)
if c.wantErr != nil {
if err == nil {
t.Errorf("want '%s', got '%s'\n", c.wantErr.Error(), err)
continue
}
if c.wantErr.Error() != err.Error() {
t.Errorf("want '%s', got '%s'\n", c.wantErr.Error(), err.Error())
}
}
}
}
func TestRelativePath(t *testing.T) {
testDir := mkTempDir()
defer os.RemoveAll(testDir)
subDir := "foo/bar/baz"
os.MkdirAll(testDir+"/"+subDir, 0777)
cases := []struct {
path1 string
path2 string
want string
}{
{
path1: testDir,
path2: testDir + "/" + subDir,
want: subDir,
},
{
path1: testDir,
path2: testDir,
want: "", // same directory return ""
},
}
for _, c := range cases {
got, _ := relativePath(c.path1, c.path2)
if got != c.want {
t.Errorf("want '%s', got '%s'\n", c.want, got)
}
}
}
func TestGetLineOption(t *testing.T) {
cases := []struct {
input string
wantLine1 int
wantLine2 int
wantErr bool
}{
{input: "", wantLine1: 0, wantLine2: 0, wantErr: false},
{input: "3", wantLine1: 3, wantLine2: 0, wantErr: false},
{input: "3-10", wantLine1: 3, wantLine2: 10, wantErr: false},
{input: "3-", wantLine1: 0, wantLine2: 0, wantErr: true},
}
for _, c := range cases {
line1, line2, err := getLineOption(c.input)
if c.wantErr && err == nil {
t.Errorf("'%s' wantErr %v, got %v\n", c.input, c.wantErr, err)
}
if line1 != c.wantLine1 || line2 != c.wantLine2 {
t.Errorf("'%s' wantErr %v, got %v\n", c.input, c.wantErr, err)
}
}
}