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

update.test.ts

109 lines4.8 KB

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