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

uninstall.test.ts

114 lines5.7 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Config from "effect/Config";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Path from "effect/Path";
7import * as HostProcess from "@t3tools/shared/HostProcess";
8​
9import { findOwnedLauncher, findRecordedLaunchers, spawnWindowsRemoval } from "./uninstall.ts";
10​
11it.layer(NodeServices.layer)("t3 uninstall launcher", (it) => {
12 it.effect("claims only a launcher that points into this home's runtime tree", () =>
13 Effect.gen(function* () {
14 const fs = yield* FileSystem.FileSystem;
15 const path = yield* Path.Path;
16 // macOS spells the temp folder /var but resolves it to /private/var.
17 const root = yield* fs
18 .makeTempDirectoryScoped({ prefix: "t3-uninstall-" })
19 .pipe(Effect.flatMap(fs.realPath));
20 const versionsDir = path.join(root, "runtime/versions");
21 const exe = path.join(versionsDir, "1.0.0/t3");
22 const otherExe = path.join(root, "other/runtime/versions/1.0.0/t3");
23 const copy = path.join(root, "copy/t3");
24 for (const file of [exe, otherExe, copy]) {
25 yield* fs.makeDirectory(path.dirname(file), { recursive: true });
26 yield* fs.writeFileString(file, "");
27 }
28 const ours = path.join(root, "bin/t3");
29 const theirs = path.join(root, "other/bin/t3");
30 yield* fs.makeDirectory(path.dirname(ours), { recursive: true });
31 yield* fs.makeDirectory(path.dirname(theirs), { recursive: true });
32 yield* fs.symlink(exe, ours);
33 yield* fs.symlink(otherExe, theirs);
34​
35 assert.equal(yield* findOwnedLauncher({ launchedAs: ours, versionsDir }), ours);
36 assert.isUndefined(yield* findOwnedLauncher({ launchedAs: theirs, versionsDir }));
37 assert.isUndefined(yield* findOwnedLauncher({ launchedAs: copy, versionsDir }));
38 assert.isUndefined(yield* findOwnedLauncher({ launchedAs: undefined, versionsDir }));
39​
40 // On Windows, only the shim exactly as written is ours; a user's wrapper stays.
41 const shimDir = path.join(root, "win-bin");
42 const shim = path.join(shimDir, "t3.cmd");
43 yield* fs.makeDirectory(shimDir);
44 const windowsOwner = (contents: string) =>
45 fs
46 .writeFileString(shim, contents)
47 .pipe(
48 Effect.andThen(findOwnedLauncher({ launchedAs: exe, versionsDir })),
49 Effect.provideService(HostProcess.Platform, "win32"),
50 Effect.provideService(HostProcess.Environment, { PATH: shimDir }),
51 );
52 assert.equal(yield* windowsOwner(`@echo off\r\n"${exe}" %*`), shim);
53 assert.isUndefined(yield* windowsOwner(`@echo off\r\nset FOO=1\r\n"${exe}" %*`));
54​
55 // npx can run another version than the installed one, so uninstall also reads the
56 // recorded launchers: through a symlinked home, and dangling after a prune.
57 const throughAlias = path.join(root, "bin/t3-alias");
58 const pruned = path.join(root, "bin/t3-pruned");
59 yield* fs.symlink(root, path.join(root, "alias"));
60 yield* fs.symlink(path.join(root, "alias/runtime/versions/1.0.0/t3"), throughAlias);
61 yield* fs.symlink(path.join(versionsDir, "0.9.0/t3"), pruned);
62 // Another program inside a version is not a launcher.
63 const sibling = path.join(root, "bin/t3-sibling");
64 yield* fs.symlink(path.join(versionsDir, "1.0.0/node"), sibling);
65 yield* fs.writeFileString(
66 path.join(root, "runtime/launchers"),
67 [ours, theirs, copy, throughAlias, pruned, sibling, path.join(root, "missing")].join("\n") +
68 "\n",
69 );
70 for (const dir of [versionsDir, path.join(root, "alias/runtime/versions")]) {
71 assert.deepStrictEqual(yield* findRecordedLaunchers(dir), [ours, throughAlias, pruned]);
72 }
73​
74 // On Windows only an unedited t3.cmd is ours; a hand-edited wrapper belongs to the user.
75 const cmd = path.join(root, "bin/t3.cmd");
76 const edited = path.join(root, "bin/edited.cmd");
77 yield* fs.writeFileString(cmd, `@echo off\r\n"${exe}" %*`);
78 yield* fs.writeFileString(edited, `@echo off\r\nset "T3CODE_HOME=D:\\other"\r\n"${exe}" %*`);
79 yield* fs.writeFileString(path.join(root, "runtime/launchers"), `${cmd}\r\n${edited}\r\n`);
80 assert.deepStrictEqual(
81 yield* findRecordedLaunchers(versionsDir).pipe(
82 Effect.provideService(HostProcess.Platform, "win32"),
83 ),
84 [cmd],
85 );
86 }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
87 );
88});
89​
90it.live.runIf(HostProcess.Platform.defaultValue() === "win32")(
91 "t3 uninstall has cmd.exe remove the runtime directory, preserving spaces and literal environment-variable syntax",
92 () =>
93 Effect.gen(function* () {
94 const fs = yield* FileSystem.FileSystem;
95 const path = yield* Path.Path;
96 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-uninstall-" });
97 const runtimeDir = path.join(root, "t3 home %COMSPEC% !COMSPEC! & data", "runtime");
98 yield* fs.makeDirectory(path.join(runtimeDir, "versions", "1.0.0"), { recursive: true });
99 yield* fs.writeFileString(path.join(runtimeDir, "versions", "1.0.0", "t3.exe"), "");
100​
101 const child = spawnWindowsRemoval(
102 yield* Config.String("ComSpec").pipe(Config.withDefault("cmd.exe")),
103 runtimeDir,
104 );
105 yield* Effect.callback<void, Error>((resume) => {
106 child.once("error", (error) => resume(Effect.fail(error)));
107 child.once("exit", (code) =>
108 resume(code === 0 ? Effect.void : Effect.die(`cmd.exe exited with ${code}`)),
109 );
110 });
111​
112 assert.isFalse(yield* fs.exists(runtimeDir));
113 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
114);