Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Option from "effect/Option";
6import * as Path from "effect/Path";
7import * as HostProcess from "@t3tools/shared/HostProcess";
8
9import { repointLauncher, resolveLauncherPath } from "./update.ts";
10
11it.layer(NodeServices.layer)("t3 update launcher", (it) => {
12 it.effect("repoints a symlink that lives in a runtime versions 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-update-" });
17 const oldExe = path.join(root, "runtime/versions/1.0.0/t3");
18 const newExe = path.join(root, "runtime/versions/2.0.0/t3");
19 const launcher = path.join(root, "bin/t3");
20 for (const file of [oldExe, newExe]) {
21 yield* fs.makeDirectory(path.dirname(file), { recursive: true });
22 yield* fs.writeFileString(file, "");
23 }
24 yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
25 yield* fs.symlink(oldExe, launcher);
26
27 const repointed = yield* repointLauncher({
28 launchedAs: launcher,
29 versionsDir: path.join(root, "runtime/versions"),
30 targetEntryPath: newExe,
31 });
32
33 assert.deepStrictEqual(Option.getOrUndefined(repointed), launcher);
34 assert.equal(yield* fs.readLink(launcher), newExe);
35 }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
36 );
37
38 it.effect("leaves a plain copy or a foreign symlink alone", () =>
39 Effect.gen(function* () {
40 const fs = yield* FileSystem.FileSystem;
41 const path = yield* Path.Path;
42 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
43 const newExe = path.join(root, "runtime/versions/2.0.0/t3");
44 const copy = path.join(root, "copy/t3");
45 const foreign = path.join(root, "foreign/t3");
46 const elsewhere = path.join(root, "elsewhere/t3");
47 const otherHome = path.join(root, "other/runtime/versions/1.0.0/t3");
48 const otherLauncher = path.join(root, "other/bin/t3");
49 for (const file of [newExe, copy, elsewhere, otherHome]) {
50 yield* fs.makeDirectory(path.dirname(file), { recursive: true });
51 yield* fs.writeFileString(file, "");
52 }
53 yield* fs.makeDirectory(path.dirname(foreign), { recursive: true });
54 yield* fs.symlink(elsewhere, foreign);
55 yield* fs.makeDirectory(path.dirname(otherLauncher), { recursive: true });
56 yield* fs.symlink(otherHome, otherLauncher);
57
58 for (const launchedAs of [copy, foreign, otherLauncher, undefined]) {
59 const repointed = yield* repointLauncher({
60 launchedAs,
61 versionsDir: path.join(root, "runtime/versions"),
62 targetEntryPath: newExe,
63 });
64 assert.equal(repointed._tag, "None", launchedAs ?? "undefined");
65 }
66 assert.equal(yield* fs.readLink(foreign), elsewhere);
67 assert.equal(yield* fs.readLink(otherLauncher), otherHome);
68 }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
69 );
70
71 it.effect("finds the launcher a bare command name resolved to on PATH", () =>
72 Effect.gen(function* () {
73 const fs = yield* FileSystem.FileSystem;
74 const path = yield* Path.Path;
75 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
76 const launcher = path.join(root, "bin/t3");
77 yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
78 yield* fs.writeFileString(launcher, "");
79
80 const bare = yield* resolveLauncherPath.pipe(
81 Effect.provideService(HostProcess.InvokedAs, "t3"),
82 Effect.provideService(HostProcess.Environment, {
83 PATH: `${path.join(root, "missing")}:${path.join(root, "bin")}`,
84 }),
85 Effect.provideService(HostProcess.WorkingDirectory, root),
86 );
87 const relative = yield* resolveLauncherPath.pipe(
88 Effect.provideService(HostProcess.InvokedAs, "./bin/t3"),
89 Effect.provideService(HostProcess.Environment, { PATH: "" }),
90 Effect.provideService(HostProcess.WorkingDirectory, root),
91 );
92 const absent = yield* resolveLauncherPath.pipe(
93 Effect.provideService(HostProcess.InvokedAs, "t3"),
94 Effect.provideService(HostProcess.Environment, { PATH: path.join(root, "missing") }),
95 Effect.provideService(HostProcess.WorkingDirectory, root),
96 );
97
98 assert.equal(bare, launcher);
99 assert.equal(relative, launcher);
100 assert.equal(absent, undefined);
101 }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
102 );
103});