72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { describe, expect, it } from "@suaveplan/testing/runner";
|
|
|
|
const SRC = join(__dirname, "..", "src");
|
|
|
|
function walk(dir: string): string[] {
|
|
const out: string[] = [];
|
|
for (const entry of readdirSync(dir)) {
|
|
const full = join(dir, entry);
|
|
if (statSync(full).isDirectory()) {
|
|
out.push(...walk(full));
|
|
} else if (full.endsWith(".ts")) {
|
|
out.push(full);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function find(
|
|
pattern: RegExp,
|
|
options: { includeTests?: boolean; onlyIndex?: boolean } = {}
|
|
): string[] {
|
|
const files = walk(SRC).filter((f) => {
|
|
if (options.includeTests !== true && f.endsWith(".test.ts"))
|
|
return false;
|
|
if (options.onlyIndex === true && !f.endsWith("index.ts")) return false;
|
|
return true;
|
|
});
|
|
const hits: string[] = [];
|
|
for (const f of files) {
|
|
const content = readFileSync(f, "utf8");
|
|
const lines = content.split("\n");
|
|
for (let i = 0; i < lines.length; i++) {
|
|
if (pattern.test(lines[i]!)) {
|
|
hits.push(`${f}:${i + 1}: ${lines[i]!.trim()}`);
|
|
}
|
|
}
|
|
}
|
|
return hits;
|
|
}
|
|
|
|
describe("emailer no-disallowed patterns", () => {
|
|
it("no vi.mock / jest.mock", { timeout: 5000 }, () => {
|
|
expect(find(/\b(vi|jest)\.mock\(/, { includeTests: true })).toEqual([]);
|
|
});
|
|
|
|
it("no console.log in production src", { timeout: 5000 }, () => {
|
|
expect(find(/console\.log\(/)).toEqual([]);
|
|
});
|
|
|
|
it("no biome-ignore comments", { timeout: 5000 }, () => {
|
|
expect(find(/biome-ignore/, { includeTests: true })).toEqual([]);
|
|
});
|
|
|
|
it("no direct vitest / bun:test imports in test files", {
|
|
timeout: 5000,
|
|
}, () => {
|
|
expect(
|
|
find(/from ['"](vitest|bun:test)['"]/, { includeTests: true })
|
|
).toEqual([]);
|
|
});
|
|
|
|
it("no export * in non-root barrels", { timeout: 5000 }, () => {
|
|
const hits = find(/^\s*export\s+(type\s+)?\*\s+from/, {
|
|
onlyIndex: true,
|
|
includeTests: true,
|
|
}).filter((h) => !h.includes(`${SRC}/index.ts:`));
|
|
expect(hits).toEqual([]);
|
|
});
|
|
});
|