diff --git a/cmd/dep/ensure_test.go b/cmd/dep/ensure_test.go index 5a3e0a8286..fd7be1c787 100644 --- a/cmd/dep/ensure_test.go +++ b/cmd/dep/ensure_test.go @@ -5,6 +5,7 @@ package main import ( + "strings" "testing" "github.com/golang/dep/test" @@ -21,72 +22,25 @@ func TestEnsureOverrides(t *testing.T) { h.TempDir("src") h.Setenv("GOPATH", h.Path(".")) - m := `package main - -import ( - "github.com/Sirupsen/logrus" - sthing "github.com/sdboyer/dep-test" -) - -type Baz sthing.Foo - -func main() { - logrus.Info("hello world") -}` - - h.TempFile("src/thing/thing.go", m) + h.TempCopy("src/thing/thing.go", "ensure_test/source1.go") h.Cd(h.Path("src/thing")) h.Run("init") h.Run("ensure", "-override", "github.com/Sirupsen/logrus@0.11.0") - expectedManifest := `{ - "overrides": { - "github.com/Sirupsen/logrus": { - "version": "0.11.0" - } - } -} -` - + expectedManifest := h.GetTestfile("ensure_test/exp_manifest1.json") manifest := h.ReadManifest() - if manifest != expectedManifest { + if exp, err := test.AreEqualJSON(expectedManifest, manifest); !exp { + h.Must(err) t.Fatalf("expected %s, got %s", expectedManifest, manifest) } sysCommit := h.GetCommit("go.googlesource.com/sys") - expectedLock := `{ - "memo": "57d20ba0289c2df60025bf6127220a5403483251bd5e523a7f9ea17752bd5482", - "projects": [ - { - "name": "github.com/Sirupsen/logrus", - "version": "v0.11.0", - "revision": "d26492970760ca5d33129d2d799e34be5c4782eb", - "packages": [ - "." - ] - }, - { - "name": "github.com/sdboyer/dep-test", - "version": "1.0.0", - "revision": "2a3a211e171803acb82d1d5d42ceb53228f51751", - "packages": [ - "." - ] - }, - { - "name": "golang.org/x/sys", - "branch": "master", - "revision": "` + sysCommit + `", - "packages": [ - "unix" - ] - } - ] -} -` + expectedLock := h.GetTestfile("ensure_test/exp_lock1.json") + expectedLock = strings.Replace(expectedLock, "{{sysCommit}}", sysCommit, -1) lock := h.ReadLock() - if lock != expectedLock { + if exp, err := test.AreEqualJSON(expectedLock, lock); !exp { + h.Must(err) t.Fatalf("expected %s, got %s", expectedLock, lock) } } diff --git a/cmd/dep/testdata/ensure_test/exp_lock1.json b/cmd/dep/testdata/ensure_test/exp_lock1.json new file mode 100644 index 0000000000..5ed2e82004 --- /dev/null +++ b/cmd/dep/testdata/ensure_test/exp_lock1.json @@ -0,0 +1,29 @@ +{ + "memo": "57d20ba0289c2df60025bf6127220a5403483251bd5e523a7f9ea17752bd5482", + "projects": [ + { + "name": "github.com/Sirupsen/logrus", + "version": "v0.11.0", + "revision": "d26492970760ca5d33129d2d799e34be5c4782eb", + "packages": [ + "." + ] + }, + { + "name": "github.com/sdboyer/dep-test", + "version": "1.0.0", + "revision": "2a3a211e171803acb82d1d5d42ceb53228f51751", + "packages": [ + "." + ] + }, + { + "name": "golang.org/x/sys", + "branch": "master", + "revision": "{{sysCommit}}", + "packages": [ + "unix" + ] + } + ] +} diff --git a/cmd/dep/testdata/ensure_test/exp_manifest1.json b/cmd/dep/testdata/ensure_test/exp_manifest1.json new file mode 100644 index 0000000000..1a64409d10 --- /dev/null +++ b/cmd/dep/testdata/ensure_test/exp_manifest1.json @@ -0,0 +1,7 @@ +{ + "overrides": { + "github.com/Sirupsen/logrus": { + "version": "0.11.0" + } + } +} diff --git a/cmd/dep/testdata/ensure_test/source1.go b/cmd/dep/testdata/ensure_test/source1.go new file mode 100644 index 0000000000..e8517114c8 --- /dev/null +++ b/cmd/dep/testdata/ensure_test/source1.go @@ -0,0 +1,12 @@ +package main + +import ( + "github.com/Sirupsen/logrus" + sthing "github.com/sdboyer/dep-test" +) + +type Baz sthing.Foo + +func main() { + logrus.Info("hello world") +} diff --git a/test/test.go b/test/test.go index 0ad55129f4..4814999ca3 100644 --- a/test/test.go +++ b/test/test.go @@ -36,6 +36,7 @@ type Helper struct { t *testing.T temps []string wd string + origWd string env []string tempdir string ran bool @@ -45,7 +46,8 @@ type Helper struct { // NewHelper initializes a new helper for testing. func NewHelper(t *testing.T) *Helper { - return &Helper{t: t} + wd, _ := os.Getwd() + return &Helper{t: t, origWd: wd} } // Must gives a fatal error if err is not nil. @@ -414,6 +416,36 @@ func (h *Helper) TempFile(path, contents string) { h.Must(ioutil.WriteFile(filepath.Join(h.tempdir, path), bytes, 0644)) } +// Location of test data +const _TEST_DATA_LOCATION = "cmd/dep/testdata" + +// GetTestfile reads a file form the testdata directory into memory. src is +// relative to ./testdata. Assumes tests take place starting in the cmd/dep +// directory. +func (h *Helper) GetTestfile(src string) string { + content, err := ioutil.ReadFile(filepath.Join(h.origWd, "testdata", src)) + if err != nil { + panic(err) + } + if strings.HasSuffix(src, ".go") { + formatted, err := format.Source(content) + if err == nil { + content = formatted + } + } + return string(content) +} + +// TempCopy copies a temporary file from testdata into the temporary directory. +// dest is relative to the temp directory location, and src is relative to +// ./testdata. Assumes tests take place starting in the cmd/dep directory. +func (h *Helper) TempCopy(dest, src string) { + content := h.GetTestfile(src) + h.makeTempdir() + h.Must(os.MkdirAll(filepath.Join(h.tempdir, filepath.Dir(dest)), 0755)) + h.Must(ioutil.WriteFile(filepath.Join(h.tempdir, dest), []byte(content), 0644)) +} + // TempDir adds a temporary directory for a run of testgo. func (h *Helper) TempDir(path string) { h.makeTempdir() diff --git a/test/util.go b/test/util.go new file mode 100644 index 0000000000..cee850f45e --- /dev/null +++ b/test/util.go @@ -0,0 +1,22 @@ +package test + +import ( + "bytes" + "encoding/json" +) + +func AreEqualJSON(s1, s2 string) (bool, error) { + var o1 interface{} + var o2 interface{} + var err error + + if err = json.Unmarshal([]byte(s1), &o1); err != nil { + return false, err + } + if err = json.Unmarshal([]byte(s2), &o2); err != nil { + return false, err + } + b1, _ := json.Marshal(o1) + b2, _ := json.Marshal(o2) + return bytes.Equal(b1, b2), nil +}