-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathget_test.go
637 lines (539 loc) · 17.7 KB
/
get_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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
package gateway
import (
"bytes"
"fmt"
"github.com/pkg/errors"
"io/ioutil"
"path/filepath"
"strings"
"testing"
"time"
"github.com/vim-volt/volt/internal/testutil"
"github.com/vim-volt/volt/lockjson"
"github.com/vim-volt/volt/pathutil"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
// Checks:
// (A) Does not show `[ERROR]`, `[WARN]` messages
// (B) Exit with zero status
// (C) Repositories are cloned at `$VOLTPATH/repos/<repos>/`
// (D) Plugconf files are installed at `$VOLTPATH/plugconf/<repos>.vim`
// (E) Directories are copied to `~/.vim/pack/volt/<repos>/`, and the contents are same
// (F) Entries are added to lock.json
// (G) tags files are created at `~/.vim/pack/volt/<repos>/doc/tags`
// (H) Output contains "! {repos} > install failed"
// (I) Output contains "! {repos} > upgrade failed"
// (J) Output contains "# {repos} > no change"
// (K) Output contains "# {repos} > already exists"
// (L) Output contains "+ {repos} > added repository to current profile"
// (M) Output contains "+ {repos} > installed"
// (N) Output contains "* {repos} > updated lock.json revision ({from}..{to})"
// (O) Output contains "* {repos} > upgraded ({from}..{to})"
// (P) Output contains "{repos}: HEAD and locked revision are different ..."
// TODO: Add test cases
// * Specify plugins which have dependency plugins without help (A, B, C, D, E, F, !G) / with help (A, B, C, D, E, F, G)
// * Specify plugins which have dependency plugins and plugins which have no dependency plugins without help (A, B, C, D, E, F, !G) / with help (A, B, C, D, E, F, G)
// Specify one plugin with help (A, B, C, D, E, F, G, M) / without help (A, B, C, D, E, F, !G, M)
func TestVoltGetOnePlugin(t *testing.T) {
for _, tt := range []struct {
withHelp bool
reposPath pathutil.ReposPath
}{
{true, pathutil.ReposPath("github.com/tyru/caw.vim")},
{false, pathutil.ReposPath("github.com/tyru/dummy")},
} {
t.Run(fmt.Sprintf("with help=%v", tt.withHelp), func(t *testing.T) {
testGetMatrix(t, func(t *testing.T, strategy string) {
// =============== setup =============== //
testutil.SetUpEnv(t)
testutil.InstallConfig(t, "strategy-"+strategy+".toml")
// =============== run =============== //
out, err := testutil.RunVolt("get", tt.reposPath.String())
// (A, B)
testutil.SuccessExit(t, out, err)
// (C)
reposDir := tt.reposPath.FullPath()
if !pathutil.Exists(reposDir) {
t.Error("repos does not exist: " + reposDir)
}
_, err = git.PlainOpen(reposDir)
if err != nil {
t.Error("not git repository: " + reposDir)
}
// (D)
plugconf := tt.reposPath.Plugconf()
if !pathutil.Exists(plugconf) {
t.Error("plugconf does not exist: " + plugconf)
}
// TODO: check plugconf has s:config(), s:loaded_on(), depends()
// (E)
vimReposDir := tt.reposPath.EncodeToPlugDirName()
if !pathutil.Exists(vimReposDir) {
t.Error("vim repos does not exist: " + vimReposDir)
}
// (F)
testReposPathWereAdded(t, tt.reposPath)
tags := filepath.Join(vimReposDir, "doc", "tags")
if tt.withHelp {
// (G)
if !pathutil.Exists(tags) {
t.Error("doc/tags was not created: " + tags)
}
} else {
// (!G)
if pathutil.Exists(tags) {
t.Error("doc/tags was created: " + tags)
}
}
})
})
}
}
// (J, K, L, M, N, O, P)
func TestVoltGetMsg(t *testing.T) {
testGetMatrix(t, func(t *testing.T, strategy string) {
// =============== setup =============== //
testutil.SetUpEnv(t)
testutil.InstallConfig(t, "strategy-"+strategy+".toml")
reposPath := pathutil.ReposPath("github.com/tyru/caw.vim")
// ===================================
// Install plugin (installed)
// ===================================
out, err := testutil.RunVolt("get", reposPath.String())
// (A, B)
testutil.SuccessExit(t, out, err)
// (M)
msg := fmt.Sprintf(fmtInstalled, reposPath)
if !bytes.Contains(out, []byte(msg)) {
t.Errorf("Output does not contain %q\n%s", msg, string(out))
}
// ===================================
// Install again (already exists)
// ===================================
out, err = testutil.RunVolt("get", reposPath.String())
// (A, B)
testutil.SuccessExit(t, out, err)
// (K)
msg = fmt.Sprintf(fmtAlreadyExists, reposPath)
if !bytes.Contains(out, []byte(msg)) {
t.Errorf("Output does not contain %q\n%s", msg, string(out))
}
// ===================================
// Upgrade one plugin (no change)
// ===================================
out, err = testutil.RunVolt("get", "-u", reposPath.String())
// (A, B)
testutil.SuccessExit(t, out, err)
// (J)
msg = fmt.Sprintf(fmtNoChange, reposPath)
if !bytes.Contains(out, []byte(msg)) {
t.Errorf("Output does not contain %q\n%s", msg, string(out))
}
// ===================================
// Remove plugin from current profile
// ===================================
out, err = testutil.RunVolt("disable", reposPath.String())
// (A, B)
testutil.SuccessExit(t, out, err)
// =====================================================================
// Add plugin to current profile (added repository to current profile")
// =====================================================================
out, err = testutil.RunVolt("get", reposPath.String())
// (A, B)
testutil.SuccessExit(t, out, err)
// (L)
msg = fmt.Sprintf(fmtAddedRepos, reposPath)
if !bytes.Contains(out, []byte(msg)) {
t.Errorf("Output does not contain %q\n%s", msg, string(out))
}
// ================
// Commit on repos
// ================
head, next, err := gitCommitOne(reposPath)
if err != nil {
t.Error("gitCommitOne() failed: " + err.Error())
}
// ================================================================
// volt build outputs "HEAD and locked revision are different ..."
// ================================================================
out, err = testutil.RunVolt("build")
outstr := string(out)
// (!A, B)
if !strings.Contains(outstr, "[WARN]") && !strings.Contains(outstr, "[ERROR]") {
t.Errorf("expected error but no error: %s", outstr)
}
if err != nil {
t.Error("expected success exit but exited with failure: " + err.Error())
}
// (P)
for _, msg := range []string{
string(reposPath) + ": HEAD and locked revision are different",
" HEAD: " + next.String(),
" locked revision: " + head.String(),
" Please run 'volt get -l' to update locked revision.",
} {
if !bytes.Contains(out, []byte(msg)) {
t.Errorf("Output does not contain %q\n%s", msg, string(out))
}
}
// ===========================================
// Update lock.json revision
// ===========================================
out, err = testutil.RunVolt("get", reposPath.String())
// (A, B)
testutil.SuccessExit(t, out, err)
// (N)
msg = fmt.Sprintf(fmtRevUpdate, reposPath, head.String(), next.String())
if !bytes.Contains(out, []byte(msg)) {
t.Errorf("Output does not contain %q\n%s", msg, string(out))
}
// ================================
// Install again (already exists)
// ================================
out, err = testutil.RunVolt("get", reposPath.String())
// (A, B)
testutil.SuccessExit(t, out, err)
// (K)
msg = fmt.Sprintf(fmtAlreadyExists, reposPath)
if !bytes.Contains(out, []byte(msg)) {
t.Errorf("Output does not contain %q\n%s", msg, string(out))
}
// ========================================================================
// volt build DOES NOT output "HEAD and locked revision are different ..."
// ========================================================================
out, err = testutil.RunVolt("build")
// (A, B)
testutil.SuccessExit(t, out, err)
// (!P)
msg = "HEAD and locked revision are different"
if bytes.Contains(out, []byte(msg)) {
t.Errorf("Output contains %q\n%s", msg, string(out))
}
// ==================================
// "git reset --hard HEAD~2" on repos
// ==================================
prev, _, err := gitResetHard(reposPath, "HEAD~2")
if err != nil {
t.Error("gitResetHard() failed: " + err.Error())
}
// ===========================================
// Update lock.json revision
// ===========================================
out, err = testutil.RunVolt("get", reposPath.String())
// (A, B)
testutil.SuccessExit(t, out, err)
// (N)
msg = fmt.Sprintf(fmtRevUpdate, reposPath, next.String(), prev.String())
if !bytes.Contains(out, []byte(msg)) {
t.Errorf("Output does not contain %q\n%s", msg, string(out))
}
// ================================
// Upgrade plugin (upgraded)
// ================================
out, err = testutil.RunVolt("get", "-u", reposPath.String())
// (A, B)
testutil.SuccessExit(t, out, err)
// (O)
msg = fmt.Sprintf(fmtUpgraded, reposPath, prev.String(), head.String())
if !bytes.Contains(out, []byte(msg)) {
t.Errorf("Output does not contain %q\n%s", msg, string(out))
}
})
}
// Specify two or more plugins without help (A, B, C, D, E, F, !G, M) / with help (A, B, C, D, E, F, G, M)
func TestVoltGetTwoOrMorePlugin(t *testing.T) {
for _, tt := range []struct {
withHelp bool
reposPathList pathutil.ReposPathList
}{
{true, pathutil.ReposPathList{
pathutil.ReposPath("github.com/tyru/caw.vim"),
pathutil.ReposPath("github.com/tyru/capture.vim"),
}},
{false, pathutil.ReposPathList{
pathutil.ReposPath("github.com/tyru/dummy"),
pathutil.ReposPath("github.com/tyru/dummy2"),
}},
} {
t.Run(fmt.Sprintf("with help=%v", tt.withHelp), func(t *testing.T) {
testGetMatrix(t, func(t *testing.T, strategy string) {
// =============== setup =============== //
testutil.SetUpEnv(t)
testutil.InstallConfig(t, "strategy-"+strategy+".toml")
// =============== run =============== //
// (A, B)
args := append([]string{"get"}, tt.reposPathList.Strings()...)
out, err := testutil.RunVolt(args...)
testutil.SuccessExit(t, out, err)
for _, reposPath := range tt.reposPathList {
// (C)
reposDir := reposPath.FullPath()
if !pathutil.Exists(reposDir) {
t.Error("repos does not exist: " + reposDir)
}
_, err := git.PlainOpen(reposDir)
if err != nil {
t.Error("not git repository: " + reposDir)
}
// (D)
plugconf := reposPath.Plugconf()
if !pathutil.Exists(plugconf) {
t.Error("plugconf does not exist: " + plugconf)
}
// TODO: check plugconf has s:config(), s:loaded_on(), depends()
// (E)
vimReposDir := reposPath.EncodeToPlugDirName()
if !pathutil.Exists(vimReposDir) {
t.Error("vim repos does not exist: " + vimReposDir)
}
// (F)
testReposPathWereAdded(t, reposPath)
// (G) and (!G)
tags := filepath.Join(vimReposDir, "doc", "tags")
if tt.withHelp {
if !pathutil.Exists(tags) {
t.Error("doc/tags was not created: " + tags)
}
} else {
if pathutil.Exists(tags) {
t.Error("doc/tags was created: " + tags)
}
}
// (M)
msg := fmt.Sprintf(fmtInstalled, reposPath)
if !bytes.Contains(out, []byte(msg)) {
t.Errorf("Output does not contain %q\n%s", msg, string(out))
}
}
})
})
}
}
// 'volt get -l' must not add disabled plugins
func TestVoltGetLoptMustNotAddDisabledPlugins(t *testing.T) {
// =============== setup =============== //
testutil.SetUpEnv(t)
// =============== run =============== //
// (A, B)
out, err := testutil.RunVolt("get", "tyru/dummy", "tyru/dummy2")
testutil.SuccessExit(t, out, err)
testReposPathWereEnabled(t, pathutil.ReposPath("github.com/tyru/dummy"))
testReposPathWereEnabled(t, pathutil.ReposPath("github.com/tyru/dummy2"))
// (A, B)
out, err = testutil.RunVolt("disable", "tyru/dummy2")
testutil.SuccessExit(t, out, err)
testReposPathWereEnabled(t, pathutil.ReposPath("github.com/tyru/dummy"))
testReposPathWereDisabled(t, pathutil.ReposPath("github.com/tyru/dummy2"))
// (A, B)
out, err = testutil.RunVolt("get", "-l")
testutil.SuccessExit(t, out, err)
testReposPathWereEnabled(t, pathutil.ReposPath("github.com/tyru/dummy"))
testReposPathWereDisabled(t, pathutil.ReposPath("github.com/tyru/dummy2"))
}
// [error] Specify invalid argument (!A, !B, !C, !D, !E, !F, !G)
func TestErrVoltGetInvalidArgs(t *testing.T) {
// =============== setup =============== //
testutil.SetUpEnv(t)
// =============== run =============== //
out, err := testutil.RunVolt("get", "caw.vim")
// (!A, !B)
testutil.FailExit(t, out, err)
for _, reposPath := range []pathutil.ReposPath{
pathutil.ReposPath("caw.vim"),
pathutil.ReposPath("github.com/caw.vim"),
} {
// (!C)
reposDir := reposPath.FullPath()
if pathutil.Exists(reposDir) {
t.Error("repos exists: " + reposDir)
}
// (!D)
plugconf := reposPath.Plugconf()
if pathutil.Exists(plugconf) {
t.Error("plugconf exists: " + plugconf)
}
// (!E)
vimReposDir := reposPath.EncodeToPlugDirName()
if pathutil.Exists(vimReposDir) {
t.Error("vim repos exists: " + vimReposDir)
}
// (!F)
testReposPathWereNotAdded(t, reposPath)
// (!G)
tags := filepath.Join(vimReposDir, "doc", "tags")
if pathutil.Exists(tags) {
t.Error("doc/tags was created: " + tags)
}
}
}
// [error] Specify plugin which does not exist (!A, !B, !C, !D, !E, !F, !G, H)
func TestErrVoltGetNotFound(t *testing.T) {
// =============== setup =============== //
testutil.SetUpEnv(t)
// =============== run =============== //
out, err := testutil.RunVolt("get", "vim-volt/not_found")
// (!A, !B)
testutil.FailExit(t, out, err)
reposPath := pathutil.ReposPath("github.com/vim-volt/not_found")
// (!C)
reposDir := reposPath.FullPath()
if pathutil.Exists(reposDir) {
t.Error("repos exists: " + reposDir)
}
// (!D)
plugconf := reposPath.Plugconf()
if pathutil.Exists(plugconf) {
t.Error("plugconf exists: " + plugconf)
}
// (!E)
vimReposDir := reposPath.EncodeToPlugDirName()
if pathutil.Exists(vimReposDir) {
t.Error("vim repos exists: " + vimReposDir)
}
// (!F)
testReposPathWereNotAdded(t, reposPath)
// (!G)
tags := filepath.Join(vimReposDir, "doc", "tags")
if pathutil.Exists(tags) {
t.Error("doc/tags was created: " + tags)
}
// (H)
msg := fmt.Sprintf(fmtInstallFailed, reposPath)
if !bytes.Contains(out, []byte(msg)) {
t.Errorf("Output does not contain %q\n%s", msg, string(out))
}
}
func testReposPathWereAdded(t *testing.T, reposPath pathutil.ReposPath) {
t.Helper()
lockJSON, err := lockjson.Read()
if err != nil {
t.Error("lockjson.Read() returned non-nil error: " + err.Error())
}
if !lockJSON.Repos.Contains(reposPath) {
t.Error("repos was not added to lock.json/repos: " + reposPath)
}
for i := range lockJSON.Profiles {
if !lockJSON.Profiles[i].ReposPath.Contains(reposPath) {
t.Error("repos was not added to lock.json/profiles/repos_path: " + reposPath)
}
}
}
func testReposPathWereEnabled(t *testing.T, reposPath pathutil.ReposPath) {
t.Helper()
checkTestReposPath(t, reposPath, true)
}
func testReposPathWereDisabled(t *testing.T, reposPath pathutil.ReposPath) {
t.Helper()
checkTestReposPath(t, reposPath, false)
}
func checkTestReposPath(t *testing.T, reposPath pathutil.ReposPath, enabled bool) {
t.Helper()
lockJSON, err := lockjson.Read()
if err != nil {
t.Error("lockjson.Read() returned non-nil error: " + err.Error())
}
if !lockJSON.Repos.Contains(reposPath) {
t.Error("repos was not added to lock.json/repos: " + reposPath)
}
for i := range lockJSON.Profiles {
if enabled {
if !lockJSON.Profiles[i].ReposPath.Contains(reposPath) {
t.Error("repos was not added to lock.json/profiles/repos_path: " + reposPath)
}
} else {
if lockJSON.Profiles[i].ReposPath.Contains(reposPath) {
t.Error("repos was added to lock.json/profiles/repos_path: " + reposPath)
}
}
}
}
func testReposPathWereNotAdded(t *testing.T, reposPath pathutil.ReposPath) {
t.Helper()
lockJSON, err := lockjson.Read()
if err != nil {
t.Error("lockjson.Read() returned non-nil error: " + err.Error())
}
if lockJSON.Repos.Contains(reposPath) {
t.Error("repos was added to lock.json/repos: " + reposPath)
}
for i := range lockJSON.Profiles {
if lockJSON.Profiles[i].ReposPath.Contains(reposPath) {
t.Error("repos was added to lock.json/profiles/repos_path: " + reposPath)
}
}
}
func testGetMatrix(t *testing.T, f func(*testing.T, string)) {
for _, strategy := range testutil.AvailableStrategies() {
t.Run(fmt.Sprintf("strategy=%v", strategy), func(t *testing.T) {
f(t, strategy)
})
}
}
func gitCommitOne(reposPath pathutil.ReposPath) (prev plumbing.Hash, current plumbing.Hash, err error) {
var (
relPath = "hello"
content = []byte("hello world!")
commitMsg = "hello world"
commitOptions = &git.CommitOptions{
Author: &object.Signature{
Name: "John Doe",
Email: "[email protected]",
When: time.Now(),
},
}
)
filename := filepath.Join(reposPath.FullPath(), relPath)
if err = ioutil.WriteFile(filename, content, 0644); err != nil {
err = errors.Wrap(err, "ioutil.WriteFile() failed")
return
}
r, err := git.PlainOpen(reposPath.FullPath())
if err != nil {
return
}
head, err := r.Head()
if err != nil {
return
} else {
prev = head.Hash()
}
w, err := r.Worktree()
if err != nil {
return
}
_, err = w.Add(relPath)
if err != nil {
return
}
current, err = w.Commit(commitMsg, commitOptions)
return
}
func gitResetHard(reposPath pathutil.ReposPath, ref string) (current plumbing.Hash, next plumbing.Hash, err error) {
r, err := git.PlainOpen(reposPath.FullPath())
if err != nil {
return
}
w, err := r.Worktree()
if err != nil {
return
}
head, err := r.Head()
if err != nil {
return
} else {
next = head.Hash()
}
rev, err := r.ResolveRevision(plumbing.Revision(ref))
if err != nil {
return
} else {
current = *rev
}
err = w.Reset(&git.ResetOptions{
Commit: current,
Mode: git.HardReset,
})
return
}