Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

uninstall.test.ts

208 lines8.9 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Config from "effect/Config";
4import * as ConfigProvider from "effect/ConfigProvider";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Layer from "effect/Layer";
8import * as Path from "effect/Path";
9import * as TestConsole from "effect/testing/TestConsole";
10import { Command } from "effect/cli";
11import * as HostProcess from "@t3tools/shared/HostProcess";
12import * as NetService from "@t3tools/shared/Net";
13import { afterEach, vi } from "vite-plus/test";
14​
15import packageJson from "../../package.json" with { type: "json" };
16import * as BootService from "../cloud/bootService.ts";
17import { posixLauncherScript } from "../cloud/pinnedRuntime.ts";
18import {
19 findOwnedLauncher,
20 findRecordedLaunchers,
21 spawnWindowsRemoval,
22 uninstallCommand,
23} from "./uninstall.ts";
24​
25afterEach(() => vi.restoreAllMocks());
26​
27it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("t3 uninstall command", (it) => {
28 it.effect("names another home's service it leaves installed", () =>
29 Effect.gen(function* () {
30 const fs = yield* FileSystem.FileSystem;
31 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-uninstall-cli-test-" });
32 const uninstalls: Array<true> = [];
33 vi.spyOn(BootService, "layer").mockReturnValue(
34 Layer.succeed(
35 BootService.BootService,
36 BootService.BootService.of({
37 status: Effect.succeed({
38 supported: true,
39 installed: true,
40 current: true,
41 installedBaseDir: "/home/me/.t3",
42 unitPath: "/home/me/.config/systemd/user/t3code.service",
43 logPath: "/home/me/.t3/userdata/logs/boot-service.log",
44 }),
45 withStopped: (operation) => operation,
46 restart: Effect.die("unexpected restart"),
47 install: () => Effect.die("unexpected install"),
48 uninstall: Effect.sync(() => {
49 uninstalls.push(true);
50 return true;
51 }),
52 }),
53 ),
54 );
55​
56 const output = yield* Command.runWith(uninstallCommand, { version: packageJson.version })([
57 "--base-dir",
58 baseDir,
59 "--yes",
60 ]).pipe(
61 Effect.andThen(TestConsole.logLines),
62 Effect.map((lines) => lines.join("\n")),
63 Effect.provideService(HostProcess.Environment, {}),
64 Effect.provideService(HostProcess.IsExecutable, false),
65 Effect.provide(
66 Layer.mergeAll(
67 ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
68 Layer.fresh(TestConsole.layer),
69 ),
70 ),
71 );
72 assert.deepStrictEqual(uninstalls, []);
73 assert.include(output, "--base-dir '/home/me/.t3'");
74 }),
75 );
76});
77​
78it.layer(NodeServices.layer)("t3 uninstall launcher", (it) => {
79 it.effect("claims only a launcher that points into this home's runtime tree", () =>
80 Effect.gen(function* () {
81 const fs = yield* FileSystem.FileSystem;
82 const path = yield* Path.Path;
83 // macOS spells the temp folder /var but resolves it to /private/var.
84 const root = yield* fs
85 .makeTempDirectoryScoped({ prefix: "t3-uninstall-" })
86 .pipe(Effect.flatMap(fs.realPath));
87 const versionsDir = path.join(root, "runtime/versions");
88 const exe = path.join(versionsDir, "1.0.0/t3");
89 const otherExe = path.join(root, "other/runtime/versions/1.0.0/t3");
90 const copy = path.join(root, "copy/t3");
91 for (const file of [exe, otherExe, copy]) {
92 yield* fs.makeDirectory(path.dirname(file), { recursive: true });
93 yield* fs.writeFileString(file, "");
94 }
95 const ours = path.join(root, "bin/t3");
96 const theirs = path.join(root, "other/bin/t3");
97 yield* fs.makeDirectory(path.dirname(ours), { recursive: true });
98 yield* fs.makeDirectory(path.dirname(theirs), { recursive: true });
99 yield* fs.symlink(exe, ours);
100 yield* fs.symlink(otherExe, theirs);
101​
102 assert.equal(yield* findOwnedLauncher({ launchedAs: ours, versionsDir }), ours);
103 assert.isUndefined(yield* findOwnedLauncher({ launchedAs: theirs, versionsDir }));
104 assert.isUndefined(yield* findOwnedLauncher({ launchedAs: copy, versionsDir }));
105 assert.isUndefined(yield* findOwnedLauncher({ launchedAs: undefined, versionsDir }));
106 // A script launcher execs by absolute path, so argv0 names the executable.
107 const scriptDir = path.join(root, "script-bin");
108 const script = path.join(scriptDir, "t3");
109 yield* fs.makeDirectory(scriptDir);
110 yield* fs.writeFileString(script, posixLauncherScript(exe), { mode: 0o755 });
111 assert.equal(
112 yield* findOwnedLauncher({ launchedAs: exe, versionsDir }).pipe(
113 Effect.provideService(HostProcess.Environment, { PATH: scriptDir }),
114 ),
115 script,
116 );
117 const editedScript = path.join(scriptDir, "t3-edited");
118 yield* fs.writeFileString(editedScript, `${posixLauncherScript(exe)}# mine\n`);
119​
120 // On Windows, only the shim exactly as written is ours; a user's wrapper stays.
121 const shimDir = path.join(root, "win-bin");
122 const shim = path.join(shimDir, "t3.cmd");
123 yield* fs.makeDirectory(shimDir);
124 const windowsOwner = (contents: string) =>
125 fs
126 .writeFileString(shim, contents)
127 .pipe(
128 Effect.andThen(findOwnedLauncher({ launchedAs: exe, versionsDir })),
129 Effect.provideService(HostProcess.Platform, "win32"),
130 Effect.provideService(HostProcess.Environment, { PATH: shimDir }),
131 );
132 assert.equal(yield* windowsOwner(`@echo off\r\n"${exe}" %*`), shim);
133 assert.isUndefined(yield* windowsOwner(`@echo off\r\nset FOO=1\r\n"${exe}" %*`));
134​
135 // npx can run another version than the installed one, so uninstall also reads the
136 // recorded launchers: through a symlinked home, and dangling after a prune.
137 const throughAlias = path.join(root, "bin/t3-alias");
138 const pruned = path.join(root, "bin/t3-pruned");
139 yield* fs.symlink(root, path.join(root, "alias"));
140 yield* fs.symlink(path.join(root, "alias/runtime/versions/1.0.0/t3"), throughAlias);
141 yield* fs.symlink(path.join(versionsDir, "0.9.0/t3"), pruned);
142 // Another program inside a version is not a launcher.
143 const sibling = path.join(root, "bin/t3-sibling");
144 yield* fs.symlink(path.join(versionsDir, "1.0.0/node"), sibling);
145 yield* fs.writeFileString(
146 path.join(root, "runtime/launchers"),
147 [
148 ours,
149 theirs,
150 copy,
151 throughAlias,
152 pruned,
153 sibling,
154 script,
155 editedScript,
156 path.join(root, "missing"),
157 ].join("\n") + "\n",
158 );
159 for (const dir of [versionsDir, path.join(root, "alias/runtime/versions")]) {
160 assert.deepStrictEqual(yield* findRecordedLaunchers(dir), [
161 ours,
162 throughAlias,
163 pruned,
164 script,
165 ]);
166 }
167​
168 // On Windows only an unedited t3.cmd is ours; a hand-edited wrapper belongs to the user.
169 const cmd = path.join(root, "bin/t3.cmd");
170 const edited = path.join(root, "bin/edited.cmd");
171 yield* fs.writeFileString(cmd, `@echo off\r\n"${exe}" %*`);
172 yield* fs.writeFileString(edited, `@echo off\r\nset "T3CODE_HOME=D:\\other"\r\n"${exe}" %*`);
173 yield* fs.writeFileString(path.join(root, "runtime/launchers"), `${cmd}\r\n${edited}\r\n`);
174 assert.deepStrictEqual(
175 yield* findRecordedLaunchers(versionsDir).pipe(
176 Effect.provideService(HostProcess.Platform, "win32"),
177 ),
178 [cmd],
179 );
180 }).pipe(Effect.provideService(HostProcess.Platform, "linux")),
181 );
182});
183​
184it.live.runIf(HostProcess.Platform.defaultValue() === "win32")(
185 "t3 uninstall has cmd.exe remove the runtime directory, preserving spaces and literal environment-variable syntax",
186 () =>
187 Effect.gen(function* () {
188 const fs = yield* FileSystem.FileSystem;
189 const path = yield* Path.Path;
190 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-uninstall-" });
191 const runtimeDir = path.join(root, "t3 home %COMSPEC% !COMSPEC! & data", "runtime");
192 yield* fs.makeDirectory(path.join(runtimeDir, "versions", "1.0.0"), { recursive: true });
193 yield* fs.writeFileString(path.join(runtimeDir, "versions", "1.0.0", "t3.exe"), "");
194​
195 const child = spawnWindowsRemoval(
196 yield* Config.String("ComSpec").pipe(Config.withDefault("cmd.exe")),
197 runtimeDir,
198 );
199 yield* Effect.callback<void, Error>((resume) => {
200 child.once("error", (error) => resume(Effect.fail(error)));
201 child.once("exit", (code) =>
202 resume(code === 0 ? Effect.void : Effect.die(`cmd.exe exited with ${code}`)),
203 );
204 });
205​
206 assert.isFalse(yield* fs.exists(runtimeDir));
207 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
208);