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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
package main
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"syscall"
"testing"
)
// runInstaller runs scripts/install.sh with the given arguments and returns
// everything it printed, failing the test if it did not exit cleanly.
func runInstaller(t *testing.T, args ...string) string {
t.Helper()
output, err := exec.Command("bash", append([]string{"scripts/install.sh"}, args...)...).CombinedOutput()
if err != nil {
t.Fatalf("scripts/install.sh %v failed: %v\n%s", args, err, output)
}
return string(output)
}
// skipUnlessShellIsAvailable skips a test where the installer cannot run.
func skipUnlessShellIsAvailable(t *testing.T) {
t.Helper()
if testing.Short() {
t.Skip("-short: the installer compiles the whole editor")
}
if runtime.GOOS == "windows" {
t.Skip("the installer is a shell script")
}
if _, err := exec.LookPath("bash"); err != nil {
t.Skip("bash is not available")
}
}
func TestTheInstallerBuildsAWorkingBinary(t *testing.T) {
skipUnlessShellIsAvailable(t)
prefix := t.TempDir()
output := runInstaller(t, "--prefix", prefix)
binary := filepath.Join(prefix, "turbo-moonbit")
info, err := os.Stat(binary)
if err != nil {
t.Fatalf("nothing was installed at %s: %v\n%s", binary, err, output)
}
if info.Mode().Perm()&0o111 == 0 {
t.Errorf("the installed file has permissions %o, want it executable", info.Mode().Perm())
}
version, err := exec.Command(binary, "-version").Output()
if err != nil {
t.Fatalf("the installed binary does not run: %v", err)
}
if !strings.Contains(string(version), "Turbo MoonBit") {
t.Errorf("-version printed %q", version)
}
}
func TestTheInstallerSaysWhereItPutThings(t *testing.T) {
skipUnlessShellIsAvailable(t)
prefix := t.TempDir()
output := runInstaller(t, "--prefix", prefix)
for _, want := range []string{"Turbo MoonBit", prefix, "PATH", "moon-lsp"} {
if !strings.Contains(output, want) {
t.Errorf("the installer never mentions %q:\n%s", want, output)
}
}
}
func TestTheInstallerWarnsWhenThePrefixIsNotOnPath(t *testing.T) {
skipUnlessShellIsAvailable(t)
prefix := t.TempDir() // a fresh temporary directory is never on PATH
output := runInstaller(t, "--prefix", prefix)
if !strings.Contains(output, "not on your PATH") {
t.Errorf("the installer did not warn about the PATH:\n%s", output)
}
if !strings.Contains(output, "export PATH=") {
t.Errorf("the installer warned without saying how to fix it:\n%s", output)
}
}
func TestTheInstallerRemovesWhatItInstalled(t *testing.T) {
skipUnlessShellIsAvailable(t)
prefix := t.TempDir()
runInstaller(t, "--prefix", prefix)
runInstaller(t, "--prefix", prefix, "--uninstall")
if _, err := os.Stat(filepath.Join(prefix, "turbo-moonbit")); !os.IsNotExist(err) {
t.Error("the binary is still there after --uninstall")
}
}
func TestUninstallingNothingIsNotAFailure(t *testing.T) {
skipUnlessShellIsAvailable(t)
output := runInstaller(t, "--prefix", t.TempDir(), "--uninstall")
if !strings.Contains(output, "nothing installed") {
t.Errorf("the installer did not say there was nothing to remove:\n%s", output)
}
}
func TestTheInstallerExplainsItself(t *testing.T) {
skipUnlessShellIsAvailable(t)
output := runInstaller(t, "--help")
for _, want := range []string{"--prefix", "--with-server", "--uninstall"} {
if !strings.Contains(output, want) {
t.Errorf("--help does not document %q:\n%s", want, output)
}
}
}
func TestTheInstallerRefusesAnUnknownOption(t *testing.T) {
skipUnlessShellIsAvailable(t)
output, err := exec.Command("bash", "scripts/install.sh", "--nonsense").CombinedOutput()
if err == nil {
t.Fatal("the installer accepted an option it does not have")
}
if !strings.Contains(string(output), "unknown option") {
t.Errorf("the installer did not say what was wrong:\n%s", output)
}
}
func TestTheInstallerNeedsADirectoryAfterPrefix(t *testing.T) {
skipUnlessShellIsAvailable(t)
output, err := exec.Command("bash", "scripts/install.sh", "--prefix").CombinedOutput()
if err == nil {
t.Fatal("--prefix was accepted with nothing after it")
}
if !strings.Contains(string(output), "needs a directory") {
t.Errorf("the installer did not say what was wrong:\n%s", output)
}
}
func TestTheInstallerRunsFromAnyDirectory(t *testing.T) {
skipUnlessShellIsAvailable(t)
repo, err := filepath.Abs(".")
if err != nil {
t.Fatalf("Abs() error = %v", err)
}
prefix := t.TempDir()
// It is invoked by absolute path from somewhere else entirely, as it would
// be from a shell alias or another script.
command := exec.Command("bash", filepath.Join(repo, "scripts", "install.sh"), "--prefix", prefix)
command.Dir = t.TempDir()
if output, err := command.CombinedOutput(); err != nil {
t.Fatalf("the installer failed when run from elsewhere: %v\n%s", err, output)
}
if _, err := os.Stat(filepath.Join(prefix, "turbo-moonbit")); err != nil {
t.Errorf("nothing was installed: %v", err)
}
}
func TestAFailedBuildLeavesTheInstalledBinaryAlone(t *testing.T) {
skipUnlessShellIsAvailable(t)
prefix := t.TempDir()
runInstaller(t, "--prefix", prefix)
binary := filepath.Join(prefix, "turbo-moonbit")
before, err := os.Stat(binary)
if err != nil {
t.Fatalf("the first install produced nothing: %v", err)
}
// A stray file in package main is exactly what a user's own scratch file
// does to this repository, and it must not cost them their installation.
stray := filepath.Join("scripts", "..", "zz_broken_on_purpose.go")
if err := os.WriteFile(stray, []byte("package main\n\nfunc main() {}\n"), 0o644); err != nil {
t.Fatalf("writing the stray file: %v", err)
}
t.Cleanup(func() { os.Remove(stray) })
output, err := exec.Command("bash", "scripts/install.sh", "--prefix", prefix).CombinedOutput()
if err == nil {
t.Fatal("the installer reported success on a build that cannot succeed")
}
if !strings.Contains(string(output), "nothing was installed") {
t.Errorf("the installer did not say the installation was untouched:\n%s", output)
}
after, err := os.Stat(binary)
if err != nil {
t.Fatalf("the failed build removed the installed binary: %v", err)
}
if !after.ModTime().Equal(before.ModTime()) {
t.Error("the failed build replaced the installed binary")
}
}
func TestReinstallingReplacesTheFileRatherThanOverwritingIt(t *testing.T) {
// macOS caches a binary's code signature against its inode. Writing new
// bytes into the same inode — which is what cp does — leaves the cached
// signature describing something else, and the kernel then refuses to
// execute it: builds fine, installs fine, "does not run". Replacing the
// directory entry with a fresh inode is what avoids that, and it makes the
// install atomic besides.
skipUnlessShellIsAvailable(t)
prefix := t.TempDir()
binary := filepath.Join(prefix, "turbo-moonbit")
runInstaller(t, "--prefix", prefix)
first := inodeOf(t, binary)
runInstaller(t, "--prefix", prefix)
second := inodeOf(t, binary)
if first == second {
t.Errorf("the reinstall wrote into the same inode (%d); it must replace the file", first)
}
}
func TestReinstallingLeavesAWorkingBinary(t *testing.T) {
skipUnlessShellIsAvailable(t)
prefix := t.TempDir()
binary := filepath.Join(prefix, "turbo-moonbit")
runInstaller(t, "--prefix", prefix)
runInstaller(t, "--prefix", prefix)
if _, err := exec.Command(binary, "-version").Output(); err != nil {
t.Fatalf("the reinstalled binary does not run: %v", err)
}
}
func TestABinaryThatWillNotRunIsReportedWithItsOwnError(t *testing.T) {
// "the installed binary does not run" on its own tells whoever hit it
// nothing they can act on. Whatever the system said has to come through.
skipUnlessShellIsAvailable(t)
if !strings.Contains(readInstaller(t), "$verify") {
t.Error("the installer discards what the binary said when it will not run")
}
}
// readInstaller returns the installer's source.
func readInstaller(t *testing.T) string {
t.Helper()
data, err := os.ReadFile("scripts/install.sh")
if err != nil {
t.Fatalf("reading the installer: %v", err)
}
return string(data)
}
// inodeOf returns a file's inode number.
func inodeOf(t *testing.T, path string) uint64 {
t.Helper()
info, err := os.Stat(path)
if err != nil {
t.Fatalf("stat %s: %v", path, err)
}
stat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
t.Skip("inode numbers are not available on this platform")
}
return uint64(stat.Ino)
}
func TestTheInstalledBinaryReportsTheCommitItWasBuiltFrom(t *testing.T) {
// The point of stamping: an installed editor must name the commit it came
// from, not a constant somebody forgot to bump before releasing.
skipUnlessShellIsAvailable(t)
prefix := t.TempDir()
runInstaller(t, "--prefix", prefix)
reported, err := exec.Command(filepath.Join(prefix, "turbo-moonbit"), "-version").Output()
if err != nil {
t.Fatalf("the installed binary does not run: %v", err)
}
commit, err := exec.Command("git", "rev-parse", "--short", "HEAD").Output()
if err != nil {
t.Skip("not a git checkout, so there is no commit to stamp")
}
if want := strings.TrimSpace(string(commit)); !strings.Contains(string(reported), want) {
t.Errorf("-version printed %q, which never mentions the commit %s", reported, want)
}
}
func TestTheInstalledBinaryDoesNotReportAnUnknownVersion(t *testing.T) {
// "unknown" is what the binary says when *no* source could name it, and
// seeing it here would mean the installer's ldflags never reached the
// linker. "devel" is a different thing: it is what a correct build of a
// checkout with no tags reports, so a checkout that has never been tagged
// must not fail this.
//
// What proves the stamp arrived either way is the commit, which only the
// linker can have supplied.
skipUnlessShellIsAvailable(t)
// Outside a git checkout the installer has nothing to stamp *with*, and
// "unknown" is then the correct answer rather than a failure — so the
// premise is checked before anything is asserted on.
commit, err := exec.Command("git", "rev-parse", "--short", "HEAD").Output()
if err != nil {
t.Skip("not a git checkout, so there is nothing for the installer to stamp")
}
prefix := t.TempDir()
runInstaller(t, "--prefix", prefix)
reported, err := exec.Command(filepath.Join(prefix, "turbo-moonbit"), "-version").Output()
if err != nil {
t.Fatalf("the installed binary does not run: %v", err)
}
if strings.Contains(string(reported), "unknown") {
t.Errorf("-version printed %q, so nothing reached the linker at all", reported)
}
if want := strings.TrimSpace(string(commit)); !strings.Contains(string(reported), want) {
t.Errorf("-version printed %q, want it to carry the commit %q", reported, want)
}
}
func TestTheInstallerStampsThroughTheLinker(t *testing.T) {
// A build outside a git checkout has nothing to describe, and must still
// build rather than passing a half-built -X flag to the linker.
script := readInstaller(t)
for _, want := range []string{"turbo-core/version", "-ldflags", "describe --tags --dirty"} {
if !strings.Contains(script, want) {
t.Errorf("the installer never mentions %q", want)
}
}
if !strings.Contains(script, `ldflags=""`) {
t.Error("the installer has no path for a checkout git cannot describe")
}
}
// moon-lsp answers about a *project*, and it works the project out by running
// moon. Installed on its own it would start, be found, and then know nothing
// about any file — which looks exactly like a server that is not running. The
// installer has to say so, which means this check has to exist and has to be
// reached.
func TestTheInstallerChecksThatTheServerHasItsToolchain(t *testing.T) {
script := readInstaller(t)
// Both halves: the check has to be written *and* reached. A function that
// is defined and never called is the shape this kind of grep test misses.
for _, want := range []string{"server_has_toolchain() {", "if ! server_has_toolchain", "answer nothing"} {
if !strings.Contains(script, want) {
t.Errorf("the installer never mentions %q", want)
}
}
}
// The install hint on the status bar and the command the installer runs must be
// the same one. Two spellings of "how do I get this" is how one of them goes
// stale without anybody noticing.
func TestTheInstallerRunsTheCommandTheEditorRecommends(t *testing.T) {
script := readInstaller(t)
if !strings.Contains(script, "cli.moonbitlang.com/install/unix.sh") {
t.Error("the installer does not name the MoonBit toolchain installer the editor's hint names")
}
}
// Finding the file is not the same as its running, and this family has been
// caught by that twice — rustup's shim for Turbo Rust, and a stale tool
// directory in Turbo Python.
func TestTheInstallerRunsTheServerRatherThanStattingIt(t *testing.T) {
if !strings.Contains(readInstaller(t), `"$candidate" --version`) {
t.Error("find_server never runs the candidate; an unusable shim would be reported as installed")
}
}
|