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.
|
|
|
|
|
2016-12-13 02:12:45 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/sdboyer/gps"
|
|
|
|
)
|
|
|
|
|
2016-12-17 03:45:16 +03:00
|
|
|
func TestNewContextNoGOPATH(t *testing.T) {
|
|
|
|
tg := testgo(t)
|
|
|
|
defer tg.cleanup()
|
|
|
|
|
|
|
|
tg.tempDir("src")
|
|
|
|
tg.cd(tg.path("."))
|
|
|
|
|
|
|
|
c, err := newContext()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("error should not have been nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c != nil {
|
|
|
|
t.Fatalf("expected context to be nil, got: %#v", c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 02:12:45 +03:00
|
|
|
func TestSplitAbsoluteProjectRoot(t *testing.T) {
|
|
|
|
tg := testgo(t)
|
|
|
|
defer tg.cleanup()
|
|
|
|
|
|
|
|
tg.tempDir("src")
|
|
|
|
tg.setenv("GOPATH", tg.path("."))
|
2017-01-18 03:20:28 +03:00
|
|
|
nestCtx := &ctx{GOPATH: tg.path(".")}
|
2016-12-13 02:12:45 +03:00
|
|
|
|
|
|
|
importPaths := []string{
|
|
|
|
"github.com/pkg/errors",
|
|
|
|
"my/silly/thing",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ip := range importPaths {
|
2017-01-18 03:20:28 +03:00
|
|
|
fullpath := filepath.Join(nestCtx.GOPATH, "src", ip)
|
|
|
|
pr, err := nestCtx.splitAbsoluteProjectRoot(fullpath)
|
2016-12-13 02:12:45 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if pr != ip {
|
|
|
|
t.Fatalf("expected %s, got %s", ip, pr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test where it should return error
|
2017-01-18 03:20:28 +03:00
|
|
|
pr, err := nestCtx.splitAbsoluteProjectRoot("tra/la/la/la")
|
2016-12-13 02:12:45 +03:00
|
|
|
if err == nil {
|
2016-12-19 22:50:52 +03:00
|
|
|
t.Fatalf("should have gotten error but did not for tra/la/la/la: %s", pr)
|
2016-12-13 02:12:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAbsoluteProjectRoot(t *testing.T) {
|
|
|
|
tg := testgo(t)
|
|
|
|
defer tg.cleanup()
|
|
|
|
|
|
|
|
tg.tempDir("src")
|
|
|
|
tg.setenv("GOPATH", tg.path("."))
|
2017-01-18 03:20:28 +03:00
|
|
|
nestCtx := &ctx{GOPATH: tg.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 {
|
|
|
|
tg.tempDir(filepath.Join("src", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, ok := range importPaths {
|
2017-01-18 03:20:28 +03:00
|
|
|
pr, err := nestCtx.absoluteProjectRoot(i)
|
2016-12-13 02:12:45 +03:00
|
|
|
if ok {
|
|
|
|
tg.must(err)
|
|
|
|
expected := tg.path(filepath.Join("src", i))
|
|
|
|
if pr != expected {
|
|
|
|
t.Fatalf("expected %s, got %q", expected, pr)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected %s to fail", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test that a file fails
|
|
|
|
tg.tempFile("src/thing/thing.go", "hello world")
|
2017-01-18 03:20:28 +03:00
|
|
|
_, err := nestCtx.absoluteProjectRoot("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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersionInWorkspace(t *testing.T) {
|
|
|
|
needsExternalNetwork(t)
|
|
|
|
needsGit(t)
|
|
|
|
|
|
|
|
tg := testgo(t)
|
|
|
|
defer tg.cleanup()
|
|
|
|
|
|
|
|
tg.tempDir("src")
|
|
|
|
tg.setenv("GOPATH", tg.path("."))
|
2017-01-18 03:20:28 +03:00
|
|
|
nestCtx := &ctx{GOPATH: tg.path(".")}
|
2016-12-13 02:12:45 +03:00
|
|
|
|
|
|
|
importPaths := map[string]struct {
|
|
|
|
rev gps.Version
|
|
|
|
checkout bool
|
|
|
|
}{
|
|
|
|
"github.com/pkg/errors": {
|
|
|
|
rev: gps.NewVersion("v0.8.0").Is("645ef00459ed84a119197bfb8d8205042c6df63d"), // semver
|
|
|
|
checkout: true,
|
|
|
|
},
|
|
|
|
"github.com/Sirupsen/logrus": {
|
|
|
|
rev: gps.Revision("42b84f9ec624953ecbf81a94feccb3f5935c5edf"), // random sha
|
|
|
|
checkout: true,
|
|
|
|
},
|
|
|
|
"github.com/rsc/go-get-default-branch": {
|
|
|
|
rev: gps.NewBranch("another-branch").Is("8e6902fdd0361e8fa30226b350e62973e3625ed5"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkout the specified revisions
|
|
|
|
for ip, info := range importPaths {
|
|
|
|
tg.runGo("get", ip)
|
|
|
|
repoDir := tg.path("src/" + ip)
|
|
|
|
if info.checkout {
|
|
|
|
tg.runGit(repoDir, "checkout", info.rev.String())
|
|
|
|
}
|
|
|
|
|
2017-01-18 03:20:28 +03:00
|
|
|
v, err := nestCtx.versionInWorkspace(gps.ProjectRoot(ip))
|
2016-12-13 02:12:45 +03:00
|
|
|
tg.must(err)
|
|
|
|
|
|
|
|
if v != info.rev {
|
|
|
|
t.Fatalf("expected %q, got %q", v.String(), info.rev.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|