-
Notifications
You must be signed in to change notification settings - Fork 128
/
repo_remote_test.go
208 lines (180 loc) · 4.38 KB
/
repo_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
// Copyright 2020 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package git
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLsRemote(t *testing.T) {
tests := []struct {
url string
opt LsRemoteOptions
expRefs []*Reference
}{
{
url: testrepo.Path(),
opt: LsRemoteOptions{
Heads: true,
Patterns: []string{"release-1.0"},
},
expRefs: []*Reference{
{
ID: "0eedd79eba4394bbef888c804e899731644367fe",
Refspec: "refs/heads/release-1.0",
},
},
}, {
url: testrepo.Path(),
opt: LsRemoteOptions{
Tags: true,
Patterns: []string{"v1.0.0"},
},
expRefs: []*Reference{
{
ID: "0eedd79eba4394bbef888c804e899731644367fe",
Refspec: "refs/tags/v1.0.0",
},
},
}, {
url: testrepo.Path(),
opt: LsRemoteOptions{
Refs: true,
Patterns: []string{"v1.0.0"},
},
expRefs: []*Reference{
{
ID: "0eedd79eba4394bbef888c804e899731644367fe",
Refspec: "refs/tags/v1.0.0",
},
},
},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
refs, err := LsRemote(test.url, test.opt)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, test.expRefs, refs)
})
}
}
func TestIsURLAccessible(t *testing.T) {
tests := []struct {
url string
expVal bool
}{
{
url: testrepo.Path(),
expVal: true,
}, {
url: os.TempDir(),
expVal: false,
},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
assert.Equal(t, test.expVal, IsURLAccessible(DefaultTimeout, test.url))
})
}
}
func TestRepository_RemoteAdd(t *testing.T) {
path := tempPath()
defer func() {
_ = os.RemoveAll(path)
}()
err := Init(path, InitOptions{
Bare: true,
})
if err != nil {
t.Fatal(err)
}
r, err := Open(path)
if err != nil {
t.Fatal(err)
}
// Add testrepo as the mirror remote and fetch right away
err = r.RemoteAdd("origin", testrepo.Path(), RemoteAddOptions{
Fetch: true,
MirrorFetch: true,
})
if err != nil {
t.Fatal(err)
}
// Check a non-default branch: release-1.0
assert.True(t, r.HasReference(RefsHeads+"release-1.0"))
}
func TestRepository_RemoteRemove(t *testing.T) {
r, cleanup, err := setupTempRepo()
if err != nil {
t.Fatal(err)
}
defer cleanup()
err = r.RemoteRemove("origin", RemoteRemoveOptions{})
assert.Nil(t, err)
err = r.RemoteRemove("origin", RemoteRemoveOptions{})
assert.Equal(t, ErrRemoteNotExist, err)
}
func TestRepository_Remotes(t *testing.T) {
r, cleanup, err := setupTempRepo()
if err != nil {
t.Fatal(err)
}
defer cleanup()
// 1 remote
remotes, err := r.Remotes()
assert.Nil(t, err)
assert.Equal(t, []string{"origin"}, remotes)
// 2 remotes
err = r.RemoteAdd("t", "t")
assert.Nil(t, err)
remotes, err = r.Remotes()
assert.Nil(t, err)
assert.Equal(t, []string{"origin", "t"}, remotes)
assert.Len(t, remotes, 2)
// 0 remotes
err = r.RemoteRemove("t")
assert.Nil(t, err)
err = r.RemoteRemove("origin")
assert.Nil(t, err)
remotes, err = r.Remotes()
assert.Nil(t, err)
assert.Equal(t, []string{}, remotes)
assert.Len(t, remotes, 0)
}
func TestRepository_RemoteURLFamily(t *testing.T) {
r, cleanup, err := setupTempRepo()
if err != nil {
t.Fatal(err)
}
defer cleanup()
err = r.RemoteSetURLDelete("origin", ".*")
assert.Equal(t, ErrNotDeleteNonPushURLs, err)
err = r.RemoteSetURL("notexist", "t")
assert.Equal(t, ErrRemoteNotExist, err)
err = r.RemoteSetURL("notexist", "t", RemoteSetURLOptions{Regex: "t"})
assert.Equal(t, ErrRemoteNotExist, err)
// Default origin URL is not easily testable
err = r.RemoteSetURL("origin", "t")
assert.Nil(t, err)
urls, err := r.RemoteGetURL("origin")
assert.Nil(t, err)
assert.Equal(t, []string{"t"}, urls)
err = r.RemoteSetURLAdd("origin", "e")
assert.Nil(t, err)
urls, err = r.RemoteGetURL("origin", RemoteGetURLOptions{All: true})
assert.Nil(t, err)
assert.Equal(t, []string{"t", "e"}, urls)
err = r.RemoteSetURL("origin", "s", RemoteSetURLOptions{Regex: "e"})
assert.Nil(t, err)
urls, err = r.RemoteGetURL("origin", RemoteGetURLOptions{All: true})
assert.Nil(t, err)
assert.Equal(t, []string{"t", "s"}, urls)
err = r.RemoteSetURLDelete("origin", "t")
assert.Nil(t, err)
urls, err = r.RemoteGetURL("origin", RemoteGetURLOptions{All: true})
assert.Nil(t, err)
assert.Equal(t, []string{"s"}, urls)
}