2017-01-18 01:07:50 +03:00
|
|
|
// 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.
|
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
package dep
|
2016-12-13 02:12:45 +03:00
|
|
|
|
|
|
|
import (
|
2017-09-02 11:21:20 +03:00
|
|
|
"fmt"
|
2017-05-10 07:57:14 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2017-01-31 07:44:03 +03:00
|
|
|
"os"
|
2016-12-13 02:12:45 +03:00
|
|
|
"path/filepath"
|
2017-02-13 05:45:20 +03:00
|
|
|
"runtime"
|
2017-02-21 21:22:38 +03:00
|
|
|
"strings"
|
2016-12-13 02:12:45 +03:00
|
|
|
"testing"
|
2017-02-13 05:45:20 +03:00
|
|
|
"unicode"
|
2016-12-13 02:12:45 +03:00
|
|
|
|
2017-05-10 07:24:12 +03:00
|
|
|
"github.com/golang/dep/internal/test"
|
2016-12-13 02:12:45 +03:00
|
|
|
)
|
|
|
|
|
2017-09-13 16:44:48 +03:00
|
|
|
func discardLogger() *log.Logger {
|
|
|
|
return log.New(ioutil.Discard, "", 0)
|
|
|
|
}
|
2017-05-10 07:57:14 +03:00
|
|
|
|
2017-05-31 04:47:18 +03:00
|
|
|
func TestCtx_ProjectImport(t *testing.T) {
|
2017-01-28 14:24:33 +03:00
|
|
|
h := test.NewHelper(t)
|
|
|
|
defer h.Cleanup()
|
2016-12-13 02:12:45 +03:00
|
|
|
|
2017-01-28 14:24:33 +03:00
|
|
|
h.TempDir("src")
|
2017-02-16 05:25:20 +03:00
|
|
|
|
2017-01-28 14:24:33 +03:00
|
|
|
h.Setenv("GOPATH", h.Path("."))
|
|
|
|
depCtx := &Ctx{GOPATH: h.Path(".")}
|
2016-12-13 02:12:45 +03:00
|
|
|
|
|
|
|
importPaths := []string{
|
|
|
|
"github.com/pkg/errors",
|
|
|
|
"my/silly/thing",
|
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
for _, want := range importPaths {
|
|
|
|
fullpath := filepath.Join(depCtx.GOPATH, "src", want)
|
2017-06-18 16:16:02 +03:00
|
|
|
h.TempDir(filepath.Join("src", want))
|
2017-05-31 04:47:18 +03:00
|
|
|
got, err := depCtx.ImportForAbs(fullpath)
|
2016-12-13 02:12:45 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-10 21:08:47 +03:00
|
|
|
if got != want {
|
|
|
|
t.Fatalf("expected %s, got %s", want, got)
|
2016-12-13 02:12:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 00:35:18 +03:00
|
|
|
// test where it should return an error when directly within $GOPATH/src
|
2017-05-31 04:47:18 +03:00
|
|
|
got, err := depCtx.ImportForAbs(filepath.Join(depCtx.GOPATH, "src"))
|
2017-06-15 01:30:25 +03:00
|
|
|
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)
|
2017-05-09 00:35:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// test where it should return an error
|
2017-05-31 04:47:18 +03:00
|
|
|
got, err = depCtx.ImportForAbs("tra/la/la/la")
|
2016-12-13 02:12:45 +03:00
|
|
|
if err == nil {
|
2017-05-09 00:35:18 +03:00
|
|
|
t.Fatalf("should have gotten an error but did not for tra/la/la/la: %s", got)
|
2016-12-13 02:12:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAbsoluteProjectRoot(t *testing.T) {
|
2017-01-28 14:24:33 +03:00
|
|
|
h := test.NewHelper(t)
|
|
|
|
defer h.Cleanup()
|
2016-12-13 02:12:45 +03:00
|
|
|
|
2017-01-28 14:24:33 +03:00
|
|
|
h.TempDir("src")
|
|
|
|
h.Setenv("GOPATH", h.Path("."))
|
|
|
|
depCtx := &Ctx{GOPATH: h.Path(".")}
|
2016-12-13 02:12:45 +03:00
|
|
|
|
|
|
|
importPaths := map[string]bool{
|
|
|
|
"github.com/pkg/errors": true,
|
|
|
|
"my/silly/thing": false,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, create := range importPaths {
|
|
|
|
if create {
|
2017-01-28 14:24:33 +03:00
|
|
|
h.TempDir(filepath.Join("src", i))
|
2016-12-13 02:12:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, ok := range importPaths {
|
2017-05-31 18:52:27 +03:00
|
|
|
got, err := depCtx.AbsForImport(i)
|
2016-12-13 02:12:45 +03:00
|
|
|
if ok {
|
2017-01-28 14:24:33 +03:00
|
|
|
h.Must(err)
|
2017-02-10 21:08:47 +03:00
|
|
|
want := h.Path(filepath.Join("src", i))
|
|
|
|
if got != want {
|
|
|
|
t.Fatalf("expected %s, got %q", want, got)
|
2016-12-13 02:12:45 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected %s to fail", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test that a file fails
|
2017-01-28 14:24:33 +03:00
|
|
|
h.TempFile("src/thing/thing.go", "hello world")
|
2017-05-31 18:52:27 +03:00
|
|
|
_, err := depCtx.AbsForImport("thing/thing.go")
|
2016-12-13 02:12:45 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("error should not be nil for a file found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-31 07:44:03 +03:00
|
|
|
func TestLoadProject(t *testing.T) {
|
2017-06-17 04:16:00 +03:00
|
|
|
h := test.NewHelper(t)
|
|
|
|
defer h.Cleanup()
|
2017-01-31 07:44:03 +03:00
|
|
|
|
2017-06-17 04:16:00 +03:00
|
|
|
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), "")
|
2017-01-31 07:44:03 +03:00
|
|
|
|
2017-01-31 21:53:31 +03:00
|
|
|
var testcases = []struct {
|
2017-06-17 04:16:00 +03:00
|
|
|
name string
|
|
|
|
lock bool
|
|
|
|
wd string
|
2017-01-31 07:44:03 +03:00
|
|
|
}{
|
2017-06-17 04:16:00 +03:00
|
|
|
{"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")},
|
2017-01-31 07:44:03 +03:00
|
|
|
}
|
|
|
|
|
2017-06-17 04:16:00 +03:00
|
|
|
for _, tc := range testcases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
ctx := &Ctx{
|
2017-09-13 16:44:48 +03:00
|
|
|
Out: discardLogger(),
|
|
|
|
Err: discardLogger(),
|
2017-06-17 04:16:00 +03:00
|
|
|
}
|
2017-05-06 02:03:46 +03:00
|
|
|
|
2017-06-17 04:16:00 +03:00
|
|
|
err := ctx.SetPaths(h.Path(tc.wd), h.Path("."))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
2017-05-06 02:03:46 +03:00
|
|
|
|
2017-06-17 04:16:00 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2017-01-31 07:44:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadProjectNotFoundErrors(t *testing.T) {
|
2017-01-31 23:28:39 +03:00
|
|
|
tg := test.NewHelper(t)
|
2017-01-31 07:44:03 +03:00
|
|
|
defer tg.Cleanup()
|
|
|
|
|
|
|
|
tg.TempDir("src")
|
|
|
|
tg.TempDir("src/test1")
|
|
|
|
tg.TempDir("src/test1/sub")
|
|
|
|
tg.Setenv("GOPATH", tg.Path("."))
|
|
|
|
|
2017-01-31 21:53:31 +03:00
|
|
|
var testcases = []struct {
|
|
|
|
lock bool
|
|
|
|
start string
|
|
|
|
path string
|
2017-01-31 07:44:03 +03:00
|
|
|
}{
|
2017-05-30 17:07:23 +03:00
|
|
|
{true, filepath.Join("src", "test1"), ""}, //direct
|
|
|
|
{true, filepath.Join("src", "test1", "sub"), ""}, //ascending
|
2017-01-31 07:44:03 +03:00
|
|
|
}
|
|
|
|
|
2017-01-31 21:53:31 +03:00
|
|
|
for _, testcase := range testcases {
|
2017-06-12 22:21:28 +03:00
|
|
|
ctx := &Ctx{GOPATHs: []string{tg.Path(".")}, WorkingDir: tg.Path(testcase.start)}
|
2017-05-06 02:03:46 +03:00
|
|
|
|
2017-05-30 17:07:23 +03:00
|
|
|
_, err := ctx.LoadProject()
|
2017-01-31 07:44:03 +03:00
|
|
|
if err == nil {
|
2017-05-30 17:07:23 +03:00
|
|
|
t.Errorf("%s: should have returned 'No Manifest Found' error", testcase.start)
|
2017-01-31 07:44:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadProjectManifestParseError(t *testing.T) {
|
2017-01-31 23:28:39 +03:00
|
|
|
tg := test.NewHelper(t)
|
2017-01-31 07:44:03 +03:00
|
|
|
defer tg.Cleanup()
|
|
|
|
|
|
|
|
tg.TempDir("src")
|
|
|
|
tg.TempDir("src/test1")
|
2017-05-10 02:13:26 +03:00
|
|
|
tg.TempFile(filepath.Join("src/test1", ManifestName), `[[constraint]]`)
|
2017-04-02 22:36:22 +03:00
|
|
|
tg.TempFile(filepath.Join("src/test1", LockName), `memo = "cdafe8641b28cd16fe025df278b0a49b9416859345d8b6ba0ace0272b74925ee"\n\n[[projects]]`)
|
2017-01-31 07:44:03 +03:00
|
|
|
tg.Setenv("GOPATH", tg.Path("."))
|
|
|
|
|
|
|
|
path := filepath.Join("src", "test1")
|
|
|
|
tg.Cd(tg.Path(path))
|
|
|
|
|
2017-05-06 02:03:46 +03:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("failed to get working directory", err)
|
|
|
|
}
|
|
|
|
|
2017-05-28 19:49:26 +03:00
|
|
|
ctx := &Ctx{
|
|
|
|
GOPATH: tg.Path("."),
|
|
|
|
WorkingDir: wd,
|
2017-09-13 16:44:48 +03:00
|
|
|
Out: discardLogger(),
|
|
|
|
Err: discardLogger(),
|
2017-05-28 19:49:26 +03:00
|
|
|
}
|
2017-05-06 02:03:46 +03:00
|
|
|
|
2017-05-30 17:07:23 +03:00
|
|
|
_, err = ctx.LoadProject()
|
2017-01-31 07:44:03 +03:00
|
|
|
if err == nil {
|
2017-05-06 02:03:46 +03:00
|
|
|
t.Fatal("should have returned 'Manifest Syntax' error")
|
2017-01-31 07:44:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadProjectLockParseError(t *testing.T) {
|
2017-01-31 23:28:39 +03:00
|
|
|
tg := test.NewHelper(t)
|
2017-01-31 07:44:03 +03:00
|
|
|
defer tg.Cleanup()
|
|
|
|
|
|
|
|
tg.TempDir("src")
|
|
|
|
tg.TempDir("src/test1")
|
2017-05-10 02:13:26 +03:00
|
|
|
tg.TempFile(filepath.Join("src/test1", ManifestName), `[[constraint]]`)
|
2017-04-02 22:36:22 +03:00
|
|
|
tg.TempFile(filepath.Join("src/test1", LockName), `memo = "cdafe8641b28cd16fe025df278b0a49b9416859345d8b6ba0ace0272b74925ee"\n\n[[projects]]`)
|
2017-01-31 07:44:03 +03:00
|
|
|
tg.Setenv("GOPATH", tg.Path("."))
|
|
|
|
|
|
|
|
path := filepath.Join("src", "test1")
|
|
|
|
tg.Cd(tg.Path(path))
|
|
|
|
|
2017-05-06 02:03:46 +03:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("failed to get working directory", err)
|
|
|
|
}
|
|
|
|
|
2017-05-28 19:49:26 +03:00
|
|
|
ctx := &Ctx{
|
|
|
|
GOPATH: tg.Path("."),
|
|
|
|
WorkingDir: wd,
|
2017-09-13 16:44:48 +03:00
|
|
|
Out: discardLogger(),
|
|
|
|
Err: discardLogger(),
|
2017-05-28 19:49:26 +03:00
|
|
|
}
|
2017-05-06 02:03:46 +03:00
|
|
|
|
2017-05-30 17:07:23 +03:00
|
|
|
_, err = ctx.LoadProject()
|
2017-01-31 07:44:03 +03:00
|
|
|
if err == nil {
|
2017-05-06 02:03:46 +03:00
|
|
|
t.Fatal("should have returned 'Lock Syntax' error")
|
2017-01-31 07:44:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadProjectNoSrcDir(t *testing.T) {
|
2017-01-31 23:28:39 +03:00
|
|
|
tg := test.NewHelper(t)
|
2017-01-31 07:44:03 +03:00
|
|
|
defer tg.Cleanup()
|
|
|
|
|
|
|
|
tg.TempDir("test1")
|
2017-05-10 02:13:26 +03:00
|
|
|
tg.TempFile(filepath.Join("test1", ManifestName), `[[constraint]]`)
|
2017-04-02 22:36:22 +03:00
|
|
|
tg.TempFile(filepath.Join("test1", LockName), `memo = "cdafe8641b28cd16fe025df278b0a49b9416859345d8b6ba0ace0272b74925ee"\n\n[[projects]]`)
|
2017-01-31 07:44:03 +03:00
|
|
|
tg.Setenv("GOPATH", tg.Path("."))
|
|
|
|
|
|
|
|
ctx := &Ctx{GOPATH: tg.Path(".")}
|
|
|
|
path := filepath.Join("test1")
|
|
|
|
tg.Cd(tg.Path(path))
|
|
|
|
|
2017-04-02 22:36:22 +03:00
|
|
|
f, _ := os.OpenFile(filepath.Join(ctx.GOPATH, "src", "test1", LockName), os.O_WRONLY, os.ModePerm)
|
2017-01-31 07:44:03 +03:00
|
|
|
defer f.Close()
|
|
|
|
|
2017-05-30 17:07:23 +03:00
|
|
|
_, err := ctx.LoadProject()
|
2017-01-31 07:44:03 +03:00
|
|
|
if err == nil {
|
2017-05-06 02:03:46 +03:00
|
|
|
t.Fatal("should have returned 'Split Absolute Root' error (no 'src' dir present)")
|
2017-01-31 07:44:03 +03:00
|
|
|
}
|
|
|
|
}
|
2017-02-13 05:45:20 +03:00
|
|
|
|
2017-09-20 09:00:34 +03:00
|
|
|
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`.
|
2017-09-02 11:21:20 +03:00
|
|
|
if runtime.GOOS != "windows" && runtime.GOOS != "darwin" {
|
|
|
|
t.Skip("skip this test on non-Windows, non-macOS")
|
|
|
|
}
|
|
|
|
|
2017-09-05 07:38:06 +03:00
|
|
|
// 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
|
2017-09-20 09:00:34 +03:00
|
|
|
// `project_test.go#TestCheckGopkgFilenames`. So not repeating here.
|
2017-09-02 11:21:20 +03:00
|
|
|
|
|
|
|
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(
|
2017-09-05 07:38:06 +03:00
|
|
|
"manifest filename %q does not match %q",
|
2017-09-02 11:21:20 +03:00
|
|
|
invalidMfName, ManifestName,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err.Error() != expectedErrMsg {
|
|
|
|
t.Fatalf("unexpected error: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 05:45:20 +03:00
|
|
|
// TestCaseInsentitive is test for Windows. This should work even though set
|
|
|
|
// difference letter cases in GOPATH.
|
|
|
|
func TestCaseInsentitiveGOPATH(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")
|
2017-05-10 02:13:26 +03:00
|
|
|
h.TempFile(filepath.Join("src/test1", ManifestName), `[[constraint]]`)
|
2017-02-13 05:45:20 +03:00
|
|
|
|
|
|
|
// 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)
|
2017-06-15 03:58:59 +03:00
|
|
|
wd := h.Path("src/test1")
|
2017-02-13 05:45:20 +03:00
|
|
|
|
2017-06-15 03:58:59 +03:00
|
|
|
depCtx := &Ctx{}
|
|
|
|
if err := depCtx.SetPaths(wd, gopath); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := depCtx.LoadProject(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-13 05:45:20 +03:00
|
|
|
|
|
|
|
ip := "github.com/pkg/errors"
|
|
|
|
fullpath := filepath.Join(depCtx.GOPATH, "src", ip)
|
2017-06-18 16:16:02 +03:00
|
|
|
h.TempDir(filepath.Join("src", ip))
|
2017-05-31 04:47:18 +03:00
|
|
|
pr, err := depCtx.ImportForAbs(fullpath)
|
2017-02-13 05:45:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if pr != ip {
|
|
|
|
t.Fatalf("expected %s, got %s", ip, pr)
|
|
|
|
}
|
|
|
|
}
|
2017-03-12 02:41:53 +03:00
|
|
|
|
2017-06-15 01:34:20 +03:00
|
|
|
func TestDetectProjectGOPATH(t *testing.T) {
|
2017-06-17 04:00:51 +03:00
|
|
|
h := test.NewHelper(t)
|
|
|
|
defer h.Cleanup()
|
2017-04-11 00:58:07 +03:00
|
|
|
|
2017-09-02 17:42:09 +03:00
|
|
|
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"))
|
2017-04-11 00:58:07 +03:00
|
|
|
|
2017-04-07 00:10:53 +03:00
|
|
|
ctx := &Ctx{
|
2017-06-17 04:00:51 +03:00
|
|
|
GOPATHs: []string{h.Path("go"), h.Path("go-two")},
|
2017-04-07 00:10:53 +03:00
|
|
|
}
|
2017-03-12 02:41:53 +03:00
|
|
|
|
2017-05-01 07:32:17 +03:00
|
|
|
testcases := []struct {
|
|
|
|
name string
|
2017-06-17 04:00:51 +03:00
|
|
|
root string
|
|
|
|
resolvedRoot string
|
2017-06-15 01:34:20 +03:00
|
|
|
GOPATH string
|
2017-05-01 07:32:17 +03:00
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
2017-06-17 04:00:51 +03:00
|
|
|
name: "project-with-no-AbsRoot",
|
|
|
|
root: "",
|
|
|
|
resolvedRoot: filepath.Join(ctx.GOPATHs[0], "src", "real", "path"),
|
|
|
|
expectErr: true,
|
2017-05-01 07:32:17 +03:00
|
|
|
},
|
|
|
|
{
|
2017-06-17 04:00:51 +03:00
|
|
|
name: "project-with-no-ResolvedAbsRoot",
|
|
|
|
root: filepath.Join(ctx.GOPATHs[0], "src", "real", "path"),
|
|
|
|
resolvedRoot: "",
|
|
|
|
expectErr: true,
|
2017-05-01 07:32:17 +03:00
|
|
|
},
|
|
|
|
{
|
2017-06-17 04:00:51 +03:00
|
|
|
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,
|
2017-05-01 07:32:17 +03:00
|
|
|
},
|
|
|
|
{
|
2017-06-17 04:00:51 +03:00
|
|
|
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"),
|
2017-05-01 07:32:17 +03:00
|
|
|
expectErr: true,
|
|
|
|
},
|
2017-06-17 04:00:51 +03:00
|
|
|
{
|
|
|
|
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"),
|
2017-09-02 17:42:09 +03:00
|
|
|
resolvedRoot: filepath.Join(ctx.GOPATHs[0], "src", "real", "path"),
|
2017-06-17 04:00:51 +03:00
|
|
|
GOPATH: ctx.GOPATHs[0],
|
|
|
|
},
|
2017-03-12 02:41:53 +03:00
|
|
|
}
|
|
|
|
|
2017-05-01 07:32:17 +03:00
|
|
|
for _, tc := range testcases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2017-06-15 01:34:20 +03:00
|
|
|
project := &Project{
|
2017-06-17 04:00:51 +03:00
|
|
|
AbsRoot: tc.root,
|
|
|
|
ResolvedAbsRoot: tc.resolvedRoot,
|
2017-05-01 07:32:17 +03:00
|
|
|
}
|
2017-03-12 02:41:53 +03:00
|
|
|
|
2017-06-15 01:34:20 +03:00
|
|
|
GOPATH, err := ctx.DetectProjectGOPATH(project)
|
2017-06-17 04:16:00 +03:00
|
|
|
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)
|
2017-05-01 07:32:17 +03:00
|
|
|
}
|
2017-06-15 01:34:20 +03:00
|
|
|
if GOPATH != tc.GOPATH {
|
2017-06-17 04:00:51 +03:00
|
|
|
t.Errorf("expected GOPATH %s, got %s", tc.GOPATH, GOPATH)
|
2017-05-01 07:32:17 +03:00
|
|
|
}
|
|
|
|
})
|
2017-03-12 02:41:53 +03:00
|
|
|
}
|
|
|
|
}
|
2017-06-03 22:15:21 +03:00
|
|
|
|
2017-06-15 01:34:20 +03:00
|
|
|
func TestDetectGOPATH(t *testing.T) {
|
2017-06-03 22:15:21 +03:00
|
|
|
th := test.NewHelper(t)
|
|
|
|
defer th.Cleanup()
|
|
|
|
|
2017-09-02 17:42:09 +03:00
|
|
|
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"))
|
2017-06-03 22:15:21 +03:00
|
|
|
|
2017-06-12 22:21:28 +03:00
|
|
|
ctx := &Ctx{GOPATHs: []string{
|
2017-06-03 22:15:21 +03:00
|
|
|
th.Path("go"),
|
|
|
|
th.Path("gotwo"),
|
|
|
|
}}
|
|
|
|
|
|
|
|
testcases := []struct {
|
2017-06-15 01:34:20 +03:00
|
|
|
GOPATH string
|
2017-06-03 22:15:21 +03:00
|
|
|
path string
|
|
|
|
err bool
|
|
|
|
}{
|
2017-09-02 17:42:09 +03:00
|
|
|
{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("code", "src", "github.com", "username", "package")), true},
|
2017-06-03 22:15:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testcases {
|
2017-06-15 01:34:20 +03:00
|
|
|
GOPATH, err := ctx.detectGOPATH(tc.path)
|
2017-06-03 22:15:21 +03:00
|
|
|
if tc.err && err == nil {
|
2017-09-02 17:42:09 +03:00
|
|
|
t.Error("expected error but got none")
|
2017-06-03 22:15:21 +03:00
|
|
|
}
|
2017-06-15 01:34:20 +03:00
|
|
|
if GOPATH != tc.GOPATH {
|
2017-09-02 17:42:09 +03:00
|
|
|
t.Errorf("expected GOPATH to be %s, got %s", GOPATH, tc.GOPATH)
|
2017-06-03 22:15:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|