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

Commit 05f1803

Browse files
author
tro3
committed
Moving to want/got pattern and adding unit test coverage
1 parent 1b5e697 commit 05f1803

File tree

6 files changed

+137
-155
lines changed

6 files changed

+137
-155
lines changed

analyzer_test.go

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,16 @@ import (
1616

1717
func TestAnalyzerInfo(t *testing.T) {
1818
a := analyzer{}
19-
n, v := a.Info()
20-
if n != "dep" {
21-
t.Errorf("analyzer.Info() returned an incorrect name: '%s' (expected 'dep')", n)
19+
gotn, gotv := a.Info()
20+
if gotn != "dep" {
21+
t.Errorf("analyzer.Info() returned an incorrect name: '%s' (expected 'dep')", gotn)
2222
}
23-
expV, err := semver.NewVersion("v0.0.1")
23+
wantv, err := semver.NewVersion("v0.0.1")
2424
if err != nil {
2525
t.Fatal(err)
26-
} else if v != expV {
27-
t.Fatalf("analyzer.Info() returned an incorrect version: %v (expected %v)", v, expV)
2826
}
29-
}
30-
31-
func TestAnalyzerErrors(t *testing.T) {
32-
h := test.NewHelper(t)
33-
defer h.Cleanup()
34-
h.TempDir("dep")
35-
36-
a := analyzer{}
37-
_, _, err := a.DeriveManifestAndLock(h.Path("dep"), "my/fake/project")
38-
if err == nil {
39-
t.Fatal("analyzer.DeriveManifestAndLock with manifest not present should have produced an error")
27+
if gotv != wantv {
28+
t.Fatalf("analyzer.Info() returned an incorrect version: %v (expected %v)", gotv, wantv)
4029
}
4130
}
4231

@@ -46,7 +35,7 @@ func TestDeriveManifestAndLock(t *testing.T) {
4635

4736
h.TempDir("dep")
4837
golden := "analyzer/manifest.json"
49-
contents := h.GetTestFileString(golden)
38+
want := h.GetTestFileString(golden)
5039
h.TempCopy(filepath.Join("dep", ManifestName), golden)
5140

5241
a := analyzer{}
@@ -56,18 +45,18 @@ func TestDeriveManifestAndLock(t *testing.T) {
5645
t.Fatal(err)
5746
}
5847

59-
b, err := m.(*Manifest).MarshalJSON()
48+
got, err := m.(*Manifest).MarshalJSON()
6049
if err != nil {
6150
t.Fatal(err)
6251
}
6352

64-
if contents != string(b) {
53+
if want != string(got) {
6554
if *test.UpdateGolden {
66-
if err := h.WriteTestFile(golden, string(b)); err != nil {
55+
if err := h.WriteTestFile(golden, string(got)); err != nil {
6756
t.Fatal(err)
6857
}
6958
} else {
70-
t.Fatalf("expected %s\n got %s", contents, string(b))
59+
t.Fatalf("expected %s\n got %s", want, string(got))
7160
}
7261
}
7362

context_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,21 @@ func TestSplitAbsoluteProjectRoot(t *testing.T) {
4343
"my/silly/thing",
4444
}
4545

46-
for _, ip := range importPaths {
47-
fullpath := filepath.Join(depCtx.GOPATH, "src", ip)
48-
pr, err := depCtx.SplitAbsoluteProjectRoot(fullpath)
46+
for _, want := range importPaths {
47+
fullpath := filepath.Join(depCtx.GOPATH, "src", want)
48+
got, err := depCtx.SplitAbsoluteProjectRoot(fullpath)
4949
if err != nil {
5050
t.Fatal(err)
5151
}
52-
if pr != ip {
53-
t.Fatalf("expected %s, got %s", ip, pr)
52+
if got != want {
53+
t.Fatalf("expected %s, got %s", want, got)
5454
}
5555
}
5656

5757
// test where it should return error
58-
pr, err := depCtx.SplitAbsoluteProjectRoot("tra/la/la/la")
58+
got, err := depCtx.SplitAbsoluteProjectRoot("tra/la/la/la")
5959
if err == nil {
60-
t.Fatalf("should have gotten error but did not for tra/la/la/la: %s", pr)
60+
t.Fatalf("should have gotten error but did not for tra/la/la/la: %s", got)
6161
}
6262
}
6363

@@ -81,12 +81,12 @@ func TestAbsoluteProjectRoot(t *testing.T) {
8181
}
8282

8383
for i, ok := range importPaths {
84-
pr, err := depCtx.absoluteProjectRoot(i)
84+
got, err := depCtx.absoluteProjectRoot(i)
8585
if ok {
8686
h.Must(err)
87-
expected := h.Path(filepath.Join("src", i))
88-
if pr != expected {
89-
t.Fatalf("expected %s, got %q", expected, pr)
87+
want := h.Path(filepath.Join("src", i))
88+
if got != want {
89+
t.Fatalf("expected %s, got %q", want, got)
9090
}
9191
continue
9292
}
@@ -140,11 +140,11 @@ func TestVersionInWorkspace(t *testing.T) {
140140
h.RunGit(repoDir, "checkout", info.rev.String())
141141
}
142142

143-
v, err := depCtx.VersionInWorkspace(gps.ProjectRoot(ip))
143+
got, err := depCtx.VersionInWorkspace(gps.ProjectRoot(ip))
144144
h.Must(err)
145145

146-
if v != info.rev {
147-
t.Fatalf("expected %q, got %q", v.String(), info.rev.String())
146+
if got != info.rev {
147+
t.Fatalf("expected %q, got %q", got.String(), info.rev.String())
148148
}
149149
}
150150
}

fs_test.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestCopyDir(t *testing.T) {
1919
defer os.RemoveAll(dir)
2020

2121
srcdir := filepath.Join(dir, "src")
22-
if err := os.MkdirAll(srcdir, 0755); err != nil {
22+
if err = os.MkdirAll(srcdir, 0755); err != nil {
2323
t.Fatal(err)
2424
}
2525

@@ -28,14 +28,14 @@ func TestCopyDir(t *testing.T) {
2828
t.Fatal(err)
2929
}
3030

31-
contents := "hello world"
32-
if _, err := srcf.Write([]byte(contents)); err != nil {
31+
want := "hello world"
32+
if _, err = srcf.Write([]byte(want)); err != nil {
3333
t.Fatal(err)
3434
}
3535
srcf.Close()
3636

3737
destdir := filepath.Join(dir, "dest")
38-
if err := CopyDir(srcdir, destdir); err != nil {
38+
if err = CopyDir(srcdir, destdir); err != nil {
3939
t.Fatal(err)
4040
}
4141

@@ -44,31 +44,31 @@ func TestCopyDir(t *testing.T) {
4444
t.Fatal(err)
4545
}
4646
if !dirOK {
47-
t.Fatalf("expected %s to be a directory", destdir)
47+
t.Fatalf("want %s to be a directory", destdir)
4848
}
4949

5050
destf := filepath.Join(destdir, "myfile")
51-
destcontents, err := ioutil.ReadFile(destf)
51+
got, err := ioutil.ReadFile(destf)
5252
if err != nil {
5353
t.Fatal(err)
5454
}
5555

56-
if contents != string(destcontents) {
57-
t.Fatalf("expected: %s, got: %s", contents, string(destcontents))
56+
if want != string(got) {
57+
t.Fatalf("want: %s, got: %s", want, string(got))
5858
}
5959

60-
srcinfo, err := os.Stat(srcf.Name())
60+
wantinfo, err := os.Stat(srcf.Name())
6161
if err != nil {
6262
t.Fatal(err)
6363
}
6464

65-
destinfo, err := os.Stat(destf)
65+
gotinfo, err := os.Stat(destf)
6666
if err != nil {
6767
t.Fatal(err)
6868
}
6969

70-
if srcinfo.Mode() != destinfo.Mode() {
71-
t.Fatalf("expected %s: %#v\n to be the same mode as %s: %#v", srcf.Name(), srcinfo.Mode(), destf, destinfo.Mode())
70+
if wantinfo.Mode() != gotinfo.Mode() {
71+
t.Fatalf("want %s: %#v\n to be the same mode as %s: %#v", srcf.Name(), wantinfo.Mode(), destf, gotinfo.Mode())
7272
}
7373
}
7474

@@ -84,8 +84,8 @@ func TestCopyFile(t *testing.T) {
8484
t.Fatal(err)
8585
}
8686

87-
contents := "hello world"
88-
if _, err := srcf.Write([]byte(contents)); err != nil {
87+
want := "hello world"
88+
if _, err := srcf.Write([]byte(want)); err != nil {
8989
t.Fatal(err)
9090
}
9191
srcf.Close()
@@ -95,27 +95,27 @@ func TestCopyFile(t *testing.T) {
9595
t.Fatal(err)
9696
}
9797

98-
destcontents, err := ioutil.ReadFile(destf)
98+
got, err := ioutil.ReadFile(destf)
9999
if err != nil {
100100
t.Fatal(err)
101101
}
102102

103-
if contents != string(destcontents) {
104-
t.Fatalf("expected: %s, got: %s", contents, string(destcontents))
103+
if want != string(got) {
104+
t.Fatalf("want: %s, got: %s", want, string(got))
105105
}
106106

107-
srcinfo, err := os.Stat(srcf.Name())
107+
wantinfo, err := os.Stat(srcf.Name())
108108
if err != nil {
109109
t.Fatal(err)
110110
}
111111

112-
destinfo, err := os.Stat(destf)
112+
gotinfo, err := os.Stat(destf)
113113
if err != nil {
114114
t.Fatal(err)
115115
}
116116

117-
if srcinfo.Mode() != destinfo.Mode() {
118-
t.Fatalf("expected %s: %#v\n to be the same mode as %s: %#v", srcf.Name(), srcinfo.Mode(), destf, destinfo.Mode())
117+
if wantinfo.Mode() != gotinfo.Mode() {
118+
t.Fatalf("want %s: %#v\n to be the same mode as %s: %#v", srcf.Name(), wantinfo.Mode(), destf, gotinfo.Mode())
119119
}
120120
}
121121

@@ -132,19 +132,19 @@ func TestIsRegular(t *testing.T) {
132132
filepath.Join(wd, "this_file_does_not_exist.thing"): false,
133133
}
134134

135-
for f, expected := range tests {
136-
fileOK, err := IsRegular(f)
135+
for f, want := range tests {
136+
got, err := IsRegular(f)
137137
if err != nil {
138-
if !expected {
138+
if !want {
139139
// this is the case where we expect an error so continue
140140
// to the check below
141141
continue
142142
}
143-
t.Fatalf("expected no error, got %v", err)
143+
t.Fatalf("want no error, got %v", err)
144144
}
145145

146-
if fileOK != expected {
147-
t.Fatalf("expected %t for %s, got %t", expected, f, fileOK)
146+
if got != want {
147+
t.Fatalf("want %t for %s, got %t", want, f, got)
148148
}
149149
}
150150

@@ -163,19 +163,19 @@ func TestIsDir(t *testing.T) {
163163
filepath.Join(wd, "this_file_does_not_exist.thing"): false,
164164
}
165165

166-
for f, expected := range tests {
167-
dirOK, err := IsDir(f)
166+
for f, want := range tests {
167+
got, err := IsDir(f)
168168
if err != nil {
169-
if !expected {
169+
if !want {
170170
// this is the case where we expect an error so continue
171171
// to the check below
172172
continue
173173
}
174-
t.Fatalf("expected no error, got %v", err)
174+
t.Fatalf("want no error, got %v", err)
175175
}
176176

177-
if dirOK != expected {
178-
t.Fatalf("expected %t for %s, got %t", expected, f, dirOK)
177+
if got != want {
178+
t.Fatalf("want %t for %s, got %t", want, f, got)
179179
}
180180
}
181181

0 commit comments

Comments
 (0)