forked from kunchenguid/no-mistakes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_script_test.go
More file actions
285 lines (255 loc) · 8.01 KB
/
Copy pathinstall_script_test.go
File metadata and controls
285 lines (255 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"archive/tar"
"compress/gzip"
"context"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)
func TestInstallScriptInstallsUserOwnedBinaryAndPathSymlink(t *testing.T) {
skipInstallScriptTestsOnWindows(t)
home := t.TempDir()
archivePath := filepath.Join(t.TempDir(), "no-mistakes-v1.2.3-darwin-arm64.tar.gz")
binaryScript := "#!/bin/sh\nexit 0\n"
makeInstallArchive(t, archivePath, binaryScript)
fakeBin := makeFakeInstallCommands(t)
localBin := filepath.Join(home, ".local", "bin")
if err := os.MkdirAll(localBin, 0o755); err != nil {
t.Fatal(err)
}
runInstallScript(t, home, fakeBin, map[string]string{
"FAKE_RELEASE_ARCHIVE": archivePath,
})
realBin := filepath.Join(home, ".no-mistakes", "bin", "no-mistakes")
assertFileContent(t, realBin, binaryScript)
assertSymlinkTarget(t, filepath.Join(localBin, "no-mistakes"), realBin)
}
func TestInstallScriptReplacesExistingPathEntryWithSymlink(t *testing.T) {
skipInstallScriptTestsOnWindows(t)
home := t.TempDir()
archivePath := filepath.Join(t.TempDir(), "no-mistakes-v1.2.3-darwin-arm64.tar.gz")
binaryScript := "#!/bin/sh\nexit 0\n"
makeInstallArchive(t, archivePath, binaryScript)
fakeBin := makeFakeInstallCommands(t)
linkDir := filepath.Join(t.TempDir(), "link-bin")
if err := os.MkdirAll(linkDir, 0o755); err != nil {
t.Fatal(err)
}
oldPath := filepath.Join(linkDir, "no-mistakes")
if err := os.WriteFile(oldPath, []byte("old-binary"), 0o755); err != nil {
t.Fatal(err)
}
runInstallScript(t, home, fakeBin, map[string]string{
"FAKE_RELEASE_ARCHIVE": archivePath,
"NO_MISTAKES_LINK_DIR": linkDir,
})
realBin := filepath.Join(home, ".no-mistakes", "bin", "no-mistakes")
assertFileContent(t, realBin, binaryScript)
assertSymlinkTarget(t, oldPath, realBin)
}
func TestInstallScriptRestartsDaemonAfterInstall(t *testing.T) {
skipInstallScriptTestsOnWindows(t)
home := t.TempDir()
archivePath := filepath.Join(t.TempDir(), "no-mistakes-v1.2.3-darwin-arm64.tar.gz")
callLog := filepath.Join(t.TempDir(), "calls.log")
makeInstallArchive(t, archivePath, "#!/bin/sh\nprintf '%s\n' \"$*\" >> \"$NO_MISTAKES_CALL_LOG\"\n")
fakeBin := makeFakeInstallCommands(t)
localBin := filepath.Join(home, ".local", "bin")
if err := os.MkdirAll(localBin, 0o755); err != nil {
t.Fatal(err)
}
runInstallScript(t, home, fakeBin, map[string]string{
"FAKE_RELEASE_ARCHIVE": archivePath,
"NO_MISTAKES_CALL_LOG": callLog,
})
data, err := os.ReadFile(callLog)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(data), "daemon restart") {
t.Fatalf("install.sh should restart the daemon after install, got calls %q", string(data))
}
}
func TestInstallScriptFailsWhenDaemonRestartFails(t *testing.T) {
skipInstallScriptTestsOnWindows(t)
home := t.TempDir()
archivePath := filepath.Join(t.TempDir(), "no-mistakes-v1.2.3-darwin-arm64.tar.gz")
callLog := filepath.Join(t.TempDir(), "calls.log")
makeInstallArchive(t, archivePath, "#!/bin/sh\nprintf '%s\n' \"$*\" >> \"$NO_MISTAKES_CALL_LOG\"\nif [ \"$1\" = \"daemon\" ] && [ \"$2\" = \"restart\" ]; then\n exit 23\nfi\n")
fakeBin := makeFakeInstallCommands(t)
localBin := filepath.Join(home, ".local", "bin")
if err := os.MkdirAll(localBin, 0o755); err != nil {
t.Fatal(err)
}
output, err := runInstallScriptCommand(t, home, fakeBin, map[string]string{
"FAKE_RELEASE_ARCHIVE": archivePath,
"NO_MISTAKES_CALL_LOG": callLog,
})
if err == nil {
t.Fatalf("install.sh should fail when daemon restart fails\n%s", output)
}
data, err := os.ReadFile(callLog)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(data), "daemon restart") {
t.Fatalf("install.sh should still attempt daemon restart, got calls %q", string(data))
}
}
func TestPowerShellInstallScriptChecksDaemonRestartFailure(t *testing.T) {
data, err := os.ReadFile(filepath.Join("docs", "install.ps1"))
if err != nil {
t.Fatal(err)
}
text := string(data)
if !strings.Contains(text, "$restart = Start-Process -FilePath \"$installDir\\no-mistakes.exe\" -ArgumentList @(") {
t.Fatal("install.ps1 should run daemon restart in a way that exposes the exit code")
}
if !strings.Contains(text, "-Wait -PassThru") {
t.Fatal("install.ps1 should wait for daemon restart to finish and inspect the process result")
}
if !strings.Contains(text, "if ($restart.ExitCode -ne 0)") {
t.Fatal("install.ps1 should fail the install when daemon restart returns a non-zero exit code")
}
if !strings.Contains(text, "throw \"Failed to restart daemon (exit code $($restart.ExitCode))\"") {
t.Fatal("install.ps1 should surface the daemon restart exit code")
}
}
func skipInstallScriptTestsOnWindows(t *testing.T) {
t.Helper()
if runtime.GOOS == "windows" {
t.Skip("install.sh is a POSIX installer; Windows uses install.ps1")
}
}
func runInstallScript(t *testing.T, home, fakeBin string, extraEnv map[string]string) {
t.Helper()
output, err := runInstallScriptCommand(t, home, fakeBin, extraEnv)
if err != nil {
t.Fatalf("install.sh failed: %v\n%s", err, output)
}
}
func runInstallScriptCommand(t *testing.T, home, fakeBin string, extraEnv map[string]string) ([]byte, error) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "sh", "docs/install.sh")
pathValue := strings.Join([]string{fakeBin, filepath.Join(home, ".local", "bin"), os.Getenv("PATH")}, string(os.PathListSeparator))
cmd.Env = append(filteredEnv(os.Environ(), "HOME", "PATH"), []string{
"HOME=" + home,
"PATH=" + pathValue,
}...)
for key, value := range extraEnv {
cmd.Env = append(cmd.Env, key+"="+value)
}
return cmd.CombinedOutput()
}
func filteredEnv(env []string, excluded ...string) []string {
blocked := make(map[string]struct{}, len(excluded))
for _, key := range excluded {
blocked[key] = struct{}{}
}
filtered := make([]string, 0, len(env))
for _, entry := range env {
key, _, found := strings.Cut(entry, "=")
if !found {
filtered = append(filtered, entry)
continue
}
if _, skip := blocked[key]; skip {
continue
}
filtered = append(filtered, entry)
}
return filtered
}
func makeInstallArchive(t *testing.T, archivePath, binaryContent string) {
t.Helper()
file, err := os.Create(archivePath)
if err != nil {
t.Fatal(err)
}
defer file.Close()
gz := gzip.NewWriter(file)
tw := tar.NewWriter(gz)
data := []byte(binaryContent)
hdr := &tar.Header{Name: "no-mistakes", Mode: 0o755, Size: int64(len(data))}
if err := tw.WriteHeader(hdr); err != nil {
t.Fatal(err)
}
if _, err := tw.Write(data); err != nil {
t.Fatal(err)
}
if err := tw.Close(); err != nil {
t.Fatal(err)
}
if err := gz.Close(); err != nil {
t.Fatal(err)
}
}
func makeFakeInstallCommands(t *testing.T) string {
t.Helper()
binDir := t.TempDir()
writeExecutable(t, filepath.Join(binDir, "uname"), `#!/bin/sh
case "$1" in
-s) printf 'Darwin\n' ;;
-m) printf 'arm64\n' ;;
*) command uname "$@" ;;
esac
`)
writeExecutable(t, filepath.Join(binDir, "curl"), `#!/bin/sh
out=""
url=""
while [ "$#" -gt 0 ]; do
case "$1" in
-o) out="$2"; shift 2 ;;
-*) shift ;;
*) url="$1"; shift ;;
esac
done
if [ -n "$out" ]; then
cp "$FAKE_RELEASE_ARCHIVE" "$out"
exit 0
fi
printf '{"tag_name":"v1.2.3"}'
`)
writeExecutable(t, filepath.Join(binDir, "sudo"), "#!/bin/sh\nexec \"$@\"\n")
return binDir
}
func writeExecutable(t *testing.T, path, content string) {
t.Helper()
if err := os.WriteFile(path, []byte(content), 0o755); err != nil {
t.Fatal(err)
}
}
func assertFileContent(t *testing.T, path, want string) {
t.Helper()
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if string(data) != want {
t.Fatalf("file %s = %q, want %q", path, string(data), want)
}
}
func assertSymlinkTarget(t *testing.T, path, wantTarget string) {
t.Helper()
info, err := os.Lstat(path)
if err != nil {
t.Fatal(err)
}
if info.Mode()&os.ModeSymlink == 0 {
t.Fatalf("%s is not a symlink", path)
}
target, err := os.Readlink(path)
if err != nil {
t.Fatal(err)
}
if target != wantTarget {
t.Fatalf("symlink %s -> %s, want %s", path, target, wantTarget)
}
}