This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
context_test.go
598 lines (510 loc) · 16 KB
/
context_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
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dep
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"unicode"
"github.com/golang/dep/internal/test"
)
func discardLogger() *log.Logger {
return log.New(ioutil.Discard, "", 0)
}
func TestCtx_ProjectImport(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()
h.TempDir("src")
h.Setenv("GOPATH", h.Path("."))
depCtx := &Ctx{GOPATH: h.Path(".")}
importPaths := []string{
"github.com/pkg/errors",
"my/silly/thing",
}
for _, want := range importPaths {
fullpath := filepath.Join(depCtx.GOPATH, "src", want)
h.TempDir(filepath.Join("src", want))
got, err := depCtx.ImportForAbs(fullpath)
if err != nil {
t.Fatal(err)
}
if got != want {
t.Fatalf("expected %s, got %s", want, got)
}
}
// test where it should return an error when directly within $GOPATH/src
got, err := depCtx.ImportForAbs(filepath.Join(depCtx.GOPATH, "src"))
if err == nil || !strings.Contains(err.Error(), "GOPATH/src") {
t.Fatalf("should have gotten an error for use directly in GOPATH/src, but got %s", got)
}
// test where it should return an error
got, err = depCtx.ImportForAbs("tra/la/la/la")
if err == nil {
t.Fatalf("should have gotten an error but did not for tra/la/la/la: %s", got)
}
}
func TestAbsoluteProjectRoot(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()
h.TempDir("src")
h.Setenv("GOPATH", h.Path("."))
depCtx := &Ctx{GOPATH: h.Path(".")}
importPaths := map[string]bool{
"github.com/pkg/errors": true,
"my/silly/thing": false,
}
for i, create := range importPaths {
if create {
h.TempDir(filepath.Join("src", i))
}
}
for i, ok := range importPaths {
got, err := depCtx.AbsForImport(i)
if ok {
h.Must(err)
want := h.Path(filepath.Join("src", i))
if got != want {
t.Fatalf("expected %s, got %q", want, got)
}
continue
}
if err == nil {
t.Fatalf("expected %s to fail", i)
}
}
// test that a file fails
h.TempFile("src/thing/thing.go", "hello world")
_, err := depCtx.AbsForImport("thing/thing.go")
if err == nil {
t.Fatal("error should not be nil for a file found")
}
}
func TestLoadProject(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()
h.TempDir(filepath.Join("src", "test1", "sub"))
h.TempFile(filepath.Join("src", "test1", ManifestName), "")
h.TempFile(filepath.Join("src", "test1", LockName), `memo = "cdafe8641b28cd16fe025df278b0a49b9416859345d8b6ba0ace0272b74925ee"`)
h.TempDir(filepath.Join("src", "test2", "sub"))
h.TempFile(filepath.Join("src", "test2", ManifestName), "")
var testcases = []struct {
name string
lock bool
wd string
}{
{"direct", true, filepath.Join("src", "test1")},
{"ascending", true, filepath.Join("src", "test1", "sub")},
{"without lock", false, filepath.Join("src", "test2")},
{"ascending without lock", false, filepath.Join("src", "test2", "sub")},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
ctx := &Ctx{
Out: discardLogger(),
Err: discardLogger(),
}
err := ctx.SetPaths(h.Path(tc.wd), h.Path("."))
if err != nil {
t.Fatalf("%+v", err)
}
p, err := ctx.LoadProject()
switch {
case err != nil:
t.Fatalf("%s: LoadProject failed: %+v", tc.wd, err)
case p.Manifest == nil:
t.Fatalf("%s: Manifest file didn't load", tc.wd)
case tc.lock && p.Lock == nil:
t.Fatalf("%s: Lock file didn't load", tc.wd)
case !tc.lock && p.Lock != nil:
t.Fatalf("%s: Non-existent Lock file loaded", tc.wd)
}
})
}
}
func TestExplicitRootProject(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()
h.TempDir(filepath.Join("src", "test1", "sub"))
h.TempFile(filepath.Join("src", "test1", ManifestName), "")
h.TempFile(filepath.Join("src", "test1", LockName), `memo = "cdafe8641b28cd16fe025df278b0a49b9416859345d8b6ba0ace0272b74925ee"`)
h.TempDir(filepath.Join("src", "test2", "sub"))
h.TempFile(filepath.Join("src", "test2", ManifestName), "")
h.Setenv("DEP_PROJECT_ROOT", "github.com/user/module")
type tcase struct {
name string
lock bool
wd string
}
var testcases = []tcase{
{"direct", true, filepath.Join("src", "test1")},
{"ascending", true, filepath.Join("src", "test1", "sub")},
{"without lock", false, filepath.Join("src", "test2")},
{"ascending without lock", false, filepath.Join("src", "test2", "sub")},
}
tf := func(withGOPATH bool, tc tcase, t *testing.T) func(t *testing.T) {
return func(t *testing.T) {
ctx := &Ctx{
Out: discardLogger(),
Err: discardLogger(),
}
var err error
if withGOPATH {
err = ctx.SetPaths(h.Path(tc.wd), h.Path("."))
} else {
err = ctx.SetPaths(h.Path(tc.wd))
}
if err != nil {
t.Fatalf("%+v", err)
}
ctx.ExplicitRoot = "github.com/user/module"
p, err := ctx.LoadProject()
switch {
case err != nil:
t.Fatalf("%s: LoadProject failed: %+v", tc.wd, err)
case p.Manifest == nil:
t.Fatalf("%s: Manifest file didn't load", tc.wd)
case tc.lock && p.Lock == nil:
t.Fatalf("%s: Lock file didn't load", tc.wd)
case !tc.lock && p.Lock != nil:
t.Fatalf("%s: Non-existent Lock file loaded", tc.wd)
}
}
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
t.Run("within-GOPATH", tf(true, tc, t))
t.Run("outside-GOPATH", tf(false, tc, t))
})
}
}
func TestLoadProjectNotFoundErrors(t *testing.T) {
tg := test.NewHelper(t)
defer tg.Cleanup()
tg.TempDir("src")
tg.TempDir("src/test1")
tg.TempDir("src/test1/sub")
tg.Setenv("GOPATH", tg.Path("."))
var testcases = []struct {
lock bool
start string
path string
}{
{true, filepath.Join("src", "test1"), ""}, //direct
{true, filepath.Join("src", "test1", "sub"), ""}, //ascending
}
for _, testcase := range testcases {
ctx := &Ctx{GOPATHs: []string{tg.Path(".")}, WorkingDir: tg.Path(testcase.start)}
_, err := ctx.LoadProject()
if err == nil {
t.Errorf("%s: should have returned 'No Manifest Found' error", testcase.start)
}
}
}
func TestLoadProjectManifestParseError(t *testing.T) {
tg := test.NewHelper(t)
defer tg.Cleanup()
tg.TempDir("src")
tg.TempDir("src/test1")
tg.TempFile(filepath.Join("src/test1", ManifestName), `[[constraint]]`)
tg.TempFile(filepath.Join("src/test1", LockName), `memo = "cdafe8641b28cd16fe025df278b0a49b9416859345d8b6ba0ace0272b74925ee"\n\n[[projects]]`)
tg.Setenv("GOPATH", tg.Path("."))
path := filepath.Join("src", "test1")
tg.Cd(tg.Path(path))
wd, err := os.Getwd()
if err != nil {
t.Fatal("failed to get working directory", err)
}
ctx := &Ctx{
GOPATH: tg.Path("."),
WorkingDir: wd,
Out: discardLogger(),
Err: discardLogger(),
}
_, err = ctx.LoadProject()
if err == nil {
t.Fatal("should have returned 'Manifest Syntax' error")
}
}
func TestLoadProjectLockParseError(t *testing.T) {
tg := test.NewHelper(t)
defer tg.Cleanup()
tg.TempDir("src")
tg.TempDir("src/test1")
tg.TempFile(filepath.Join("src/test1", ManifestName), `[[constraint]]`)
tg.TempFile(filepath.Join("src/test1", LockName), `memo = "cdafe8641b28cd16fe025df278b0a49b9416859345d8b6ba0ace0272b74925ee"\n\n[[projects]]`)
tg.Setenv("GOPATH", tg.Path("."))
path := filepath.Join("src", "test1")
tg.Cd(tg.Path(path))
wd, err := os.Getwd()
if err != nil {
t.Fatal("failed to get working directory", err)
}
ctx := &Ctx{
GOPATH: tg.Path("."),
WorkingDir: wd,
Out: discardLogger(),
Err: discardLogger(),
}
_, err = ctx.LoadProject()
if err == nil {
t.Fatal("should have returned 'Lock Syntax' error")
}
}
func TestLoadProjectNoSrcDir(t *testing.T) {
tg := test.NewHelper(t)
defer tg.Cleanup()
tg.TempDir("test1")
tg.TempFile(filepath.Join("test1", ManifestName), `[[constraint]]`)
tg.TempFile(filepath.Join("test1", LockName), `memo = "cdafe8641b28cd16fe025df278b0a49b9416859345d8b6ba0ace0272b74925ee"\n\n[[projects]]`)
tg.Setenv("GOPATH", tg.Path("."))
ctx := &Ctx{GOPATH: tg.Path(".")}
path := filepath.Join("test1")
tg.Cd(tg.Path(path))
f, _ := os.OpenFile(filepath.Join(ctx.GOPATH, "src", "test1", LockName), os.O_WRONLY, os.ModePerm)
defer f.Close()
_, err := ctx.LoadProject()
if err == nil {
t.Fatal("should have returned 'Split Absolute Root' error (no 'src' dir present)")
}
}
func TestLoadProjectGopkgFilenames(t *testing.T) {
// We are trying to skip this test on file systems which are case-sensiive. We could
// have used `fs.IsCaseSensitiveFilesystem` for this check. However, the code we are
// testing also relies on `fs.IsCaseSensitiveFilesystem`. So a bug in
// `fs.IsCaseSensitiveFilesystem` could prevent this test from being run. This is the
// only scenario where we prefer the OS heuristic over doing the actual work of
// validating filesystem case sensitivity via `fs.IsCaseSensitiveFilesystem`.
if runtime.GOOS != "windows" && runtime.GOOS != "darwin" {
t.Skip("skip this test on non-Windows, non-macOS")
}
// Here we test that a manifest filename with incorrect case throws an error. Similar
// error will also be thrown for the lock file as well which has been tested in
// `project_test.go#TestCheckGopkgFilenames`. So not repeating here.
h := test.NewHelper(t)
defer h.Cleanup()
invalidMfName := strings.ToLower(ManifestName)
wd := filepath.Join("src", "test")
h.TempFile(filepath.Join(wd, invalidMfName), "")
ctx := &Ctx{
Out: discardLogger(),
Err: discardLogger(),
}
err := ctx.SetPaths(h.Path(wd), h.Path("."))
if err != nil {
t.Fatalf("%+v", err)
}
_, err = ctx.LoadProject()
if err == nil {
t.Fatal("should have returned 'Manifest Filename' error")
}
expectedErrMsg := fmt.Sprintf(
"manifest filename %q does not match %q",
invalidMfName, ManifestName,
)
if err.Error() != expectedErrMsg {
t.Fatalf("unexpected error: %+v", err)
}
}
// TestCaseInsensitive is test for Windows. This should work even though set
// difference letter cases in GOPATH.
func TestCaseInsensitiveGOPATH(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("skip this test on non-Windows")
}
h := test.NewHelper(t)
defer h.Cleanup()
h.TempDir("src")
h.TempDir("src/test1")
h.TempFile(filepath.Join("src/test1", ManifestName), `
[[constraint]]
name = "github.com/foo/bar"
branch = "master"`)
// Shuffle letter case
rs := []rune(strings.ToLower(h.Path(".")))
for i, r := range rs {
if unicode.IsLower(r) {
rs[i] = unicode.ToUpper(r)
} else {
rs[i] = unicode.ToLower(r)
}
}
gopath := string(rs)
h.Setenv("GOPATH", gopath)
wd := h.Path("src/test1")
depCtx := &Ctx{}
if err := depCtx.SetPaths(wd, gopath); err != nil {
t.Fatal(err)
}
if _, err := depCtx.LoadProject(); err != nil {
t.Fatal(err)
}
ip := "github.com/pkg/errors"
fullpath := filepath.Join(depCtx.GOPATH, "src", ip)
h.TempDir(filepath.Join("src", ip))
pr, err := depCtx.ImportForAbs(fullpath)
if err != nil {
t.Fatal(err)
}
if pr != ip {
t.Fatalf("expected %s, got %s", ip, pr)
}
}
func TestDetectProjectGOPATH(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()
h.TempDir(filepath.Join("sym", "symlink"))
h.TempDir(filepath.Join("go", "src", "sym", "path"))
h.TempDir(filepath.Join("go", "src", "real", "path"))
h.TempDir(filepath.Join("go-two", "src", "real", "path"))
h.TempDir(filepath.Join("go-two", "src", "sym"))
ctx := &Ctx{
GOPATHs: []string{h.Path("go"), h.Path("go-two")},
}
testcases := []struct {
name string
root string
resolvedRoot string
GOPATH string
expectErr bool
}{
{
name: "project-with-no-AbsRoot",
root: "",
resolvedRoot: filepath.Join(ctx.GOPATHs[0], "src", "real", "path"),
expectErr: true,
},
{
name: "project-with-no-ResolvedAbsRoot",
root: filepath.Join(ctx.GOPATHs[0], "src", "real", "path"),
resolvedRoot: "",
expectErr: true,
},
{
name: "AbsRoot-is-not-within-any-GOPATH",
root: filepath.Join(h.Path("."), "src", "real", "path"),
resolvedRoot: filepath.Join(h.Path("."), "src", "real", "path"),
expectErr: true,
},
{
name: "neither-AbsRoot-nor-ResolvedAbsRoot-are-in-any-GOPATH",
root: filepath.Join(h.Path("."), "src", "sym", "path"),
resolvedRoot: filepath.Join(h.Path("."), "src", "real", "path"),
expectErr: true,
},
{
name: "both-AbsRoot-and-ResolvedAbsRoot-are-in-the-same-GOPATH",
root: filepath.Join(ctx.GOPATHs[0], "src", "sym", "path"),
resolvedRoot: filepath.Join(ctx.GOPATHs[0], "src", "real", "path"),
expectErr: true,
},
{
name: "AbsRoot-and-ResolvedAbsRoot-are-each-within-a-different-GOPATH",
root: filepath.Join(ctx.GOPATHs[0], "src", "sym", "path"),
resolvedRoot: filepath.Join(ctx.GOPATHs[1], "src", "real", "path"),
expectErr: true,
},
{
name: "AbsRoot-is-not-a-symlink",
root: filepath.Join(ctx.GOPATHs[0], "src", "real", "path"),
resolvedRoot: filepath.Join(ctx.GOPATHs[0], "src", "real", "path"),
GOPATH: ctx.GOPATHs[0],
},
{
name: "AbsRoot-is-a-symlink-to-ResolvedAbsRoot",
root: filepath.Join(h.Path("."), "sym", "symlink"),
resolvedRoot: filepath.Join(ctx.GOPATHs[0], "src", "real", "path"),
GOPATH: ctx.GOPATHs[0],
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
project := &Project{
AbsRoot: tc.root,
ResolvedAbsRoot: tc.resolvedRoot,
}
GOPATH, err := ctx.DetectProjectGOPATH(project)
if !tc.expectErr && err != nil {
t.Fatalf("%+v", err)
} else if tc.expectErr && err == nil {
t.Fatalf("expected an error, got nil and gopath %s", GOPATH)
}
if GOPATH != tc.GOPATH {
t.Errorf("expected GOPATH %s, got %s", tc.GOPATH, GOPATH)
}
})
}
}
func TestDetectGOPATH(t *testing.T) {
th := test.NewHelper(t)
defer th.Cleanup()
th.TempDir(filepath.Join("code", "src", "github.com", "username", "package"))
th.TempDir(filepath.Join("go", "src", "github.com", "username", "package"))
th.TempDir(filepath.Join("gotwo", "src", "github.com", "username", "package"))
th.TempDir(filepath.Join("gothree", "sep", "src", "github.com", "username", "package"))
sep := string(os.PathSeparator)
ctx := &Ctx{GOPATHs: []string{
th.Path("go"),
th.Path("gotwo"),
th.Path("gothree") + sep + sep + "sep",
}}
testcases := []struct {
GOPATH string
path string
err bool
}{
{th.Path("go"), th.Path(filepath.Join("go", "src", "github.com", "username", "package")), false},
{th.Path("go"), th.Path(filepath.Join("go", "src", "github.com", "username", "package")), false},
{th.Path("gotwo"), th.Path(filepath.Join("gotwo", "src", "github.com", "username", "package")), false},
{th.Path(filepath.Join("gothree", "sep")),
th.Path(filepath.Join("gothree", "sep", "src", "github.com", "username", "package")), false},
{"", th.Path(filepath.Join("code", "src", "github.com", "username", "package")), true},
}
for _, tc := range testcases {
GOPATH, err := ctx.detectGOPATH(tc.path)
if tc.err && err == nil {
t.Error("expected error but got none")
}
if GOPATH != tc.GOPATH {
t.Errorf("expected GOPATH to be %s, got %s", tc.GOPATH, GOPATH)
}
}
}
func TestDepCachedir(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()
h.TempDir("cache")
// Create the directory for default cachedir location.
h.TempDir(filepath.Join("go", "pkg", "dep"))
testCachedir := h.Path("cache")
gopath := h.Path("go")
discardLgr := discardLogger()
cases := []struct {
cachedir string
wantCachedir string
}{
// If `Cachedir` is not set in the context, it should use `$GOPATH/pkg/dep`.
{cachedir: "", wantCachedir: h.Path(filepath.Join("go", "pkg", "dep"))},
// If `Cachedir` is set in the context, it should use that.
{cachedir: testCachedir, wantCachedir: testCachedir},
}
for _, c := range cases {
ctx := &Ctx{
GOPATH: gopath,
Cachedir: c.cachedir,
Out: discardLgr,
Err: discardLgr,
}
sm, err := ctx.SourceManager()
h.Must(err)
defer sm.Release()
if sm.Cachedir() != c.wantCachedir {
t.Errorf("expected cachedir to be %s, got %s", c.wantCachedir, sm.Cachedir())
}
}
}