Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 597afe1

Browse files
committed
fixup! internal/cmd/update_test.go: refactor unit tests
1 parent 972847e commit 597afe1

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

internal/cmd/update_test.go

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
func Test_updater_run(t *testing.T) {
2828
t.Parallel()
2929

30+
// params holds parameters for each test case
3031
type params struct {
3132
ConfirmF func(string) (string, error)
3233
Ctx context.Context
@@ -36,6 +37,7 @@ func Test_updater_run(t *testing.T) {
3637
VersionF func() string
3738
}
3839

40+
// fromParams creates a new updater from params
3941
fromParams := func(p *params) *updater {
4042
return &updater{
4143
confirmF: p.ConfirmF,
@@ -73,21 +75,22 @@ func Test_updater_run(t *testing.T) {
7375
}
7476

7577
run(t, "update coder - noop", func(t *testing.T, p *params) {
76-
fakeFile(p.Fakefs, fakeExePath, 0755, fakeNewVersion)
78+
fakeFile(t, p.Fakefs, fakeExePath, 0755, fakeNewVersion)
7779
p.HttpClient.M[fakeCoderURL+"/api"] = newFakeGetterResponse([]byte{}, 401, variadicS("coder-version: "+fakeNewVersion), nil)
7880
p.VersionF = func() string { return fakeNewVersion }
7981
u := fromParams(p)
82+
assertFileContent(t, p.Fakefs, fakeExePath, fakeNewVersion)
8083
err := u.Run(p.Ctx, false, fakeCoderURL)
8184
assert.Success(t, "update coder - noop", err)
8285
assertFileContent(t, p.Fakefs, fakeExePath, fakeNewVersion)
8386
})
8487

8588
run(t, "update coder - old to new", func(t *testing.T, p *params) {
86-
fakeFile(p.Fakefs, fakeExePath, 0755, fakeOldVersion)
89+
fakeFile(t, p.Fakefs, fakeExePath, 0755, fakeOldVersion)
8790
p.HttpClient.M[fakeCoderURL+"/api"] = newFakeGetterResponse([]byte{}, 401, variadicS("coder-version: "+fakeNewVersion), nil)
8891
p.HttpClient.M[fakeReleaseURL] = newFakeGetterResponse(fakeValidTgzBytes, 200, variadicS(), nil)
8992
p.VersionF = func() string { return fakeOldVersion }
90-
p.ConfirmF = func(string) (string, error) { return "", nil }
93+
p.ConfirmF = fakeConfirmYes
9194
u := fromParams(p)
9295
assertFileContent(t, p.Fakefs, fakeExePath, fakeOldVersion)
9396
err := u.Run(p.Ctx, false, fakeCoderURL)
@@ -96,22 +99,22 @@ func Test_updater_run(t *testing.T) {
9699
})
97100

98101
run(t, "update coder - old to new forced", func(t *testing.T, p *params) {
99-
fakeFile(p.Fakefs, fakeExePath, 0755, fakeOldVersion)
102+
fakeFile(t, p.Fakefs, fakeExePath, 0755, fakeOldVersion)
100103
p.HttpClient.M[fakeCoderURL+"/api"] = newFakeGetterResponse([]byte{}, 401, variadicS("coder-version: "+fakeNewVersion), nil)
101104
p.HttpClient.M[fakeReleaseURL] = newFakeGetterResponse(fakeValidTgzBytes, 200, variadicS(), nil)
102105
p.VersionF = func() string { return fakeOldVersion }
103106
u := fromParams(p)
104107
assertFileContent(t, p.Fakefs, fakeExePath, fakeOldVersion)
105108
err := u.Run(p.Ctx, true, fakeCoderURL)
106-
assert.Success(t, "update coder - old to new", err)
109+
assert.Success(t, "update coder - old to new forced", err)
107110
assertFileContent(t, p.Fakefs, fakeExePath, fakeNewVersion)
108111
})
109112

110113
run(t, "update coder - user cancelled", func(t *testing.T, p *params) {
111-
fakeFile(p.Fakefs, fakeExePath, 0755, fakeOldVersion)
114+
fakeFile(t, p.Fakefs, fakeExePath, 0755, fakeOldVersion)
112115
p.HttpClient.M[fakeCoderURL+"/api"] = newFakeGetterResponse([]byte{}, 401, variadicS("coder-version: "+fakeNewVersion), nil)
113116
p.VersionF = func() string { return fakeOldVersion }
114-
p.ConfirmF = func(string) (string, error) { return "", promptui.ErrAbort }
117+
p.ConfirmF = fakeConfirmNo
115118
u := fromParams(p)
116119
assertFileContent(t, p.Fakefs, fakeExePath, fakeOldVersion)
117120
err := u.Run(p.Ctx, false, fakeCoderURL)
@@ -120,6 +123,7 @@ func Test_updater_run(t *testing.T) {
120123
})
121124
}
122125

126+
// fakeGetter mocks HTTP requests
123127
type fakeGetter struct {
124128
M map[string]*fakeGetterResponse
125129
T *testing.T
@@ -131,6 +135,7 @@ func newFakeGetter(t *testing.T) *fakeGetter {
131135
}
132136
}
133137

138+
// Get returns the configured response for url. If no response configured, test fails immediately.
134139
func (f *fakeGetter) Get(url string) (*http.Response, error) {
135140
val, ok := f.M[url]
136141
if !ok {
@@ -146,6 +151,7 @@ type fakeGetterResponse struct {
146151
Err error
147152
}
148153

154+
// newFakeGetterResponse is a convenience function for mocking HTTP requests
149155
func newFakeGetterResponse(body []byte, code int, headers []string, err error) *fakeGetterResponse {
150156
resp := &http.Response{}
151157
resp.Body = ioutil.NopCloser(bytes.NewReader(body))
@@ -169,10 +175,6 @@ func variadicS(s ...string) []string {
169175
return s
170176
}
171177

172-
// func (f *fakeGetter) Get(url string) (*http.Response, error) {
173-
// return f.GetF(url)
174-
// }
175-
176178
func fakeConfirmYes(_ string) (string, error) {
177179
return "y", nil
178180
}
@@ -181,24 +183,9 @@ func fakeConfirmNo(_ string) (string, error) {
181183
return "", promptui.ErrAbort
182184
}
183185

184-
func fakeResponse(body []byte, code int, headers ...string) *http.Response {
185-
resp := &http.Response{}
186-
resp.Body = ioutil.NopCloser(bytes.NewReader(body))
187-
resp.StatusCode = code
188-
resp.Header = http.Header{}
189-
190-
for _, e := range headers {
191-
parts := strings.Split(e, ":")
192-
k := strings.ToLower(strings.TrimSpace(parts[0]))
193-
v := strings.ToLower(strings.TrimSpace(strings.Join(parts[1:], ":")))
194-
resp.Header.Set(k, v)
195-
}
196-
197-
return resp
198-
}
199-
200186
//nolint:unparam
201-
func fakeFile(fs afero.Fs, name string, perm fs.FileMode, content string) {
187+
func fakeFile(t *testing.T, fs afero.Fs, name string, perm fs.FileMode, content string) {
188+
t.Helper()
202189
f, err := fs.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, perm)
203190
if err != nil {
204191
panic(err)
@@ -213,6 +200,7 @@ func fakeFile(fs afero.Fs, name string, perm fs.FileMode, content string) {
213200

214201
//nolint:unparam
215202
func assertFileContent(t *testing.T, fs afero.Fs, name string, content string) {
203+
t.Helper()
216204
f, err := fs.OpenFile(name, os.O_RDONLY, 0)
217205
assert.Success(t, "open file "+name, err)
218206
defer f.Close()

0 commit comments

Comments
 (0)