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 WorkspacePaths from "./WorkspacePaths.ts";
9
10const layerTest = Layer.empty.pipe(
11 Layer.provideMerge(WorkspacePaths.layer),
12 Layer.provideMerge(NodeServices.layer),
13);
14
15const makeTempDir = Effect.fn("makeTempDir")(function* () {
16 const fileSystem = yield* FileSystem.FileSystem;
17 return yield* fileSystem.makeTempDirectoryScoped({
18 prefix: "t3code-project-paths-",
19 });
20});
21
22it.layer(layerTest)("WorkspacePathsLive", (it) => {
23 describe("normalizeWorkspaceRoot", () => {
24 it.effect("creates missing directories when createIfMissing is enabled", () =>
25 Effect.gen(function* () {
26 const workspacePaths = yield* WorkspacePaths.WorkspacePaths;
27 const fileSystem = yield* FileSystem.FileSystem;
28 const cwd = yield* makeTempDir();
29 const path = yield* Path.Path;
30 const missingPath = path.join(cwd, "nested", "new-project");
31
32 const resolved = yield* workspacePaths.normalizeWorkspaceRoot(missingPath, {
33 createIfMissing: true,
34 });
35 const stat = yield* fileSystem.stat(resolved);
36
37 expect(resolved).toBe(missingPath);
38 expect(stat.type).toBe("Directory");
39 }),
40 );
41 });
42
43 describe("resolveRelativePathWithinRoot", () => {
44 it.effect("rejects paths that escape the workspace root", () =>
45 Effect.gen(function* () {
46 const workspacePaths = yield* WorkspacePaths.WorkspacePaths;
47 const cwd = yield* makeTempDir();
48
49 const error = yield* workspacePaths
50 .resolveRelativePathWithinRoot({
51 workspaceRoot: cwd,
52 relativePath: "../escape.md",
53 })
54 .pipe(Effect.flip);
55
56 expect(error.message).toContain(
57 "Workspace file path must be relative to the project root: ../escape.md",
58 );
59 }),
60 );
61 });
62});