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

Commit a15e2c2

Browse files
author
tro3
committed
Converting analyzer_test.go to golden-file pattern
1 parent d322aba commit a15e2c2

File tree

5 files changed

+30
-22
lines changed

5 files changed

+30
-22
lines changed

_testdata/analyzer/manifest.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"dependencies": {
3+
"github.com/pkg/errors": {
4+
"version": ">=0.8.0, <1.0.0"
5+
},
6+
"github.com/sdboyer/gps": {
7+
"version": ">=0.12.0, <1.0.0"
8+
}
9+
}
10+
}

analyzer_test.go

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,21 @@ import (
99
"os"
1010
"path/filepath"
1111
"testing"
12+
13+
"github.com/golang/dep/test"
1214
)
1315

1416
func TestDeriveManifestAndLock(t *testing.T) {
15-
dir, err := ioutil.TempDir("", "dep")
16-
if err != nil {
17-
t.Fatal(err)
18-
}
19-
defer os.RemoveAll(dir)
17+
h := test.NewHelper(t)
18+
defer h.Cleanup()
2019

21-
contents := `{
22-
"dependencies": {
23-
"github.com/pkg/errors": {
24-
"version": ">=0.8.0, <1.0.0"
25-
},
26-
"github.com/sdboyer/gps": {
27-
"version": ">=0.12.0, <1.0.0"
28-
}
29-
}
30-
}
31-
`
32-
33-
if err := ioutil.WriteFile(filepath.Join(dir, ManifestName), []byte(contents), 0644); err != nil {
34-
t.Fatal(err)
35-
}
20+
h.TempDir("dep")
21+
contents := h.GetTestFileString("analyzer/manifest.json")
22+
h.TempCopy(filepath.Join("dep", ManifestName), "analyzer/manifest.json")
3623

3724
a := analyzer{}
3825

39-
m, l, err := a.DeriveManifestAndLock(dir, "my/fake/project")
26+
m, l, err := a.DeriveManifestAndLock(h.Path("dep"), "my/fake/project")
4027
if err != nil {
4128
t.Fatal(err)
4229
}
@@ -46,7 +33,8 @@ func TestDeriveManifestAndLock(t *testing.T) {
4633
t.Fatal(err)
4734
}
4835

49-
if (string(b)) != contents {
36+
if exp, err := test.AreEqualJSON(contents, string(b)); !exp {
37+
h.Must(err)
5038
t.Fatalf("expected %s\n got %s", contents, string(b))
5139
}
5240

lock_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616

1717
func TestReadLock(t *testing.T) {
1818
h := test.NewHelper(t)
19+
defer h.Cleanup()
20+
1921
_, err := readLock(h.GetTestFileReader("lock/error.json"))
2022
if err == nil {
2123
t.Error("Reading lock with invalid props should have caused error, but did not")
@@ -47,6 +49,8 @@ func TestReadLock(t *testing.T) {
4749

4850
func TestWriteLock(t *testing.T) {
4951
h := test.NewHelper(t)
52+
defer h.Cleanup()
53+
5054
lg := h.GetTestFileString("lock/golden.json")
5155
memo, _ := hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e")
5256
l := &Lock{

manifest_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515

1616
func TestReadManifest(t *testing.T) {
1717
h := test.NewHelper(t)
18+
defer h.Cleanup()
19+
1820
_, err := readManifest(h.GetTestFileReader("manifest/error.json"))
1921
if err == nil {
2022
t.Error("Reading manifest with invalid props should have caused error, but did not")
@@ -59,6 +61,8 @@ func TestReadManifest(t *testing.T) {
5961

6062
func TestWriteManifest(t *testing.T) {
6163
h := test.NewHelper(t)
64+
defer h.Cleanup()
65+
6266
jg := h.GetTestFileString("manifest/golden.json")
6367
c, _ := gps.NewSemverConstraint("^v0.12.0")
6468
m := &Manifest{

txn_writer_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ func TestTxnWriter(t *testing.T) {
6060
test.NeedsGit(t)
6161

6262
h := test.NewHelper(t)
63+
defer h.Cleanup()
64+
6365
h.TempDir("")
6466
defer h.Cleanup()
6567

0 commit comments

Comments
 (0)