From b872a85b9f27de27de8b51d98dd11391f72a440f Mon Sep 17 00:00:00 2001 From: tro3 Date: Wed, 1 Feb 2017 12:14:41 -0800 Subject: [PATCH 01/12] Converting manifest_test.go to golden-file pattern --- _testdata/manifest/error.json | 21 ++++++++++ _testdata/manifest/golden.json | 19 +++++++++ manifest_test.go | 74 +++++++++++++--------------------- test/test.go | 46 ++++++++++++++++++++- test/util.go | 22 ++++++++++ 5 files changed, 135 insertions(+), 47 deletions(-) create mode 100644 _testdata/manifest/error.json create mode 100644 _testdata/manifest/golden.json create mode 100644 test/util.go diff --git a/_testdata/manifest/error.json b/_testdata/manifest/error.json new file mode 100644 index 0000000000..33b6a9bb36 --- /dev/null +++ b/_testdata/manifest/error.json @@ -0,0 +1,21 @@ +{ + "dependencies": { + "github.com/sdboyer/gps": { + "branch": "master", + "revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb", + "version": "^v0.12.0", + "source": "https://github.com/sdboyer/gps" + } + }, + "overrides": { + "github.com/sdboyer/gps": { + "branch": "master", + "revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb", + "version": "^v0.12.0", + "source": "https://github.com/sdboyer/gps" + } + }, + "ignores": [ + "github.com/foo/bar" + ] +} diff --git a/_testdata/manifest/golden.json b/_testdata/manifest/golden.json new file mode 100644 index 0000000000..b406d144f0 --- /dev/null +++ b/_testdata/manifest/golden.json @@ -0,0 +1,19 @@ +{ + "dependencies": { + "github.com/babble/brook": { + "revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb" + }, + "github.com/sdboyer/gps": { + "version": ">=0.12.0, <1.0.0" + } + }, + "overrides": { + "github.com/sdboyer/gps": { + "branch": "master", + "source": "https://github.com/sdboyer/gps" + } + }, + "ignores": [ + "github.com/foo/bar" + ] +} diff --git a/manifest_test.go b/manifest_test.go index 03fc6d87c9..22f4cf5b00 100644 --- a/manifest_test.go +++ b/manifest_test.go @@ -9,62 +9,41 @@ import ( "strings" "testing" + "github.com/golang/dep/test" "github.com/sdboyer/gps" ) -const je = `{ - "dependencies": { - "github.com/sdboyer/gps": { - "branch": "master", - "revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb", - "version": "^v0.12.0", - "source": "https://github.com/sdboyer/gps" - } - }, - "overrides": { - "github.com/sdboyer/gps": { - "branch": "master", - "revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb", - "version": "^v0.12.0", - "source": "https://github.com/sdboyer/gps" - } - }, - "ignores": [ - "github.com/foo/bar" - ] -} -` - -const jg = `{ - "dependencies": { - "github.com/babble/brook": { - "revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb" - }, - "github.com/sdboyer/gps": { - "version": ">=0.12.0, <1.0.0" - } - }, - "overrides": { - "github.com/sdboyer/gps": { - "branch": "master", - "source": "https://github.com/sdboyer/gps" - } - }, - "ignores": [ - "github.com/foo/bar" - ] -} -` +// const jg = `{ +// "dependencies": { +// "github.com/babble/brook": { +// "revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb" +// }, +// "github.com/sdboyer/gps": { +// "version": ">=0.12.0, <1.0.0" +// } +// }, +// "overrides": { +// "github.com/sdboyer/gps": { +// "branch": "master", +// "source": "https://github.com/sdboyer/gps" +// } +// }, +// "ignores": [ +// "github.com/foo/bar" +// ] +// } +// ` func TestReadManifest(t *testing.T) { - _, err := readManifest(strings.NewReader(je)) + h := test.NewHelper(t) + _, err := readManifest(h.GetTestFileReader("manifest/error.json")) if err == nil { t.Error("Reading manifest with invalid props should have caused error, but did not") } else if !strings.Contains(err.Error(), "multiple constraints") { t.Errorf("Unexpected error %q; expected multiple constraint error", err) } - m2, err := readManifest(strings.NewReader(jg)) + m2, err := readManifest(h.GetTestFileReader("manifest/golden.json")) if err != nil { t.Fatalf("Should have read Manifest correctly, but got err %q", err) } @@ -100,6 +79,8 @@ func TestReadManifest(t *testing.T) { } func TestWriteManifest(t *testing.T) { + h := test.NewHelper(t) + jg := h.GetTestFileString("manifest/golden.json") c, _ := gps.NewSemverConstraint("^v0.12.0") m := &Manifest{ Dependencies: map[gps.ProjectRoot]gps.ProjectProperties{ @@ -124,7 +105,8 @@ func TestWriteManifest(t *testing.T) { t.Fatalf("Error while marshaling valid manifest to JSON: %q", err) } - if string(b) != jg { + if exp, err := test.AreEqualJSON(string(b), jg); !exp { + h.Must(err) t.Errorf("Valid manifest did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", string(b), jg) } } diff --git a/test/test.go b/test/test.go index 0ad55129f4..6dbc68a503 100644 --- a/test/test.go +++ b/test/test.go @@ -8,6 +8,7 @@ import ( "bytes" "fmt" "go/format" + "io" "io/ioutil" "os" "os/exec" @@ -36,6 +37,7 @@ type Helper struct { t *testing.T temps []string wd string + origWd string env []string tempdir string ran bool @@ -45,7 +47,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 +417,47 @@ func (h *Helper) TempFile(path, contents string) { h.Must(ioutil.WriteFile(filepath.Join(h.tempdir, path), bytes, 0644)) } +// GetTestFileBytes 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) GetTestFileBytes(src string) []byte { + 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 content +} + +// GetTestFileString 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) GetTestFileString(src string) string { + return string(h.GetTestFileBytes(src)) +} + +// GetTestFileReader 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) GetTestFileReader(src string) io.Reader { + return strings.NewReader(h.GetTestFileString(src)) +} + +// 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.GetTestFileBytes(src) + h.makeTempdir() + h.Must(os.MkdirAll(filepath.Join(h.tempdir, filepath.Dir(dest)), 0755)) + h.Must(ioutil.WriteFile(filepath.Join(h.tempdir, dest), 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 +} From a4943311923d21cce5ca573fc6ea33e17e710b45 Mon Sep 17 00:00:00 2001 From: tro3 Date: Wed, 1 Feb 2017 13:11:09 -0800 Subject: [PATCH 02/12] Adding copyright and tossing extraneous comment --- manifest_test.go | 21 --------------------- test/util.go | 4 ++++ 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/manifest_test.go b/manifest_test.go index 22f4cf5b00..0c5a0b4ca5 100644 --- a/manifest_test.go +++ b/manifest_test.go @@ -13,27 +13,6 @@ import ( "github.com/sdboyer/gps" ) -// const jg = `{ -// "dependencies": { -// "github.com/babble/brook": { -// "revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb" -// }, -// "github.com/sdboyer/gps": { -// "version": ">=0.12.0, <1.0.0" -// } -// }, -// "overrides": { -// "github.com/sdboyer/gps": { -// "branch": "master", -// "source": "https://github.com/sdboyer/gps" -// } -// }, -// "ignores": [ -// "github.com/foo/bar" -// ] -// } -// ` - func TestReadManifest(t *testing.T) { h := test.NewHelper(t) _, err := readManifest(h.GetTestFileReader("manifest/error.json")) diff --git a/test/util.go b/test/util.go index cee850f45e..84591ef277 100644 --- a/test/util.go +++ b/test/util.go @@ -1,3 +1,7 @@ +// Copyright 2016 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. + package test import ( From 40842a64b8e8e7975aac914bde5cf27ada7119a9 Mon Sep 17 00:00:00 2001 From: tro3 Date: Wed, 1 Feb 2017 15:10:29 -0800 Subject: [PATCH 03/12] Converting lock_test.go to golden-file pattern --- _testdata/lock/error.json | 14 +++++++++++++ _testdata/lock/golden.json | 13 ++++++++++++ lock_test.go | 42 ++++++++------------------------------ 3 files changed, 35 insertions(+), 34 deletions(-) create mode 100644 _testdata/lock/error.json create mode 100644 _testdata/lock/golden.json diff --git a/_testdata/lock/error.json b/_testdata/lock/error.json new file mode 100644 index 0000000000..6bb77cc9f2 --- /dev/null +++ b/_testdata/lock/error.json @@ -0,0 +1,14 @@ +{ + "memo": "2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e", + "projects": [ + { + "name": "github.com/sdboyer/gps", + "branch": "master", + "version": "v0.12.0", + "revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb", + "packages": [ + "." + ] + } + ] +} diff --git a/_testdata/lock/golden.json b/_testdata/lock/golden.json new file mode 100644 index 0000000000..540c612565 --- /dev/null +++ b/_testdata/lock/golden.json @@ -0,0 +1,13 @@ +{ + "memo": "2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e", + "projects": [ + { + "name": "github.com/sdboyer/gps", + "branch": "master", + "revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb", + "packages": [ + "." + ] + } + ] +} diff --git a/lock_test.go b/lock_test.go index 335cf31c83..4562744a9e 100644 --- a/lock_test.go +++ b/lock_test.go @@ -10,49 +10,20 @@ import ( "strings" "testing" + "github.com/golang/dep/test" "github.com/sdboyer/gps" ) -const le = `{ - "memo": "2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e", - "projects": [ - { - "name": "github.com/sdboyer/gps", - "branch": "master", - "version": "v0.12.0", - "revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb", - "packages": [ - "." - ] - } - ] -} -` - -const lg = `{ - "memo": "2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e", - "projects": [ - { - "name": "github.com/sdboyer/gps", - "branch": "master", - "revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb", - "packages": [ - "." - ] - } - ] -} -` - func TestReadLock(t *testing.T) { - _, err := readLock(strings.NewReader(le)) + h := test.NewHelper(t) + _, err := readLock(h.GetTestFileReader("lock/error.json")) if err == nil { t.Error("Reading lock with invalid props should have caused error, but did not") } else if !strings.Contains(err.Error(), "both a branch") { t.Errorf("Unexpected error %q; expected multiple version error", err) } - l, err := readLock(strings.NewReader(lg)) + l, err := readLock(h.GetTestFileReader("lock/golden.json")) if err != nil { t.Fatalf("Should have read Lock correctly, but got err %q", err) } @@ -75,6 +46,8 @@ func TestReadLock(t *testing.T) { } func TestWriteLock(t *testing.T) { + h := test.NewHelper(t) + lg := h.GetTestFileString("lock/golden.json") memo, _ := hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e") l := &Lock{ Memo: memo, @@ -92,7 +65,8 @@ func TestWriteLock(t *testing.T) { t.Fatalf("Error while marshaling valid lock to JSON: %q", err) } - if string(b) != lg { + if exp, err := test.AreEqualJSON(string(b), lg); !exp { + h.Must(err) t.Errorf("Valid lock did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", string(b), lg) } } From d322aba971b6a533a04e20fcca2b4bdcbd870ada Mon Sep 17 00:00:00 2001 From: tro3 Date: Wed, 1 Feb 2017 15:20:38 -0800 Subject: [PATCH 04/12] Converting txn_writer_test.go to golden-file pattern --- _testdata/txn_writer/expected_lock.json | 13 ++++++ _testdata/txn_writer/expected_manifest.json | 7 +++ txn_writer_test.go | 50 ++++++++------------- 3 files changed, 38 insertions(+), 32 deletions(-) create mode 100644 _testdata/txn_writer/expected_lock.json create mode 100644 _testdata/txn_writer/expected_manifest.json diff --git a/_testdata/txn_writer/expected_lock.json b/_testdata/txn_writer/expected_lock.json new file mode 100644 index 0000000000..52e0af9c05 --- /dev/null +++ b/_testdata/txn_writer/expected_lock.json @@ -0,0 +1,13 @@ +{ + "memo": "595716d270828e763c811ef79c9c41f85b1d1bfbdfe85280036405c03772206c", + "projects": [ + { + "name": "github.com/sdboyer/dep-test", + "version": "1.0.0", + "revision": "2a3a211e171803acb82d1d5d42ceb53228f51751", + "packages": [ + "." + ] + } + ] +} diff --git a/_testdata/txn_writer/expected_manifest.json b/_testdata/txn_writer/expected_manifest.json new file mode 100644 index 0000000000..f25e2fb855 --- /dev/null +++ b/_testdata/txn_writer/expected_manifest.json @@ -0,0 +1,7 @@ +{ + "dependencies": { + "github.com/sdboyer/dep-test": { + "version": "1.0.0" + } + } +} diff --git a/txn_writer_test.go b/txn_writer_test.go index 7f34bddf92..9ca3fd2ca3 100644 --- a/txn_writer_test.go +++ b/txn_writer_test.go @@ -8,7 +8,6 @@ import ( "os" "path/filepath" "strconv" - "strings" "testing" "github.com/golang/dep/test" @@ -93,32 +92,12 @@ func TestTxnWriter(t *testing.T) { 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": [ - "." - ] - } - ] -} -` + expectedManifest := h.GetTestFileString("txn_writer/expected_manifest.json") + expectedLock := h.GetTestFileString("txn_writer/expected_lock.json") - m, err := readManifest(strings.NewReader(expectedManifest)) + m, err := readManifest(h.GetTestFileReader("txn_writer/expected_manifest.json")) h.Must(err) - l, err := readLock(strings.NewReader(expectedLock)) + l, err := readLock(h.GetTestFileReader("txn_writer/expected_lock.json")) h.Must(err) // Just write manifest @@ -129,7 +108,8 @@ func TestTxnWriter(t *testing.T) { h.MustNotExist(vpath) diskm := h.ReadManifest() - if diskm != expectedManifest { + if exp, err := test.AreEqualJSON(expectedManifest, diskm); !exp { + h.Must(err) t.Fatalf("expected %s, got %s", expectedManifest, diskm) } @@ -141,12 +121,14 @@ func TestTxnWriter(t *testing.T) { h.MustNotExist(vpath) diskm = h.ReadManifest() - if diskm != expectedManifest { + if exp, err := test.AreEqualJSON(expectedManifest, diskm); !exp { + h.Must(err) t.Fatalf("expected %s, got %s", expectedManifest, diskm) } diskl := h.ReadLock() - if diskl != expectedLock { + if exp, err := test.AreEqualJSON(expectedLock, diskl); !exp { + h.Must(err) t.Fatalf("expected %s, got %s", expectedLock, diskl) } @@ -157,12 +139,14 @@ func TestTxnWriter(t *testing.T) { h.MustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test")) diskm = h.ReadManifest() - if diskm != expectedManifest { + if exp, err := test.AreEqualJSON(expectedManifest, diskm); !exp { + h.Must(err) t.Fatalf("expected %s, got %s", expectedManifest, diskm) } diskl = h.ReadLock() - if diskl != expectedLock { + if exp, err := test.AreEqualJSON(expectedLock, diskl); !exp { + h.Must(err) t.Fatalf("expected %s, got %s", expectedLock, diskl) } @@ -189,7 +173,8 @@ func TestTxnWriter(t *testing.T) { h.MustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test")) diskl = h.ReadLock() - if diskl != expectedLock { + if exp, err := test.AreEqualJSON(expectedLock, diskl); !exp { + h.Must(err) t.Fatalf("expected %s, got %s", expectedLock, diskl) } @@ -202,7 +187,8 @@ func TestTxnWriter(t *testing.T) { h.MustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test")) diskl = h.ReadLock() - if diskl != expectedLock { + if exp, err := test.AreEqualJSON(expectedLock, diskl); !exp { + h.Must(err) t.Fatalf("expected %s, got %s", expectedLock, diskl) } From a15e2c2922cdbe5057f5c6f5499c873f10c612ee Mon Sep 17 00:00:00 2001 From: tro3 Date: Wed, 1 Feb 2017 18:18:35 -0800 Subject: [PATCH 05/12] Converting analyzer_test.go to golden-file pattern --- _testdata/analyzer/manifest.json | 10 ++++++++++ analyzer_test.go | 32 ++++++++++---------------------- lock_test.go | 4 ++++ manifest_test.go | 4 ++++ txn_writer_test.go | 2 ++ 5 files changed, 30 insertions(+), 22 deletions(-) create mode 100644 _testdata/analyzer/manifest.json diff --git a/_testdata/analyzer/manifest.json b/_testdata/analyzer/manifest.json new file mode 100644 index 0000000000..6cb1c2b5ec --- /dev/null +++ b/_testdata/analyzer/manifest.json @@ -0,0 +1,10 @@ +{ + "dependencies": { + "github.com/pkg/errors": { + "version": ">=0.8.0, <1.0.0" + }, + "github.com/sdboyer/gps": { + "version": ">=0.12.0, <1.0.0" + } + } +} diff --git a/analyzer_test.go b/analyzer_test.go index dd4ab9aa5c..d3313bf5df 100644 --- a/analyzer_test.go +++ b/analyzer_test.go @@ -9,34 +9,21 @@ import ( "os" "path/filepath" "testing" + + "github.com/golang/dep/test" ) func TestDeriveManifestAndLock(t *testing.T) { - dir, err := ioutil.TempDir("", "dep") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + h := test.NewHelper(t) + defer h.Cleanup() - contents := `{ - "dependencies": { - "github.com/pkg/errors": { - "version": ">=0.8.0, <1.0.0" - }, - "github.com/sdboyer/gps": { - "version": ">=0.12.0, <1.0.0" - } - } -} -` - - if err := ioutil.WriteFile(filepath.Join(dir, ManifestName), []byte(contents), 0644); err != nil { - t.Fatal(err) - } + h.TempDir("dep") + contents := h.GetTestFileString("analyzer/manifest.json") + h.TempCopy(filepath.Join("dep", ManifestName), "analyzer/manifest.json") a := analyzer{} - m, l, err := a.DeriveManifestAndLock(dir, "my/fake/project") + m, l, err := a.DeriveManifestAndLock(h.Path("dep"), "my/fake/project") if err != nil { t.Fatal(err) } @@ -46,7 +33,8 @@ func TestDeriveManifestAndLock(t *testing.T) { t.Fatal(err) } - if (string(b)) != contents { + if exp, err := test.AreEqualJSON(contents, string(b)); !exp { + h.Must(err) t.Fatalf("expected %s\n got %s", contents, string(b)) } diff --git a/lock_test.go b/lock_test.go index 4562744a9e..c26a516060 100644 --- a/lock_test.go +++ b/lock_test.go @@ -16,6 +16,8 @@ import ( func TestReadLock(t *testing.T) { h := test.NewHelper(t) + defer h.Cleanup() + _, err := readLock(h.GetTestFileReader("lock/error.json")) if err == nil { t.Error("Reading lock with invalid props should have caused error, but did not") @@ -47,6 +49,8 @@ func TestReadLock(t *testing.T) { func TestWriteLock(t *testing.T) { h := test.NewHelper(t) + defer h.Cleanup() + lg := h.GetTestFileString("lock/golden.json") memo, _ := hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e") l := &Lock{ diff --git a/manifest_test.go b/manifest_test.go index 0c5a0b4ca5..85ffac9e1d 100644 --- a/manifest_test.go +++ b/manifest_test.go @@ -15,6 +15,8 @@ import ( func TestReadManifest(t *testing.T) { h := test.NewHelper(t) + defer h.Cleanup() + _, err := readManifest(h.GetTestFileReader("manifest/error.json")) if err == nil { t.Error("Reading manifest with invalid props should have caused error, but did not") @@ -59,6 +61,8 @@ func TestReadManifest(t *testing.T) { func TestWriteManifest(t *testing.T) { h := test.NewHelper(t) + defer h.Cleanup() + jg := h.GetTestFileString("manifest/golden.json") c, _ := gps.NewSemverConstraint("^v0.12.0") m := &Manifest{ diff --git a/txn_writer_test.go b/txn_writer_test.go index 9ca3fd2ca3..c27c374313 100644 --- a/txn_writer_test.go +++ b/txn_writer_test.go @@ -60,6 +60,8 @@ func TestTxnWriter(t *testing.T) { test.NeedsGit(t) h := test.NewHelper(t) + defer h.Cleanup() + h.TempDir("") defer h.Cleanup() From 54787d16cba20f5a4392e39c4ef795f3c9598b3b Mon Sep 17 00:00:00 2001 From: tro3 Date: Fri, 3 Feb 2017 08:04:57 -0800 Subject: [PATCH 06/12] Cleaning up test file functions --- lock_test.go | 4 ++-- manifest_test.go | 4 ++-- test/test.go | 53 ++++++++++++++++++++++------------------------ txn_writer_test.go | 4 ++-- 4 files changed, 31 insertions(+), 34 deletions(-) diff --git a/lock_test.go b/lock_test.go index c26a516060..27608d7316 100644 --- a/lock_test.go +++ b/lock_test.go @@ -18,14 +18,14 @@ func TestReadLock(t *testing.T) { h := test.NewHelper(t) defer h.Cleanup() - _, err := readLock(h.GetTestFileReader("lock/error.json")) + _, err := readLock(h.GetTestFile("lock/error.json")) if err == nil { t.Error("Reading lock with invalid props should have caused error, but did not") } else if !strings.Contains(err.Error(), "both a branch") { t.Errorf("Unexpected error %q; expected multiple version error", err) } - l, err := readLock(h.GetTestFileReader("lock/golden.json")) + l, err := readLock(h.GetTestFile("lock/golden.json")) if err != nil { t.Fatalf("Should have read Lock correctly, but got err %q", err) } diff --git a/manifest_test.go b/manifest_test.go index 85ffac9e1d..ee5afee40c 100644 --- a/manifest_test.go +++ b/manifest_test.go @@ -17,14 +17,14 @@ func TestReadManifest(t *testing.T) { h := test.NewHelper(t) defer h.Cleanup() - _, err := readManifest(h.GetTestFileReader("manifest/error.json")) + _, err := readManifest(h.GetTestFile("manifest/error.json")) if err == nil { t.Error("Reading manifest with invalid props should have caused error, but did not") } else if !strings.Contains(err.Error(), "multiple constraints") { t.Errorf("Unexpected error %q; expected multiple constraint error", err) } - m2, err := readManifest(h.GetTestFileReader("manifest/golden.json")) + m2, err := readManifest(h.GetTestFile("manifest/golden.json")) if err != nil { t.Fatalf("Should have read Manifest correctly, but got err %q", err) } diff --git a/test/test.go b/test/test.go index 6dbc68a503..60976c6cc4 100644 --- a/test/test.go +++ b/test/test.go @@ -47,7 +47,10 @@ type Helper struct { // NewHelper initializes a new helper for testing. func NewHelper(t *testing.T) *Helper { - wd, _ := os.Getwd() + wd, err := os.Getwd() + if err != nil { + panic(err) + } return &Helper{t: t, origWd: wd} } @@ -417,45 +420,39 @@ func (h *Helper) TempFile(path, contents string) { h.Must(ioutil.WriteFile(filepath.Join(h.tempdir, path), bytes, 0644)) } -// GetTestFileBytes 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) GetTestFileBytes(src string) []byte { - content, err := ioutil.ReadFile(filepath.Join(h.origWd, "_testdata", src)) +// GetTestFile reads a file from the testdata directory into memory. src is +// relative to ./_testdata. +func (h *Helper) GetTestFile(src string) io.ReadCloser { + content, err := os.Open(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 content } -// GetTestFileString reads a file form the testdata directory into memory. src is -// relative to ./testdata. Assumes tests take place starting in the cmd/dep -// directory. +// GetTestFileString reads a file from the testdata directory into memory. src is +// relative to ./_testdata. func (h *Helper) GetTestFileString(src string) string { - return string(h.GetTestFileBytes(src)) -} - -// GetTestFileReader 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) GetTestFileReader(src string) io.Reader { - return strings.NewReader(h.GetTestFileString(src)) + content, err := ioutil.ReadAll(h.GetTestFile(src)) + if err != nil { + panic(err) + } + 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. +// ./_testdata. func (h *Helper) TempCopy(dest, src string) { - content := h.GetTestFileBytes(src) - h.makeTempdir() - h.Must(os.MkdirAll(filepath.Join(h.tempdir, filepath.Dir(dest)), 0755)) - h.Must(ioutil.WriteFile(filepath.Join(h.tempdir, dest), content, 0644)) + in := h.GetTestFile(src) + defer in.Close() + h.TempDir(filepath.Dir(dest)) + out, err := os.Create(filepath.Join(h.tempdir, dest)) + if err != nil { + panic(err) + } + defer out.Close() + io.Copy(out, in) } // TempDir adds a temporary directory for a run of testgo. diff --git a/txn_writer_test.go b/txn_writer_test.go index c27c374313..2c6ac12618 100644 --- a/txn_writer_test.go +++ b/txn_writer_test.go @@ -97,9 +97,9 @@ func TestTxnWriter(t *testing.T) { expectedManifest := h.GetTestFileString("txn_writer/expected_manifest.json") expectedLock := h.GetTestFileString("txn_writer/expected_lock.json") - m, err := readManifest(h.GetTestFileReader("txn_writer/expected_manifest.json")) + m, err := readManifest(h.GetTestFile("txn_writer/expected_manifest.json")) h.Must(err) - l, err := readLock(h.GetTestFileReader("txn_writer/expected_lock.json")) + l, err := readLock(h.GetTestFile("txn_writer/expected_lock.json")) h.Must(err) // Just write manifest From 82a35c2821214ad9215007892472770feb8d7e73 Mon Sep 17 00:00:00 2001 From: tro3 Date: Fri, 3 Feb 2017 20:27:11 -0800 Subject: [PATCH 07/12] Removing AreJSONEqual function --- _testdata/analyzer/manifest.json | 16 ++++++------- _testdata/txn_writer/expected_lock.json | 22 ++++++++--------- _testdata/txn_writer/expected_manifest.json | 10 ++++---- analyzer_test.go | 3 +-- lock_test.go | 3 +-- manifest_test.go | 3 +-- test/util.go | 26 --------------------- txn_writer_test.go | 21 ++++++----------- 8 files changed, 34 insertions(+), 70 deletions(-) delete mode 100644 test/util.go diff --git a/_testdata/analyzer/manifest.json b/_testdata/analyzer/manifest.json index 6cb1c2b5ec..b1d3c47733 100644 --- a/_testdata/analyzer/manifest.json +++ b/_testdata/analyzer/manifest.json @@ -1,10 +1,10 @@ { - "dependencies": { - "github.com/pkg/errors": { - "version": ">=0.8.0, <1.0.0" - }, - "github.com/sdboyer/gps": { - "version": ">=0.12.0, <1.0.0" - } - } + "dependencies": { + "github.com/pkg/errors": { + "version": ">=0.8.0, <1.0.0" + }, + "github.com/sdboyer/gps": { + "version": ">=0.12.0, <1.0.0" + } + } } diff --git a/_testdata/txn_writer/expected_lock.json b/_testdata/txn_writer/expected_lock.json index 52e0af9c05..40b8c8f836 100644 --- a/_testdata/txn_writer/expected_lock.json +++ b/_testdata/txn_writer/expected_lock.json @@ -1,13 +1,13 @@ { - "memo": "595716d270828e763c811ef79c9c41f85b1d1bfbdfe85280036405c03772206c", - "projects": [ - { - "name": "github.com/sdboyer/dep-test", - "version": "1.0.0", - "revision": "2a3a211e171803acb82d1d5d42ceb53228f51751", - "packages": [ - "." - ] - } - ] + "memo": "595716d270828e763c811ef79c9c41f85b1d1bfbdfe85280036405c03772206c", + "projects": [ + { + "name": "github.com/sdboyer/dep-test", + "version": "1.0.0", + "revision": "2a3a211e171803acb82d1d5d42ceb53228f51751", + "packages": [ + "." + ] + } + ] } diff --git a/_testdata/txn_writer/expected_manifest.json b/_testdata/txn_writer/expected_manifest.json index f25e2fb855..4ba1517ee1 100644 --- a/_testdata/txn_writer/expected_manifest.json +++ b/_testdata/txn_writer/expected_manifest.json @@ -1,7 +1,7 @@ { - "dependencies": { - "github.com/sdboyer/dep-test": { - "version": "1.0.0" - } - } + "dependencies": { + "github.com/sdboyer/dep-test": { + "version": "1.0.0" + } + } } diff --git a/analyzer_test.go b/analyzer_test.go index d3313bf5df..c37dc253d9 100644 --- a/analyzer_test.go +++ b/analyzer_test.go @@ -33,8 +33,7 @@ func TestDeriveManifestAndLock(t *testing.T) { t.Fatal(err) } - if exp, err := test.AreEqualJSON(contents, string(b)); !exp { - h.Must(err) + if contents != string(b) { t.Fatalf("expected %s\n got %s", contents, string(b)) } diff --git a/lock_test.go b/lock_test.go index 27608d7316..1d7d13f597 100644 --- a/lock_test.go +++ b/lock_test.go @@ -69,8 +69,7 @@ func TestWriteLock(t *testing.T) { t.Fatalf("Error while marshaling valid lock to JSON: %q", err) } - if exp, err := test.AreEqualJSON(string(b), lg); !exp { - h.Must(err) + if string(b) != lg { t.Errorf("Valid lock did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", string(b), lg) } } diff --git a/manifest_test.go b/manifest_test.go index ee5afee40c..26e9310684 100644 --- a/manifest_test.go +++ b/manifest_test.go @@ -88,8 +88,7 @@ func TestWriteManifest(t *testing.T) { t.Fatalf("Error while marshaling valid manifest to JSON: %q", err) } - if exp, err := test.AreEqualJSON(string(b), jg); !exp { - h.Must(err) + if string(b) != jg { t.Errorf("Valid manifest did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", string(b), jg) } } diff --git a/test/util.go b/test/util.go deleted file mode 100644 index 84591ef277..0000000000 --- a/test/util.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2016 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. - -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 -} diff --git a/txn_writer_test.go b/txn_writer_test.go index 2c6ac12618..3f9365cf2d 100644 --- a/txn_writer_test.go +++ b/txn_writer_test.go @@ -110,8 +110,7 @@ func TestTxnWriter(t *testing.T) { h.MustNotExist(vpath) diskm := h.ReadManifest() - if exp, err := test.AreEqualJSON(expectedManifest, diskm); !exp { - h.Must(err) + if expectedManifest != diskm { t.Fatalf("expected %s, got %s", expectedManifest, diskm) } @@ -123,14 +122,12 @@ func TestTxnWriter(t *testing.T) { h.MustNotExist(vpath) diskm = h.ReadManifest() - if exp, err := test.AreEqualJSON(expectedManifest, diskm); !exp { - h.Must(err) + if expectedManifest != diskm { t.Fatalf("expected %s, got %s", expectedManifest, diskm) } diskl := h.ReadLock() - if exp, err := test.AreEqualJSON(expectedLock, diskl); !exp { - h.Must(err) + if expectedLock != diskl { t.Fatalf("expected %s, got %s", expectedLock, diskl) } @@ -141,14 +138,12 @@ func TestTxnWriter(t *testing.T) { h.MustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test")) diskm = h.ReadManifest() - if exp, err := test.AreEqualJSON(expectedManifest, diskm); !exp { - h.Must(err) + if expectedManifest != diskm { t.Fatalf("expected %s, got %s", expectedManifest, diskm) } diskl = h.ReadLock() - if exp, err := test.AreEqualJSON(expectedLock, diskl); !exp { - h.Must(err) + if expectedLock != diskl { t.Fatalf("expected %s, got %s", expectedLock, diskl) } @@ -175,8 +170,7 @@ func TestTxnWriter(t *testing.T) { h.MustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test")) diskl = h.ReadLock() - if exp, err := test.AreEqualJSON(expectedLock, diskl); !exp { - h.Must(err) + if expectedLock != diskl { t.Fatalf("expected %s, got %s", expectedLock, diskl) } @@ -189,8 +183,7 @@ func TestTxnWriter(t *testing.T) { h.MustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test")) diskl = h.ReadLock() - if exp, err := test.AreEqualJSON(expectedLock, diskl); !exp { - h.Must(err) + if expectedLock != diskl { t.Fatalf("expected %s, got %s", expectedLock, diskl) } From 18efed64f66d23ed65c82c0e119e0ebd6422a5cb Mon Sep 17 00:00:00 2001 From: tro3 Date: Wed, 8 Feb 2017 18:37:31 -0800 Subject: [PATCH 08/12] Adding -update test flag for golden files --- analyzer_test.go | 13 +++++++-- fs_test.go | 4 +-- lock_test.go | 15 ++++++++-- manifest_test.go | 11 ++++++-- project_test.go | 2 +- test/test.go | 21 ++++++++++---- .../analyzer/manifest.json | 0 {_testdata => testdata}/lock/error.json | 0 {_testdata => testdata}/lock/golden.json | 0 {_testdata => testdata}/manifest/error.json | 0 {_testdata => testdata}/manifest/golden.json | 0 .../rootfind/manifest.json | 0 .../rootfind/subdir/.gitkeep | 0 .../txn_writer/expected_lock.json | 0 .../txn_writer/expected_manifest.json | 0 txn_writer_test.go | 28 +++++++++++++++---- 16 files changed, 71 insertions(+), 23 deletions(-) rename {_testdata => testdata}/analyzer/manifest.json (100%) rename {_testdata => testdata}/lock/error.json (100%) rename {_testdata => testdata}/lock/golden.json (100%) rename {_testdata => testdata}/manifest/error.json (100%) rename {_testdata => testdata}/manifest/golden.json (100%) rename {_testdata => testdata}/rootfind/manifest.json (100%) rename {_testdata => testdata}/rootfind/subdir/.gitkeep (100%) rename {_testdata => testdata}/txn_writer/expected_lock.json (100%) rename {_testdata => testdata}/txn_writer/expected_manifest.json (100%) diff --git a/analyzer_test.go b/analyzer_test.go index c37dc253d9..05f84b70f5 100644 --- a/analyzer_test.go +++ b/analyzer_test.go @@ -18,8 +18,9 @@ func TestDeriveManifestAndLock(t *testing.T) { defer h.Cleanup() h.TempDir("dep") - contents := h.GetTestFileString("analyzer/manifest.json") - h.TempCopy(filepath.Join("dep", ManifestName), "analyzer/manifest.json") + golden := "analyzer/manifest.json" + contents := h.GetTestFileString(golden) + h.TempCopy(filepath.Join("dep", ManifestName), golden) a := analyzer{} @@ -34,7 +35,13 @@ func TestDeriveManifestAndLock(t *testing.T) { } if contents != string(b) { - t.Fatalf("expected %s\n got %s", contents, string(b)) + if *test.UpdateGolden { + if err := h.WriteTestFile(golden, string(b)); err != nil { + t.Fatal(err) + } + } else { + t.Fatalf("expected %s\n got %s", contents, string(b)) + } } if l != nil { diff --git a/fs_test.go b/fs_test.go index f520057e7a..9745829607 100644 --- a/fs_test.go +++ b/fs_test.go @@ -127,7 +127,7 @@ func TestIsRegular(t *testing.T) { tests := map[string]bool{ wd: false, - filepath.Join(wd, "_testdata"): false, + filepath.Join(wd, "testdata"): false, filepath.Join(wd, "cmd", "dep", "main.go"): true, filepath.Join(wd, "this_file_does_not_exist.thing"): false, } @@ -158,7 +158,7 @@ func TestIsDir(t *testing.T) { tests := map[string]bool{ wd: true, - filepath.Join(wd, "_testdata"): true, + filepath.Join(wd, "testdata"): true, filepath.Join(wd, "main.go"): false, filepath.Join(wd, "this_file_does_not_exist.thing"): false, } diff --git a/lock_test.go b/lock_test.go index 1d7d13f597..60e5619b26 100644 --- a/lock_test.go +++ b/lock_test.go @@ -25,7 +25,8 @@ func TestReadLock(t *testing.T) { t.Errorf("Unexpected error %q; expected multiple version error", err) } - l, err := readLock(h.GetTestFile("lock/golden.json")) + golden := "lock/golden.json" + l, err := readLock(h.GetTestFile(golden)) if err != nil { t.Fatalf("Should have read Lock correctly, but got err %q", err) } @@ -44,6 +45,7 @@ func TestReadLock(t *testing.T) { if !reflect.DeepEqual(l, l2) { t.Error("Valid lock did not parse as expected") + } } @@ -51,7 +53,8 @@ func TestWriteLock(t *testing.T) { h := test.NewHelper(t) defer h.Cleanup() - lg := h.GetTestFileString("lock/golden.json") + golden := "lock/golden.json" + lg := h.GetTestFileString(golden) memo, _ := hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e") l := &Lock{ Memo: memo, @@ -70,6 +73,12 @@ func TestWriteLock(t *testing.T) { } if string(b) != lg { - t.Errorf("Valid lock did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", string(b), lg) + if *test.UpdateGolden { + if err = h.WriteTestFile(golden, string(b)); err != nil { + t.Fatal(err) + } + } else { + t.Errorf("Valid lock did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", lg, string(b)) + } } } diff --git a/manifest_test.go b/manifest_test.go index 26e9310684..0d945f2e4a 100644 --- a/manifest_test.go +++ b/manifest_test.go @@ -63,7 +63,8 @@ func TestWriteManifest(t *testing.T) { h := test.NewHelper(t) defer h.Cleanup() - jg := h.GetTestFileString("manifest/golden.json") + golden := "manifest/golden.json" + jg := h.GetTestFileString(golden) c, _ := gps.NewSemverConstraint("^v0.12.0") m := &Manifest{ Dependencies: map[gps.ProjectRoot]gps.ProjectProperties{ @@ -89,6 +90,12 @@ func TestWriteManifest(t *testing.T) { } if string(b) != jg { - t.Errorf("Valid manifest did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", string(b), jg) + if *test.UpdateGolden { + if err = h.WriteTestFile(golden, string(b)); err != nil { + t.Fatal(err) + } + } else { + t.Errorf("Valid manifest did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", jg, string(b)) + } } } diff --git a/project_test.go b/project_test.go index d1ad234613..a346c2931c 100644 --- a/project_test.go +++ b/project_test.go @@ -20,7 +20,7 @@ func TestFindRoot(t *testing.T) { t.Fatal(err) } - want := filepath.Join(wd, "_testdata", "rootfind") + want := filepath.Join(wd, "testdata", "rootfind") got1, err := findProjectRoot(want) if err != nil { t.Errorf("Unexpected error while finding root: %s", err) diff --git a/test/test.go b/test/test.go index 60976c6cc4..14f54ceffc 100644 --- a/test/test.go +++ b/test/test.go @@ -6,6 +6,7 @@ package test import ( "bytes" + "flag" "fmt" "go/format" "io" @@ -21,8 +22,9 @@ import ( ) var ( - ExeSuffix string // ".exe" on Windows - mu sync.Mutex + ExeSuffix string // ".exe" on Windows + mu sync.Mutex + UpdateGolden = flag.Bool("update", false, "update .golden files") ) func init() { @@ -420,10 +422,17 @@ func (h *Helper) TempFile(path, contents string) { h.Must(ioutil.WriteFile(filepath.Join(h.tempdir, path), bytes, 0644)) } +// WriteTestFile writes a file to the testdata directory from memory. src is +// relative to ./testdata. +func (h *Helper) WriteTestFile(src string, content string) error { + err := ioutil.WriteFile(filepath.Join(h.origWd, "testdata", src), []byte(content), 0666) + return err +} + // GetTestFile reads a file from the testdata directory into memory. src is -// relative to ./_testdata. +// relative to ./testdata. func (h *Helper) GetTestFile(src string) io.ReadCloser { - content, err := os.Open(filepath.Join(h.origWd, "_testdata", src)) + content, err := os.Open(filepath.Join(h.origWd, "testdata", src)) if err != nil { panic(err) } @@ -431,7 +440,7 @@ func (h *Helper) GetTestFile(src string) io.ReadCloser { } // GetTestFileString reads a file from the testdata directory into memory. src is -// relative to ./_testdata. +// relative to ./testdata. func (h *Helper) GetTestFileString(src string) string { content, err := ioutil.ReadAll(h.GetTestFile(src)) if err != nil { @@ -442,7 +451,7 @@ func (h *Helper) GetTestFileString(src string) string { // 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. +// ./testdata. func (h *Helper) TempCopy(dest, src string) { in := h.GetTestFile(src) defer in.Close() diff --git a/_testdata/analyzer/manifest.json b/testdata/analyzer/manifest.json similarity index 100% rename from _testdata/analyzer/manifest.json rename to testdata/analyzer/manifest.json diff --git a/_testdata/lock/error.json b/testdata/lock/error.json similarity index 100% rename from _testdata/lock/error.json rename to testdata/lock/error.json diff --git a/_testdata/lock/golden.json b/testdata/lock/golden.json similarity index 100% rename from _testdata/lock/golden.json rename to testdata/lock/golden.json diff --git a/_testdata/manifest/error.json b/testdata/manifest/error.json similarity index 100% rename from _testdata/manifest/error.json rename to testdata/manifest/error.json diff --git a/_testdata/manifest/golden.json b/testdata/manifest/golden.json similarity index 100% rename from _testdata/manifest/golden.json rename to testdata/manifest/golden.json diff --git a/_testdata/rootfind/manifest.json b/testdata/rootfind/manifest.json similarity index 100% rename from _testdata/rootfind/manifest.json rename to testdata/rootfind/manifest.json diff --git a/_testdata/rootfind/subdir/.gitkeep b/testdata/rootfind/subdir/.gitkeep similarity index 100% rename from _testdata/rootfind/subdir/.gitkeep rename to testdata/rootfind/subdir/.gitkeep diff --git a/_testdata/txn_writer/expected_lock.json b/testdata/txn_writer/expected_lock.json similarity index 100% rename from _testdata/txn_writer/expected_lock.json rename to testdata/txn_writer/expected_lock.json diff --git a/_testdata/txn_writer/expected_manifest.json b/testdata/txn_writer/expected_manifest.json similarity index 100% rename from _testdata/txn_writer/expected_manifest.json rename to testdata/txn_writer/expected_manifest.json diff --git a/txn_writer_test.go b/txn_writer_test.go index 3f9365cf2d..98d49b0818 100644 --- a/txn_writer_test.go +++ b/txn_writer_test.go @@ -94,12 +94,14 @@ func TestTxnWriter(t *testing.T) { reset() // super basic manifest and lock - expectedManifest := h.GetTestFileString("txn_writer/expected_manifest.json") - expectedLock := h.GetTestFileString("txn_writer/expected_lock.json") + goldenMan := "txn_writer/expected_manifest.json" + goldenLock := "txn_writer/expected_lock.json" + expectedManifest := h.GetTestFileString(goldenMan) + expectedLock := h.GetTestFileString(goldenLock) - m, err := readManifest(h.GetTestFile("txn_writer/expected_manifest.json")) + m, err := readManifest(h.GetTestFile(goldenMan)) h.Must(err) - l, err := readLock(h.GetTestFile("txn_writer/expected_lock.json")) + l, err := readLock(h.GetTestFile(goldenLock)) h.Must(err) // Just write manifest @@ -111,7 +113,14 @@ func TestTxnWriter(t *testing.T) { diskm := h.ReadManifest() if expectedManifest != diskm { - t.Fatalf("expected %s, got %s", expectedManifest, diskm) + if *test.UpdateGolden { + expectedManifest = diskm + if err = h.WriteTestFile(goldenMan, diskm); err != nil { + t.Fatal(err) + } + } else { + t.Fatalf("expected %s, got %s", expectedManifest, diskm) + } } // Manifest and lock, but no vendor @@ -128,7 +137,14 @@ func TestTxnWriter(t *testing.T) { diskl := h.ReadLock() if expectedLock != diskl { - t.Fatalf("expected %s, got %s", expectedLock, diskl) + if *test.UpdateGolden { + expectedLock = diskl + if err = h.WriteTestFile(goldenLock, diskl); err != nil { + t.Fatal(err) + } + } else { + t.Fatalf("expected %s, got %s", expectedLock, diskl) + } } h.Must(sw.WriteAllSafe(true)) From 1b5e697d22bae76375323aa997d5836cb16f6ed5 Mon Sep 17 00:00:00 2001 From: tro3 Date: Fri, 10 Feb 2017 07:39:30 -0800 Subject: [PATCH 09/12] Increasing coverage for lock and analyzer --- .gitignore | 2 + analyzer.go | 2 +- analyzer_test.go | 27 +++++ lock.go | 2 +- lock_test.go | 107 ++++++++++++++++++-- testdata/lock/{error.json => error0.json} | 0 testdata/lock/error1.json | 13 +++ testdata/lock/error2.json | 11 ++ testdata/lock/{golden.json => golden0.json} | 0 testdata/lock/golden1.json | 13 +++ 10 files changed, 166 insertions(+), 11 deletions(-) rename testdata/lock/{error.json => error0.json} (100%) create mode 100644 testdata/lock/error1.json create mode 100644 testdata/lock/error2.json rename testdata/lock/{golden.json => golden0.json} (100%) create mode 100644 testdata/lock/golden1.json diff --git a/.gitignore b/.gitignore index e4236754a4..09dbe079ab 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ /dep /testdep /dep.exe +debug +debug.* diff --git a/analyzer.go b/analyzer.go index 31448ec7aa..cbc77f0382 100644 --- a/analyzer.go +++ b/analyzer.go @@ -37,7 +37,7 @@ func (a analyzer) DeriveManifestAndLock(path string, n gps.ProjectRoot) (gps.Man return m, nil, nil } -func (a analyzer) Info() (name string, version *semver.Version) { +func (a analyzer) Info() (string, *semver.Version) { v, _ := semver.NewVersion("v0.0.1") return "dep", v } diff --git a/analyzer_test.go b/analyzer_test.go index 05f84b70f5..9d4813b675 100644 --- a/analyzer_test.go +++ b/analyzer_test.go @@ -10,9 +10,36 @@ import ( "path/filepath" "testing" + "github.com/Masterminds/semver" "github.com/golang/dep/test" ) +func TestAnalyzerInfo(t *testing.T) { + a := analyzer{} + n, v := a.Info() + if n != "dep" { + t.Errorf("analyzer.Info() returned an incorrect name: '%s' (expected 'dep')", n) + } + expV, err := semver.NewVersion("v0.0.1") + if err != nil { + t.Fatal(err) + } else if v != expV { + t.Fatalf("analyzer.Info() returned an incorrect version: %v (expected %v)", v, expV) + } +} + +func TestAnalyzerErrors(t *testing.T) { + h := test.NewHelper(t) + defer h.Cleanup() + h.TempDir("dep") + + a := analyzer{} + _, _, err := a.DeriveManifestAndLock(h.Path("dep"), "my/fake/project") + if err == nil { + t.Fatal("analyzer.DeriveManifestAndLock with manifest not present should have produced an error") + } +} + func TestDeriveManifestAndLock(t *testing.T) { h := test.NewHelper(t) defer h.Cleanup() diff --git a/lock.go b/lock.go index a727ee45f0..88b2a5dc10 100644 --- a/lock.go +++ b/lock.go @@ -64,7 +64,7 @@ func readLock(r io.Reader) (*Lock, error) { } else if ld.Branch != "" { v = gps.NewBranch(ld.Branch).Is(r) } else if r == "" { - return nil, fmt.Errorf("lock file has entry for %s, but specifies no version", ld.Name) + return nil, fmt.Errorf("lock file has entry for %s, but specifies no branch or version", ld.Name) } id := gps.ProjectIdentifier{ diff --git a/lock_test.go b/lock_test.go index 60e5619b26..ccfc03c6cd 100644 --- a/lock_test.go +++ b/lock_test.go @@ -18,14 +18,7 @@ func TestReadLock(t *testing.T) { h := test.NewHelper(t) defer h.Cleanup() - _, err := readLock(h.GetTestFile("lock/error.json")) - if err == nil { - t.Error("Reading lock with invalid props should have caused error, but did not") - } else if !strings.Contains(err.Error(), "both a branch") { - t.Errorf("Unexpected error %q; expected multiple version error", err) - } - - golden := "lock/golden.json" + golden := "lock/golden0.json" l, err := readLock(h.GetTestFile(golden)) if err != nil { t.Fatalf("Should have read Lock correctly, but got err %q", err) @@ -45,7 +38,28 @@ func TestReadLock(t *testing.T) { if !reflect.DeepEqual(l, l2) { t.Error("Valid lock did not parse as expected") + } + + golden = "lock/golden1.json" + l, err = readLock(h.GetTestFile(golden)) + if err != nil { + t.Fatalf("Should have read Lock correctly, but got err %q", err) + } + + b, _ = hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e") + l2 = &Lock{ + Memo: b, + P: []gps.LockedProject{ + gps.NewLockedProject( + gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/gps")}, + gps.NewVersion("0.12.2").Is(gps.Revision("d05d5aca9f895d19e9265839bffeadd74a2d2ecb")), + []string{"."}, + ), + }, + } + if !reflect.DeepEqual(l, l2) { + t.Error("Valid lock did not parse as expected") } } @@ -53,7 +67,7 @@ func TestWriteLock(t *testing.T) { h := test.NewHelper(t) defer h.Cleanup() - golden := "lock/golden.json" + golden := "lock/golden0.json" lg := h.GetTestFileString(golden) memo, _ := hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e") l := &Lock{ @@ -81,4 +95,79 @@ func TestWriteLock(t *testing.T) { t.Errorf("Valid lock did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", lg, string(b)) } } + + golden = "lock/golden1.json" + lg = h.GetTestFileString(golden) + memo, _ = hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e") + l = &Lock{ + Memo: memo, + P: []gps.LockedProject{ + gps.NewLockedProject( + gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/gps")}, + gps.NewVersion("0.12.2").Is(gps.Revision("d05d5aca9f895d19e9265839bffeadd74a2d2ecb")), + []string{"."}, + ), + }, + } + + b, err = l.MarshalJSON() + if err != nil { + t.Fatalf("Error while marshaling valid lock to JSON: %q", err) + } + + if string(b) != lg { + if *test.UpdateGolden { + if err = h.WriteTestFile(golden, string(b)); err != nil { + t.Fatal(err) + } + } else { + t.Errorf("Valid lock did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", lg, string(b)) + } + } +} + +func TestReadLockErrors(t *testing.T) { + h := test.NewHelper(t) + defer h.Cleanup() + var err error + + tests := []struct { + name string + file string + }{ + {"specified both", "lock/error0.json"}, + {"invalid hash", "lock/error1.json"}, + {"no branch or version", "lock/error2.json"}, + } + + for _, tst := range tests { + _, err = readLock(h.GetTestFile(tst.file)) + if err == nil { + t.Errorf("Reading lock with %s should have caused error, but did not", tst.name) + } else if !strings.Contains(err.Error(), tst.name) { + t.Errorf("Unexpected error %q; expected %s error", err, tst.name) + } + } + + // _, err = readLock(h.GetTestFile("lock/error0.json")) + // if err == nil { + // t.Error("Reading lock with invalid props should have caused error, but did not") + // } else if !strings.Contains(err.Error(), "both a branch") { + // t.Errorf("Unexpected error %q; expected multiple version error", err) + // } + // + // _, err = readLock(h.GetTestFile("lock/error1.json")) + // if err == nil { + // t.Error("Reading lock with invalid hash should have caused error, but did not") + // } else if !strings.Contains(err.Error(), "invalid hash") { + // t.Errorf("Unexpected error %q; expected invalid hash error", err) + // } + // + // _, err = readLock(h.GetTestFile("lock/error2.json")) + // if err == nil { + // t.Error("Reading lock with invalid props should have caused error, but did not") + // } else if !strings.Contains(err.Error(), "no version") { + // t.Errorf("Unexpected error %q; expected no version error", err) + // } + } diff --git a/testdata/lock/error.json b/testdata/lock/error0.json similarity index 100% rename from testdata/lock/error.json rename to testdata/lock/error0.json diff --git a/testdata/lock/error1.json b/testdata/lock/error1.json new file mode 100644 index 0000000000..0f47f0d90a --- /dev/null +++ b/testdata/lock/error1.json @@ -0,0 +1,13 @@ +{ + "memo": "000aaa2a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e", + "projects": [ + { + "name": "github.com/sdboyer/gps", + "branch": "master", + "revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb", + "packages": [ + "." + ] + } + ] +} diff --git a/testdata/lock/error2.json b/testdata/lock/error2.json new file mode 100644 index 0000000000..429f922d7b --- /dev/null +++ b/testdata/lock/error2.json @@ -0,0 +1,11 @@ +{ + "memo": "2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e", + "projects": [ + { + "name": "github.com/sdboyer/gps", + "packages": [ + "." + ] + } + ] +} diff --git a/testdata/lock/golden.json b/testdata/lock/golden0.json similarity index 100% rename from testdata/lock/golden.json rename to testdata/lock/golden0.json diff --git a/testdata/lock/golden1.json b/testdata/lock/golden1.json new file mode 100644 index 0000000000..2eaeadd5ab --- /dev/null +++ b/testdata/lock/golden1.json @@ -0,0 +1,13 @@ +{ + "memo": "2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e", + "projects": [ + { + "name": "github.com/sdboyer/gps", + "version": "0.12.2", + "revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb", + "packages": [ + "." + ] + } + ] +} From 59437df873ba88f6e354074551c866b98e3a181d Mon Sep 17 00:00:00 2001 From: tro3 Date: Fri, 10 Feb 2017 10:08:47 -0800 Subject: [PATCH 10/12] Moving to want/got pattern and adding unit test coverage --- .gitignore | 2 -- analyzer_test.go | 33 ++++++++---------------- context_test.go | 28 ++++++++++----------- fs_test.go | 60 ++++++++++++++++++++++---------------------- lock_test.go | 54 ++++++++++++---------------------------- manifest_test.go | 49 +++++++++++++++++++++++------------- txn_writer_test.go | 62 +++++++++++++++++++++++----------------------- 7 files changed, 134 insertions(+), 154 deletions(-) diff --git a/.gitignore b/.gitignore index 09dbe079ab..e4236754a4 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,3 @@ /dep /testdep /dep.exe -debug -debug.* diff --git a/analyzer_test.go b/analyzer_test.go index 9d4813b675..a3dbf9463c 100644 --- a/analyzer_test.go +++ b/analyzer_test.go @@ -16,27 +16,16 @@ import ( func TestAnalyzerInfo(t *testing.T) { a := analyzer{} - n, v := a.Info() - if n != "dep" { - t.Errorf("analyzer.Info() returned an incorrect name: '%s' (expected 'dep')", n) + gotn, gotv := a.Info() + if gotn != "dep" { + t.Errorf("analyzer.Info() returned an incorrect name: '%s' (expected 'dep')", gotn) } - expV, err := semver.NewVersion("v0.0.1") + wantv, err := semver.NewVersion("v0.0.1") if err != nil { t.Fatal(err) - } else if v != expV { - t.Fatalf("analyzer.Info() returned an incorrect version: %v (expected %v)", v, expV) } -} - -func TestAnalyzerErrors(t *testing.T) { - h := test.NewHelper(t) - defer h.Cleanup() - h.TempDir("dep") - - a := analyzer{} - _, _, err := a.DeriveManifestAndLock(h.Path("dep"), "my/fake/project") - if err == nil { - t.Fatal("analyzer.DeriveManifestAndLock with manifest not present should have produced an error") + if gotv != wantv { + t.Fatalf("analyzer.Info() returned an incorrect version: %v (expected %v)", gotv, wantv) } } @@ -46,7 +35,7 @@ func TestDeriveManifestAndLock(t *testing.T) { h.TempDir("dep") golden := "analyzer/manifest.json" - contents := h.GetTestFileString(golden) + want := h.GetTestFileString(golden) h.TempCopy(filepath.Join("dep", ManifestName), golden) a := analyzer{} @@ -56,18 +45,18 @@ func TestDeriveManifestAndLock(t *testing.T) { t.Fatal(err) } - b, err := m.(*Manifest).MarshalJSON() + got, err := m.(*Manifest).MarshalJSON() if err != nil { t.Fatal(err) } - if contents != string(b) { + if want != string(got) { if *test.UpdateGolden { - if err := h.WriteTestFile(golden, string(b)); err != nil { + if err := h.WriteTestFile(golden, string(got)); err != nil { t.Fatal(err) } } else { - t.Fatalf("expected %s\n got %s", contents, string(b)) + t.Fatalf("expected %s\n got %s", want, string(got)) } } diff --git a/context_test.go b/context_test.go index 773a28fb7c..58bc0e7b4b 100644 --- a/context_test.go +++ b/context_test.go @@ -43,21 +43,21 @@ func TestSplitAbsoluteProjectRoot(t *testing.T) { "my/silly/thing", } - for _, ip := range importPaths { - fullpath := filepath.Join(depCtx.GOPATH, "src", ip) - pr, err := depCtx.SplitAbsoluteProjectRoot(fullpath) + for _, want := range importPaths { + fullpath := filepath.Join(depCtx.GOPATH, "src", want) + got, err := depCtx.SplitAbsoluteProjectRoot(fullpath) if err != nil { t.Fatal(err) } - if pr != ip { - t.Fatalf("expected %s, got %s", ip, pr) + if got != want { + t.Fatalf("expected %s, got %s", want, got) } } // test where it should return error - pr, err := depCtx.SplitAbsoluteProjectRoot("tra/la/la/la") + got, err := depCtx.SplitAbsoluteProjectRoot("tra/la/la/la") if err == nil { - t.Fatalf("should have gotten error but did not for tra/la/la/la: %s", pr) + t.Fatalf("should have gotten error but did not for tra/la/la/la: %s", got) } } @@ -81,12 +81,12 @@ func TestAbsoluteProjectRoot(t *testing.T) { } for i, ok := range importPaths { - pr, err := depCtx.absoluteProjectRoot(i) + got, err := depCtx.absoluteProjectRoot(i) if ok { h.Must(err) - expected := h.Path(filepath.Join("src", i)) - if pr != expected { - t.Fatalf("expected %s, got %q", expected, pr) + want := h.Path(filepath.Join("src", i)) + if got != want { + t.Fatalf("expected %s, got %q", want, got) } continue } @@ -140,11 +140,11 @@ func TestVersionInWorkspace(t *testing.T) { h.RunGit(repoDir, "checkout", info.rev.String()) } - v, err := depCtx.VersionInWorkspace(gps.ProjectRoot(ip)) + got, err := depCtx.VersionInWorkspace(gps.ProjectRoot(ip)) h.Must(err) - if v != info.rev { - t.Fatalf("expected %q, got %q", v.String(), info.rev.String()) + if got != info.rev { + t.Fatalf("expected %q, got %q", got.String(), info.rev.String()) } } } diff --git a/fs_test.go b/fs_test.go index 9745829607..adff7c9e31 100644 --- a/fs_test.go +++ b/fs_test.go @@ -19,7 +19,7 @@ func TestCopyDir(t *testing.T) { defer os.RemoveAll(dir) srcdir := filepath.Join(dir, "src") - if err := os.MkdirAll(srcdir, 0755); err != nil { + if err = os.MkdirAll(srcdir, 0755); err != nil { t.Fatal(err) } @@ -28,14 +28,14 @@ func TestCopyDir(t *testing.T) { t.Fatal(err) } - contents := "hello world" - if _, err := srcf.Write([]byte(contents)); err != nil { + want := "hello world" + if _, err = srcf.Write([]byte(want)); err != nil { t.Fatal(err) } srcf.Close() destdir := filepath.Join(dir, "dest") - if err := CopyDir(srcdir, destdir); err != nil { + if err = CopyDir(srcdir, destdir); err != nil { t.Fatal(err) } @@ -48,27 +48,27 @@ func TestCopyDir(t *testing.T) { } destf := filepath.Join(destdir, "myfile") - destcontents, err := ioutil.ReadFile(destf) + got, err := ioutil.ReadFile(destf) if err != nil { t.Fatal(err) } - if contents != string(destcontents) { - t.Fatalf("expected: %s, got: %s", contents, string(destcontents)) + if want != string(got) { + t.Fatalf("expected: %s, got: %s", want, string(got)) } - srcinfo, err := os.Stat(srcf.Name()) + wantinfo, err := os.Stat(srcf.Name()) if err != nil { t.Fatal(err) } - destinfo, err := os.Stat(destf) + gotinfo, err := os.Stat(destf) if err != nil { t.Fatal(err) } - if srcinfo.Mode() != destinfo.Mode() { - t.Fatalf("expected %s: %#v\n to be the same mode as %s: %#v", srcf.Name(), srcinfo.Mode(), destf, destinfo.Mode()) + if wantinfo.Mode() != gotinfo.Mode() { + t.Fatalf("expected %s: %#v\n to be the same mode as %s: %#v", srcf.Name(), wantinfo.Mode(), destf, gotinfo.Mode()) } } @@ -84,8 +84,8 @@ func TestCopyFile(t *testing.T) { t.Fatal(err) } - contents := "hello world" - if _, err := srcf.Write([]byte(contents)); err != nil { + want := "hello world" + if _, err := srcf.Write([]byte(want)); err != nil { t.Fatal(err) } srcf.Close() @@ -95,27 +95,27 @@ func TestCopyFile(t *testing.T) { t.Fatal(err) } - destcontents, err := ioutil.ReadFile(destf) + got, err := ioutil.ReadFile(destf) if err != nil { t.Fatal(err) } - if contents != string(destcontents) { - t.Fatalf("expected: %s, got: %s", contents, string(destcontents)) + if want != string(got) { + t.Fatalf("expected: %s, got: %s", want, string(got)) } - srcinfo, err := os.Stat(srcf.Name()) + wantinfo, err := os.Stat(srcf.Name()) if err != nil { t.Fatal(err) } - destinfo, err := os.Stat(destf) + gotinfo, err := os.Stat(destf) if err != nil { t.Fatal(err) } - if srcinfo.Mode() != destinfo.Mode() { - t.Fatalf("expected %s: %#v\n to be the same mode as %s: %#v", srcf.Name(), srcinfo.Mode(), destf, destinfo.Mode()) + if wantinfo.Mode() != gotinfo.Mode() { + t.Fatalf("expected %s: %#v\n to be the same mode as %s: %#v", srcf.Name(), wantinfo.Mode(), destf, gotinfo.Mode()) } } @@ -132,10 +132,10 @@ func TestIsRegular(t *testing.T) { filepath.Join(wd, "this_file_does_not_exist.thing"): false, } - for f, expected := range tests { - fileOK, err := IsRegular(f) + for f, want := range tests { + got, err := IsRegular(f) if err != nil { - if !expected { + if !want { // this is the case where we expect an error so continue // to the check below continue @@ -143,8 +143,8 @@ func TestIsRegular(t *testing.T) { t.Fatalf("expected no error, got %v", err) } - if fileOK != expected { - t.Fatalf("expected %t for %s, got %t", expected, f, fileOK) + if got != want { + t.Fatalf("expected %t for %s, got %t", want, f, got) } } @@ -163,10 +163,10 @@ func TestIsDir(t *testing.T) { filepath.Join(wd, "this_file_does_not_exist.thing"): false, } - for f, expected := range tests { - dirOK, err := IsDir(f) + for f, want := range tests { + got, err := IsDir(f) if err != nil { - if !expected { + if !want { // this is the case where we expect an error so continue // to the check below continue @@ -174,8 +174,8 @@ func TestIsDir(t *testing.T) { t.Fatalf("expected no error, got %v", err) } - if dirOK != expected { - t.Fatalf("expected %t for %s, got %t", expected, f, dirOK) + if got != want { + t.Fatalf("expected %t for %s, got %t", want, f, got) } } diff --git a/lock_test.go b/lock_test.go index ccfc03c6cd..ea9318ed82 100644 --- a/lock_test.go +++ b/lock_test.go @@ -19,13 +19,13 @@ func TestReadLock(t *testing.T) { defer h.Cleanup() golden := "lock/golden0.json" - l, err := readLock(h.GetTestFile(golden)) + got, err := readLock(h.GetTestFile(golden)) if err != nil { t.Fatalf("Should have read Lock correctly, but got err %q", err) } b, _ := hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e") - l2 := &Lock{ + want := &Lock{ Memo: b, P: []gps.LockedProject{ gps.NewLockedProject( @@ -36,18 +36,18 @@ func TestReadLock(t *testing.T) { }, } - if !reflect.DeepEqual(l, l2) { + if !reflect.DeepEqual(got, want) { t.Error("Valid lock did not parse as expected") } golden = "lock/golden1.json" - l, err = readLock(h.GetTestFile(golden)) + got, err = readLock(h.GetTestFile(golden)) if err != nil { t.Fatalf("Should have read Lock correctly, but got err %q", err) } b, _ = hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e") - l2 = &Lock{ + want = &Lock{ Memo: b, P: []gps.LockedProject{ gps.NewLockedProject( @@ -58,7 +58,7 @@ func TestReadLock(t *testing.T) { }, } - if !reflect.DeepEqual(l, l2) { + if !reflect.DeepEqual(got, want) { t.Error("Valid lock did not parse as expected") } } @@ -68,7 +68,7 @@ func TestWriteLock(t *testing.T) { defer h.Cleanup() golden := "lock/golden0.json" - lg := h.GetTestFileString(golden) + want := h.GetTestFileString(golden) memo, _ := hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e") l := &Lock{ Memo: memo, @@ -81,23 +81,23 @@ func TestWriteLock(t *testing.T) { }, } - b, err := l.MarshalJSON() + got, err := l.MarshalJSON() if err != nil { t.Fatalf("Error while marshaling valid lock to JSON: %q", err) } - if string(b) != lg { + if string(got) != want { if *test.UpdateGolden { - if err = h.WriteTestFile(golden, string(b)); err != nil { + if err = h.WriteTestFile(golden, string(got)); err != nil { t.Fatal(err) } } else { - t.Errorf("Valid lock did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", lg, string(b)) + t.Errorf("Valid lock did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", string(got), want) } } golden = "lock/golden1.json" - lg = h.GetTestFileString(golden) + want = h.GetTestFileString(golden) memo, _ = hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e") l = &Lock{ Memo: memo, @@ -110,18 +110,18 @@ func TestWriteLock(t *testing.T) { }, } - b, err = l.MarshalJSON() + got, err = l.MarshalJSON() if err != nil { t.Fatalf("Error while marshaling valid lock to JSON: %q", err) } - if string(b) != lg { + if string(got) != want { if *test.UpdateGolden { - if err = h.WriteTestFile(golden, string(b)); err != nil { + if err = h.WriteTestFile(golden, string(got)); err != nil { t.Fatal(err) } } else { - t.Errorf("Valid lock did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", lg, string(b)) + t.Errorf("Valid lock did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", string(got), want) } } } @@ -148,26 +148,4 @@ func TestReadLockErrors(t *testing.T) { t.Errorf("Unexpected error %q; expected %s error", err, tst.name) } } - - // _, err = readLock(h.GetTestFile("lock/error0.json")) - // if err == nil { - // t.Error("Reading lock with invalid props should have caused error, but did not") - // } else if !strings.Contains(err.Error(), "both a branch") { - // t.Errorf("Unexpected error %q; expected multiple version error", err) - // } - // - // _, err = readLock(h.GetTestFile("lock/error1.json")) - // if err == nil { - // t.Error("Reading lock with invalid hash should have caused error, but did not") - // } else if !strings.Contains(err.Error(), "invalid hash") { - // t.Errorf("Unexpected error %q; expected invalid hash error", err) - // } - // - // _, err = readLock(h.GetTestFile("lock/error2.json")) - // if err == nil { - // t.Error("Reading lock with invalid props should have caused error, but did not") - // } else if !strings.Contains(err.Error(), "no version") { - // t.Errorf("Unexpected error %q; expected no version error", err) - // } - } diff --git a/manifest_test.go b/manifest_test.go index 0d945f2e4a..d064c3763c 100644 --- a/manifest_test.go +++ b/manifest_test.go @@ -17,20 +17,13 @@ func TestReadManifest(t *testing.T) { h := test.NewHelper(t) defer h.Cleanup() - _, err := readManifest(h.GetTestFile("manifest/error.json")) - if err == nil { - t.Error("Reading manifest with invalid props should have caused error, but did not") - } else if !strings.Contains(err.Error(), "multiple constraints") { - t.Errorf("Unexpected error %q; expected multiple constraint error", err) - } - - m2, err := readManifest(h.GetTestFile("manifest/golden.json")) + got, err := readManifest(h.GetTestFile("manifest/golden.json")) if err != nil { t.Fatalf("Should have read Manifest correctly, but got err %q", err) } c, _ := gps.NewSemverConstraint(">=0.12.0, <1.0.0") - em := Manifest{ + want := Manifest{ Dependencies: map[gps.ProjectRoot]gps.ProjectProperties{ gps.ProjectRoot("github.com/sdboyer/gps"): { Constraint: c, @@ -48,13 +41,13 @@ func TestReadManifest(t *testing.T) { Ignores: []string{"github.com/foo/bar"}, } - if !reflect.DeepEqual(m2.Dependencies, em.Dependencies) { + if !reflect.DeepEqual(got.Dependencies, want.Dependencies) { t.Error("Valid manifest's dependencies did not parse as expected") } - if !reflect.DeepEqual(m2.Ovr, em.Ovr) { + if !reflect.DeepEqual(got.Ovr, want.Ovr) { t.Error("Valid manifest's overrides did not parse as expected") } - if !reflect.DeepEqual(m2.Ignores, em.Ignores) { + if !reflect.DeepEqual(got.Ignores, want.Ignores) { t.Error("Valid manifest's ignores did not parse as expected") } } @@ -64,7 +57,7 @@ func TestWriteManifest(t *testing.T) { defer h.Cleanup() golden := "manifest/golden.json" - jg := h.GetTestFileString(golden) + want := h.GetTestFileString(golden) c, _ := gps.NewSemverConstraint("^v0.12.0") m := &Manifest{ Dependencies: map[gps.ProjectRoot]gps.ProjectProperties{ @@ -84,18 +77,40 @@ func TestWriteManifest(t *testing.T) { Ignores: []string{"github.com/foo/bar"}, } - b, err := m.MarshalJSON() + got, err := m.MarshalJSON() if err != nil { t.Fatalf("Error while marshaling valid manifest to JSON: %q", err) } - if string(b) != jg { + if string(got) != want { if *test.UpdateGolden { - if err = h.WriteTestFile(golden, string(b)); err != nil { + if err = h.WriteTestFile(golden, string(got)); err != nil { t.Fatal(err) } } else { - t.Errorf("Valid manifest did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", jg, string(b)) + t.Errorf("Valid manifest did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", string(got), want) + } + } +} + +func TestReadManifestErrors(t *testing.T) { + h := test.NewHelper(t) + defer h.Cleanup() + var err error + + tests := []struct { + name string + file string + }{ + {"multiple constraints", "manifest/error.json"}, + } + + for _, tst := range tests { + _, err = readManifest(h.GetTestFile(tst.file)) + if err == nil { + t.Errorf("Reading manifest with %s should have caused error, but did not", tst.name) + } else if !strings.Contains(err.Error(), tst.name) { + t.Errorf("Unexpected error %q; expected %s error", err, tst.name) } } } diff --git a/txn_writer_test.go b/txn_writer_test.go index 98d49b0818..fd994b92ff 100644 --- a/txn_writer_test.go +++ b/txn_writer_test.go @@ -94,14 +94,14 @@ func TestTxnWriter(t *testing.T) { reset() // super basic manifest and lock - goldenMan := "txn_writer/expected_manifest.json" - goldenLock := "txn_writer/expected_lock.json" - expectedManifest := h.GetTestFileString(goldenMan) - expectedLock := h.GetTestFileString(goldenLock) + goldenm := "txn_writer/expected_manifest.json" + goldenl := "txn_writer/expected_lock.json" + wantm := h.GetTestFileString(goldenm) + wantl := h.GetTestFileString(goldenl) - m, err := readManifest(h.GetTestFile(goldenMan)) + m, err := readManifest(h.GetTestFile(goldenm)) h.Must(err) - l, err := readLock(h.GetTestFile(goldenLock)) + l, err := readLock(h.GetTestFile(goldenl)) h.Must(err) // Just write manifest @@ -111,15 +111,15 @@ func TestTxnWriter(t *testing.T) { h.MustNotExist(lpath) h.MustNotExist(vpath) - diskm := h.ReadManifest() - if expectedManifest != diskm { + gotm := h.ReadManifest() + if wantm != gotm { if *test.UpdateGolden { - expectedManifest = diskm - if err = h.WriteTestFile(goldenMan, diskm); err != nil { + wantm = gotm + if err = h.WriteTestFile(goldenm, gotm); err != nil { t.Fatal(err) } } else { - t.Fatalf("expected %s, got %s", expectedManifest, diskm) + t.Fatalf("expected %s, got %s", wantm, gotm) } } @@ -130,20 +130,20 @@ func TestTxnWriter(t *testing.T) { h.MustExist(lpath) h.MustNotExist(vpath) - diskm = h.ReadManifest() - if expectedManifest != diskm { - t.Fatalf("expected %s, got %s", expectedManifest, diskm) + gotm = h.ReadManifest() + if wantm != gotm { + t.Fatalf("expected %s, got %s", wantm, gotm) } - diskl := h.ReadLock() - if expectedLock != diskl { + gotl := h.ReadLock() + if wantl != gotl { if *test.UpdateGolden { - expectedLock = diskl - if err = h.WriteTestFile(goldenLock, diskl); err != nil { + wantl = gotl + if err = h.WriteTestFile(goldenl, gotl); err != nil { t.Fatal(err) } } else { - t.Fatalf("expected %s, got %s", expectedLock, diskl) + t.Fatalf("expected %s, got %s", wantl, gotl) } } @@ -153,14 +153,14 @@ func TestTxnWriter(t *testing.T) { h.MustExist(vpath) h.MustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test")) - diskm = h.ReadManifest() - if expectedManifest != diskm { - t.Fatalf("expected %s, got %s", expectedManifest, diskm) + gotm = h.ReadManifest() + if wantm != gotm { + t.Fatalf("expected %s, got %s", wantm, gotm) } - diskl = h.ReadLock() - if expectedLock != diskl { - t.Fatalf("expected %s, got %s", expectedLock, diskl) + gotl = h.ReadLock() + if wantl != gotl { + t.Fatalf("expected %s, got %s", wantl, gotl) } // start fresh, ignoring the manifest now @@ -185,9 +185,9 @@ func TestTxnWriter(t *testing.T) { h.MustExist(vpath) h.MustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test")) - diskl = h.ReadLock() - if expectedLock != diskl { - t.Fatalf("expected %s, got %s", expectedLock, diskl) + gotl = h.ReadLock() + if wantl != gotl { + t.Fatalf("expected %s, got %s", wantl, gotl) } // repeat op to ensure good behavior when vendor dir already exists @@ -198,9 +198,9 @@ func TestTxnWriter(t *testing.T) { h.MustExist(vpath) h.MustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test")) - diskl = h.ReadLock() - if expectedLock != diskl { - t.Fatalf("expected %s, got %s", expectedLock, diskl) + gotl = h.ReadLock() + if wantl != gotl { + t.Fatalf("expected %s, got %s", wantl, gotl) } // TODO test txn rollback cases. maybe we can force errors with chmodding? From cf250cd3690eba8bb586700b1bc5e65a279b9ad4 Mon Sep 17 00:00:00 2001 From: tro3 Date: Sat, 11 Feb 2017 09:15:39 -0800 Subject: [PATCH 11/12] Remove TestAnalyzerInfo() per feedback --- analyzer_test.go | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/analyzer_test.go b/analyzer_test.go index a3dbf9463c..611889b76f 100644 --- a/analyzer_test.go +++ b/analyzer_test.go @@ -10,25 +10,9 @@ import ( "path/filepath" "testing" - "github.com/Masterminds/semver" "github.com/golang/dep/test" ) -func TestAnalyzerInfo(t *testing.T) { - a := analyzer{} - gotn, gotv := a.Info() - if gotn != "dep" { - t.Errorf("analyzer.Info() returned an incorrect name: '%s' (expected 'dep')", gotn) - } - wantv, err := semver.NewVersion("v0.0.1") - if err != nil { - t.Fatal(err) - } - if gotv != wantv { - t.Fatalf("analyzer.Info() returned an incorrect version: %v (expected %v)", gotv, wantv) - } -} - func TestDeriveManifestAndLock(t *testing.T) { h := test.NewHelper(t) defer h.Cleanup() From 24b9e3181a64d12872189dce8131cc7ccc9c2f43 Mon Sep 17 00:00:00 2001 From: tro3 Date: Sat, 11 Feb 2017 09:22:25 -0800 Subject: [PATCH 12/12] Fix test issue after upstream merge --- fs_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs_test.go b/fs_test.go index e5ea04bd34..a5c1317275 100644 --- a/fs_test.go +++ b/fs_test.go @@ -193,7 +193,7 @@ func TestIsEmpty(t *testing.T) { h.TempDir("empty") tests := map[string]string{ wd: "true", - "_testdata": "true", + "testdata": "true", filepath.Join(wd, "fs.go"): "err", filepath.Join(wd, "this_file_does_not_exist.thing"): "false", h.Path("empty"): "false",