@@ -27,6 +27,7 @@ const (
27
27
func Test_updater_run (t * testing.T ) {
28
28
t .Parallel ()
29
29
30
+ // params holds parameters for each test case
30
31
type params struct {
31
32
ConfirmF func (string ) (string , error )
32
33
Ctx context.Context
@@ -36,6 +37,7 @@ func Test_updater_run(t *testing.T) {
36
37
VersionF func () string
37
38
}
38
39
40
+ // fromParams creates a new updater from params
39
41
fromParams := func (p * params ) * updater {
40
42
return & updater {
41
43
confirmF : p .ConfirmF ,
@@ -73,21 +75,22 @@ func Test_updater_run(t *testing.T) {
73
75
}
74
76
75
77
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 )
77
79
p .HttpClient .M [fakeCoderURL + "/api" ] = newFakeGetterResponse ([]byte {}, 401 , variadicS ("coder-version: " + fakeNewVersion ), nil )
78
80
p .VersionF = func () string { return fakeNewVersion }
79
81
u := fromParams (p )
82
+ assertFileContent (t , p .Fakefs , fakeExePath , fakeNewVersion )
80
83
err := u .Run (p .Ctx , false , fakeCoderURL )
81
84
assert .Success (t , "update coder - noop" , err )
82
85
assertFileContent (t , p .Fakefs , fakeExePath , fakeNewVersion )
83
86
})
84
87
85
88
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 )
87
90
p .HttpClient .M [fakeCoderURL + "/api" ] = newFakeGetterResponse ([]byte {}, 401 , variadicS ("coder-version: " + fakeNewVersion ), nil )
88
91
p .HttpClient .M [fakeReleaseURL ] = newFakeGetterResponse (fakeValidTgzBytes , 200 , variadicS (), nil )
89
92
p .VersionF = func () string { return fakeOldVersion }
90
- p .ConfirmF = func ( string ) ( string , error ) { return "" , nil }
93
+ p .ConfirmF = fakeConfirmYes
91
94
u := fromParams (p )
92
95
assertFileContent (t , p .Fakefs , fakeExePath , fakeOldVersion )
93
96
err := u .Run (p .Ctx , false , fakeCoderURL )
@@ -96,22 +99,22 @@ func Test_updater_run(t *testing.T) {
96
99
})
97
100
98
101
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 )
100
103
p .HttpClient .M [fakeCoderURL + "/api" ] = newFakeGetterResponse ([]byte {}, 401 , variadicS ("coder-version: " + fakeNewVersion ), nil )
101
104
p .HttpClient .M [fakeReleaseURL ] = newFakeGetterResponse (fakeValidTgzBytes , 200 , variadicS (), nil )
102
105
p .VersionF = func () string { return fakeOldVersion }
103
106
u := fromParams (p )
104
107
assertFileContent (t , p .Fakefs , fakeExePath , fakeOldVersion )
105
108
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 )
107
110
assertFileContent (t , p .Fakefs , fakeExePath , fakeNewVersion )
108
111
})
109
112
110
113
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 )
112
115
p .HttpClient .M [fakeCoderURL + "/api" ] = newFakeGetterResponse ([]byte {}, 401 , variadicS ("coder-version: " + fakeNewVersion ), nil )
113
116
p .VersionF = func () string { return fakeOldVersion }
114
- p .ConfirmF = func ( string ) ( string , error ) { return "" , promptui . ErrAbort }
117
+ p .ConfirmF = fakeConfirmNo
115
118
u := fromParams (p )
116
119
assertFileContent (t , p .Fakefs , fakeExePath , fakeOldVersion )
117
120
err := u .Run (p .Ctx , false , fakeCoderURL )
@@ -120,6 +123,7 @@ func Test_updater_run(t *testing.T) {
120
123
})
121
124
}
122
125
126
+ // fakeGetter mocks HTTP requests
123
127
type fakeGetter struct {
124
128
M map [string ]* fakeGetterResponse
125
129
T * testing.T
@@ -131,6 +135,7 @@ func newFakeGetter(t *testing.T) *fakeGetter {
131
135
}
132
136
}
133
137
138
+ // Get returns the configured response for url. If no response configured, test fails immediately.
134
139
func (f * fakeGetter ) Get (url string ) (* http.Response , error ) {
135
140
val , ok := f .M [url ]
136
141
if ! ok {
@@ -146,6 +151,7 @@ type fakeGetterResponse struct {
146
151
Err error
147
152
}
148
153
154
+ // newFakeGetterResponse is a convenience function for mocking HTTP requests
149
155
func newFakeGetterResponse (body []byte , code int , headers []string , err error ) * fakeGetterResponse {
150
156
resp := & http.Response {}
151
157
resp .Body = ioutil .NopCloser (bytes .NewReader (body ))
@@ -169,10 +175,6 @@ func variadicS(s ...string) []string {
169
175
return s
170
176
}
171
177
172
- // func (f *fakeGetter) Get(url string) (*http.Response, error) {
173
- // return f.GetF(url)
174
- // }
175
-
176
178
func fakeConfirmYes (_ string ) (string , error ) {
177
179
return "y" , nil
178
180
}
@@ -181,24 +183,9 @@ func fakeConfirmNo(_ string) (string, error) {
181
183
return "" , promptui .ErrAbort
182
184
}
183
185
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
-
200
186
//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 ()
202
189
f , err := fs .OpenFile (name , os .O_RDWR | os .O_CREATE | os .O_TRUNC , perm )
203
190
if err != nil {
204
191
panic (err )
@@ -213,6 +200,7 @@ func fakeFile(fs afero.Fs, name string, perm fs.FileMode, content string) {
213
200
214
201
//nolint:unparam
215
202
func assertFileContent (t * testing.T , fs afero.Fs , name string , content string ) {
203
+ t .Helper ()
216
204
f , err := fs .OpenFile (name , os .O_RDONLY , 0 )
217
205
assert .Success (t , "open file " + name , err )
218
206
defer f .Close ()
0 commit comments