@@ -19,11 +19,12 @@ import (
19
19
)
20
20
21
21
const (
22
- fakeExePathLinux = "/home/user/bin/coder"
23
- fakeCoderURL = "https://my.cdr.dev"
24
- fakeNewVersion = "1.23.4-rc.5+678-gabcdef-12345678"
25
- fakeOldVersion = "1.22.4-rc.5+678-gabcdef-12345678"
26
- fakeReleaseURL = "https://github.com/cdr/coder-cli/releases/download/v1.23.4/coder-cli-linux-amd64.tar.gz"
22
+ fakeExePathLinux = "/home/user/bin/coder"
23
+ fakeExePathWindows = `C:\Users\user\bin\coder.exe`
24
+ fakeCoderURL = "https://my.cdr.dev"
25
+ fakeNewVersion = "1.23.4-rc.5+678-gabcdef-12345678"
26
+ fakeOldVersion = "1.22.4-rc.5+678-gabcdef-12345678"
27
+ fakeReleaseURL = "https://github.com/cdr/coder-cli/releases/download/v1.23.4/coder-cli-linux-amd64.tar.gz"
27
28
)
28
29
29
30
func Test_updater_run (t * testing.T ) {
@@ -36,6 +37,7 @@ func Test_updater_run(t *testing.T) {
36
37
ExecutablePath string
37
38
Fakefs afero.Fs
38
39
HttpClient * fakeGetter
40
+ OsF func () string
39
41
VersionF func () string
40
42
}
41
43
@@ -46,6 +48,7 @@ func Test_updater_run(t *testing.T) {
46
48
executablePath : p .ExecutablePath ,
47
49
fs : p .Fakefs ,
48
50
httpClient : p .HttpClient ,
51
+ osF : p .OsF ,
49
52
versionF : p .VersionF ,
50
53
}
51
54
}
@@ -65,6 +68,8 @@ func Test_updater_run(t *testing.T) {
65
68
ExecutablePath : fakeExePathLinux ,
66
69
Fakefs : fakefs ,
67
70
HttpClient : newFakeGetter (t ),
71
+ // Default to GOOS=linux
72
+ OsF : func () string { return "linux" },
68
73
// This must be overridden inside run()
69
74
VersionF : func () string {
70
75
t .Errorf ("unhandled VersionF" )
@@ -100,6 +105,35 @@ func Test_updater_run(t *testing.T) {
100
105
assertFileContent (t , p .Fakefs , fakeExePathLinux , fakeNewVersion )
101
106
})
102
107
108
+ run (t , "update coder - old to new - binary renamed" , func (t * testing.T , p * params ) {
109
+ p .ExecutablePath = "/home/user/bin/coder-cli"
110
+ fakeFile (t , p .Fakefs , p .ExecutablePath , 0755 , fakeOldVersion )
111
+ p .HttpClient .M [fakeCoderURL + "/api" ] = newFakeGetterResponse ([]byte {}, 401 , variadicS ("coder-version: " + fakeNewVersion ), nil )
112
+ p .HttpClient .M [fakeReleaseURL ] = newFakeGetterResponse (fakeValidTgzBytes , 200 , variadicS (), nil )
113
+ p .VersionF = func () string { return fakeOldVersion }
114
+ p .ConfirmF = fakeConfirmYes
115
+ u := fromParams (p )
116
+ assertFileContent (t , p .Fakefs , p .ExecutablePath , fakeOldVersion )
117
+ err := u .Run (p .Ctx , false , fakeCoderURL )
118
+ assert .Success (t , "update coder - old to new - binary renamed" , err )
119
+ assertFileContent (t , p .Fakefs , p .ExecutablePath , fakeNewVersion )
120
+ })
121
+
122
+ run (t , "update coder - old to new - windows" , func (t * testing.T , p * params ) {
123
+ p .OsF = func () string { return "windows" }
124
+ p .ExecutablePath = fakeExePathWindows
125
+ fakeFile (t , p .Fakefs , fakeExePathWindows , 0755 , fakeOldVersion )
126
+ p .HttpClient .M [fakeCoderURL + "/api" ] = newFakeGetterResponse ([]byte {}, 401 , variadicS ("coder-version: " + fakeNewVersion ), nil )
127
+ p .HttpClient .M [fakeReleaseURL ] = newFakeGetterResponse (fakeValidZipBytes , 200 , variadicS (), nil )
128
+ p .VersionF = func () string { return fakeOldVersion }
129
+ p .ConfirmF = fakeConfirmYes
130
+ u := fromParams (p )
131
+ assertFileContent (t , p .Fakefs , fakeExePathWindows , fakeOldVersion )
132
+ err := u .Run (p .Ctx , false , fakeCoderURL )
133
+ assert .Success (t , "update coder - old to new - windows" , err )
134
+ assertFileContent (t , p .Fakefs , fakeExePathWindows , fakeNewVersion )
135
+ })
136
+
103
137
run (t , "update coder - old to new forced" , func (t * testing.T , p * params ) {
104
138
fakeFile (t , p .Fakefs , fakeExePathLinux , 0755 , fakeOldVersion )
105
139
p .HttpClient .M [fakeCoderURL + "/api" ] = newFakeGetterResponse ([]byte {}, 401 , variadicS ("coder-version: " + fakeNewVersion ), nil )
@@ -308,8 +342,15 @@ func assertFileContent(t *testing.T, fs afero.Fs, name string, content string) {
308
342
assert .Equal (t , "assert content equal" , content , string (b ))
309
343
}
310
344
311
- // this is a valid tgz file containing a single file named 'coder' with permissions 0751
345
+ // this is a valid tgz archive containing a single file named 'coder' with permissions 0751
312
346
// containing the string "1.23.4-rc.5+678-gabcdef-12345678".
313
347
var fakeValidTgzBytes , _ = base64 .StdEncoding .DecodeString (`H4sIAAAAAAAAA+3QsQ4CIRCEYR6F3oC7wIqvc3KnpQnq+3tGCwsTK3LN/zWTTDWZuG/XeeluJFlV
314
348
s1dqNfnOtyJOi4qllHOuTlSTqPMydNXH43afuvfu3w3jb9qExpRjCb1F2x3qMVymU5uXc9CUi63F
315
349
1vsAAAAAAAAAAAAAAAAAAL89AYuL424AKAAA` )
350
+
351
+ // this is a valid zip archive containing a single file named 'coder.exe' with permissions 0751
352
+ // containing the string "1.23.4-rc.5+678-gabcdef-12345678".
353
+ var fakeValidZipBytes , _ = base64 .StdEncoding .DecodeString (`UEsDBAoAAAAAAAtfDVNCHNDCIAAAACAAAAAJABwAY29kZXIuZXhlVVQJAAPmXRZh/10WYXV4CwAB
354
+ BOgDAAAE6AMAADEuMjMuNC1yYy41KzY3OC1nYWJjZGVmLTEyMzQ1Njc4UEsBAh4DCgAAAAAAC18N
355
+ U0Ic0MIgAAAAIAAAAAkAGAAAAAAAAQAAAO2BAAAAAGNvZGVyLmV4ZVVUBQAD5l0WYXV4CwABBOgD
356
+ AAAE6AMAAFBLBQYAAAAAAQABAE8AAABjAAAAAAA=` )
0 commit comments