The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off - FileSystem cannot create a FIFO, and reads spy on native realpath. |
| 2 | import * as NodeChildProcess from "node:child_process"; |
| 3 | import * as NodeFSP from "node:fs/promises"; |
| 4 | |
| 5 | import * as NodePath from "@effect/platform-node/NodePath"; |
| 6 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 7 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 8 | import { it, describe, expect } from "@effect/vitest"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as FileSystem from "effect/FileSystem"; |
| 11 | import * as Layer from "effect/Layer"; |
| 12 | import * as Path from "effect/Path"; |
| 13 | |
| 14 | import * as ServerConfig from "../config.ts"; |
| 15 | import { makeFakeWslHost } from "../testUtils/fakeWsl.ts"; |
| 16 | import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; |
| 17 | import * as VcsProcess from "../vcs/VcsProcess.ts"; |
| 18 | import * as KnownWorkspaces from "./KnownWorkspaces.ts"; |
| 19 | import * as WorkspaceEntries from "./WorkspaceEntries.ts"; |
| 20 | import * as WorkspaceFileSystem from "./WorkspaceFileSystem.ts"; |
| 21 | import * as WorkspacePaths from "./WorkspacePaths.ts"; |
| 22 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 23 | import { symlinksSupported } from "@t3tools/shared/testing/symlinks"; |
| 24 | import { vi } from "vite-plus/test"; |
| 25 | |
| 26 | vi.mock("node:fs/promises", async (importOriginal) => { |
| 27 | const actual = await importOriginal<typeof NodeFSP>(); |
| 28 | return { ...actual, realpath: vi.fn(actual.realpath), open: vi.fn(actual.open) }; |
| 29 | }); |
| 30 | |
| 31 | const injectShortReads = Effect.fn("injectShortReads")(function* (target: string) { |
| 32 | const native = yield* Effect.promise(() => vi.importActual<typeof NodeFSP>("node:fs/promises")); |
| 33 | const open = vi.mocked(NodeFSP.open).mockImplementation(async (path, flags, mode) => { |
| 34 | const handle = await native.open(path, flags, mode); |
| 35 | if (path === target) { |
| 36 | const read = handle.read.bind(handle); |
| 37 | Object.defineProperty(handle, "read", { |
| 38 | value: (buffer: Uint8Array, offset: number, length: number, position: number) => |
| 39 | read(buffer, offset, Math.min(length, 2), position), |
| 40 | }); |
| 41 | } |
| 42 | return handle; |
| 43 | }); |
| 44 | yield* Effect.addFinalizer(() => Effect.sync(() => open.mockImplementation(native.open))); |
| 45 | }); |
| 46 | |
| 47 | const whitespaceFilePaths = |
| 48 | HostProcess.Platform.defaultValue() === "win32" |
| 49 | ? [" report.txt"] |
| 50 | : [" report.txt", "report.txt ", "\treport.txt\t", " "]; |
| 51 | |
| 52 | const UNKNOWN_WORKSPACE_PREFIX = "t3code-unknown-workspace-"; |
| 53 | const layerKnownWorkspacesTest = Layer.mock(KnownWorkspaces.KnownWorkspaces)({ |
| 54 | assertContains: (cwd) => |
| 55 | cwd.includes(UNKNOWN_WORKSPACE_PREFIX) |
| 56 | ? Effect.fail(new KnownWorkspaces.UnknownWorkspaceError({ cwd })) |
| 57 | : Effect.void, |
| 58 | }); |
| 59 | |
| 60 | const layerProject = WorkspaceFileSystem.layer.pipe( |
| 61 | Layer.provide(layerKnownWorkspacesTest), |
| 62 | Layer.provide(WorkspacePaths.layer), |
| 63 | Layer.provide(WorkspaceEntries.layer.pipe(Layer.provide(WorkspacePaths.layer))), |
| 64 | ); |
| 65 | |
| 66 | const layerTest = Layer.empty.pipe( |
| 67 | Layer.provideMerge(layerProject), |
| 68 | Layer.provideMerge(WorkspaceEntries.layer.pipe(Layer.provide(WorkspacePaths.layer))), |
| 69 | Layer.provideMerge(WorkspacePaths.layer), |
| 70 | Layer.provideMerge(VcsDriverRegistry.layer.pipe(Layer.provide(VcsProcess.layer))), |
| 71 | Layer.provideMerge( |
| 72 | ServerConfig.ServerConfig.layerTest(process.cwd(), { |
| 73 | prefix: "t3-workspace-files-test-", |
| 74 | }), |
| 75 | ), |
| 76 | Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 77 | ); |
| 78 | |
| 79 | const makeTempDir = Effect.gen(function* () { |
| 80 | const fileSystem = yield* FileSystem.FileSystem; |
| 81 | return yield* fileSystem.makeTempDirectoryScoped({ |
| 82 | prefix: "t3code-workspace-files-", |
| 83 | }); |
| 84 | }); |
| 85 | |
| 86 | const writeTextFile = Effect.fn("writeTextFile")(function* ( |
| 87 | cwd: string, |
| 88 | relativePath: string, |
| 89 | contents = "", |
| 90 | ) { |
| 91 | const fileSystem = yield* FileSystem.FileSystem; |
| 92 | const path = yield* Path.Path; |
| 93 | const absolutePath = path.join(cwd, relativePath); |
| 94 | yield* fileSystem |
| 95 | .makeDirectory(path.dirname(absolutePath), { recursive: true }) |
| 96 | .pipe(Effect.orDie); |
| 97 | yield* fileSystem.writeFileString(absolutePath, contents).pipe(Effect.orDie); |
| 98 | }); |
| 99 | |
| 100 | const makeWslHost = Effect.gen(function* () { |
| 101 | const { mountRoot, underWsl } = yield* makeFakeWslHost; |
| 102 | const service = yield* underWsl( |
| 103 | WorkspaceFileSystem.make.pipe(Effect.provide(layerKnownWorkspacesTest)), |
| 104 | ); |
| 105 | return { mountRoot, service, underWsl }; |
| 106 | }); |
| 107 | |
| 108 | it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (it) => { |
| 109 | describe("getMetadata", () => { |
| 110 | it.effect( |
| 111 | "reports actual kinds for extensionless files, dotfiles, and dotted directories", |
| 112 | () => |
| 113 | Effect.gen(function* () { |
| 114 | const service = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 115 | const fs = yield* FileSystem.FileSystem; |
| 116 | const path = yield* Path.Path; |
| 117 | const cwd = yield* makeTempDir; |
| 118 | yield* writeTextFile(cwd, "custom-name", "hello"); |
| 119 | yield* writeTextFile(cwd, ".npmrc", "registry=test"); |
| 120 | yield* fs.makeDirectory(path.join(cwd, "folder.ts")); |
| 121 | const file = path.join(cwd, "custom-name"); |
| 122 | const result = yield* service.getMetadata({ |
| 123 | paths: [ |
| 124 | file, |
| 125 | path.join(cwd, ".npmrc"), |
| 126 | path.join(cwd, "folder.ts"), |
| 127 | file, |
| 128 | path.join(cwd, "missing"), |
| 129 | "relative-path", |
| 130 | ], |
| 131 | }); |
| 132 | expect(result.entries).toEqual([ |
| 133 | { kind: "file", byteLength: 5 }, |
| 134 | { kind: "file", byteLength: 13 }, |
| 135 | { kind: "directory" }, |
| 136 | { kind: "file", byteLength: 5 }, |
| 137 | null, |
| 138 | null, |
| 139 | ]); |
| 140 | }), |
| 141 | ); |
| 142 | |
| 143 | it.effect("detects an extensionless script and image without returning their contents", () => |
| 144 | Effect.gen(function* () { |
| 145 | const service = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 146 | const fs = yield* FileSystem.FileSystem; |
| 147 | const path = yield* Path.Path; |
| 148 | const cwd = yield* makeTempDir; |
| 149 | const script = "#!/usr/bin/env python3\nprint('hello')\n"; |
| 150 | yield* writeTextFile(cwd, "run", script); |
| 151 | const image = path.join(cwd, "image"); |
| 152 | yield* fs.writeFile( |
| 153 | image, |
| 154 | new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), |
| 155 | ); |
| 156 | expect( |
| 157 | (yield* service.getMetadata({ paths: [path.join(cwd, "run"), image] })).entries, |
| 158 | ).toEqual([ |
| 159 | { kind: "file", byteLength: script.length, mimeType: "text/x-python" }, |
| 160 | { kind: "file", byteLength: 8, mimeType: "image/png" }, |
| 161 | ]); |
| 162 | }), |
| 163 | ); |
| 164 | |
| 165 | // A file chip for `file://attacker.test/s/a.ts` in an agent's message sent |
| 166 | // the Windows user's NTLM credentials to attacker.test as it rendered. |
| 167 | it.effect("never stats a network path", () => |
| 168 | Effect.gen(function* () { |
| 169 | const fs = yield* FileSystem.FileSystem; |
| 170 | const statted: Array<string> = []; |
| 171 | const service = yield* WorkspaceFileSystem.make.pipe( |
| 172 | Effect.provideService( |
| 173 | FileSystem.FileSystem, |
| 174 | FileSystem.FileSystem.of({ |
| 175 | ...fs, |
| 176 | stat: (requestedPath) => { |
| 177 | statted.push(requestedPath); |
| 178 | return fs.stat(requestedPath); |
| 179 | }, |
| 180 | }), |
| 181 | ), |
| 182 | Effect.provide(layerKnownWorkspacesTest), |
| 183 | ); |
| 184 | const paths = [ |
| 185 | "\\\\attacker.test\\s\\a.ts", |
| 186 | "//attacker.test/s/a.ts", |
| 187 | "/\\attacker.test\\s", |
| 188 | ]; |
| 189 | expect((yield* service.getMetadata({ paths })).entries).toEqual([null, null, null]); |
| 190 | expect(statted).toEqual([]); |
| 191 | }), |
| 192 | ); |
| 193 | |
| 194 | it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( |
| 195 | "reports a Windows drive path under WSL as the file it maps to", |
| 196 | () => |
| 197 | Effect.gen(function* () { |
| 198 | const { mountRoot, service, underWsl } = yield* makeWslHost; |
| 199 | yield* writeTextFile(mountRoot, "c/Users/me/Documents/notes.md", "# Notes\n"); |
| 200 | |
| 201 | const { entries } = yield* underWsl( |
| 202 | service.getMetadata({ |
| 203 | paths: [ |
| 204 | "C:\\Users\\me\\Documents\\notes.md", |
| 205 | "C:/Users/me/Documents/notes.md", |
| 206 | "C:/Users/me/Documents/notes\u0000.md", |
| 207 | ], |
| 208 | }), |
| 209 | ); |
| 210 | |
| 211 | expect(entries).toEqual([ |
| 212 | { kind: "file", byteLength: 8 }, |
| 213 | { kind: "file", byteLength: 8 }, |
| 214 | null, |
| 215 | ]); |
| 216 | }), |
| 217 | ); |
| 218 | }); |
| 219 | |
| 220 | describe("readFile", () => { |
| 221 | it.effect("reads UTF-8 files relative to the workspace root", () => |
| 222 | Effect.gen(function* () { |
| 223 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 224 | const cwd = yield* makeTempDir; |
| 225 | yield* writeTextFile(cwd, "src/index.ts", "export const answer = 42;\n"); |
| 226 | |
| 227 | const result = yield* workspaceFileSystem.readFile({ |
| 228 | cwd, |
| 229 | relativePath: "src/index.ts", |
| 230 | }); |
| 231 | |
| 232 | expect(result).toEqual({ |
| 233 | relativePath: "src/index.ts", |
| 234 | contents: "export const answer = 42;\n", |
| 235 | byteLength: 26, |
| 236 | truncated: false, |
| 237 | }); |
| 238 | |
| 239 | const bomContents = "\uFEFFexport const answer = 42;\n"; |
| 240 | yield* writeTextFile(cwd, "bom.ts", bomContents); |
| 241 | const bomRead = yield* workspaceFileSystem.readFile({ cwd, relativePath: "bom.ts" }); |
| 242 | yield* workspaceFileSystem.writeFile({ |
| 243 | cwd, |
| 244 | relativePath: "bom.ts", |
| 245 | contents: bomRead.contents.replace("42", "43"), |
| 246 | }); |
| 247 | const fs = yield* FileSystem.FileSystem; |
| 248 | const saved = yield* fs.readFile(`${cwd}/bom.ts`); |
| 249 | expect([...saved]).toEqual([...new TextEncoder().encode(bomContents.replace("42", "43"))]); |
| 250 | |
| 251 | yield* writeTextFile(cwd, "report.txt", "ordinary file"); |
| 252 | for (const relativePath of whitespaceFilePaths) { |
| 253 | yield* writeTextFile(cwd, relativePath, "whitespace file"); |
| 254 | const read = yield* workspaceFileSystem.readFile({ cwd, relativePath }); |
| 255 | expect(read.contents).toBe("whitespace file"); |
| 256 | expect(read.relativePath).toBe(relativePath); |
| 257 | } |
| 258 | }), |
| 259 | ); |
| 260 | |
| 261 | it.effect("reports an optional file as absent when the workspace root was removed", () => |
| 262 | Effect.gen(function* () { |
| 263 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 264 | const path = yield* Path.Path; |
| 265 | const cwd = path.join(yield* makeTempDir, "removed-worktree"); |
| 266 | |
| 267 | const result = yield* workspaceFileSystem.readFile({ |
| 268 | cwd, |
| 269 | relativePath: "t3.json", |
| 270 | allowMissing: true, |
| 271 | }); |
| 272 | expect(result).toEqual({ |
| 273 | relativePath: "t3.json", |
| 274 | contents: "", |
| 275 | byteLength: 0, |
| 276 | truncated: false, |
| 277 | exists: false, |
| 278 | }); |
| 279 | |
| 280 | const error = yield* workspaceFileSystem |
| 281 | .readFile({ cwd, relativePath: "t3.json" }) |
| 282 | .pipe(Effect.flip); |
| 283 | expect(error).toMatchObject({ operation: "realpath-workspace-root" }); |
| 284 | }), |
| 285 | ); |
| 286 | |
| 287 | it.effect("finishes short reads before decoding UTF-8 text", () => |
| 288 | Effect.gen(function* () { |
| 289 | const files = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 290 | const fs = yield* FileSystem.FileSystem; |
| 291 | const cwd = yield* makeTempDir; |
| 292 | const contents = "A café 🐈\n"; |
| 293 | yield* writeTextFile(cwd, "short.txt", contents); |
| 294 | const target = yield* fs.realPath(`${cwd}/short.txt`); |
| 295 | yield* injectShortReads(target); |
| 296 | expect(yield* files.readFile({ cwd, relativePath: "short.txt" })).toEqual({ |
| 297 | relativePath: "short.txt", |
| 298 | contents, |
| 299 | byteLength: Buffer.byteLength(contents), |
| 300 | truncated: false, |
| 301 | }); |
| 302 | }), |
| 303 | ); |
| 304 | |
| 305 | it.effect("rejects binary bytes delivered after an initial short read", () => |
| 306 | Effect.gen(function* () { |
| 307 | const files = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 308 | const fs = yield* FileSystem.FileSystem; |
| 309 | const cwd = yield* makeTempDir; |
| 310 | yield* writeTextFile(cwd, "short.txt", "ab\0cd"); |
| 311 | const target = yield* fs.realPath(`${cwd}/short.txt`); |
| 312 | yield* injectShortReads(target); |
| 313 | const error = yield* files.readFile({ cwd, relativePath: "short.txt" }).pipe(Effect.flip); |
| 314 | expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceBinaryFileError); |
| 315 | }), |
| 316 | ); |
| 317 | |
| 318 | it.effect("reads host files outside the workspace root by absolute path", () => |
| 319 | Effect.gen(function* () { |
| 320 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 321 | const path = yield* Path.Path; |
| 322 | const cwd = yield* makeTempDir; |
| 323 | const outsideDir = yield* makeTempDir; |
| 324 | yield* writeTextFile(outsideDir, "cleanup-report.md", "# Report\n"); |
| 325 | const absolutePath = path.join(outsideDir, "cleanup-report.md"); |
| 326 | |
| 327 | const result = yield* workspaceFileSystem.readFile({ |
| 328 | cwd, |
| 329 | relativePath: absolutePath, |
| 330 | }); |
| 331 | |
| 332 | expect(result).toEqual({ |
| 333 | relativePath: absolutePath, |
| 334 | contents: "# Report\n", |
| 335 | byteLength: 9, |
| 336 | truncated: false, |
| 337 | }); |
| 338 | |
| 339 | for (const relativePath of whitespaceFilePaths) { |
| 340 | yield* writeTextFile(outsideDir, relativePath, "whitespace file"); |
| 341 | const requestedPath = path.join(outsideDir, relativePath); |
| 342 | const read = yield* workspaceFileSystem.readFile({ cwd, relativePath: requestedPath }); |
| 343 | expect(read.contents).toBe("whitespace file"); |
| 344 | expect(read.relativePath).toBe(requestedPath); |
| 345 | } |
| 346 | }), |
| 347 | ); |
| 348 | |
| 349 | it.effect.each(["~/.claude/CLAUDE.md", "~\\.claude/CLAUDE.md"])( |
| 350 | "reads %s from the server home outside the workspace", |
| 351 | (relativePath) => |
| 352 | Effect.gen(function* () { |
| 353 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 354 | const cwd = yield* makeTempDir; |
| 355 | const home = yield* makeTempDir; |
| 356 | yield* writeTextFile(home, ".claude/CLAUDE.md", "# Notes\n"); |
| 357 | yield* writeTextFile(cwd, "~/.claude/CLAUDE.md", "Workspace decoy\n"); |
| 358 | |
| 359 | const result = yield* workspaceFileSystem |
| 360 | .readFile({ cwd, relativePath }) |
| 361 | .pipe(Effect.provideService(HostProcess.HomeDirectory, home)); |
| 362 | |
| 363 | expect(result).toEqual({ |
| 364 | relativePath, |
| 365 | contents: "# Notes\n", |
| 366 | byteLength: 8, |
| 367 | truncated: false, |
| 368 | }); |
| 369 | }), |
| 370 | ); |
| 371 | |
| 372 | it.effect("does not read server state files", () => |
| 373 | Effect.gen(function* () { |
| 374 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 375 | const config = yield* ServerConfig.ServerConfig; |
| 376 | const path = yield* Path.Path; |
| 377 | const cwd = yield* makeTempDir; |
| 378 | yield* writeTextFile(config.secretsDir, "example.bin", "example\n"); |
| 379 | yield* writeTextFile(config.stateDir, "statev2.sqlite", "example\n"); |
| 380 | |
| 381 | for (const input of [ |
| 382 | { cwd, relativePath: path.join(config.secretsDir, "example.bin") }, |
| 383 | { cwd: config.stateDir, relativePath: "statev2.sqlite" }, |
| 384 | ]) { |
| 385 | const error = yield* workspaceFileSystem.readFile(input).pipe(Effect.flip); |
| 386 | expect(error).toBeInstanceOf(WorkspacePaths.WorkspacePathOutsideRootError); |
| 387 | } |
| 388 | const metadata = yield* workspaceFileSystem.getMetadata({ |
| 389 | paths: [ |
| 390 | path.join(config.secretsDir, "example.bin"), |
| 391 | path.join(config.stateDir, "statev2.sqlite"), |
| 392 | config.stateDir, |
| 393 | ], |
| 394 | }); |
| 395 | expect(metadata.entries).toEqual([null, null, { kind: "directory" }]); |
| 396 | }), |
| 397 | ); |
| 398 | |
| 399 | it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( |
| 400 | "rejects a FIFO without blocking on open", |
| 401 | () => |
| 402 | Effect.gen(function* () { |
| 403 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 404 | const path = yield* Path.Path; |
| 405 | const cwd = yield* makeTempDir; |
| 406 | const outsideDir = yield* makeTempDir; |
| 407 | const fifoPath = path.join(outsideDir, "pipe"); |
| 408 | yield* Effect.promise( |
| 409 | () => |
| 410 | new Promise<void>((resolve, reject) => |
| 411 | NodeChildProcess.execFile("mkfifo", [fifoPath], (error) => |
| 412 | error ? reject(error) : resolve(), |
| 413 | ), |
| 414 | ), |
| 415 | ); |
| 416 | |
| 417 | const error = yield* workspaceFileSystem |
| 418 | .readFile({ cwd, relativePath: fifoPath }) |
| 419 | .pipe(Effect.flip); |
| 420 | |
| 421 | expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspacePathNotFileError); |
| 422 | expect((yield* workspaceFileSystem.getMetadata({ paths: [fifoPath] })).entries).toEqual([ |
| 423 | { kind: "other" }, |
| 424 | ]); |
| 425 | }), |
| 426 | ); |
| 427 | |
| 428 | it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( |
| 429 | "reads a Windows drive path under WSL from where wslpath mounts it", |
| 430 | () => |
| 431 | Effect.gen(function* () { |
| 432 | const { mountRoot, service, underWsl } = yield* makeWslHost; |
| 433 | const cwd = yield* makeTempDir; |
| 434 | yield* writeTextFile(mountRoot, "c/Users/me/Documents/notes.md", "# Notes\n"); |
| 435 | |
| 436 | for (const relativePath of [ |
| 437 | "C:\\Users\\me\\Documents\\notes.md", |
| 438 | "C:/Users/me/Documents/notes.md", |
| 439 | ]) { |
| 440 | const result = yield* underWsl(service.readFile({ cwd, relativePath })); |
| 441 | expect(result).toMatchObject({ contents: "# Notes\n", truncated: false }); |
| 442 | } |
| 443 | }), |
| 444 | ); |
| 445 | |
| 446 | // Agents on Windows copy paths from Git Bash, which prints `C:\x` as `/c/x`. |
| 447 | it.effect.runIf(HostProcess.Platform.defaultValue() === "win32")( |
| 448 | "reads a Git Bash drive path on Windows", |
| 449 | () => |
| 450 | Effect.gen(function* () { |
| 451 | const service = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 452 | const fs = yield* FileSystem.FileSystem; |
| 453 | const path = yield* Path.Path; |
| 454 | const cwd = yield* makeTempDir; |
| 455 | const filePath = path.join(yield* fs.realPath(yield* makeTempDir), "notes.md"); |
| 456 | yield* fs.writeFileString(filePath, "# Notes\n"); |
| 457 | const gitBashPath = `/${filePath[0]!.toLowerCase()}${filePath.slice(2).replaceAll("\\", "/")}`; |
| 458 | |
| 459 | expect((yield* service.getMetadata({ paths: [gitBashPath] })).entries).toEqual([ |
| 460 | { kind: "file", byteLength: 8 }, |
| 461 | ]); |
| 462 | const result = yield* service.readFile({ cwd, relativePath: gitBashPath }); |
| 463 | expect(result).toMatchObject({ contents: "# Notes\n", truncated: false }); |
| 464 | }), |
| 465 | ); |
| 466 | |
| 467 | it.effect("refuses a network path outside the workspace", () => |
| 468 | Effect.gen(function* () { |
| 469 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 470 | |
| 471 | const error = yield* workspaceFileSystem |
| 472 | .readFile({ cwd: "//nas.test/share/project", relativePath: "//attacker.test/s/a.md" }) |
| 473 | .pipe(Effect.flip); |
| 474 | |
| 475 | expect(error).toBeInstanceOf(WorkspacePaths.WorkspacePathOutsideRootError); |
| 476 | }), |
| 477 | ); |
| 478 | |
| 479 | // This resolves inside the project, but Windows reads `\\?\` paths as |
| 480 | // written, so realpath on the raw string signed in to attacker.test. |
| 481 | it.effect("reads the network path it checked", () => |
| 482 | Effect.gen(function* () { |
| 483 | const workspaceFileSystem = yield* WorkspaceFileSystem.make.pipe( |
| 484 | Effect.provide(Layer.merge(layerKnownWorkspacesTest, NodePath.layerWin32)), |
| 485 | ); |
| 486 | // Rejects without touching the filesystem, so a Windows runner never looks the share up. |
| 487 | const realpath = vi |
| 488 | .mocked(NodeFSP.realpath) |
| 489 | .mockClear() |
| 490 | .mockRejectedValueOnce(Object.assign(new Error("ENOENT"), { code: "ENOENT" })); |
| 491 | |
| 492 | yield* workspaceFileSystem |
| 493 | .readFile({ |
| 494 | cwd: "\\\\?\\UNC\\nas.test\\share\\project", |
| 495 | relativePath: "\\\\?\\UNC\\attacker.test\\s\\..\\..\\nas.test\\share\\project\\a.md", |
| 496 | }) |
| 497 | .pipe(Effect.exit); |
| 498 | |
| 499 | expect(realpath.mock.calls.map(([target]) => target)).toEqual([ |
| 500 | "\\\\?\\UNC\\nas.test\\share\\project\\a.md", |
| 501 | ]); |
| 502 | }), |
| 503 | ); |
| 504 | |
| 505 | // Resolving `..` before realpath would read beside the link instead of its target. |
| 506 | // Windows collapses `..` before the filesystem sees a path, so it has no such reads. |
| 507 | it.effect.skipIf(!symlinksSupported || HostProcess.Platform.defaultValue() === "win32")( |
| 508 | "reads `..` after a link from the link's target", |
| 509 | () => |
| 510 | Effect.gen(function* () { |
| 511 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 512 | const fs = yield* FileSystem.FileSystem; |
| 513 | const path = yield* Path.Path; |
| 514 | const root = yield* makeTempDir; |
| 515 | yield* writeTextFile(root, "target/parent.md", "beside the target\n"); |
| 516 | yield* writeTextFile(root, "parent.md", "beside the link\n"); |
| 517 | yield* fs.makeDirectory(path.join(root, "target", "child")); |
| 518 | yield* fs.symlink(path.join(root, "target", "child"), path.join(root, "link")); |
| 519 | |
| 520 | const result = yield* workspaceFileSystem.readFile({ |
| 521 | cwd: root, |
| 522 | relativePath: `${root}/link/../parent.md`, |
| 523 | }); |
| 524 | |
| 525 | expect(result.contents).toBe("beside the target\n"); |
| 526 | }), |
| 527 | ); |
| 528 | |
| 529 | it.effect.skipIf(!symlinksSupported)( |
| 530 | "rejects symlinks that resolve outside the workspace root", |
| 531 | () => |
| 532 | Effect.gen(function* () { |
| 533 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 534 | const fileSystem = yield* FileSystem.FileSystem; |
| 535 | const path = yield* Path.Path; |
| 536 | const cwd = yield* makeTempDir; |
| 537 | const outsideDir = yield* makeTempDir; |
| 538 | yield* writeTextFile(outsideDir, "secret.txt", "outside\n"); |
| 539 | yield* fileSystem.symlink( |
| 540 | path.join(outsideDir, "secret.txt"), |
| 541 | path.join(cwd, "linked-secret.txt"), |
| 542 | ); |
| 543 | |
| 544 | const error = yield* workspaceFileSystem |
| 545 | .readFile({ cwd, relativePath: "linked-secret.txt" }) |
| 546 | .pipe(Effect.flip); |
| 547 | const resolvedWorkspaceRoot = yield* fileSystem.realPath(cwd); |
| 548 | const resolvedPath = yield* fileSystem.realPath(path.join(outsideDir, "secret.txt")); |
| 549 | |
| 550 | expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceFilePathEscapeError); |
| 551 | expect(error).toMatchObject({ |
| 552 | workspaceRoot: cwd, |
| 553 | relativePath: "linked-secret.txt", |
| 554 | resolvedWorkspaceRoot, |
| 555 | resolvedPath, |
| 556 | }); |
| 557 | expect("cause" in error).toBe(false); |
| 558 | |
| 559 | yield* fileSystem.symlink(outsideDir, path.join(cwd, "linked-dir")); |
| 560 | const missingChildError = yield* workspaceFileSystem |
| 561 | .readFile( |
| 562 | Object.assign( |
| 563 | { cwd, relativePath: "linked-dir/missing/t3.json" }, |
| 564 | { allowMissing: true }, |
| 565 | ), |
| 566 | ) |
| 567 | .pipe(Effect.flip); |
| 568 | expect(missingChildError).toBeInstanceOf( |
| 569 | WorkspaceFileSystem.WorkspaceFilePathEscapeError, |
| 570 | ); |
| 571 | }), |
| 572 | ); |
| 573 | |
| 574 | it.effect("drops a multibyte character split by the read limit", () => |
| 575 | Effect.gen(function* () { |
| 576 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 577 | const fileSystem = yield* FileSystem.FileSystem; |
| 578 | const path = yield* Path.Path; |
| 579 | const cwd = yield* makeTempDir; |
| 580 | const limit = 1024 * 1024; |
| 581 | const euro = new TextEncoder().encode("€"); |
| 582 | const bytes = new Uint8Array(limit - 1 + euro.length).fill(0x61); |
| 583 | bytes.set(euro, limit - 1); |
| 584 | yield* fileSystem.writeFile(path.join(cwd, "large.txt"), bytes); |
| 585 | |
| 586 | const result = yield* workspaceFileSystem.readFile({ cwd, relativePath: "large.txt" }); |
| 587 | |
| 588 | expect(result.truncated).toBe(true); |
| 589 | expect(result.byteLength).toBe(bytes.length); |
| 590 | expect(result.contents).toBe("a".repeat(limit - 1)); |
| 591 | }), |
| 592 | ); |
| 593 | |
| 594 | it.effect.each([ |
| 595 | { name: "a NUL byte", bytes: [0x61, 0, 0x62] }, |
| 596 | { name: "invalid UTF-8", bytes: [0x63, 0x61, 0x66, 0xe9] }, |
| 597 | ])("rejects files with $name without leaking their contents", ({ bytes }) => |
| 598 | Effect.gen(function* () { |
| 599 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 600 | const fileSystem = yield* FileSystem.FileSystem; |
| 601 | const path = yield* Path.Path; |
| 602 | const cwd = yield* makeTempDir; |
| 603 | const absolutePath = path.join(cwd, "asset.bin"); |
| 604 | yield* fileSystem.writeFile(absolutePath, Uint8Array.from(bytes)); |
| 605 | |
| 606 | const error = yield* workspaceFileSystem |
| 607 | .readFile({ cwd, relativePath: "asset.bin" }) |
| 608 | .pipe(Effect.flip); |
| 609 | const resolvedPath = yield* fileSystem.realPath(absolutePath); |
| 610 | |
| 611 | expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceBinaryFileError); |
| 612 | expect(error).toMatchObject({ |
| 613 | workspaceRoot: cwd, |
| 614 | relativePath: "asset.bin", |
| 615 | resolvedPath, |
| 616 | }); |
| 617 | expect("cause" in error).toBe(false); |
| 618 | expect("contents" in error).toBe(false); |
| 619 | }), |
| 620 | ); |
| 621 | }); |
| 622 | |
| 623 | describe("writeFile", () => { |
| 624 | it.effect("rejects writes outside a known workspace", () => |
| 625 | Effect.gen(function* () { |
| 626 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 627 | const fileSystem = yield* FileSystem.FileSystem; |
| 628 | const cwd = yield* fileSystem.makeTempDirectoryScoped({ |
| 629 | prefix: UNKNOWN_WORKSPACE_PREFIX, |
| 630 | }); |
| 631 | |
| 632 | const error = yield* workspaceFileSystem |
| 633 | .writeFile({ cwd, relativePath: "plan.md", contents: "# nope\n" }) |
| 634 | .pipe(Effect.flip); |
| 635 | |
| 636 | expect(error).toBeInstanceOf(KnownWorkspaces.UnknownWorkspaceError); |
| 637 | expect(yield* fileSystem.readDirectory(cwd)).toEqual([]); |
| 638 | }), |
| 639 | ); |
| 640 | |
| 641 | it.effect("rejects writes by absolute path", () => |
| 642 | Effect.gen(function* () { |
| 643 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 644 | const path = yield* Path.Path; |
| 645 | const cwd = yield* makeTempDir; |
| 646 | const outsideDir = yield* makeTempDir; |
| 647 | const absolutePath = path.join(outsideDir, "cleanup-report.md"); |
| 648 | |
| 649 | const error = yield* workspaceFileSystem |
| 650 | .writeFile({ cwd, relativePath: absolutePath, contents: "# Edited\n" }) |
| 651 | .pipe(Effect.flip); |
| 652 | |
| 653 | expect(error).toBeInstanceOf(WorkspacePaths.WorkspacePathOutsideRootError); |
| 654 | }), |
| 655 | ); |
| 656 | |
| 657 | it.effect("invalidates workspace entry search cache after writes", () => |
| 658 | Effect.gen(function* () { |
| 659 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 660 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 661 | const cwd = yield* makeTempDir; |
| 662 | yield* writeTextFile(cwd, "src/existing.ts", "export {};\n"); |
| 663 | |
| 664 | const beforeWrite = yield* workspaceEntries.list({ cwd }); |
| 665 | expect(beforeWrite.entries.some((entry) => entry.path === "plans/effect-rpc.md")).toBe( |
| 666 | false, |
| 667 | ); |
| 668 | |
| 669 | yield* workspaceFileSystem.writeFile({ |
| 670 | cwd, |
| 671 | relativePath: "plans/effect-rpc.md", |
| 672 | contents: "# Plan\n", |
| 673 | }); |
| 674 | |
| 675 | const afterWrite = yield* workspaceEntries.list({ cwd }); |
| 676 | expect(afterWrite.entries).toEqual( |
| 677 | expect.arrayContaining([expect.objectContaining({ path: "plans/effect-rpc.md" })]), |
| 678 | ); |
| 679 | expect(afterWrite.truncated).toBe(false); |
| 680 | }), |
| 681 | ); |
| 682 | |
| 683 | it.effect("saves over an existing file without rescanning the workspace", () => |
| 684 | Effect.gen(function* () { |
| 685 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 686 | const fileSystem = yield* FileSystem.FileSystem; |
| 687 | const path = yield* Path.Path; |
| 688 | const cwd = yield* makeTempDir; |
| 689 | yield* writeTextFile(cwd, "src/main.ts", "export {};\n"); |
| 690 | |
| 691 | yield* workspaceFileSystem.writeFile({ |
| 692 | cwd, |
| 693 | relativePath: "src/main.ts", |
| 694 | contents: "export const edited = true;\n", |
| 695 | }); |
| 696 | |
| 697 | expect(yield* fileSystem.readFileString(path.join(cwd, "src/main.ts"))).toBe( |
| 698 | "export const edited = true;\n", |
| 699 | ); |
| 700 | |
| 701 | yield* writeTextFile(cwd, "report.txt", "ordinary file"); |
| 702 | for (const relativePath of whitespaceFilePaths) { |
| 703 | yield* writeTextFile(cwd, relativePath, "whitespace file"); |
| 704 | const written = yield* workspaceFileSystem.writeFile({ |
| 705 | cwd, |
| 706 | relativePath, |
| 707 | contents: "edited", |
| 708 | }); |
| 709 | expect(yield* fileSystem.readFileString(path.join(cwd, "report.txt"))).toBe( |
| 710 | "ordinary file", |
| 711 | ); |
| 712 | expect(yield* fileSystem.readFileString(path.join(cwd, relativePath))).toBe("edited"); |
| 713 | expect(written.relativePath).toBe(relativePath); |
| 714 | } |
| 715 | }).pipe( |
| 716 | Effect.provide( |
| 717 | Layer.fresh(WorkspaceFileSystem.layer).pipe( |
| 718 | Layer.provide(layerKnownWorkspacesTest), |
| 719 | Layer.provide(WorkspacePaths.layer), |
| 720 | Layer.provide( |
| 721 | Layer.mock(WorkspaceEntries.WorkspaceEntries)({ |
| 722 | refresh: () => Effect.die("An overwrite rescanned the workspace."), |
| 723 | }), |
| 724 | ), |
| 725 | ), |
| 726 | ), |
| 727 | ), |
| 728 | ); |
| 729 | |
| 730 | it.effect("rejects writes outside the workspace root", () => |
| 731 | Effect.gen(function* () { |
| 732 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 733 | const cwd = yield* makeTempDir; |
| 734 | const path = yield* Path.Path; |
| 735 | const fileSystem = yield* FileSystem.FileSystem; |
| 736 | |
| 737 | for (const relativePath of ["../escape.md", "../escape.md ", " folder/../../escape.md"]) { |
| 738 | const error = yield* workspaceFileSystem |
| 739 | .writeFile({ cwd, relativePath, contents: "# nope\n" }) |
| 740 | .pipe(Effect.flip); |
| 741 | |
| 742 | expect(error).toBeInstanceOf(WorkspacePaths.WorkspacePathOutsideRootError); |
| 743 | } |
| 744 | |
| 745 | const escapedPath = path.resolve(cwd, "..", "escape.md"); |
| 746 | const escapedStat = yield* fileSystem |
| 747 | .stat(escapedPath) |
| 748 | .pipe(Effect.orElseSucceed(() => null)); |
| 749 | expect(escapedStat).toBeNull(); |
| 750 | }), |
| 751 | ); |
| 752 | |
| 753 | it.effect.skipIf(!symlinksSupported)( |
| 754 | "rejects writes through symlinks that resolve outside the workspace root", |
| 755 | () => |
| 756 | Effect.gen(function* () { |
| 757 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 758 | const fileSystem = yield* FileSystem.FileSystem; |
| 759 | const path = yield* Path.Path; |
| 760 | const cwd = yield* makeTempDir; |
| 761 | const outsideDir = yield* makeTempDir; |
| 762 | yield* writeTextFile(outsideDir, "existing.md", "outside\n"); |
| 763 | yield* fileSystem.symlink(outsideDir, path.join(cwd, "linked-dir")); |
| 764 | yield* fileSystem.symlink( |
| 765 | path.join(outsideDir, "existing.md"), |
| 766 | path.join(cwd, "linked-file.md"), |
| 767 | ); |
| 768 | yield* fileSystem.symlink( |
| 769 | path.join(outsideDir, "missing.md"), |
| 770 | path.join(cwd, "dangling.md"), |
| 771 | ); |
| 772 | |
| 773 | for (const relativePath of [ |
| 774 | "linked-dir/new.md", |
| 775 | "linked-dir/nested/new.md", |
| 776 | "linked-file.md", |
| 777 | ]) { |
| 778 | const error = yield* workspaceFileSystem |
| 779 | .writeFile({ cwd, relativePath, contents: "# nope\n" }) |
| 780 | .pipe(Effect.flip); |
| 781 | expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceFilePathEscapeError); |
| 782 | } |
| 783 | const danglingError = yield* workspaceFileSystem |
| 784 | .writeFile({ cwd, relativePath: "dangling.md", contents: "# nope\n" }) |
| 785 | .pipe(Effect.flip); |
| 786 | expect(danglingError).toBeInstanceOf( |
| 787 | WorkspaceFileSystem.WorkspaceFileSystemOperationError, |
| 788 | ); |
| 789 | expect(danglingError).toMatchObject({ operation: "realpath-target" }); |
| 790 | |
| 791 | expect((yield* fileSystem.readDirectory(outsideDir)).toSorted()).toEqual(["existing.md"]); |
| 792 | expect(yield* fileSystem.readFileString(path.join(outsideDir, "existing.md"))).toBe( |
| 793 | "outside\n", |
| 794 | ); |
| 795 | }), |
| 796 | ); |
| 797 | |
| 798 | it.effect.skipIf(!symlinksSupported)( |
| 799 | "writes through symlinks that stay inside the workspace root", |
| 800 | () => |
| 801 | Effect.gen(function* () { |
| 802 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 803 | const fileSystem = yield* FileSystem.FileSystem; |
| 804 | const path = yield* Path.Path; |
| 805 | const parentDir = yield* makeTempDir; |
| 806 | const cwd = path.join(parentDir, "workspace"); |
| 807 | const cwdAlias = path.join(parentDir, "workspace-alias"); |
| 808 | yield* writeTextFile(cwd, "docs/README.md", "old\n"); |
| 809 | yield* fileSystem.symlink(path.join(cwd, "docs"), path.join(cwd, "docs-alias")); |
| 810 | yield* fileSystem.symlink("docs/README.md", path.join(cwd, "README.md")); |
| 811 | yield* fileSystem.symlink(cwd, cwdAlias); |
| 812 | |
| 813 | yield* workspaceFileSystem.writeFile({ |
| 814 | cwd, |
| 815 | relativePath: "docs-alias/plans/plan.md", |
| 816 | contents: "# Plan\n", |
| 817 | }); |
| 818 | yield* workspaceFileSystem.writeFile({ |
| 819 | cwd, |
| 820 | relativePath: "README.md", |
| 821 | contents: "new\n", |
| 822 | }); |
| 823 | yield* workspaceFileSystem.writeFile({ |
| 824 | cwd: cwdAlias, |
| 825 | relativePath: "notes.md", |
| 826 | contents: "notes\n", |
| 827 | }); |
| 828 | // `..` after a link in cwd is resolved by name, as for the relative path. |
| 829 | yield* fileSystem.symlink(path.join(cwd, "docs/plans"), path.join(cwd, "plans-alias")); |
| 830 | yield* workspaceFileSystem.writeFile({ |
| 831 | cwd: `${path.join(cwd, "plans-alias")}${path.sep}..`, |
| 832 | relativePath: "dotdot.md", |
| 833 | contents: "dotdot\n", |
| 834 | }); |
| 835 | |
| 836 | expect(yield* fileSystem.readFileString(path.join(cwd, "docs/plans/plan.md"))).toBe( |
| 837 | "# Plan\n", |
| 838 | ); |
| 839 | expect(yield* fileSystem.readFileString(path.join(cwd, "docs/README.md"))).toBe("new\n"); |
| 840 | expect(yield* fileSystem.readLink(path.join(cwd, "README.md"))).toBe("docs/README.md"); |
| 841 | expect(yield* fileSystem.readFileString(path.join(cwd, "notes.md"))).toBe("notes\n"); |
| 842 | expect(yield* fileSystem.readFileString(path.join(cwd, "dotdot.md"))).toBe("dotdot\n"); |
| 843 | }), |
| 844 | ); |
| 845 | |
| 846 | // On Windows `\` is a separator, so the name cannot exist there. |
| 847 | it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( |
| 848 | "keeps a backslash in a file name as part of the name", |
| 849 | () => |
| 850 | Effect.gen(function* () { |
| 851 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 852 | const fileSystem = yield* FileSystem.FileSystem; |
| 853 | const cwd = yield* makeTempDir; |
| 854 | yield* writeTextFile(cwd, "a\\b.md", "old\n"); |
| 855 | |
| 856 | yield* workspaceFileSystem.writeFile({ cwd, relativePath: "a\\b.md", contents: "new\n" }); |
| 857 | |
| 858 | expect( |
| 859 | (yield* workspaceFileSystem.readFile({ cwd, relativePath: "a\\b.md" })).contents, |
| 860 | ).toBe("new\n"); |
| 861 | expect(yield* fileSystem.readDirectory(cwd)).toEqual(["a\\b.md"]); |
| 862 | }), |
| 863 | ); |
| 864 | |
| 865 | it.effect("refuses writes into git's own folders", () => |
| 866 | Effect.gen(function* () { |
| 867 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 868 | const fileSystem = yield* FileSystem.FileSystem; |
| 869 | const path = yield* Path.Path; |
| 870 | const cwd = yield* makeTempDir; |
| 871 | yield* writeTextFile(cwd, ".git/config", "[core]\n"); |
| 872 | yield* writeTextFile(cwd, ".bare/HEAD", "ref: refs/heads/main\n"); |
| 873 | yield* writeTextFile(cwd, ".bare/objects/info/packs"); |
| 874 | yield* writeTextFile(cwd, ".bare/refs/heads/main"); |
| 875 | yield* writeTextFile(cwd, ".bare/hooks/pre-push.sample"); |
| 876 | // Only a linked worktree's `.git` file ends the upward check, not any `.git` folder. |
| 877 | yield* fileSystem.makeDirectory(path.join(cwd, ".bare/hooks/.git")); |
| 878 | yield* writeTextFile(cwd, ".bare/worktrees/main/HEAD", "ref: refs/heads/main\n"); |
| 879 | yield* writeTextFile(cwd, ".bare/worktrees/main/commondir", "../..\n"); |
| 880 | if (symlinksSupported) yield* fileSystem.symlink(".git", path.join(cwd, "docs")); |
| 881 | |
| 882 | for (const [folder, relativePath] of [ |
| 883 | [cwd, ".git/config"], |
| 884 | [cwd, ".git/hooks/pre-commit"], |
| 885 | [cwd, "sub/.git"], |
| 886 | [cwd, ".GIT/config"], |
| 887 | [cwd, ".gıt/config"], |
| 888 | [cwd, ".git. /config"], |
| 889 | [cwd, ".git::$INDEX_ALLOCATION/config"], |
| 890 | [cwd, "GIT~1/config"], |
| 891 | [path.join(cwd, ".git"), "config"], |
| 892 | [cwd, ".bare/config"], |
| 893 | [path.join(cwd, ".bare"), "hooks/post-checkout"], |
| 894 | [path.join(cwd, ".bare/hooks"), "post-checkout"], |
| 895 | [path.join(cwd, ".bare/worktrees/main"), "commondir"], |
| 896 | ...(symlinksSupported ? [[cwd, "docs/config"] as const] : []), |
| 897 | ] as const) { |
| 898 | const error = yield* workspaceFileSystem |
| 899 | .writeFile({ cwd: folder, relativePath, contents: "[core]\n\tfsmonitor = x\n" }) |
| 900 | .pipe(Effect.flip); |
| 901 | expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceFilePathEscapeError); |
| 902 | } |
| 903 | |
| 904 | expect(yield* fileSystem.readFileString(path.join(cwd, ".git/config"))).toBe("[core]\n"); |
| 905 | expect(yield* fileSystem.exists(path.join(cwd, ".bare/config"))).toBe(false); |
| 906 | expect((yield* fileSystem.readDirectory(path.join(cwd, ".bare/hooks"))).toSorted()).toEqual( |
| 907 | [".git", "pre-push.sample"], |
| 908 | ); |
| 909 | expect(yield* fileSystem.exists(path.join(cwd, "sub"))).toBe(false); |
| 910 | expect( |
| 911 | yield* fileSystem.readFileString(path.join(cwd, ".bare/worktrees/main/commondir")), |
| 912 | ).toBe("../..\n"); |
| 913 | }), |
| 914 | ); |
| 915 | |
| 916 | it.effect.skipIf(!symlinksSupported)( |
| 917 | "refuses writes through a git config that links to a file outside git's folders", |
| 918 | () => |
| 919 | Effect.gen(function* () { |
| 920 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 921 | const fileSystem = yield* FileSystem.FileSystem; |
| 922 | const path = yield* Path.Path; |
| 923 | const cwd = yield* makeTempDir; |
| 924 | yield* writeTextFile(cwd, "shared/config", "[core]\n"); |
| 925 | yield* fileSystem.makeDirectory(path.join(cwd, ".git")); |
| 926 | yield* fileSystem.symlink("../shared/config", path.join(cwd, ".git/config")); |
| 927 | |
| 928 | const error = yield* workspaceFileSystem |
| 929 | .writeFile({ cwd, relativePath: ".git/config", contents: "[core]\n\tfsmonitor = x\n" }) |
| 930 | .pipe(Effect.flip); |
| 931 | |
| 932 | expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspaceFilePathEscapeError); |
| 933 | expect(yield* fileSystem.readFileString(path.join(cwd, "shared/config"))).toBe( |
| 934 | "[core]\n", |
| 935 | ); |
| 936 | }), |
| 937 | ); |
| 938 | |
| 939 | it.effect("writes files that sit beside git's folders", () => |
| 940 | Effect.gen(function* () { |
| 941 | const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 942 | const fileSystem = yield* FileSystem.FileSystem; |
| 943 | const path = yield* Path.Path; |
| 944 | const parentDir = yield* makeTempDir; |
| 945 | const bare = path.join(parentDir, "repo.git"); |
| 946 | yield* writeTextFile(bare, "HEAD", "ref: refs/heads/main\n"); |
| 947 | yield* writeTextFile(bare, "objects/info/packs"); |
| 948 | yield* writeTextFile(bare, "refs/heads/main"); |
| 949 | yield* writeTextFile(bare, "main/.git", `gitdir: ${path.join(bare, "worktrees/main")}\n`); |
| 950 | |
| 951 | for (const [cwd, relativePath] of [ |
| 952 | [path.join(bare, "main"), ".gitignore"], |
| 953 | [path.join(bare, "main"), ".gitattributes"], |
| 954 | [path.join(bare, "main"), ".github/workflows/ci.yml"], |
| 955 | [path.join(bare, "main"), "vendor/lib.git/README.md"], |
| 956 | [path.join(bare, "main/.github"), "CODEOWNERS"], |
| 957 | [bare, "main/plan.md"], |
| 958 | ] as const) { |
| 959 | yield* workspaceFileSystem.writeFile({ cwd, relativePath, contents: "ok\n" }); |
| 960 | expect(yield* fileSystem.readFileString(path.join(cwd, relativePath))).toBe("ok\n"); |
| 961 | } |
| 962 | }), |
| 963 | ); |
| 964 | }); |
| 965 | }); |