@@ -11,6 +11,7 @@ import (
11
11
"net/http"
12
12
"os"
13
13
"path/filepath"
14
+ "runtime"
14
15
"strings"
15
16
"testing"
16
17
@@ -32,7 +33,6 @@ const (
32
33
var (
33
34
apiPrivateVersionURL = fakeCoderURL + "/api/private/version"
34
35
fakeNewVersionJson = fmt .Sprintf (`{"version":%q}` , fakeNewVersion )
35
- fakeOldVersionJson = fmt .Sprintf (`{"version":%q}` , fakeOldVersion )
36
36
)
37
37
38
38
func Test_updater_run (t * testing.T ) {
@@ -42,6 +42,7 @@ func Test_updater_run(t *testing.T) {
42
42
type params struct {
43
43
ConfirmF func (string ) (string , error )
44
44
Ctx context.Context
45
+ Execer * fakeExecer
45
46
ExecutablePath string
46
47
Fakefs afero.Fs
47
48
HttpClient * fakeGetter
@@ -53,6 +54,7 @@ func Test_updater_run(t *testing.T) {
53
54
fromParams := func (p * params ) * updater {
54
55
return & updater {
55
56
confirmF : p .ConfirmF ,
57
+ execF : p .Execer .ExecF ,
56
58
executablePath : p .ExecutablePath ,
57
59
fs : p .Fakefs ,
58
60
httpClient : p .HttpClient ,
@@ -65,13 +67,16 @@ func Test_updater_run(t *testing.T) {
65
67
t .Logf ("running %s" , name )
66
68
ctx := context .Background ()
67
69
fakefs := afero .NewMemMapFs ()
70
+ execer := newFakeExecer (t )
71
+ execer .M ["brew --prefix" ] = fakeExecerResult {[]byte {}, os .ErrNotExist }
68
72
params := & params {
69
73
// This must be overridden inside run()
70
74
ConfirmF : func (string ) (string , error ) {
71
75
t .Errorf ("unhandled ConfirmF" )
72
76
t .FailNow ()
73
77
return "" , nil
74
78
},
79
+ Execer : execer ,
75
80
Ctx : ctx ,
76
81
ExecutablePath : fakeExePathLinux ,
77
82
Fakefs : fakefs ,
@@ -270,6 +275,43 @@ func Test_updater_run(t *testing.T) {
270
275
assert .ErrorContains (t , "update coder - read-only fs" , err , "failed to create file" )
271
276
assertFileContent (t , p .Fakefs , fakeExePathLinux , fakeOldVersion )
272
277
})
278
+
279
+ if runtime .GOOS == "windows" {
280
+ run (t , "update coder - path blocklist - windows" , func (t * testing.T , p * params ) {
281
+ p .ExecutablePath = `C:\Windows\system32\coder.exe`
282
+ u := fromParams (p )
283
+ err := u .Run (p .Ctx , false , fakeCoderURL )
284
+ assert .ErrorContains (t , "update coder - path blocklist - windows" , err , "cowardly refusing to update coder binary" )
285
+ })
286
+ } else {
287
+ run (t , "update coder - path blocklist - coder assets dir" , func (t * testing.T , p * params ) {
288
+ p .ExecutablePath = `/var/tmp/coder/coder`
289
+ u := fromParams (p )
290
+ err := u .Run (p .Ctx , false , fakeCoderURL )
291
+ assert .ErrorContains (t , "update coder - path blocklist - windows" , err , "cowardly refusing to update coder binary" )
292
+ })
293
+ run (t , "update coder - path blocklist - old homebrew prefix" , func (t * testing.T , p * params ) {
294
+ p .Execer .M ["brew --prefix" ] = fakeExecerResult {[]byte ("/usr/local" ), nil }
295
+ p .ExecutablePath = `/usr/local/bin/coder`
296
+ u := fromParams (p )
297
+ err := u .Run (p .Ctx , false , fakeCoderURL )
298
+ assert .ErrorContains (t , "update coder - path blocklist - old homebrew prefix" , err , "cowardly refusing to update coder binary" )
299
+ })
300
+ run (t , "update coder - path blocklist - new homebrew prefix" , func (t * testing.T , p * params ) {
301
+ p .Execer .M ["brew --prefix" ] = fakeExecerResult {[]byte ("/opt/homebrew" ), nil }
302
+ p .ExecutablePath = `/opt/homebrew/bin/coder`
303
+ u := fromParams (p )
304
+ err := u .Run (p .Ctx , false , fakeCoderURL )
305
+ assert .ErrorContains (t , "update coder - path blocklist - new homebrew prefix" , err , "cowardly refusing to update coder binary" )
306
+ })
307
+ run (t , "update coder - path blocklist - linuxbrew" , func (t * testing.T , p * params ) {
308
+ p .Execer .M ["brew --prefix" ] = fakeExecerResult {[]byte ("/home/user/.linuxbrew" ), nil }
309
+ p .ExecutablePath = `/home/user/.linuxbrew/bin/coder`
310
+ u := fromParams (p )
311
+ err := u .Run (p .Ctx , false , fakeCoderURL )
312
+ assert .ErrorContains (t , "update coder - path blocklist - linuxbrew" , err , "cowardly refusing to update coder binary" )
313
+ })
314
+ }
273
315
}
274
316
275
317
// fakeGetter mocks HTTP requests
@@ -378,3 +420,31 @@ var fakeValidZipBytes, _ = base64.StdEncoding.DecodeString(`UEsDBAoAAAAAAAtfDVNC
378
420
BOgDAAAE6AMAADEuMjMuNC1yYy41KzY3OC1nYWJjZGVmLTEyMzQ1Njc4UEsBAh4DCgAAAAAAC18N
379
421
U0Ic0MIgAAAAIAAAAAkAGAAAAAAAAQAAAO2BAAAAAGNvZGVyLmV4ZVVUBQAD5l0WYXV4CwABBOgD
380
422
AAAE6AMAAFBLBQYAAAAAAQABAE8AAABjAAAAAAA=` )
423
+
424
+ type fakeExecer struct {
425
+ M map [string ]fakeExecerResult
426
+ T * testing.T
427
+ }
428
+
429
+ func (f * fakeExecer ) ExecF (_ context.Context , cmd string , args ... string ) ([]byte , error ) {
430
+ cmdAndArgs := strings .Join (append ([]string {cmd }, args ... ), " " )
431
+ val , ok := f .M [cmdAndArgs ]
432
+ if ! ok {
433
+ f .T .Errorf ("unhandled cmd %q" , cmd )
434
+ f .T .FailNow ()
435
+ return nil , nil // will never happen
436
+ }
437
+ return val .Output , val .Err
438
+ }
439
+
440
+ func newFakeExecer (t * testing.T ) * fakeExecer {
441
+ return & fakeExecer {
442
+ M : make (map [string ]fakeExecerResult ),
443
+ T : t ,
444
+ }
445
+ }
446
+
447
+ type fakeExecerResult struct {
448
+ Output []byte
449
+ Err error
450
+ }
0 commit comments