-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathwalker.test.ts
More file actions
82 lines (72 loc) · 3.02 KB
/
Copy pathwalker.test.ts
File metadata and controls
82 lines (72 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { test, expect, beforeEach, afterEach } from "bun:test";
import { mkdir, writeFile, rm, symlink } from "fs/promises";
import { join } from "path";
import { tmpdir } from "os";
import { scanDirectory } from "../src/scan/walker.ts";
let dir: string;
beforeEach(async () => {
dir = join(tmpdir(), `codemogger-test-${Date.now()}`);
await mkdir(dir, { recursive: true });
});
afterEach(async () => {
await rm(dir, { recursive: true, force: true });
});
test("finds TypeScript files", async () => {
await writeFile(join(dir, "a.ts"), "export const x = 1;");
await writeFile(join(dir, "b.ts"), "export const y = 2;");
const { files, errors } = await scanDirectory(dir);
expect(errors).toHaveLength(0);
expect(files.map(f => f.relPath).sort()).toEqual(["a.ts", "b.ts"]);
});
test("skips files above 1MB", async () => {
await writeFile(join(dir, "big.ts"), "x".repeat(1_100_000));
await writeFile(join(dir, "small.ts"), "export const x = 1;");
const { files } = await scanDirectory(dir);
expect(files.map(f => f.relPath)).toEqual(["small.ts"]);
});
test("respects .gitignore patterns", async () => {
await writeFile(join(dir, ".gitignore"), "ignored/\n");
await mkdir(join(dir, "ignored"));
await writeFile(join(dir, "ignored", "secret.ts"), "export const s = 1;");
await writeFile(join(dir, "visible.ts"), "export const v = 1;");
const { files } = await scanDirectory(dir);
expect(files.map(f => f.relPath)).toEqual(["visible.ts"]);
});
test("does not follow directory symlinks", async () => {
const target = join(tmpdir(), `codemogger-link-target-${Date.now()}`);
await mkdir(target);
await writeFile(join(target, "hidden.ts"), "export const h = 1;");
await symlink(target, join(dir, "link"));
await writeFile(join(dir, "real.ts"), "export const r = 1;");
try {
const { files } = await scanDirectory(dir);
expect(files.map(f => f.relPath)).toEqual(["real.ts"]);
} finally {
await rm(target, { recursive: true, force: true });
}
});
test("does not follow file symlinks", async () => {
const target = join(tmpdir(), `codemogger-file-target-${Date.now()}.ts`);
await writeFile(target, "export const h = 1;");
await symlink(target, join(dir, "link.ts"));
await writeFile(join(dir, "real.ts"), "export const r = 1;");
try {
const { files } = await scanDirectory(dir);
expect(files.map(f => f.relPath)).toEqual(["real.ts"]);
} finally {
await rm(target, { force: true });
}
});
test("reports unreadable .gitignore as error", async () => {
// Write a directory named .gitignore so readFile() throws EISDIR
await mkdir(join(dir, ".gitignore"));
await writeFile(join(dir, "a.ts"), "export const x = 1;");
const { errors } = await scanDirectory(dir);
expect(errors.some(e => e.includes(".gitignore"))).toBe(true);
});
test("filters by language", async () => {
await writeFile(join(dir, "a.ts"), "export const x = 1;");
await writeFile(join(dir, "b.rs"), "fn main() {}");
const { files } = await scanDirectory(dir, ["typescript"]);
expect(files.map(f => f.relPath)).toEqual(["a.ts"]);
});