Adding -update test flag for golden files

This commit is contained in:
tro3 2017-02-08 18:37:31 -08:00 коммит произвёл sam boyer
Родитель 00de8c9707
Коммит 38bce0cc56
16 изменённых файлов: 71 добавлений и 23 удалений

Просмотреть файл

@ -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 {

Просмотреть файл

@ -129,7 +129,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,
}
@ -160,7 +160,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,
}

Просмотреть файл

@ -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))
}
}
}

Просмотреть файл

@ -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))
}
}
}

Просмотреть файл

@ -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)

Просмотреть файл

@ -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()

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

@ -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))