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

Commit 0c16a76

Browse files
tro3sdboyer
tro3
authored andcommitted
Moving to want/got pattern and adding unit test coverage
1 parent fceb52f commit 0c16a76

7 files changed

+134
-154
lines changed

.gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,3 @@
55
/dep
66
/testdep
77
/dep.exe
8-
debug
9-
debug.*

analyzer_test.go

+11-22
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

+14-14
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

+30-30
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestCopyDir(t *testing.T) {
2121
defer os.RemoveAll(dir)
2222

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

@@ -30,14 +30,14 @@ func TestCopyDir(t *testing.T) {
3030
t.Fatal(err)
3131
}
3232

33-
contents := "hello world"
34-
if _, err := srcf.Write([]byte(contents)); err != nil {
33+
want := "hello world"
34+
if _, err = srcf.Write([]byte(want)); err != nil {
3535
t.Fatal(err)
3636
}
3737
srcf.Close()
3838

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

@@ -50,27 +50,27 @@ func TestCopyDir(t *testing.T) {
5050
}
5151

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

58-
if contents != string(destcontents) {
59-
t.Fatalf("expected: %s, got: %s", contents, string(destcontents))
58+
if want != string(got) {
59+
t.Fatalf("expected: %s, got: %s", want, string(got))
6060
}
6161

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

67-
destinfo, err := os.Stat(destf)
67+
gotinfo, err := os.Stat(destf)
6868
if err != nil {
6969
t.Fatal(err)
7070
}
7171

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

@@ -86,8 +86,8 @@ func TestCopyFile(t *testing.T) {
8686
t.Fatal(err)
8787
}
8888

89-
contents := "hello world"
90-
if _, err := srcf.Write([]byte(contents)); err != nil {
89+
want := "hello world"
90+
if _, err := srcf.Write([]byte(want)); err != nil {
9191
t.Fatal(err)
9292
}
9393
srcf.Close()
@@ -97,27 +97,27 @@ func TestCopyFile(t *testing.T) {
9797
t.Fatal(err)
9898
}
9999

100-
destcontents, err := ioutil.ReadFile(destf)
100+
got, err := ioutil.ReadFile(destf)
101101
if err != nil {
102102
t.Fatal(err)
103103
}
104104

105-
if contents != string(destcontents) {
106-
t.Fatalf("expected: %s, got: %s", contents, string(destcontents))
105+
if want != string(got) {
106+
t.Fatalf("expected: %s, got: %s", want, string(got))
107107
}
108108

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

114-
destinfo, err := os.Stat(destf)
114+
gotinfo, err := os.Stat(destf)
115115
if err != nil {
116116
t.Fatal(err)
117117
}
118118

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

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

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

148-
if fileOK != expected {
149-
t.Fatalf("expected %t for %s, got %t", expected, f, fileOK)
148+
if got != want {
149+
t.Fatalf("expected %t for %s, got %t", want, f, got)
150150
}
151151
}
152152

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

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

179-
if dirOK != expected {
180-
t.Fatalf("expected %t for %s, got %t", expected, f, dirOK)
179+
if got != want {
180+
t.Fatalf("expected %t for %s, got %t", want, f, got)
181181
}
182182
}
183183

0 commit comments

Comments
 (0)