@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 06:00 UTC

main
ts 560 lines 18.7 KB
Raw
1 import { describe, expect, it, layer } from "@alchemy.run/test";
2 import * as NodeServices from "@effect/platform-node/NodeServices";
3 import * as Effect from "effect/Effect";
4 import * as FileSystem from "effect/FileSystem";
5 import * as Path from "effect/Path";
6 import * as ChildProcess from "effect/process/ChildProcess";
7 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
8 import {
9 combineIgnoreRules,
10 type IgnoreDialect,
11 listFileSystemDirectory,
12 parseIgnoreRules,
13 walkIgnoring,
14 type WalkEntryType,
15 } from "@/Util/Ignore.ts";
16
17 // ---------------------------------------------------------------------------
18 // In-memory file trees
19 //
20 // "a/b.txt" a file (parents are implied)
21 // "a/empty/" an empty directory
22 // "a/link -> ../x" a symlink
23 // "locked/!" an unreadable directory: listing it fails
24 // ---------------------------------------------------------------------------
25
26 interface Tree {
27 readonly children: Map<string, Map<string, WalkEntryType>>;
28 readonly links: Map<string, string>;
29 readonly unreadable: Set<string>;
30 }
31
32 const tree = (...entries: string[]): Tree => {
33 const children = new Map<string, Map<string, WalkEntryType>>([["", new Map()]]);
34 const links = new Map<string, string>();
35 const unreadable = new Set<string>();
36 const add = (path: string, type: WalkEntryType) => {
37 const parts = path.split("/");
38 for (let index = 0; index < parts.length; index++) {
39 const parent = parts.slice(0, index).join("/");
40 const self = parts.slice(0, index + 1).join("/");
41 const isLeaf = index === parts.length - 1;
42 children.get(parent)!.set(parts[index], isLeaf ? type : "Directory");
43 if (!isLeaf || type === "Directory") {
44 if (!children.has(self)) children.set(self, new Map());
45 }
46 }
47 };
48 for (const entry of entries) {
49 if (entry.endsWith("/!")) {
50 const directory = entry.slice(0, -2);
51 add(directory, "Directory");
52 unreadable.add(directory);
53 } else if (entry.includes(" -> ")) {
54 const [path, target] = entry.split(" -> ");
55 add(path, "SymbolicLink");
56 links.set(path, target);
57 } else if (entry.endsWith("/")) {
58 add(entry.slice(0, -1), "Directory");
59 } else {
60 add(entry, "File");
61 }
62 }
63 return { children, links, unreadable };
64 };
65
66 /** Walk an in-memory tree, recording which directories were read. */
67 const walkTree = (fileTree: Tree, ignore: string, dialect: IgnoreDialect, force: string[] = []) =>
68 Effect.gen(function* () {
69 const reads: string[] = [];
70 const entries = yield* walkIgnoring({
71 rules: parseIgnoreRules(ignore, dialect),
72 force,
73 list: (directory) =>
74 Effect.suspend(() => {
75 reads.push(directory);
76 if (fileTree.unreadable.has(directory)) {
77 return Effect.fail(`EACCES: ${directory}`);
78 }
79 return Effect.succeed(
80 [...fileTree.children.get(directory)!].map(([name, type]) => ({ name, type })),
81 );
82 }),
83 });
84 return {
85 kept: entries.map((entry) => (entry.type === "Directory" ? `${entry.path}/` : entry.path)),
86 files: entries.filter((entry) => entry.type !== "Directory").map((entry) => entry.path),
87 reads,
88 };
89 });
90
91 interface Case {
92 readonly name: string;
93 readonly ignore: string;
94 readonly tree: string[];
95 /** Expected non-directory entries kept by the walk. */
96 readonly files: string[];
97 /** Directories the walk must never read. */
98 readonly notRead?: string[];
99 }
100
101 // ---------------------------------------------------------------------------
102 // gitignore
103 // ---------------------------------------------------------------------------
104
105 const gitignoreCases: Case[] = [
106 {
107 name: "a bare name matches at any depth",
108 ignore: "foo\n",
109 tree: ["foo", "a/foo", "b/c/foo", "a/bar"],
110 files: [".gitignore", "a/bar"],
111 },
112 {
113 name: "a leading slash anchors to the root",
114 ignore: "/foo\n",
115 tree: ["foo", "a/foo"],
116 files: [".gitignore", "a/foo"],
117 },
118 {
119 name: "a trailing slash matches directories only",
120 ignore: "build/\n",
121 tree: ["build/x", "a/build/y", "lib/build"],
122 files: [".gitignore", "lib/build"],
123 notRead: ["build", "a/build"],
124 },
125 {
126 name: "a wildcard matches at any depth",
127 ignore: "*.log\n",
128 tree: ["x.log", "a/b.log", "a.txt"],
129 files: [".gitignore", "a.txt"],
130 },
131 {
132 name: "a later negation re-includes a file",
133 ignore: "*.log\n!keep.log\n",
134 tree: ["x.log", "keep.log", "a/keep.log", "a/y.log"],
135 files: [".gitignore", "a/keep.log", "keep.log"],
136 },
137 {
138 name: "nothing below an excluded directory can be re-included",
139 ignore: "logs/\n!logs/keep.log\n",
140 tree: ["logs/keep.log", "logs/x.log", "app.ts"],
141 files: [".gitignore", "app.ts"],
142 notRead: ["logs"],
143 },
144 {
145 name: "excluding a directory's contents allows re-inclusion",
146 ignore: "logs/*\n!logs/keep.log\n",
147 tree: ["logs/keep.log", "logs/x.log"],
148 files: [".gitignore", "logs/keep.log"],
149 },
150 {
151 name: "**/ matches the directory at any depth",
152 ignore: "**/cache\n",
153 tree: ["cache/a", "x/cache/b", "x/y/cache/c", "x/keep"],
154 files: [".gitignore", "x/keep"],
155 notRead: ["cache", "x/cache", "x/y/cache"],
156 },
157 {
158 name: "/**/ spans zero or more directories",
159 ignore: "a/**/b\n",
160 tree: ["a/b", "a/x/b", "a/x/y/b", "c/a/b"],
161 files: [".gitignore", "c/a/b"],
162 },
163 {
164 name: "a single * does not cross directories",
165 ignore: "doc/*.txt\n",
166 tree: ["doc/x.txt", "doc/sub/y.txt"],
167 files: [".gitignore", "doc/sub/y.txt"],
168 },
169 {
170 name: "a pattern with an inner slash is anchored to the root",
171 ignore: "a/b\n",
172 tree: ["a/b", "x/a/b"],
173 files: [".gitignore", "x/a/b"],
174 },
175 {
176 name: "comments, blank lines, and escaped #",
177 ignore: "# comment\n\n\\#hash\n",
178 tree: ["#hash", "# comment", "keep"],
179 files: ["# comment", ".gitignore", "keep"],
180 },
181 {
182 name: "character classes and ?",
183 ignore: "file[0-9].txt\na?.md\n",
184 tree: ["file1.txt", "fileA.txt", "ab.md", "abc.md"],
185 files: [".gitignore", "abc.md", "fileA.txt"],
186 },
187 {
188 name: "the ignore file can exclude itself",
189 ignore: ".gitignore\nsecret\n",
190 tree: ["secret", "public"],
191 files: ["public"],
192 },
193 ];
194
195 // ---------------------------------------------------------------------------
196 // dockerignore
197 // ---------------------------------------------------------------------------
198
199 const dockerignoreCases: Case[] = [
200 {
201 name: "a bare name matches only at the root",
202 ignore: "foo\n",
203 tree: ["foo", "a/foo", "a/bar"],
204 files: [".dockerignore", "a/bar", "a/foo"],
205 },
206 {
207 name: "**/ matches at any depth",
208 ignore: "**/foo\n",
209 tree: ["foo", "a/foo", "a/b/foo", "a/bar"],
210 files: [".dockerignore", "a/bar"],
211 },
212 {
213 name: "a wildcard matches only at the root",
214 ignore: "*.log\n",
215 tree: ["x.log", "a/b.log", "a.txt"],
216 files: [".dockerignore", "a.txt", "a/b.log"],
217 },
218 {
219 name: "a later negation re-includes below an excluded directory",
220 ignore: "logs\n!logs/keep.log\n",
221 tree: ["logs/keep.log", "logs/x.log", "app.ts"],
222 files: [".dockerignore", "app.ts", "logs/keep.log"],
223 },
224 {
225 name: "an excluded directory is not read",
226 ignore: "node_modules\n",
227 tree: ["node_modules/pkg/index.js", "src/index.ts"],
228 files: [".dockerignore", "src/index.ts"],
229 notRead: ["node_modules"],
230 },
231 {
232 name: "dir/** prunes the directory",
233 ignore: "node_modules/**\n",
234 tree: ["node_modules/pkg/index.js", "src/index.ts"],
235 files: [".dockerignore", "src/index.ts"],
236 notRead: ["node_modules"],
237 },
238 {
239 name: "a negation elsewhere does not stop pruning",
240 ignore: "ignored\n!other/keep\n",
241 tree: ["ignored/deep/x", "other/keep", "other/drop"],
242 files: [".dockerignore", "other/drop", "other/keep"],
243 notRead: ["ignored"],
244 },
245 {
246 name: "the last matching rule wins",
247 ignore: "!keep.txt\n*.txt\n",
248 tree: ["keep.txt", "drop.txt", "a.md"],
249 files: [".dockerignore", "a.md"],
250 },
251 {
252 name: "an allowlist re-includes one subtree",
253 ignore: "**\n!src\n",
254 tree: ["src/index.ts", "src/lib/a.ts", "docs/readme.md", "package.json"],
255 files: ["src/index.ts", "src/lib/a.ts"],
256 notRead: ["docs"],
257 },
258 {
259 name: "leading / and ./ are stripped",
260 ignore: "/foo\n./bar\n",
261 tree: ["foo", "bar", "a/foo"],
262 files: [".dockerignore", "a/foo"],
263 },
264 {
265 name: "a trailing slash also matches a file",
266 ignore: "build/\n",
267 tree: ["build", "lib/build"],
268 files: [".dockerignore", "lib/build"],
269 },
270 {
271 name: "/**/ spans zero or more directories",
272 ignore: "a/**/b\n",
273 tree: ["a/b", "a/x/b", "a/x/y/b", "c/a/b"],
274 files: [".dockerignore", "c/a/b"],
275 },
276 {
277 name: "comments and escaped !",
278 ignore: "# comment\n\\!bang\n",
279 tree: ["!bang", "# comment", "keep"],
280 files: ["# comment", ".dockerignore", "keep"],
281 },
282 {
283 name: "the ignore file can exclude itself",
284 ignore: ".dockerignore\nsecret\n",
285 tree: ["secret", "public"],
286 files: ["public"],
287 },
288 ];
289
290 const withIgnoreFile = (dialect: IgnoreDialect, testCase: Case) =>
291 tree(dialect === "gitignore" ? ".gitignore" : ".dockerignore", ...testCase.tree);
292
293 describe("Ignore rules (in-memory trees)", { tags: ["unit", "local"] }, () => {
294 for (const [dialect, cases] of [
295 ["gitignore", gitignoreCases],
296 ["dockerignore", dockerignoreCases],
297 ] as const) {
298 describe(dialect, () => {
299 for (const testCase of cases) {
300 it.effect(testCase.name, () =>
301 Effect.gen(function* () {
302 const { files, reads } = yield* walkTree(
303 withIgnoreFile(dialect, testCase),
304 testCase.ignore,
305 dialect,
306 );
307 expect(files).toEqual([...testCase.files].sort());
308 for (const directory of testCase.notRead ?? []) {
309 expect(reads).not.toContain(directory);
310 }
311 }),
312 );
313 }
314 });
315 }
316
317 describe("dialects disagree", () => {
318 for (const [name, ignore, files, git, docker] of [
319 ["bare name depth", "foo\n", ["foo", "a/foo"], [], ["a/foo"]],
320 ["wildcard depth", "*.log\n", ["x.log", "a/y.log"], [], ["a/y.log"]],
321 [
322 "re-including below an excluded directory",
323 "logs\n!logs/keep.log\n",
324 ["logs/keep.log", "logs/x.log"],
325 [],
326 ["logs/keep.log"],
327 ],
328 ] as const) {
329 it.effect(name, () =>
330 Effect.gen(function* () {
331 const kept = (dialect: IgnoreDialect) =>
332 walkTree(tree(...files), ignore, dialect).pipe(Effect.map((result) => result.files));
333 expect(yield* kept("gitignore")).toEqual([...git]);
334 expect(yield* kept("dockerignore")).toEqual([...docker]);
335 }),
336 );
337 }
338 });
339
340 describe("walking", () => {
341 it.effect("never reads an excluded, unreadable directory", () =>
342 Effect.gen(function* () {
343 for (const dialect of ["gitignore", "dockerignore"] as const) {
344 const { files } = yield* walkTree(
345 tree("data/postgres/!", "src/index.ts"),
346 "data\n",
347 dialect,
348 );
349 expect(files).toEqual(["src/index.ts"]);
350 }
351 }),
352 );
353
354 it.effect("fails on an unreadable directory that is not excluded", () =>
355 Effect.gen(function* () {
356 const error = yield* walkTree(tree("data/!", "src/index.ts"), "", "dockerignore").pipe(
357 Effect.flip,
358 );
359 expect(error).toBe("EACCES: data");
360 }),
361 );
362
363 it.effect("lists symlinks as links and never follows them", () =>
364 Effect.gen(function* () {
365 const { kept, reads } = yield* walkTree(
366 tree("app/node_modules -> ../node_modules", "node_modules/pkg/index.js", "loop -> ."),
367 "",
368 "dockerignore",
369 );
370 expect(kept).toContain("app/node_modules");
371 expect(kept).toContain("loop");
372 expect(reads).not.toContain("loop");
373 expect(reads).not.toContain("app/node_modules");
374 }),
375 );
376
377 it.effect("lists empty and excluded-but-walked directories correctly", () =>
378 Effect.gen(function* () {
379 const { kept } = yield* walkTree(
380 tree("empty/", "logs/keep.log", "logs/x.log"),
381 "logs\n!logs/keep.log\n",
382 "dockerignore",
383 );
384 // `logs` itself is excluded but walked; `empty` is kept.
385 expect(kept).toEqual(["empty/", "logs/keep.log"]);
386 }),
387 );
388
389 it.effect("forced paths are kept and their parents are never pruned", () =>
390 Effect.gen(function* () {
391 const { files } = yield* walkTree(
392 tree("build/Dockerfile", "build/out.js"),
393 "build\n",
394 "dockerignore",
395 ["build/Dockerfile"],
396 );
397 expect(files).toEqual(["build/Dockerfile"]);
398 }),
399 );
400
401 it.effect("combines rule sets anchored at different directories", () =>
402 Effect.gen(function* () {
403 // The walk root is `packages/app`; the repository root ignores
404 // `/packages/app/dist` and `*.log`, the app ignores `.cache`.
405 const rules = combineIgnoreRules("gitignore", [
406 parseIgnoreRules("/packages/app/dist\n*.log\n", "gitignore", {
407 prefix: "packages/app",
408 }),
409 parseIgnoreRules(".cache\n", "gitignore"),
410 ]);
411 const fileTree = tree("dist/a.js", "x.log", ".cache/y", "src/index.ts");
412 const entries = yield* walkIgnoring({
413 rules,
414 list: (directory) =>
415 Effect.succeed(
416 [...fileTree.children.get(directory)!].map(([name, type]) => ({ name, type })),
417 ),
418 });
419 expect(entries.map((entry) => entry.path)).toEqual(["src", "src/index.ts"]);
420 }),
421 );
422
423 // A project inside an ignored folder (a staging dir, a generated
424 // workspace) still lists its own files: only directories below the
425 // walk root can exclude their contents.
426 it.effect("an ancestor rule excluding the walk root does not hide it", () =>
427 Effect.gen(function* () {
428 const rules = parseIgnoreRules(".tmp\n*.log\n", "gitignore", {
429 prefix: "packages/alchemy/.tmp/fixture-1",
430 });
431 const fileTree = tree("index.html", "src/main.ts", "debug.log");
432 const entries = yield* walkIgnoring({
433 rules,
434 list: (directory) =>
435 Effect.succeed(
436 [...fileTree.children.get(directory)!].map(([name, type]) => ({ name, type })),
437 ),
438 });
439 expect(entries.map((entry) => entry.path)).toEqual(["index.html", "src", "src/main.ts"]);
440 }),
441 );
442 });
443 });
444
445 // ---------------------------------------------------------------------------
446 // Conformance: the same cases on disk, against the tools themselves.
447 // ---------------------------------------------------------------------------
448
449 const materialize = Effect.fn(function* (root: string, entries: string[]) {
450 const fs = yield* FileSystem.FileSystem;
451 const path = yield* Path.Path;
452 for (const entry of entries) {
453 if (entry.includes(" -> ")) {
454 const [link, target] = entry.split(" -> ");
455 yield* fs.makeDirectory(path.dirname(path.join(root, link)), { recursive: true });
456 yield* fs.symlink(target, path.join(root, link));
457 } else if (entry.endsWith("/")) {
458 yield* fs.makeDirectory(path.join(root, entry), { recursive: true });
459 } else {
460 yield* fs.makeDirectory(path.dirname(path.join(root, entry)), { recursive: true });
461 yield* fs.writeFileString(path.join(root, entry), entry);
462 }
463 }
464 });
465
466 const ours = Effect.fn(function* (root: string, ignore: string, dialect: IgnoreDialect) {
467 const entries = yield* walkIgnoring({
468 list: yield* listFileSystemDirectory(root),
469 rules: parseIgnoreRules(ignore, dialect),
470 });
471 return entries.filter((entry) => entry.type !== "Directory").map((entry) => entry.path);
472 });
473
474 /** All non-directory paths under a root, sorted. */
475 const listFiles = (root: string) =>
476 Effect.gen(function* () {
477 const entries = yield* walkIgnoring({ list: yield* listFileSystemDirectory(root) });
478 return entries.filter((entry) => entry.type !== "Directory").map((entry) => entry.path);
479 });
480
481 const run = (command: string, args: string[], cwd: string) =>
482 Effect.gen(function* () {
483 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
484 return yield* spawner.string(
485 ChildProcess.make(command, args, {
486 cwd,
487 // Keep the user's global git config and excludes out of the result.
488 env: { GIT_CONFIG_GLOBAL: "/dev/null", GIT_CONFIG_NOSYSTEM: "1" },
489 extendEnv: true,
490 }),
491 );
492 });
493
494 layer(NodeServices.layer)("Ignore rules match the real tools", (it) => {
495 for (const testCase of gitignoreCases) {
496 it.effect(
497 `git: ${testCase.name}`,
498 () =>
499 Effect.gen(function* () {
500 const fs = yield* FileSystem.FileSystem;
501 const path = yield* Path.Path;
502 const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-ignore-git-" });
503 yield* materialize(root, testCase.tree);
504 yield* fs.writeFileString(path.join(root, ".gitignore"), testCase.ignore);
505 yield* run("git", ["init", "-q"], root);
506 const git = (yield* run(
507 "git",
508 ["ls-files", "--others", "--exclude-standard", "-z"],
509 root,
510 ))
511 .split("\0")
512 .filter((entry) => entry.length > 0)
513 .sort();
514 // Git never lists its own `.git` directory.
515 const kept = (yield* ours(root, testCase.ignore, "gitignore")).filter(
516 (entry) => !entry.startsWith(".git/"),
517 );
518 expect(kept).toEqual(git);
519 }),
520 { tags: ["local"] },
521 );
522 }
523
524 for (const testCase of dockerignoreCases) {
525 it.effect(
526 `docker: ${testCase.name}`,
527 () =>
528 Effect.gen(function* () {
529 const fs = yield* FileSystem.FileSystem;
530 const path = yield* Path.Path;
531 const scratch = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-ignore-docker-" });
532 const context = path.join(scratch, "context");
533 const out = path.join(scratch, "out");
534 yield* materialize(context, testCase.tree);
535 yield* fs.writeFileString(path.join(context, ".dockerignore"), testCase.ignore);
536 // Outside the context so the Dockerfile itself is not part of it.
537 const dockerfile = path.join(scratch, "Containerfile");
538 yield* fs.writeFileString(dockerfile, "FROM scratch\nCOPY . /\n");
539 yield* run(
540 "docker",
541 [
542 "buildx",
543 "build",
544 "-q",
545 "-f",
546 dockerfile,
547 "--output",
548 `type=local,dest=${out}`,
549 context,
550 ],
551 scratch,
552 );
553 expect(yield* ours(context, testCase.ignore, "dockerignore")).toEqual(
554 yield* listFiles(out),
555 );
556 }),
557 { tags: ["provider:docker", "local"], timeout: 60_000 },
558 );
559 }
560 });