зеркало из https://github.com/golang/dep.git
add initial init test
Signed-off-by: Jess Frazelle <acidburn@google.com>
This commit is contained in:
Родитель
2a0d87b21d
Коммит
68ac070971
126
dep_test.go
126
dep_test.go
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/format"
|
"go/format"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
@ -28,6 +27,7 @@ func init() {
|
||||||
|
|
||||||
// The TestMain function creates a dep command for testing purposes and
|
// The TestMain function creates a dep command for testing purposes and
|
||||||
// deletes it after the tests have been run.
|
// deletes it after the tests have been run.
|
||||||
|
// Most of this is taken from https://github.com/golang/go/blob/master/src/cmd/go/go_test.go and reused here.
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
args := []string{"build", "-o", "testdep" + exeSuffix}
|
args := []string{"build", "-o", "testdep" + exeSuffix}
|
||||||
out, err := exec.Command("go", args...).CombinedOutput()
|
out, err := exec.Command("go", args...).CombinedOutput()
|
||||||
|
@ -164,12 +164,12 @@ func (tg *testgoData) doRun(args []string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tg.t.Logf("running testgo %v", args)
|
tg.t.Logf("running testdep %v", args)
|
||||||
var prog string
|
var prog string
|
||||||
if tg.wd == "" {
|
if tg.wd == "" {
|
||||||
prog = "./testgo" + exeSuffix
|
prog = "./testdep" + exeSuffix
|
||||||
} else {
|
} else {
|
||||||
prog = filepath.Join(tg.wd, "testgo"+exeSuffix)
|
prog = filepath.Join(tg.wd, "testdep"+exeSuffix)
|
||||||
}
|
}
|
||||||
cmd := exec.Command(prog, args...)
|
cmd := exec.Command(prog, args...)
|
||||||
tg.stdout.Reset()
|
tg.stdout.Reset()
|
||||||
|
@ -207,6 +207,30 @@ func (tg *testgoData) runFail(args ...string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// runGo runs a go command, and expects it to succeed.
|
||||||
|
func (tg *testgoData) runGo(args ...string) {
|
||||||
|
cmd := exec.Command("go", args...)
|
||||||
|
tg.stdout.Reset()
|
||||||
|
tg.stderr.Reset()
|
||||||
|
cmd.Stdout = &tg.stdout
|
||||||
|
cmd.Stderr = &tg.stderr
|
||||||
|
cmd.Dir = tg.wd
|
||||||
|
cmd.Env = tg.env
|
||||||
|
status := cmd.Run()
|
||||||
|
if tg.stdout.Len() > 0 {
|
||||||
|
tg.t.Log("go standard output:")
|
||||||
|
tg.t.Log(tg.stdout.String())
|
||||||
|
}
|
||||||
|
if tg.stderr.Len() > 0 {
|
||||||
|
tg.t.Log("go standard error:")
|
||||||
|
tg.t.Log(tg.stderr.String())
|
||||||
|
}
|
||||||
|
if status != nil {
|
||||||
|
tg.t.Logf("go %v failed unexpectedly: %v", args, status)
|
||||||
|
tg.t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// runGit runs a git command, and expects it to succeed.
|
// runGit runs a git command, and expects it to succeed.
|
||||||
func (tg *testgoData) runGit(dir string, args ...string) {
|
func (tg *testgoData) runGit(dir string, args ...string) {
|
||||||
cmd := exec.Command("git", args...)
|
cmd := exec.Command("git", args...)
|
||||||
|
@ -429,73 +453,6 @@ func (tg *testgoData) mustNotExist(path string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// wantExecutable fails with msg if path is not executable.
|
|
||||||
func (tg *testgoData) wantExecutable(path, msg string) {
|
|
||||||
if st, err := os.Stat(path); err != nil {
|
|
||||||
if !os.IsNotExist(err) {
|
|
||||||
tg.t.Log(err)
|
|
||||||
}
|
|
||||||
tg.t.Fatal(msg)
|
|
||||||
} else {
|
|
||||||
if runtime.GOOS != "windows" && st.Mode()&0111 == 0 {
|
|
||||||
tg.t.Fatalf("binary %s exists but is not executable", path)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// wantArchive fails if path is not an archive.
|
|
||||||
func (tg *testgoData) wantArchive(path string) {
|
|
||||||
f, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
tg.t.Fatal(err)
|
|
||||||
}
|
|
||||||
buf := make([]byte, 100)
|
|
||||||
io.ReadFull(f, buf)
|
|
||||||
f.Close()
|
|
||||||
if !bytes.HasPrefix(buf, []byte("!<arch>\n")) {
|
|
||||||
tg.t.Fatalf("file %s exists but is not an archive", path)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// isStale reports whether pkg is stale, and why
|
|
||||||
func (tg *testgoData) isStale(pkg string) (bool, string) {
|
|
||||||
tg.run("list", "-f", "{{.Stale}}:{{.StaleReason}}", pkg)
|
|
||||||
v := strings.TrimSpace(tg.getStdout())
|
|
||||||
f := strings.SplitN(v, ":", 2)
|
|
||||||
if len(f) == 2 {
|
|
||||||
switch f[0] {
|
|
||||||
case "true":
|
|
||||||
return true, f[1]
|
|
||||||
case "false":
|
|
||||||
return false, f[1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tg.t.Fatalf("unexpected output checking staleness of package %v: %v", pkg, v)
|
|
||||||
panic("unreachable")
|
|
||||||
}
|
|
||||||
|
|
||||||
// wantStale fails with msg if pkg is not stale.
|
|
||||||
func (tg *testgoData) wantStale(pkg, reason, msg string) {
|
|
||||||
stale, why := tg.isStale(pkg)
|
|
||||||
if !stale {
|
|
||||||
tg.t.Fatal(msg)
|
|
||||||
}
|
|
||||||
if reason == "" && why != "" || !strings.Contains(why, reason) {
|
|
||||||
tg.t.Errorf("wrong reason for Stale=true: %q, want %q", why, reason)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// wantNotStale fails with msg if pkg is stale.
|
|
||||||
func (tg *testgoData) wantNotStale(pkg, reason, msg string) {
|
|
||||||
stale, why := tg.isStale(pkg)
|
|
||||||
if stale {
|
|
||||||
tg.t.Fatal(msg)
|
|
||||||
}
|
|
||||||
if reason == "" && why != "" || !strings.Contains(why, reason) {
|
|
||||||
tg.t.Errorf("wrong reason for Stale=false: %q, want %q", why, reason)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// cleanup cleans up a test that runs testgo.
|
// cleanup cleans up a test that runs testgo.
|
||||||
func (tg *testgoData) cleanup() {
|
func (tg *testgoData) cleanup() {
|
||||||
if tg.wd != "" {
|
if tg.wd != "" {
|
||||||
|
@ -513,13 +470,22 @@ func (tg *testgoData) cleanup() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// failSSH puts an ssh executable in the PATH that always fails.
|
// readManifest returns the manifest in the current directory.
|
||||||
// This is to stub out uses of ssh by go get.
|
func (tg *testgoData) readManifest() string {
|
||||||
func (tg *testgoData) failSSH() {
|
m := filepath.Join(tg.pwd(), "manifest.json")
|
||||||
wd, err := os.Getwd()
|
tg.mustExist(m)
|
||||||
if err != nil {
|
|
||||||
tg.t.Fatal(err)
|
f, err := ioutil.ReadFile(m)
|
||||||
}
|
tg.must(err)
|
||||||
fail := filepath.Join(wd, "testdata/failssh")
|
return string(f)
|
||||||
tg.setenv("PATH", fmt.Sprintf("%v%c%v", fail, filepath.ListSeparator, os.Getenv("PATH")))
|
}
|
||||||
|
|
||||||
|
// readLock returns the lock in the current directory.
|
||||||
|
func (tg *testgoData) readLock() string {
|
||||||
|
l := filepath.Join(tg.pwd(), "lock.json")
|
||||||
|
tg.mustExist(l)
|
||||||
|
|
||||||
|
f, err := ioutil.ReadFile(l)
|
||||||
|
tg.must(err)
|
||||||
|
return string(f)
|
||||||
}
|
}
|
||||||
|
|
100
init_test.go
100
init_test.go
|
@ -1,6 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"os/exec"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestContains(t *testing.T) {
|
func TestContains(t *testing.T) {
|
||||||
a := []string{"a", "b", "abcd"}
|
a := []string{"a", "b", "abcd"}
|
||||||
|
@ -29,3 +32,98 @@ func TestIsStdLib(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInit(t *testing.T) {
|
||||||
|
// test must also have external network
|
||||||
|
if _, err := exec.LookPath("git"); err != nil {
|
||||||
|
t.Skip("skipping because git binary not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
tg := testgo(t)
|
||||||
|
defer tg.cleanup()
|
||||||
|
|
||||||
|
tg.tempDir("src")
|
||||||
|
tg.setenv("GOPATH", tg.path("."))
|
||||||
|
|
||||||
|
importPaths := map[string]string{
|
||||||
|
"github.com/pkg/errors": "v0.8.0", // semver
|
||||||
|
"github.com/Sirupsen/logrus": "42b84f9ec624953ecbf81a94feccb3f5935c5edf", // random sha
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkout the specified revisions
|
||||||
|
for ip, rev := range importPaths {
|
||||||
|
tg.runGo("get", ip)
|
||||||
|
repoDir := tg.path("src/" + ip)
|
||||||
|
tg.runGit(repoDir, "checkout", rev)
|
||||||
|
}
|
||||||
|
|
||||||
|
m := `package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Sirupsen/logrus"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
err := nil
|
||||||
|
if err != nil {
|
||||||
|
errors.Wrap(err, "thing")
|
||||||
|
}
|
||||||
|
logrus.Info("hello world")
|
||||||
|
}`
|
||||||
|
|
||||||
|
tg.tempFile("src/thing/thing.go", m)
|
||||||
|
tg.cd(tg.path("src/thing"))
|
||||||
|
|
||||||
|
tg.run("init")
|
||||||
|
|
||||||
|
expectedManifest := `{
|
||||||
|
"dependencies": {
|
||||||
|
"github.com/Sirupsen/logrus": {
|
||||||
|
"revision": "42b84f9ec624953ecbf81a94feccb3f5935c5edf"
|
||||||
|
},
|
||||||
|
"github.com/pkg/errors": {
|
||||||
|
"version": ">=0.8.0, <1.0.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
manifest := tg.readManifest()
|
||||||
|
if manifest != expectedManifest {
|
||||||
|
t.Fatalf("expected %s, got %s", expectedManifest, manifest)
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedLock := `{
|
||||||
|
"memo": "0d682aa197bf6e94b9af8298acea5b7957b9a5674570cc16b3e8436846928a57",
|
||||||
|
"projects": [
|
||||||
|
{
|
||||||
|
"name": "github.com/Sirupsen/logrus",
|
||||||
|
"revision": "42b84f9ec624953ecbf81a94feccb3f5935c5edf",
|
||||||
|
"packages": [
|
||||||
|
"."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "github.com/pkg/errors",
|
||||||
|
"version": "v0.8.0",
|
||||||
|
"revision": "645ef00459ed84a119197bfb8d8205042c6df63d",
|
||||||
|
"packages": [
|
||||||
|
"."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "golang.org/x/sys",
|
||||||
|
"branch": "master",
|
||||||
|
"revision": "478fcf54317e52ab69f40bb4c7a1520288d7f7ea",
|
||||||
|
"packages": [
|
||||||
|
"unix"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
`
|
||||||
|
lock := tg.readLock()
|
||||||
|
if lock != expectedLock {
|
||||||
|
t.Fatalf("expected %s, got %s", expectedLock, lock)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Загрузка…
Ссылка в новой задаче