Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit bba3159

Browse files
authored
Merge pull request #495 from ChrisHines/optional-windows-symlink-testing
Optional windows symlink testing
2 parents 0202c65 + 731ae1a commit bba3159

File tree

1 file changed

+57
-44
lines changed

1 file changed

+57
-44
lines changed

context_test.go

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -352,22 +352,15 @@ func TestResolveProjectRoot(t *testing.T) {
352352
tg := test.NewHelper(t)
353353
defer tg.Cleanup()
354354

355-
tg.TempDir("go")
356-
tg.TempDir("go/src")
357-
tg.TempDir("go/src/real")
358355
tg.TempDir("go/src/real/path")
359356
tg.TempDir("go/src/sym")
360357

361-
tg.TempDir("gotwo") // Another directory used as a GOPATH
362-
tg.TempDir("gotwo/src")
363-
tg.TempDir("gotwo/src/real")
358+
// Another directory used as a GOPATH
364359
tg.TempDir("gotwo/src/real/path")
365360
tg.TempDir("gotwo/src/sym")
366361

367362
tg.TempDir("sym") // Directory for symlinks
368363

369-
tg.Setenv("GOPATH", tg.Path(filepath.Join(".", "go")))
370-
371364
ctx := &Ctx{
372365
GOPATH: tg.Path(filepath.Join(".", "go")),
373366
GOPATHS: []string{
@@ -376,45 +369,65 @@ func TestResolveProjectRoot(t *testing.T) {
376369
},
377370
}
378371

379-
realPath := filepath.Join(ctx.GOPATH, "src", "real", "path")
380-
realPathTwo := filepath.Join(ctx.GOPATHS[1], "src", "real", "path")
381-
symlinkedPath := filepath.Join(tg.Path("."), "sym", "symlink")
382-
symlinkedInGoPath := filepath.Join(ctx.GOPATH, "src/sym/path")
383-
symlinkedInOtherGoPath := filepath.Join(tg.Path("."), "sym", "symtwo")
384-
os.Symlink(realPath, symlinkedPath)
385-
os.Symlink(realPath, symlinkedInGoPath)
386-
os.Symlink(realPathTwo, symlinkedInOtherGoPath)
387-
388-
// Real path should be returned, no symlinks to deal with
389-
p, err := ctx.resolveProjectRoot(realPath)
390-
if err != nil {
391-
t.Fatalf("Error resolving project root: %s", err)
392-
}
393-
if p != realPath {
394-
t.Fatalf("Want path to be %s, got %s", realPath, p)
372+
testcases := []struct {
373+
name string
374+
path string
375+
resolvedPath string
376+
symlink bool
377+
expectErr bool
378+
}{
379+
{
380+
name: "no-symlinks",
381+
path: filepath.Join(ctx.GOPATH, "src/real/path"),
382+
resolvedPath: filepath.Join(ctx.GOPATH, "src/real/path"),
383+
},
384+
{
385+
name: "symlink-outside-gopath",
386+
path: filepath.Join(tg.Path("."), "sym/symlink"),
387+
resolvedPath: filepath.Join(ctx.GOPATH, "src/real/path"),
388+
symlink: true,
389+
},
390+
{
391+
name: "symlink-in-another-gopath",
392+
path: filepath.Join(tg.Path("."), "sym/symtwo"),
393+
resolvedPath: filepath.Join(ctx.GOPATHS[1], "src/real/path"),
394+
symlink: true,
395+
},
396+
{
397+
name: "symlink-in-gopath",
398+
path: filepath.Join(ctx.GOPATH, "src/sym/path"),
399+
resolvedPath: filepath.Join(ctx.GOPATH, "src/real/path"),
400+
symlink: true,
401+
expectErr: true,
402+
},
395403
}
396404

397-
// Real path should be returned, symlink is outside GOPATH
398-
p, err = ctx.resolveProjectRoot(symlinkedPath)
399-
if err != nil {
400-
t.Fatalf("Error resolving project root: %s", err)
401-
}
402-
if p != realPath {
403-
t.Fatalf("Want path to be %s, got %s", realPath, p)
404-
}
405+
for _, tc := range testcases {
406+
t.Run(tc.name, func(t *testing.T) {
407+
if tc.symlink {
408+
if err := os.Symlink(tc.resolvedPath, tc.path); err != nil {
409+
if runtime.GOOS == "windows" {
410+
t.Skipf("Not testing Windows symlinks because: %s", err)
411+
} else {
412+
t.Fatal(err)
413+
}
414+
}
415+
}
405416

406-
// Real path should be returned, symlink is in another GOPATH
407-
p, err = ctx.resolveProjectRoot(symlinkedInOtherGoPath)
408-
if err != nil {
409-
t.Fatalf("Error resolving project root: %s", err)
410-
}
411-
if p != realPathTwo {
412-
t.Fatalf("Want path to be %s, got %s", realPathTwo, p)
413-
}
417+
p, err := ctx.resolveProjectRoot(tc.path)
418+
if err != nil {
419+
if !tc.expectErr {
420+
t.Fatalf("Error resolving project root: %s", err)
421+
}
422+
return
423+
}
424+
if err == nil && tc.expectErr {
425+
t.Fatal("Wanted an error")
426+
}
414427

415-
// Symlinked path is inside GOPATH, should return error
416-
_, err = ctx.resolveProjectRoot(symlinkedInGoPath)
417-
if err == nil {
418-
t.Fatalf("Wanted an error")
428+
if p != tc.resolvedPath {
429+
t.Errorf("Want path to be %s, got %s", tc.resolvedPath, p)
430+
}
431+
})
419432
}
420433
}

0 commit comments

Comments
 (0)