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

WorkspaceFileSystem.test.ts

674 lines27.0 KB

1// @effect-diagnostics nodeBuiltinImport:off - FileSystem cannot create a FIFO.
2import * as NodeChildProcess from "node:child_process";
3​
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import { it, describe, expect } from "@effect/vitest";
6import * as Effect from "effect/Effect";
7import * as FileSystem from "effect/FileSystem";
8import * as Layer from "effect/Layer";
9import * as Path from "effect/Path";
10​
11import * as ServerConfig from "../config.ts";
12import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
13import * as VcsProcess from "../vcs/VcsProcess.ts";
14import * as KnownWorkspaces from "./KnownWorkspaces.ts";
15import * as WorkspaceEntries from "./WorkspaceEntries.ts";
16import * as WorkspaceFileSystem from "./WorkspaceFileSystem.ts";
17import * as WorkspacePaths from "./WorkspacePaths.ts";
18import * as HostProcess from "@t3tools/shared/HostProcess";
19import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
20​
21// Temp folders count as known workspaces unless named as unknown.
22const UNKNOWN_WORKSPACE_PREFIX = "t3code-unknown-workspace-";
23const layerKnownWorkspacesTest = Layer.mock(KnownWorkspaces.KnownWorkspaces)({
24 assertContains: (cwd) =>
25 cwd.includes(UNKNOWN_WORKSPACE_PREFIX)
26 ? Effect.fail(new KnownWorkspaces.UnknownWorkspaceError({ cwd }))
27 : Effect.void,
28});
29​
30const layerProject = WorkspaceFileSystem.layer.pipe(
31 Layer.provide(layerKnownWorkspacesTest),
32 Layer.provide(WorkspacePaths.layer),
33 Layer.provide(WorkspaceEntries.layer.pipe(Layer.provide(WorkspacePaths.layer))),
34);
35​
36const layerTest = Layer.empty.pipe(
37 Layer.provideMerge(layerProject),
38 Layer.provideMerge(WorkspaceEntries.layer.pipe(Layer.provide(WorkspacePaths.layer))),
39 Layer.provideMerge(WorkspacePaths.layer),
40 Layer.provideMerge(VcsDriverRegistry.layer.pipe(Layer.provide(VcsProcess.layer))),
41 Layer.provide(
42 ServerConfig.ServerConfig.layerTest(process.cwd(), {
43 prefix: "t3-workspace-files-test-",
44 }),
45 ),
46 Layer.provideMerge(NodeServices.layer),
47);
48​
49const makeTempDir = Effect.gen(function* () {
50 const fileSystem = yield* FileSystem.FileSystem;
51 return yield* fileSystem.makeTempDirectoryScoped({
52 prefix: "t3code-workspace-files-",
53 });
54});
55​
56const writeTextFile = Effect.fn("writeTextFile")(function* (
57 cwd: string,
58 relativePath: string,
59 contents = "",
60) {
61 const fileSystem = yield* FileSystem.FileSystem;
62 const path = yield* Path.Path;
63 const absolutePath = path.join(cwd, relativePath);
64 yield* fileSystem
65 .makeDirectory(path.dirname(absolutePath), { recursive: true })
66 .pipe(Effect.orDie);
67 yield* fileSystem.writeFileString(absolutePath, contents).pipe(Effect.orDie);
68});
69​
70it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (it) => {
71 describe("getMetadata", () => {
72 it.effect("expands home-relative paths on the server", () =>
73 Effect.gen(function* () {
74 const service = yield* WorkspaceFileSystem.WorkspaceFileSystem;
75 const home = yield* makeTempDir;
76 yield* writeTextFile(home, "file", "hello");
77 expect(
78 (yield* service
79 .getMetadata({ paths: ["~/file"] })
80 .pipe(Effect.provideService(HostProcess.HomeDirectory, home))).entries,
81 ).toEqual([{ kind: "file", byteLength: 5 }]);
82 }),
83 );
84​
85 it.effect(
86 "reports actual kinds for extensionless files, dotfiles, and dotted directories",
87 () =>
88 Effect.gen(function* () {
89 const service = yield* WorkspaceFileSystem.WorkspaceFileSystem;
90 const fs = yield* FileSystem.FileSystem;
91 const path = yield* Path.Path;
92 const cwd = yield* makeTempDir;
93 yield* writeTextFile(cwd, "custom-name", "hello");
94 yield* writeTextFile(cwd, ".npmrc", "registry=test");
95 yield* fs.makeDirectory(path.join(cwd, "folder.ts"));
96 const file = path.join(cwd, "custom-name");
97 const result = yield* service.getMetadata({
98 paths: [
99 file,
100 path.join(cwd, ".npmrc"),
101 path.join(cwd, "folder.ts"),
102 file,
103 path.join(cwd, "missing"),
104 "relative-path",
105 ],
106 });
107 expect(result.entries).toEqual([
108 { kind: "file", byteLength: 5 },
109 { kind: "file", byteLength: 13 },
110 { kind: "directory" },
111 { kind: "file", byteLength: 5 },
112 null,
113 null,
114 ]);
115 }),
116 );
117​
118 it.effect("detects an extensionless script and image without returning their contents", () =>
119 Effect.gen(function* () {
120 const service = yield* WorkspaceFileSystem.WorkspaceFileSystem;
121 const fs = yield* FileSystem.FileSystem;
122 const path = yield* Path.Path;
123 const cwd = yield* makeTempDir;
124 const script = "#!/usr/bin/env python3\nprint('hello')\n";
125 yield* writeTextFile(cwd, "run", script);
126 const image = path.join(cwd, "image");
127 yield* fs.writeFile(
128 image,
129 new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
130 );
131 expect(
132 (yield* service.getMetadata({ paths: [path.join(cwd, "run"), image] })).entries,
133 ).toEqual([
134 { kind: "file", byteLength: script.length, mimeType: "text/x-python" },
135 { kind: "file", byteLength: 8, mimeType: "image/png" },
136 ]);
137 }),
138 );
139​
140 it.effect.skipIf(!symlinksSupported)("follows file and directory symlinks", () =>
141 Effect.gen(function* () {
142 const service = yield* WorkspaceFileSystem.WorkspaceFileSystem;
143 const fs = yield* FileSystem.FileSystem;
144 const path = yield* Path.Path;
145 const cwd = yield* makeTempDir;
146 yield* writeTextFile(cwd, "target", "text");
147 yield* fs.makeDirectory(path.join(cwd, "directory"));
148 const fileLink = path.join(cwd, "file-link");
149 const directoryLink = path.join(cwd, "directory-link");
150 yield* fs.symlink(path.join(cwd, "target"), fileLink);
151 yield* fs.symlink(path.join(cwd, "directory"), directoryLink);
152 expect((yield* service.getMetadata({ paths: [fileLink, directoryLink] })).entries).toEqual([
153 { kind: "file", byteLength: 4 },
154 { kind: "directory" },
155 ]);
156 }),
157 );
158 });
159​
160 describe("readFile", () => {
161 it.effect("reads UTF-8 files relative to the workspace root", () =>
162 Effect.gen(function* () {
163 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
164 const cwd = yield* makeTempDir;
165 yield* writeTextFile(cwd, "src/index.ts", "export const answer = 42;\n");
166​
167 const result = yield* workspaceFileSystem.readFile({
168 cwd,
169 relativePath: "src/index.ts",
170 });
171​
172 expect(result).toEqual({
173 relativePath: "src/index.ts",
174 contents: "export const answer = 42;\n",
175 byteLength: 26,
176 truncated: false,
177 });
178 }),
179 );
180​
181 it.effect("reads host files outside the workspace root by absolute path", () =>
182 Effect.gen(function* () {
183 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
184 const path = yield* Path.Path;
185 const cwd = yield* makeTempDir;
186 const outsideDir = yield* makeTempDir;
187 yield* writeTextFile(outsideDir, "cleanup-report.md", "# Report\n");
188 const absolutePath = path.join(outsideDir, "cleanup-report.md");
189​
190 const result = yield* workspaceFileSystem.readFile({
191 cwd,
192 relativePath: absolutePath,
193 });
194​
195 expect(result).toEqual({
196 relativePath: absolutePath,
197 contents: "# Report\n",
198 byteLength: 9,
199 truncated: false,
200 });
201 }),
202 );
203​
204 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
205 "rejects a FIFO without blocking on open",
206 () =>
207 Effect.gen(function* () {
208 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
209 const path = yield* Path.Path;
210 const cwd = yield* makeTempDir;
211 const outsideDir = yield* makeTempDir;
212 const fifoPath = path.join(outsideDir, "pipe");
213 yield* Effect.promise(
214 () =>
215 new Promise<void>((resolve, reject) =>
216 NodeChildProcess.execFile("mkfifo", [fifoPath], (error) =>
217 error ? reject(error) : resolve(),
218 ),
219 ),
220 );
221​
222 const error = yield* workspaceFileSystem
223 .readFile({ cwd, relativePath: fifoPath })
224 .pipe(Effect.flip);
225​
226 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspacePathNotFileError);
227 expect((yield* workspaceFileSystem.getMetadata({ paths: [fifoPath] })).entries).toEqual([
228 { kind: "other" },
229 ]);
230 }),
231 );
232​
233 it.effect("rejects reads outside the workspace root", () =>
234 Effect.gen(function* () {
235 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
236 const cwd = yield* makeTempDir;
237​
238 const error = yield* workspaceFileSystem
239 .readFile({ cwd, relativePath: "../escape.md" })
240 .pipe(Effect.flip);
241​
242 expect(error.message).toContain(
243 "Workspace file path must be relative to the project root: ../escape.md",
244 );
245 }),
246 );
247​
248 it.effect.skipIf(!symlinksSupported)(
249 "rejects symlinks that resolve outside the workspace root",
250 () =>
251 Effect.gen(function* () {
252 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
253 const fileSystem = yield* FileSystem.FileSystem;
254 const path = yield* Path.Path;
255 const cwd = yield* makeTempDir;
256 const outsideDir = yield* makeTempDir;
257 yield* writeTextFile(outsideDir, "secret.txt", "outside\n");
258 yield* fileSystem.symlink(
259 path.join(outsideDir, "secret.txt"),
260 path.join(cwd, "linked-secret.txt"),
261 );
262​
263 const error = yield* workspaceFileSystem
264 .readFile({ cwd, relativePath: "linked-secret.txt" })
265 .pipe(Effect.flip);
266 const resolvedWorkspaceRoot = yield* fileSystem.realPath(cwd);
267 const resolvedPath = yield* fileSystem.realPath(path.join(outsideDir, "secret.txt"));
268​
269 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceFilePathEscapeError);
270 expect(error).toMatchObject({
271 workspaceRoot: cwd,
272 relativePath: "linked-secret.txt",
273 resolvedWorkspaceRoot,
274 resolvedPath,
275 });
276 expect("cause" in error).toBe(false);
277 }),
278 );
279​
280 it.effect("rejects directories without manufacturing an I/O cause", () =>
281 Effect.gen(function* () {
282 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
283 const fileSystem = yield* FileSystem.FileSystem;
284 const path = yield* Path.Path;
285 const cwd = yield* makeTempDir;
286 yield* fileSystem.makeDirectory(path.join(cwd, "src"));
287​
288 const error = yield* workspaceFileSystem
289 .readFile({ cwd, relativePath: "src" })
290 .pipe(Effect.flip);
291 const resolvedPath = yield* fileSystem.realPath(path.join(cwd, "src"));
292​
293 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspacePathNotFileError);
294 expect(error).toMatchObject({
295 workspaceRoot: cwd,
296 relativePath: "src",
297 resolvedPath,
298 });
299 expect("cause" in error).toBe(false);
300 }),
301 );
302​
303 it.effect("rejects binary files without leaking their contents into the error", () =>
304 Effect.gen(function* () {
305 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
306 const fileSystem = yield* FileSystem.FileSystem;
307 const path = yield* Path.Path;
308 const cwd = yield* makeTempDir;
309 const absolutePath = path.join(cwd, "asset.bin");
310 yield* fileSystem.writeFile(absolutePath, Uint8Array.from([0x61, 0, 0x62]));
311​
312 const error = yield* workspaceFileSystem
313 .readFile({ cwd, relativePath: "asset.bin" })
314 .pipe(Effect.flip);
315 const resolvedPath = yield* fileSystem.realPath(absolutePath);
316​
317 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceBinaryFileError);
318 expect(error).toMatchObject({
319 workspaceRoot: cwd,
320 relativePath: "asset.bin",
321 resolvedPath,
322 });
323 expect("cause" in error).toBe(false);
324 expect("contents" in error).toBe(false);
325 }),
326 );
327​
328 it.effect("preserves the real cause and path for I/O failures", () =>
329 Effect.gen(function* () {
330 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
331 const path = yield* Path.Path;
332 const cwd = yield* makeTempDir;
333 const resolvedPath = path.join(cwd, "missing.txt");
334​
335 const error = yield* workspaceFileSystem
336 .readFile({ cwd, relativePath: "missing.txt" })
337 .pipe(Effect.flip);
338​
339 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceFileSystemOperationError);
340 expect(error).toMatchObject({
341 workspaceRoot: cwd,
342 relativePath: "missing.txt",
343 resolvedPath,
344 operationPath: resolvedPath,
345 operation: "realpath-target",
346 });
347 expect(error.cause).toBeInstanceOf(Error);
348 expect((error.cause as NodeJS.ErrnoException).code).toBe("ENOENT");
349 }),
350 );
351 });
352​
353 describe("writeFile", () => {
354 it.effect("writes files relative to the workspace root", () =>
355 Effect.gen(function* () {
356 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
357 const cwd = yield* makeTempDir;
358 const fileSystem = yield* FileSystem.FileSystem;
359 const path = yield* Path.Path;
360 const result = yield* workspaceFileSystem.writeFile({
361 cwd,
362 relativePath: "plans/effect-rpc.md",
363 contents: "# Plan\n",
364 });
365 const saved = yield* fileSystem
366 .readFileString(path.join(cwd, "plans/effect-rpc.md"))
367 .pipe(Effect.orDie);
368​
369 expect(result).toEqual({ relativePath: "plans/effect-rpc.md" });
370 expect(saved).toBe("# Plan\n");
371 }),
372 );
373​
374 it.effect("rejects writes outside a known workspace", () =>
375 Effect.gen(function* () {
376 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
377 const fileSystem = yield* FileSystem.FileSystem;
378 const cwd = yield* fileSystem.makeTempDirectoryScoped({
379 prefix: UNKNOWN_WORKSPACE_PREFIX,
380 });
381​
382 const error = yield* workspaceFileSystem
383 .writeFile({ cwd, relativePath: "plan.md", contents: "# nope\n" })
384 .pipe(Effect.flip);
385​
386 expect(error).toBeInstanceOf(KnownWorkspaces.UnknownWorkspaceError);
387 expect(yield* fileSystem.readDirectory(cwd)).toEqual([]);
388 }),
389 );
390​
391 it.effect("rejects writes by absolute path", () =>
392 Effect.gen(function* () {
393 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
394 const path = yield* Path.Path;
395 const cwd = yield* makeTempDir;
396 const outsideDir = yield* makeTempDir;
397 const absolutePath = path.join(outsideDir, "cleanup-report.md");
398​
399 const error = yield* workspaceFileSystem
400 .writeFile({ cwd, relativePath: absolutePath, contents: "# Edited\n" })
401 .pipe(Effect.flip);
402​
403 expect(error).toBeInstanceOf(WorkspacePaths.WorkspacePathOutsideRootError);
404 }),
405 );
406​
407 it.effect("invalidates workspace entry search cache after writes", () =>
408 Effect.gen(function* () {
409 const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
410 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
411 const cwd = yield* makeTempDir;
412 yield* writeTextFile(cwd, "src/existing.ts", "export {};\n");
413​
414 const beforeWrite = yield* workspaceEntries.list({ cwd });
415 expect(beforeWrite.entries.some((entry) => entry.path === "plans/effect-rpc.md")).toBe(
416 false,
417 );
418​
419 yield* workspaceFileSystem.writeFile({
420 cwd,
421 relativePath: "plans/effect-rpc.md",
422 contents: "# Plan\n",
423 });
424​
425 const afterWrite = yield* workspaceEntries.list({ cwd });
426 expect(afterWrite.entries).toEqual(
427 expect.arrayContaining([expect.objectContaining({ path: "plans/effect-rpc.md" })]),
428 );
429 expect(afterWrite.truncated).toBe(false);
430 }),
431 );
432​
433 it.effect("rejects writes outside the workspace root", () =>
434 Effect.gen(function* () {
435 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
436 const cwd = yield* makeTempDir;
437 const path = yield* Path.Path;
438 const fileSystem = yield* FileSystem.FileSystem;
439​
440 const error = yield* workspaceFileSystem
441 .writeFile({
442 cwd,
443 relativePath: "../escape.md",
444 contents: "# nope\n",
445 })
446 .pipe(Effect.flip);
447​
448 expect(error.message).toContain(
449 "Workspace file path must be relative to the project root: ../escape.md",
450 );
451​
452 const escapedPath = path.resolve(cwd, "..", "escape.md");
453 const escapedStat = yield* fileSystem
454 .stat(escapedPath)
455 .pipe(Effect.orElseSucceed(() => null));
456 expect(escapedStat).toBeNull();
457 }),
458 );
459​
460 it.effect.skipIf(!symlinksSupported)(
461 "rejects writes through symlinks that resolve outside the workspace root",
462 () =>
463 Effect.gen(function* () {
464 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
465 const fileSystem = yield* FileSystem.FileSystem;
466 const path = yield* Path.Path;
467 const cwd = yield* makeTempDir;
468 const outsideDir = yield* makeTempDir;
469 yield* writeTextFile(outsideDir, "existing.md", "outside\n");
470 yield* fileSystem.symlink(outsideDir, path.join(cwd, "linked-dir"));
471 yield* fileSystem.symlink(
472 path.join(outsideDir, "existing.md"),
473 path.join(cwd, "linked-file.md"),
474 );
475 yield* fileSystem.symlink(
476 path.join(outsideDir, "missing.md"),
477 path.join(cwd, "dangling.md"),
478 );
479​
480 for (const relativePath of [
481 "linked-dir/new.md",
482 "linked-dir/nested/new.md",
483 "linked-file.md",
484 ]) {
485 const error = yield* workspaceFileSystem
486 .writeFile({ cwd, relativePath, contents: "# nope\n" })
487 .pipe(Effect.flip);
488 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceFilePathEscapeError);
489 }
490 const danglingError = yield* workspaceFileSystem
491 .writeFile({ cwd, relativePath: "dangling.md", contents: "# nope\n" })
492 .pipe(Effect.flip);
493 expect(danglingError).toBeInstanceOf(
494 WorkspaceFileSystem.WorkspaceFileSystemOperationError,
495 );
496 expect(danglingError).toMatchObject({ operation: "realpath-target" });
497​
498 expect((yield* fileSystem.readDirectory(outsideDir)).toSorted()).toEqual(["existing.md"]);
499 expect(yield* fileSystem.readFileString(path.join(outsideDir, "existing.md"))).toBe(
500 "outside\n",
501 );
502 }),
503 );
504​
505 it.effect.skipIf(!symlinksSupported)(
506 "writes through symlinks that stay inside the workspace root",
507 () =>
508 Effect.gen(function* () {
509 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
510 const fileSystem = yield* FileSystem.FileSystem;
511 const path = yield* Path.Path;
512 const parentDir = yield* makeTempDir;
513 const cwd = path.join(parentDir, "workspace");
514 const cwdAlias = path.join(parentDir, "workspace-alias");
515 yield* writeTextFile(cwd, "docs/README.md", "old\n");
516 yield* fileSystem.symlink(path.join(cwd, "docs"), path.join(cwd, "docs-alias"));
517 yield* fileSystem.symlink("docs/README.md", path.join(cwd, "README.md"));
518 yield* fileSystem.symlink(cwd, cwdAlias);
519​
520 yield* workspaceFileSystem.writeFile({
521 cwd,
522 relativePath: "docs-alias/plans/plan.md",
523 contents: "# Plan\n",
524 });
525 yield* workspaceFileSystem.writeFile({
526 cwd,
527 relativePath: "README.md",
528 contents: "new\n",
529 });
530 yield* workspaceFileSystem.writeFile({
531 cwd: cwdAlias,
532 relativePath: "notes.md",
533 contents: "notes\n",
534 });
535 // `..` after a link in cwd is resolved by name, as for the relative path.
536 yield* fileSystem.symlink(path.join(cwd, "docs/plans"), path.join(cwd, "plans-alias"));
537 yield* workspaceFileSystem.writeFile({
538 cwd: `${path.join(cwd, "plans-alias")}${path.sep}..`,
539 relativePath: "dotdot.md",
540 contents: "dotdot\n",
541 });
542​
543 expect(yield* fileSystem.readFileString(path.join(cwd, "docs/plans/plan.md"))).toBe(
544 "# Plan\n",
545 );
546 expect(yield* fileSystem.readFileString(path.join(cwd, "docs/README.md"))).toBe("new\n");
547 expect(yield* fileSystem.readLink(path.join(cwd, "README.md"))).toBe("docs/README.md");
548 expect(yield* fileSystem.readFileString(path.join(cwd, "notes.md"))).toBe("notes\n");
549 expect(yield* fileSystem.readFileString(path.join(cwd, "dotdot.md"))).toBe("dotdot\n");
550 }),
551 );
552​
553 // On Windows `\` is a separator, so the name cannot exist there.
554 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
555 "keeps a backslash in a file name as part of the name",
556 () =>
557 Effect.gen(function* () {
558 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
559 const fileSystem = yield* FileSystem.FileSystem;
560 const cwd = yield* makeTempDir;
561 yield* writeTextFile(cwd, "a\\b.md", "old\n");
562​
563 yield* workspaceFileSystem.writeFile({ cwd, relativePath: "a\\b.md", contents: "new\n" });
564​
565 expect(
566 (yield* workspaceFileSystem.readFile({ cwd, relativePath: "a\\b.md" })).contents,
567 ).toBe("new\n");
568 expect(yield* fileSystem.readDirectory(cwd)).toEqual(["a\\b.md"]);
569 }),
570 );
571​
572 it.effect("refuses writes into git's own folders", () =>
573 Effect.gen(function* () {
574 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
575 const fileSystem = yield* FileSystem.FileSystem;
576 const path = yield* Path.Path;
577 const cwd = yield* makeTempDir;
578 yield* writeTextFile(cwd, ".git/config", "[core]\n");
579 // A bare repository named `.bare`, and a linked worktree's git folder inside it.
580 yield* writeTextFile(cwd, ".bare/HEAD", "ref: refs/heads/main\n");
581 yield* writeTextFile(cwd, ".bare/objects/info/packs");
582 yield* writeTextFile(cwd, ".bare/refs/heads/main");
583 yield* writeTextFile(cwd, ".bare/hooks/pre-push.sample");
584 // Only a linked worktree's `.git` file ends the upward check, not any `.git` folder.
585 yield* fileSystem.makeDirectory(path.join(cwd, ".bare/hooks/.git"));
586 yield* writeTextFile(cwd, ".bare/worktrees/main/HEAD", "ref: refs/heads/main\n");
587 yield* writeTextFile(cwd, ".bare/worktrees/main/commondir", "../..\n");
588 if (symlinksSupported) yield* fileSystem.symlink(".git", path.join(cwd, "docs"));
589​
590 for (const [folder, relativePath] of [
591 [cwd, ".git/config"],
592 [cwd, ".git/hooks/pre-commit"],
593 [cwd, "sub/.git"],
594 [cwd, ".GIT/config"],
595 [cwd, ".gıt/config"],
596 [cwd, ".git. /config"],
597 [cwd, ".git::$INDEX_ALLOCATION/config"],
598 [cwd, "GIT~1/config"],
599 [path.join(cwd, ".git"), "config"],
600 [cwd, ".bare/config"],
601 [path.join(cwd, ".bare"), "hooks/post-checkout"],
602 [path.join(cwd, ".bare/hooks"), "post-checkout"],
603 [path.join(cwd, ".bare/worktrees/main"), "commondir"],
604 ...(symlinksSupported ? [[cwd, "docs/config"] as const] : []),
605 ] as const) {
606 const error = yield* workspaceFileSystem
607 .writeFile({ cwd: folder, relativePath, contents: "[core]\n\tfsmonitor = x\n" })
608 .pipe(Effect.flip);
609 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceFilePathEscapeError);
610 }
611​
612 expect(yield* fileSystem.readFileString(path.join(cwd, ".git/config"))).toBe("[core]\n");
613 expect(yield* fileSystem.exists(path.join(cwd, ".bare/config"))).toBe(false);
614 expect((yield* fileSystem.readDirectory(path.join(cwd, ".bare/hooks"))).toSorted()).toEqual(
615 [".git", "pre-push.sample"],
616 );
617 expect(yield* fileSystem.exists(path.join(cwd, "sub"))).toBe(false);
618 expect(
619 yield* fileSystem.readFileString(path.join(cwd, ".bare/worktrees/main/commondir")),
620 ).toBe("../..\n");
621 }),
622 );
623​
624 it.effect.skipIf(!symlinksSupported)(
625 "refuses writes through a git config that links to a file outside git's folders",
626 () =>
627 Effect.gen(function* () {
628 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
629 const fileSystem = yield* FileSystem.FileSystem;
630 const path = yield* Path.Path;
631 const cwd = yield* makeTempDir;
632 yield* writeTextFile(cwd, "shared/config", "[core]\n");
633 yield* fileSystem.makeDirectory(path.join(cwd, ".git"));
634 yield* fileSystem.symlink("../shared/config", path.join(cwd, ".git/config"));
635​
636 const error = yield* workspaceFileSystem
637 .writeFile({ cwd, relativePath: ".git/config", contents: "[core]\n\tfsmonitor = x\n" })
638 .pipe(Effect.flip);
639​
640 expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceFilePathEscapeError);
641 expect(yield* fileSystem.readFileString(path.join(cwd, "shared/config"))).toBe(
642 "[core]\n",
643 );
644 }),
645 );
646​
647 it.effect("writes files that sit beside git's folders", () =>
648 Effect.gen(function* () {
649 const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
650 const fileSystem = yield* FileSystem.FileSystem;
651 const path = yield* Path.Path;
652 const parentDir = yield* makeTempDir;
653 // A bare repository with a worktree checked out inside it.
654 const bare = path.join(parentDir, "repo.git");
655 yield* writeTextFile(bare, "HEAD", "ref: refs/heads/main\n");
656 yield* writeTextFile(bare, "objects/info/packs");
657 yield* writeTextFile(bare, "refs/heads/main");
658 yield* writeTextFile(bare, "main/.git", `gitdir: ${path.join(bare, "worktrees/main")}\n`);
659​
660 for (const [cwd, relativePath] of [
661 [path.join(bare, "main"), ".gitignore"],
662 [path.join(bare, "main"), ".gitattributes"],
663 [path.join(bare, "main"), ".github/workflows/ci.yml"],
664 [path.join(bare, "main"), "vendor/lib.git/README.md"],
665 [path.join(bare, "main/.github"), "CODEOWNERS"],
666 [bare, "main/plan.md"],
667 ] as const) {
668 yield* workspaceFileSystem.writeFile({ cwd, relativePath, contents: "ok\n" });
669 expect(yield* fileSystem.readFileString(path.join(cwd, relativePath))).toBe("ok\n");
670 }
671 }),
672 );
673 });
674});