Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { it, describe, expect } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Path from "effect/Path";
7
8import * as HostProcess from "@t3tools/shared/HostProcess";
9
10import * as WorkspacePaths from "./WorkspacePaths.ts";
11
12const layerTest = Layer.empty.pipe(
13 Layer.provideMerge(WorkspacePaths.layer),
14 Layer.provideMerge(NodeServices.layer),
15);
16
17const makeTempDir = Effect.fn("makeTempDir")(function* () {
18 const fileSystem = yield* FileSystem.FileSystem;
19 return yield* fileSystem.makeTempDirectoryScoped({
20 prefix: "t3code-project-paths-",
21 });
22});
23
24it.layer(layerTest)("WorkspacePathsLive", (it) => {
25 describe("normalizeWorkspaceRoot", () => {
26 it.effect("creates missing directories when createIfMissing is enabled", () =>
27 Effect.gen(function* () {
28 const workspacePaths = yield* WorkspacePaths.WorkspacePaths;
29 const fileSystem = yield* FileSystem.FileSystem;
30 const cwd = yield* makeTempDir();
31 const path = yield* Path.Path;
32 const missingPath = path.join(cwd, "nested", "new-project");
33
34 const resolved = yield* workspacePaths.normalizeWorkspaceRoot(missingPath, {
35 createIfMissing: true,
36 });
37 const stat = yield* fileSystem.stat(resolved);
38
39 expect(resolved).toBe(missingPath);
40 expect(stat.type).toBe("Directory");
41 }),
42 );
43 });
44
45 describe("resolveRelativePathWithinRoot", () => {
46 it.effect("rejects paths that escape the workspace root", () =>
47 Effect.gen(function* () {
48 const workspacePaths = yield* WorkspacePaths.WorkspacePaths;
49 const cwd = yield* makeTempDir();
50
51 const error = yield* workspacePaths
52 .resolveRelativePathWithinRoot({
53 workspaceRoot: cwd,
54 relativePath: "../escape.md",
55 })
56 .pipe(Effect.flip);
57
58 expect(error.message).toContain(
59 "Workspace file path must be relative to the project root: ../escape.md",
60 );
61 }),
62 );
63
64 // On Windows `\` is a separator, so these names cannot exist there.
65 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
66 "keeps a backslash in a name as part of the name",
67 () =>
68 Effect.gen(function* () {
69 const workspacePaths = yield* WorkspacePaths.WorkspacePaths;
70 const path = yield* Path.Path;
71 const cwd = yield* makeTempDir();
72
73 for (const relativePath of ["weird\\name.txt", "..\\..\\etc"]) {
74 expect(
75 yield* workspacePaths.resolveRelativePathWithinRoot({
76 workspaceRoot: cwd,
77 relativePath,
78 }),
79 ).toEqual({ absolutePath: path.join(cwd, relativePath), relativePath });
80 }
81 }),
82 );
83 });
84});