This repository has been archived by the owner on Feb 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathvcs_repo_test.go
342 lines (289 loc) · 7.57 KB
/
vcs_repo_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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package gps
import (
"context"
"errors"
"io/ioutil"
"os"
"testing"
"time"
"github.com/Masterminds/vcs"
)
// original implementation of these test files come from
// https://github.com/Masterminds/vcs test files
func TestErrs(t *testing.T) {
err := newVcsLocalErrorOr("", context.Canceled, "")
if err != context.Canceled {
t.Errorf("context errors should always pass through, got %s", err)
}
err = newVcsRemoteErrorOr("", context.Canceled, "")
if err != context.Canceled {
t.Errorf("context errors should always pass through, got %s", err)
}
err = newVcsLocalErrorOr("", context.DeadlineExceeded, "")
if err != context.DeadlineExceeded {
t.Errorf("context errors should always pass through, got %s", err)
}
err = newVcsRemoteErrorOr("", context.DeadlineExceeded, "")
if err != context.DeadlineExceeded {
t.Errorf("context errors should always pass through, got %s", err)
}
err = newVcsLocalErrorOr("foo", errors.New("bar"), "baz")
if _, is := err.(*vcs.LocalError); !is {
t.Errorf("should have gotten local error, got %T %v", err, err)
}
err = newVcsRemoteErrorOr("foo", errors.New("bar"), "baz")
if _, is := err.(*vcs.RemoteError); !is {
t.Errorf("should have gotten remote error, got %T %v", err, err)
}
}
func testSvnRepo(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("Skipping slow test in short mode")
}
ctx := context.Background()
tempDir, err := ioutil.TempDir("", "go-vcs-svn-tests")
if err != nil {
t.Fatal(err)
}
defer func() {
err = os.RemoveAll(tempDir)
if err != nil {
t.Error(err)
}
}()
rep, err := vcs.NewSvnRepo("https://github.com/Masterminds/VCSTestRepo/trunk", tempDir+string(os.PathSeparator)+"VCSTestRepo")
if err != nil {
t.Fatal(err)
}
repo := &svnRepo{rep}
// Do an initial checkout.
err = repo.get(ctx)
if err != nil {
t.Fatalf("Unable to checkout SVN repo. Err was %#v", err)
}
// Verify SVN repo is a SVN repo
if !repo.CheckLocal() {
t.Fatal("Problem checking out repo or SVN CheckLocal is not working")
}
// Update the version to a previous version.
err = repo.updateVersion(ctx, "r2")
if err != nil {
t.Fatalf("Unable to update SVN repo version. Err was %s", err)
}
// Use Version to verify we are on the right version.
v, err := repo.Version()
if err != nil {
t.Fatal(err)
}
if v != "2" {
t.Fatal("Error checking checked SVN out version")
}
// Perform an update which should take up back to the latest version.
err = repo.update(ctx)
if err != nil {
t.Fatal(err)
}
// Make sure we are on a newer version because of the update.
v, err = repo.Version()
if err != nil {
t.Fatal(err)
}
if v == "2" {
t.Fatal("Error with version. Still on old version. Update failed")
}
ci, err := repo.CommitInfo("2")
if err != nil {
t.Fatal(err)
}
if ci.Commit != "2" {
t.Error("Svn.CommitInfo wrong commit id")
}
if ci.Author != "matt.farina" {
t.Error("Svn.CommitInfo wrong author")
}
if ci.Message != "Update README.md" {
t.Error("Svn.CommitInfo wrong message")
}
ti, err := time.Parse(time.RFC3339Nano, "2015-07-29T13:46:20.000000Z")
if err != nil {
t.Fatal(err)
}
if !ti.Equal(ci.Date) {
t.Error("Svn.CommitInfo wrong date")
}
_, err = repo.CommitInfo("555555555")
if err != vcs.ErrRevisionUnavailable {
t.Error("Svn didn't return expected ErrRevisionUnavailable")
}
}
func testHgRepo(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("Skipping slow test in short mode")
}
ctx := context.Background()
tempDir, err := ioutil.TempDir("", "go-vcs-hg-tests")
if err != nil {
t.Fatal(err)
}
defer func() {
err = os.RemoveAll(tempDir)
if err != nil {
t.Error(err)
}
}()
rep, err := vcs.NewHgRepo("https://bitbucket.org/mattfarina/testhgrepo", tempDir+"/testhgrepo")
if err != nil {
t.Fatal(err)
}
repo := &hgRepo{rep}
// Do an initial clone.
err = repo.get(ctx)
if err != nil {
t.Fatalf("Unable to clone Hg repo. Err was %s", err)
}
// Verify Hg repo is a Hg repo
if !repo.CheckLocal() {
t.Fatal("Problem checking out repo or Hg CheckLocal is not working")
}
// Set the version using the short hash.
err = repo.updateVersion(ctx, "a5494ba2177f")
if err != nil {
t.Fatalf("Unable to update Hg repo version. Err was %s", err)
}
// Use Version to verify we are on the right version.
v, err := repo.Version()
if err != nil {
t.Fatal(err)
}
if v != "a5494ba2177ff9ef26feb3c155dfecc350b1a8ef" {
t.Fatalf("Error checking checked out Hg version: %s", v)
}
// Perform an update.
err = repo.fetch(ctx)
if err != nil {
t.Fatal(err)
}
}
func testGitRepo(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("Skipping slow test in short mode")
}
ctx := context.Background()
tempDir, err := ioutil.TempDir("", "go-vcs-git-tests")
if err != nil {
t.Fatal(err)
}
defer func() {
err = os.RemoveAll(tempDir)
if err != nil {
t.Error(err)
}
}()
rep, err := vcs.NewGitRepo("https://github.com/Masterminds/VCSTestRepo", tempDir+"/VCSTestRepo")
if err != nil {
t.Fatal(err)
}
repo := &gitRepo{rep}
// Do an initial clone.
err = repo.get(ctx)
if err != nil {
t.Fatalf("Unable to clone Git repo. Err was %s", err)
}
// Verify Git repo is a Git repo
if !repo.CheckLocal() {
t.Fatal("Problem checking out repo or Git CheckLocal is not working")
}
// Perform an update.
err = repo.fetch(ctx)
if err != nil {
t.Fatal(err)
}
v, err := repo.Current()
if err != nil {
t.Fatalf("Error trying Git Current: %s", err)
}
if v != "master" {
t.Fatalf("Current failed to detect Git on tip of master. Got version: %s", v)
}
// Set the version using the short hash.
err = repo.updateVersion(ctx, "806b07b")
if err != nil {
t.Fatalf("Unable to update Git repo version. Err was %s", err)
}
// Once a ref has been checked out the repo is in a detached head state.
// Trying to pull in an update in this state will cause an error. Update
// should cleanly handle this. Pulling on a branch (tested elsewhere) and
// skipping that here.
err = repo.fetch(ctx)
if err != nil {
t.Fatal(err)
}
// Use Version to verify we are on the right version.
v, err = repo.Version()
if err != nil {
t.Fatal(err)
}
if v != "806b07b08faa21cfbdae93027904f80174679402" {
t.Fatal("Error checking checked out Git version")
}
}
func testBzrRepo(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("Skipping slow test in short mode")
}
ctx := context.Background()
tempDir, err := ioutil.TempDir("", "go-vcs-bzr-tests")
if err != nil {
t.Fatal(err)
}
defer func() {
err = os.RemoveAll(tempDir)
if err != nil {
t.Error(err)
}
}()
rep, err := vcs.NewBzrRepo("https://launchpad.net/govcstestbzrrepo", tempDir+"/govcstestbzrrepo")
if err != nil {
t.Fatal(err)
}
repo := &bzrRepo{rep}
// Do an initial clone.
err = repo.get(ctx)
if err != nil {
t.Fatalf("Unable to clone Bzr repo. Err was %s", err)
}
// Verify Bzr repo is a Bzr repo
if !repo.CheckLocal() {
t.Fatal("Problem checking out repo or Bzr CheckLocal is not working")
}
v, err := repo.Current()
if err != nil {
t.Fatalf("Error trying Bzr Current: %s", err)
}
if v != "-1" {
t.Fatalf("Current failed to detect Bzr on tip of branch. Got version: %s", v)
}
err = repo.updateVersion(ctx, "2")
if err != nil {
t.Fatalf("Unable to update Bzr repo version. Err was %s", err)
}
// Use Version to verify we are on the right version.
v, err = repo.Version()
if err != nil {
t.Fatal(err)
}
if v != "2" {
t.Fatal("Error checking checked out Bzr version")
}
v, err = repo.Current()
if err != nil {
t.Fatalf("Error trying Bzr Current: %s", err)
}
if v != "2" {
t.Fatalf("Current failed to detect Bzr on rev 2 of branch. Got version: %s", v)
}
}