Open History to see the last commit
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, 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 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-uninstall-" });
17 const versionsDir = path.join(root, "runtime/versions");
18 const exe = path.join(versionsDir, "1.0.0/t3");
19 const otherExe = path.join(root, "other/runtime/versions/1.0.0/t3");
20 const copy = path.join(root, "copy/t3");
21 for (const file of [exe, otherExe, copy]) {
22 yield* fs.makeDirectory(path.dirname(file), { recursive: true });
23 yield* fs.writeFileString(file, "");
24 }
25 const ours = path.join(root, "bin/t3");
26 const theirs = path.join(root, "other/bin/t3");
27 yield* fs.makeDirectory(path.dirname(ours), { recursive: true });
28 yield* fs.makeDirectory(path.dirname(theirs), { recursive: true });
29 yield* fs.symlink(exe, ours);
30 yield* fs.symlink(otherExe, theirs);
31
32 assert.equal(yield* findOwnedLauncher({ launchedAs: ours, versionsDir }), ours);
33 assert.isUndefined(yield* findOwnedLauncher({ launchedAs: theirs, versionsDir }));
34 assert.isUndefined(yield* findOwnedLauncher({ launchedAs: copy, versionsDir }));
35 assert.isUndefined(yield* findOwnedLauncher({ launchedAs: undefined, versionsDir }));
36 }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
37 );
38});
39
40it.live.runIf(HostProcess.Platform.defaultValue() === "win32")(
41 "t3 uninstall has cmd.exe remove the runtime directory, preserving spaces and literal environment-variable syntax",
42 () =>
43 Effect.gen(function* () {
44 const fs = yield* FileSystem.FileSystem;
45 const path = yield* Path.Path;
46 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-uninstall-" });
47 const runtimeDir = path.join(root, "t3 home %COMSPEC% !COMSPEC! & data", "runtime");
48 yield* fs.makeDirectory(path.join(runtimeDir, "versions", "1.0.0"), { recursive: true });
49 yield* fs.writeFileString(path.join(runtimeDir, "versions", "1.0.0", "t3.exe"), "");
50
51 const child = spawnWindowsRemoval(
52 yield* Config.String("ComSpec").pipe(Config.withDefault("cmd.exe")),
53 runtimeDir,
54 );
55 yield* Effect.callback<void, Error>((resume) => {
56 child.once("error", (error) => resume(Effect.fail(error)));
57 child.once("exit", (code) =>
58 resume(code === 0 ? Effect.void : Effect.die(`cmd.exe exited with ${code}`)),
59 );
60 });
61
62 assert.isFalse(yield* fs.exists(runtimeDir));
63 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
64);