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

Moving to golden-file pattern for unit tests #215

Merged
merged 14 commits into from
Feb 13, 2017
Merged
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
2 changes: 1 addition & 1 deletion analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
42 changes: 18 additions & 24 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,39 @@ 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")
golden := "analyzer/manifest.json"
want := h.GetTestFileString(golden)
h.TempCopy(filepath.Join("dep", ManifestName), golden)

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

b, err := m.(*Manifest).MarshalJSON()
got, err := m.(*Manifest).MarshalJSON()
if err != nil {
t.Fatal(err)
}

if (string(b)) != contents {
t.Fatalf("expected %s\n got %s", contents, string(b))
if want != string(got) {
if *test.UpdateGolden {
if err := h.WriteTestFile(golden, string(got)); err != nil {
t.Fatal(err)
}
} else {
t.Fatalf("expected %s\n got %s", want, string(got))
}
}

if l != nil {
Expand Down
28 changes: 14 additions & 14 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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())
}
}
}
Expand Down
66 changes: 33 additions & 33 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,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)
}

Expand All @@ -30,14 +30,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)
}

Expand All @@ -50,27 +50,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())
}
}

Expand All @@ -86,8 +86,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()
Expand All @@ -97,27 +97,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())
}
}

Expand All @@ -129,24 +129,24 @@ 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,
}

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

Expand All @@ -160,24 +160,24 @@ 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,
}

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

Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Loading