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

Commit 5bf7f56

Browse files
committed
internal/cmd/update_test.go: test for windows
1 parent 513282a commit 5bf7f56

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

internal/cmd/update.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type updater struct {
3434
executablePath string
3535
fs afero.Fs
3636
httpClient getter
37+
osF func() string
3738
versionF func() string
3839
}
3940

@@ -54,11 +55,12 @@ func updateCmd() *cobra.Command {
5455
}
5556

5657
updater := &updater{
58+
confirmF: defaultConfirm,
59+
executablePath: os.Args[0],
5760
httpClient: httpClient,
5861
fs: afero.NewOsFs(),
59-
confirmF: defaultConfirm,
62+
osF: func() string { return runtime.GOOS },
6063
versionF: func() string { return version.Version },
61-
executablePath: os.Args[0],
6264
}
6365
return updater.Run(ctx, force, coderURL)
6466
},
@@ -155,7 +157,13 @@ func (u *updater) Run(ctx context.Context, force bool, coderURLString string) er
155157

156158
// TODO: validate the checksum of the downloaded file. GitHub does not currently provide this information
157159
// and we do not generate them yet.
158-
updatedBinary, err := extractFromArchive("coder", downloadBuf.Bytes())
160+
var updatedBinaryName string
161+
if u.osF() == "windows" {
162+
updatedBinaryName = "coder.exe"
163+
} else {
164+
updatedBinaryName = "coder"
165+
}
166+
updatedBinary, err := extractFromArchive(updatedBinaryName, downloadBuf.Bytes())
159167
if err != nil {
160168
return clog.Fatal("failed to extract coder binary from archive", clog.Causef(err.Error()))
161169
}

internal/cmd/update_test.go

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ import (
1919
)
2020

2121
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"
2728
)
2829

2930
func Test_updater_run(t *testing.T) {
@@ -36,6 +37,7 @@ func Test_updater_run(t *testing.T) {
3637
ExecutablePath string
3738
Fakefs afero.Fs
3839
HttpClient *fakeGetter
40+
OsF func() string
3941
VersionF func() string
4042
}
4143

@@ -46,6 +48,7 @@ func Test_updater_run(t *testing.T) {
4648
executablePath: p.ExecutablePath,
4749
fs: p.Fakefs,
4850
httpClient: p.HttpClient,
51+
osF: p.OsF,
4952
versionF: p.VersionF,
5053
}
5154
}
@@ -65,6 +68,8 @@ func Test_updater_run(t *testing.T) {
6568
ExecutablePath: fakeExePathLinux,
6669
Fakefs: fakefs,
6770
HttpClient: newFakeGetter(t),
71+
// Default to GOOS=linux
72+
OsF: func() string { return "linux" },
6873
// This must be overridden inside run()
6974
VersionF: func() string {
7075
t.Errorf("unhandled VersionF")
@@ -100,6 +105,35 @@ func Test_updater_run(t *testing.T) {
100105
assertFileContent(t, p.Fakefs, fakeExePathLinux, fakeNewVersion)
101106
})
102107

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+
103137
run(t, "update coder - old to new forced", func(t *testing.T, p *params) {
104138
fakeFile(t, p.Fakefs, fakeExePathLinux, 0755, fakeOldVersion)
105139
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) {
308342
assert.Equal(t, "assert content equal", content, string(b))
309343
}
310344

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
312346
// containing the string "1.23.4-rc.5+678-gabcdef-12345678".
313347
var fakeValidTgzBytes, _ = base64.StdEncoding.DecodeString(`H4sIAAAAAAAAA+3QsQ4CIRCEYR6F3oC7wIqvc3KnpQnq+3tGCwsTK3LN/zWTTDWZuG/XeeluJFlV
314348
s1dqNfnOtyJOi4qllHOuTlSTqPMydNXH43afuvfu3w3jb9qExpRjCb1F2x3qMVymU5uXc9CUi63F
315349
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

Comments
 (0)