Repository navigation
Expand file tree
/
Copy pathinstall.test.ts
More file actions
288 lines (268 loc) · 13.2 KB
/
Copy pathinstall.test.ts
File metadata and controls
288 lines (268 loc) · 13.2 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
286
287
288
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, it } from "@effect/vitest";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as TestClock from "effect/testing/TestClock";
import packageJson from "../../package.json" with { type: "json" };
import { installCli, isForegroundProcStat, isForegroundPs, offerCliInstall } from "./install.ts";
const VERSION = packageJson.version;
/** A standalone runtime in a temp T3 home, started by the npm launcher from an npx cache. */
const setup = Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-install-" });
const runtimeDir = path.join(root, ".t3/runtime");
const executable = path.join(runtimeDir, "versions", VERSION, "t3");
yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
yield* fs.writeFileString(executable, "");
const npmPackage = path.join(root, ".npm/_npx/abc/node_modules/t3");
yield* fs.makeDirectory(path.join(npmPackage, "bin"), { recursive: true });
yield* fs.writeFileString(
path.join(npmPackage, "package.json"),
JSON.stringify({ name: "t3", version: VERSION }),
);
const binDir = path.join(root, "bin");
const provide = <A, E, R>(effect: Effect.Effect<A, E, R>, env: NodeJS.ProcessEnv = {}) =>
effect.pipe(
Effect.provideService(HostProcess.Platform, "linux"),
Effect.provideService(HostProcess.HomeDirectory, root),
Effect.provideService(HostProcess.IsExecutable, true),
Effect.provideService(HostProcess.ExecutablePath, executable),
Effect.provideService(HostProcess.Environment, {
PATH: "/usr/bin",
SHELL: "/bin/zsh",
T3CODE_INSTALL_BIN_DIR: binDir,
T3CODE_NPM_LAUNCHER_PATH: path.join(npmPackage, "bin/t3.mjs"),
...env,
}),
);
return { root, npmPackage, runtimeDir, executable, link: path.join(binDir, "t3"), provide };
});
it.layer(NodeServices.layer)("t3 install", (it) => {
it.effect("links the running runtime, records it, and never replaces another t3", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const { root, runtimeDir, executable, link, provide } = yield* setup;
const older = path.join(runtimeDir, "versions/0.0.1/t3");
yield* fs.makeDirectory(path.dirname(link), { recursive: true });
yield* fs.symlink(older, link);
const installed = yield* provide(installCli());
assert.equal(yield* fs.readLink(link), executable);
assert.isFalse(installed.onPath);
assert.include(installed.pathCommand, "$HOME/bin");
// A link written through a symlinked T3 home is still ours.
const alias = path.join(root, "alias");
yield* fs.symlink(path.join(root, ".t3"), alias);
yield* fs.remove(link);
yield* fs.symlink(path.join(alias, "runtime/versions", VERSION, "t3"), link);
const onPath = yield* provide(installCli(), { PATH: `/usr/bin:${path.join(root, "bin")}/` });
assert.isTrue(onPath.onPath);
// Another t3 earlier on PATH still runs first, so `t3` would not run this one.
const otherBin = path.join(root, "other-bin");
yield* fs.makeDirectory(otherBin);
yield* fs.writeFileString(path.join(otherBin, "t3"), "");
const shadowed = yield* provide(installCli(), {
PATH: `${otherBin}:${path.join(root, "bin")}`,
});
assert.isFalse(shadowed.onPath);
assert.equal(shadowed.shadowedBy, path.join(otherBin, "t3"));
assert.isUndefined(shadowed.pathCommand);
const recorded = yield* fs.readFileString(path.join(runtimeDir, "launchers"));
assert.equal(recorded, `\n${yield* fs.realPath(path.join(root, "bin"))}/t3\n`);
yield* fs.remove(link);
yield* fs.writeFileString(link, "#!/bin/sh\n");
const error = yield* provide(installCli()).pipe(Effect.flip);
assert.include(error.message, "Another t3 is already at");
assert.equal(yield* fs.readFileString(link), "#!/bin/sh\n");
// Only `<version>/t3` is ours, not another program inside a version.
yield* fs.remove(link);
yield* fs.symlink(path.join(runtimeDir, "versions/0.0.1/node"), link);
const sibling = yield* provide(installCli()).pipe(Effect.flip);
assert.include(sibling.message, "Another t3 is already at");
// This home's desktop launcher is replaced, as install.sh does.
yield* fs.remove(link);
yield* fs.symlink(path.join(root, ".t3/bin/t3"), link);
yield* provide(installCli());
assert.equal(yield* fs.readLink(link), executable);
// A failed swap over our own link leaves a staging path it did not create alone.
yield* fs.remove(link);
yield* fs.symlink(older, link);
const staging = `${link}.${process.pid}.tmp`;
yield* fs.writeFileString(staging, "someone else's\n");
const linkFailed = yield* provide(installCli()).pipe(Effect.flip);
assert.include(linkFailed.message, "Could not link");
assert.equal(yield* fs.readFileString(staging), "someone else's\n");
yield* fs.remove(staging);
// An empty spot is created exclusively: a file that appears first is kept.
yield* fs.remove(link);
const racing = installCli().pipe(
Effect.provideService(FileSystem.FileSystem, {
...fs,
symlink: (target, path) =>
fs
.writeFileString(path, "#!/bin/sh\necho raced\n")
.pipe(Effect.andThen(fs.symlink(target, path))),
}),
);
assert.include((yield* provide(racing).pipe(Effect.flip)).message, "Could not link");
assert.equal(yield* fs.readFileString(link), "#!/bin/sh\necho raced\n");
yield* fs.remove(link);
// Windows writes a t3.cmd: only over its own, and only for paths cmd can run.
const shim = path.join(root, "bin", "t3.cmd");
const windows = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
provide(effect.pipe(Effect.provideService(HostProcess.Platform, "win32")));
const desktopShim =
'@echo off\r\nrem Written by T3 Code\r\n"C:\\T3 Code\\T3 Code.exe" "x" %*\r\n';
yield* fs.writeFileString(shim, desktopShim);
const foreignShim = yield* windows(installCli()).pipe(Effect.flip);
assert.include(foreignShim.message, "Another t3 is already at");
assert.equal(yield* fs.readFileString(shim), desktopShim);
// A user's wrapper that also runs a managed t3.exe is still theirs.
const wrapper = `@echo off\r\nset FOO=1\r\n"${path.join(runtimeDir, "versions/0.0.1/t3.exe")}" %*`;
yield* fs.writeFileString(shim, wrapper);
yield* windows(installCli()).pipe(Effect.flip);
assert.equal(yield* fs.readFileString(shim), wrapper);
// A dangling symlink is not free; writing through it would land outside the bin folder.
const elsewhere = path.join(root, "elsewhere.cmd");
yield* fs.remove(shim);
yield* fs.symlink(elsewhere, shim);
yield* windows(installCli()).pipe(Effect.flip);
assert.isFalse(yield* fs.exists(elsewhere));
yield* fs.remove(shim);
yield* fs.writeFileString(
shim,
`@echo off\r\n"${path.join(runtimeDir, "versions/0.0.1/t3.exe")}" %*`,
);
const windowsInstalled = yield* windows(installCli());
assert.equal(yield* fs.readFileString(shim), `@echo off\r\n"${executable}" %*`);
// A PowerShell command for this window; the saved PATH is left to the user.
assert.equal(windowsInstalled.pathCommand, `$env:Path += ';${path.join(root, "bin")}'`);
const unrunnable = yield* windows(
installCli().pipe(
Effect.provideService(
HostProcess.ExecutablePath,
path.join(runtimeDir, "versions/100%/t3"),
),
),
).pipe(Effect.flip);
assert.include(unrunnable.message, "has % or non-ASCII characters");
}).pipe(Effect.scoped),
);
it.effect("asks npx users without a t3 command once per minor release and installs on yes", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const { root, npmPackage, runtimeDir, executable, link, provide } = yield* setup;
let asked = 0;
const offer = (
answer: boolean | undefined | Effect.Effect<boolean>,
options: {
readonly args?: ReadonlyArray<string>;
readonly env?: NodeJS.ProcessEnv;
readonly interactive?: boolean;
readonly foreground?: boolean;
} = {},
) =>
provide(
offerCliInstall({
args: options.args ?? ["connect"],
interactive: options.interactive ?? true,
foreground: Effect.succeed(options.foreground ?? true),
confirm: Effect.andThen(
Effect.sync(() => (asked += 1)),
Effect.isEffect(answer) ? answer : Effect.succeed(answer),
),
}),
options.env,
);
// Scripts, CI, sudo, dumb terminals, background jobs, and help never ask.
yield* offer(false, { interactive: false });
yield* offer(false, { env: { CI: "true" } });
yield* offer(false, { env: { SUDO_USER: "theo" } });
yield* offer(false, { env: { TERM: "dumb" } });
yield* offer(false, { env: { npm_config_yes: "true" } });
yield* offer(false, { args: ["--completions", "zsh"] });
yield* offer(false, { foreground: false });
yield* offer(false, { args: ["connect", "--help"] });
yield* offer(false, { args: ["--log-level", "debug", "install"] });
yield* offer(false, { args: ["status", "--json"] });
yield* offer(false, { args: ["up", "--yes"] });
yield* offer(false, { args: ["status", "--json=true"] });
const globalBin = path.join(root, "global-bin");
yield* fs.makeDirectory(globalBin);
yield* fs.writeFileString(path.join(globalBin, "t3"), "");
yield* offer(false, { env: { PATH: globalBin } });
assert.equal(asked, 0);
assert.isFalse(yield* fs.exists(link));
// A runner's own .bin on PATH is not an installed t3.
const npxBin = path.join(npmPackage, "../.bin");
const pnpmBin = path.join(root, ".cache/pnpm/dlx/abc/node_modules/.bin");
for (const bin of [npxBin, pnpmBin]) {
yield* fs.makeDirectory(bin, { recursive: true });
yield* fs.writeFileString(path.join(bin, "t3"), "");
}
assert.isFalse(yield* offer(undefined, { env: { PATH: `${npxBin}:${pnpmBin}` } }));
assert.equal(asked, 1);
assert.isFalse(yield* fs.exists(path.join(runtimeDir, "install-prompt-declined")));
asked = 0;
// `service install` is not `t3 install`; it still leaves the command unlinked.
yield* offer(false, { args: ["service", "install"] });
assert.equal(asked, 1);
yield* offer(false);
assert.equal(asked, 1);
assert.isFalse(yield* fs.exists(link));
yield* fs.writeFileString(path.join(runtimeDir, "install-prompt-declined"), "0.0\n");
yield* offer(true);
assert.equal(asked, 2);
assert.equal(yield* fs.readLink(link), executable);
yield* fs.remove(path.join(runtimeDir, "install-prompt-declined"));
yield* offer(true);
assert.equal(asked, 2);
// A yes that cannot install, and no answer at all, also wait for the next minor release.
const declined = path.join(runtimeDir, "install-prompt-declined");
yield* fs.remove(link);
yield* fs.remove(path.join(runtimeDir, "launchers"));
yield* fs.writeFileString(link, "#!/bin/sh\necho mine\n");
asked = 0;
assert.isTrue(yield* offer(true));
assert.equal(yield* fs.readFileString(link), "#!/bin/sh\necho mine\n");
assert.equal(
(yield* fs.readFileString(declined)).trim(),
VERSION.split(".").slice(0, 2).join("."),
);
yield* offer(true);
assert.equal(asked, 1);
yield* fs.remove(declined);
yield* fs.remove(link);
const waiting = yield* Deferred.make<void>();
const unanswered = yield* offer(
Effect.andThen(Deferred.succeed(waiting, undefined), Effect.never),
).pipe(Effect.forkChild);
yield* Deferred.await(waiting);
yield* TestClock.adjust("30 seconds");
assert.isTrue(yield* Fiber.join(unanswered));
assert.isTrue(yield* fs.exists(declined));
assert.isFalse(yield* fs.exists(link));
}).pipe(Effect.scoped),
);
it.effect("reads the terminal's foreground group from /proc and ps", () =>
Effect.sync(() => {
// The command name can hold spaces and parentheses.
const stat = (pgrp: number, tpgid: number) =>
`4242 (t3 (serve) x) S 100 ${pgrp} 100 34816 ${tpgid} 4194560 0 0`;
assert.isTrue(isForegroundProcStat(stat(4242, 4242)));
assert.isFalse(isForegroundProcStat(stat(4242, 4300)));
assert.isFalse(isForegroundProcStat(stat(4242, -1)));
assert.isFalse(isForegroundProcStat(""));
assert.isTrue(isForegroundPs(" 4242 4242\n"));
assert.isFalse(isForegroundPs(" 4300 4242\n"));
assert.isFalse(isForegroundPs(" -1 4242\n"));
assert.isFalse(isForegroundPs(""));
}),
);
});