Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

"Golden file" testing pattern #212

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 9 additions & 55 deletions cmd/dep/ensure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package main

import (
"strings"
"testing"

"github.com/golang/dep/test"
Expand All @@ -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/[email protected]")

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)
}
}
Expand Down
29 changes: 29 additions & 0 deletions cmd/dep/testdata/ensure_test/exp_lock1.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
]
}
7 changes: 7 additions & 0 deletions cmd/dep/testdata/ensure_test/exp_manifest1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"overrides": {
"github.com/Sirupsen/logrus": {
"version": "0.11.0"
}
}
}
12 changes: 12 additions & 0 deletions cmd/dep/testdata/ensure_test/source1.go
Original file line number Diff line number Diff line change
@@ -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")
}
34 changes: 33 additions & 1 deletion test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Helper struct {
t *testing.T
temps []string
wd string
origWd string
env []string
tempdir string
ran bool
Expand All @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
22 changes: 22 additions & 0 deletions test/util.go
Original file line number Diff line number Diff line change
@@ -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
}