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-30 08:39:08 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-01-27 23:39:07 +03:00
|
|
|
|
|
|
|
"github.com/golang/dep/test"
|
2016-12-30 08:39:08 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTxnWriterBadInputs(t *testing.T) {
|
2017-01-27 23:39:07 +03:00
|
|
|
tg := test.Testgo(t)
|
|
|
|
defer tg.Cleanup()
|
2016-12-30 08:39:08 +03:00
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
tg.TempDir("txnwriter")
|
|
|
|
td := tg.Path(".")
|
2016-12-30 08:39:08 +03:00
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
var sw SafeWriter
|
2016-12-30 08:39:08 +03:00
|
|
|
|
|
|
|
// no root
|
2017-01-27 23:39:07 +03:00
|
|
|
if err := sw.WriteAllSafe(false); err == nil {
|
2016-12-30 08:39:08 +03:00
|
|
|
t.Errorf("should have errored without a root path, but did not")
|
|
|
|
}
|
2017-01-27 23:39:07 +03:00
|
|
|
sw.Root = td
|
2016-12-30 08:39:08 +03:00
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
if err := sw.WriteAllSafe(false); err != nil {
|
2016-12-30 08:39:08 +03:00
|
|
|
t.Errorf("write with only root should be fine, just a no-op, but got err %q", err)
|
|
|
|
}
|
2017-01-27 23:39:07 +03:00
|
|
|
if err := sw.WriteAllSafe(true); err == nil {
|
2016-12-30 08:39:08 +03:00
|
|
|
t.Errorf("should fail because no source manager was provided for writing vendor")
|
|
|
|
}
|
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
if err := sw.WriteAllSafe(true); err == nil {
|
2016-12-30 08:39:08 +03:00
|
|
|
t.Errorf("should fail because no lock was provided from which to write vendor")
|
|
|
|
}
|
|
|
|
// now check dir validation
|
2017-01-27 23:39:07 +03:00
|
|
|
sw.Root = filepath.Join(td, "nonexistent")
|
|
|
|
if err := sw.WriteAllSafe(false); err == nil {
|
2016-12-30 08:39:08 +03:00
|
|
|
t.Errorf("should have errored with nonexistent dir for root path, but did not")
|
|
|
|
}
|
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
sw.Root = filepath.Join(td, "myfile")
|
|
|
|
srcf, err := os.Create(sw.Root)
|
2016-12-30 08:39:08 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
srcf.Close()
|
2017-01-27 23:39:07 +03:00
|
|
|
if err := sw.WriteAllSafe(false); err == nil {
|
2016-12-30 08:39:08 +03:00
|
|
|
t.Errorf("should have errored when root path is a file, but did not")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTxnWriter(t *testing.T) {
|
2017-01-27 23:39:07 +03:00
|
|
|
test.NeedsExternalNetwork(t)
|
|
|
|
test.NeedsGit(t)
|
2016-12-30 08:39:08 +03:00
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
tg := test.Testgo(t)
|
|
|
|
tg.TempDir("")
|
|
|
|
defer tg.Cleanup()
|
2016-12-30 08:39:08 +03:00
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
c := &Ctx{
|
|
|
|
GOPATH: tg.Path("."),
|
2016-12-30 08:39:08 +03:00
|
|
|
}
|
2017-01-27 23:39:07 +03:00
|
|
|
sm, err := c.SourceManager()
|
2017-01-14 02:58:43 +03:00
|
|
|
defer sm.Release()
|
2017-01-27 23:39:07 +03:00
|
|
|
tg.Must(err)
|
2016-12-30 08:39:08 +03:00
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
var sw SafeWriter
|
2016-12-30 08:39:08 +03:00
|
|
|
var mpath, lpath, vpath string
|
|
|
|
var count int
|
|
|
|
reset := func() {
|
|
|
|
pr := filepath.Join("src", "txnwriter"+strconv.Itoa(count))
|
2017-01-27 23:39:07 +03:00
|
|
|
tg.TempDir(pr)
|
2016-12-30 08:39:08 +03:00
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
sw = SafeWriter{
|
|
|
|
Root: tg.Path(pr),
|
|
|
|
SourceManager: sm,
|
2016-12-30 08:39:08 +03:00
|
|
|
}
|
2017-01-27 23:39:07 +03:00
|
|
|
tg.Cd(sw.Root)
|
2016-12-30 08:39:08 +03:00
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
mpath = filepath.Join(sw.Root, ManifestName)
|
|
|
|
lpath = filepath.Join(sw.Root, LockName)
|
|
|
|
vpath = filepath.Join(sw.Root, "vendor")
|
2016-12-30 08:39:08 +03:00
|
|
|
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
reset()
|
|
|
|
|
|
|
|
// super basic manifest and lock
|
|
|
|
expectedManifest := `{
|
|
|
|
"dependencies": {
|
|
|
|
"github.com/sdboyer/dep-test": {
|
|
|
|
"version": "1.0.0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
expectedLock := `{
|
|
|
|
"memo": "595716d270828e763c811ef79c9c41f85b1d1bfbdfe85280036405c03772206c",
|
|
|
|
"projects": [
|
|
|
|
{
|
|
|
|
"name": "github.com/sdboyer/dep-test",
|
|
|
|
"version": "1.0.0",
|
|
|
|
"revision": "2a3a211e171803acb82d1d5d42ceb53228f51751",
|
|
|
|
"packages": [
|
|
|
|
"."
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
m, err := readManifest(strings.NewReader(expectedManifest))
|
2017-01-27 23:39:07 +03:00
|
|
|
tg.Must(err)
|
2016-12-30 08:39:08 +03:00
|
|
|
l, err := readLock(strings.NewReader(expectedLock))
|
2017-01-27 23:39:07 +03:00
|
|
|
tg.Must(err)
|
2016-12-30 08:39:08 +03:00
|
|
|
|
|
|
|
// Just write manifest
|
2017-01-27 23:39:07 +03:00
|
|
|
sw.Manifest = m
|
|
|
|
tg.Must(sw.WriteAllSafe(false))
|
|
|
|
tg.MustExist(mpath)
|
|
|
|
tg.MustNotExist(lpath)
|
|
|
|
tg.MustNotExist(vpath)
|
2016-12-30 08:39:08 +03:00
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
diskm := tg.ReadManifest()
|
2016-12-30 08:39:08 +03:00
|
|
|
if diskm != expectedManifest {
|
|
|
|
t.Fatalf("expected %s, got %s", expectedManifest, diskm)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manifest and lock, but no vendor
|
2017-01-27 23:39:07 +03:00
|
|
|
sw.Lock = l
|
|
|
|
tg.Must(sw.WriteAllSafe(false))
|
|
|
|
tg.MustExist(mpath)
|
|
|
|
tg.MustExist(lpath)
|
|
|
|
tg.MustNotExist(vpath)
|
2016-12-30 08:39:08 +03:00
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
diskm = tg.ReadManifest()
|
2016-12-30 08:39:08 +03:00
|
|
|
if diskm != expectedManifest {
|
|
|
|
t.Fatalf("expected %s, got %s", expectedManifest, diskm)
|
|
|
|
}
|
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
diskl := tg.ReadLock()
|
2016-12-30 08:39:08 +03:00
|
|
|
if diskl != expectedLock {
|
|
|
|
t.Fatalf("expected %s, got %s", expectedLock, diskl)
|
|
|
|
}
|
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
tg.Must(sw.WriteAllSafe(true))
|
|
|
|
tg.MustExist(mpath)
|
|
|
|
tg.MustExist(lpath)
|
|
|
|
tg.MustExist(vpath)
|
|
|
|
tg.MustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test"))
|
2016-12-30 08:39:08 +03:00
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
diskm = tg.ReadManifest()
|
2016-12-30 08:39:08 +03:00
|
|
|
if diskm != expectedManifest {
|
|
|
|
t.Fatalf("expected %s, got %s", expectedManifest, diskm)
|
|
|
|
}
|
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
diskl = tg.ReadLock()
|
2016-12-30 08:39:08 +03:00
|
|
|
if diskl != expectedLock {
|
|
|
|
t.Fatalf("expected %s, got %s", expectedLock, diskl)
|
|
|
|
}
|
|
|
|
|
|
|
|
// start fresh, ignoring the manifest now
|
|
|
|
reset()
|
2017-01-27 23:39:07 +03:00
|
|
|
sw.Lock = l
|
|
|
|
sw.NewLock = l
|
2016-12-30 08:39:08 +03:00
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
tg.Must(sw.WriteAllSafe(false))
|
2016-12-30 08:39:08 +03:00
|
|
|
// locks are equivalent, so nothing gets written
|
2017-01-27 23:39:07 +03:00
|
|
|
tg.MustNotExist(mpath)
|
|
|
|
tg.MustNotExist(lpath)
|
|
|
|
tg.MustNotExist(vpath)
|
2016-12-30 08:39:08 +03:00
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
l2 := new(Lock)
|
2016-12-30 08:39:08 +03:00
|
|
|
*l2 = *l
|
|
|
|
// zero out the input hash to ensure non-equivalency
|
|
|
|
l2.Memo = []byte{}
|
2017-01-27 23:39:07 +03:00
|
|
|
sw.Lock = l2
|
|
|
|
tg.Must(sw.WriteAllSafe(true))
|
|
|
|
tg.MustNotExist(mpath)
|
|
|
|
tg.MustExist(lpath)
|
|
|
|
tg.MustExist(vpath)
|
|
|
|
tg.MustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test"))
|
|
|
|
|
|
|
|
diskl = tg.ReadLock()
|
2016-12-30 08:39:08 +03:00
|
|
|
if diskl != expectedLock {
|
|
|
|
t.Fatalf("expected %s, got %s", expectedLock, diskl)
|
|
|
|
}
|
|
|
|
|
|
|
|
// repeat op to ensure good behavior when vendor dir already exists
|
2017-01-27 23:39:07 +03:00
|
|
|
sw.Lock = nil
|
|
|
|
tg.Must(sw.WriteAllSafe(true))
|
|
|
|
tg.MustNotExist(mpath)
|
|
|
|
tg.MustExist(lpath)
|
|
|
|
tg.MustExist(vpath)
|
|
|
|
tg.MustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test"))
|
|
|
|
|
|
|
|
diskl = tg.ReadLock()
|
2016-12-30 08:39:08 +03:00
|
|
|
if diskl != expectedLock {
|
|
|
|
t.Fatalf("expected %s, got %s", expectedLock, diskl)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO test txn rollback cases. maybe we can force errors with chmodding?
|
|
|
|
}
|