从 Git 跟踪成员推导目录,根目录始终检查,其余目录权重严格大于 5 时要求 README。 直接子目录和文件各计 1,README 不计数,同名脚本与唯一测试成对计 1。 新增独立命令、针对性测试、算法说明和实际缺口报告,不新增配置文件或批量生成文档。 验证:938 项测试通过,类型与版本检查通过,校验脚本及测试独立类型检查通过。 仓库审计:80 个目录中 28 个需要 README,11 个通过,17 个缺失。
134 lines
6.3 KiB
TypeScript
134 lines
6.3 KiB
TypeScript
import { afterEach, expect, test } from "bun:test";
|
|
import { execFileSync, spawnSync } from "node:child_process";
|
|
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { dirname, join, resolve } from "node:path";
|
|
import { checkReadmes, fileUnits } from "./check-readme.js";
|
|
|
|
const roots: string[] = [];
|
|
afterEach(() => { for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); });
|
|
function fixture(readme = true) {
|
|
const root = mkdtempSync(join(tmpdir(), "hose readme 检查 "));
|
|
roots.push(root);
|
|
const git = (...args: string[]) => execFileSync("git", ["-C", root, ...args], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] });
|
|
git("init", "--quiet");
|
|
const write = (path: string, content = "fixture", tracked = true) => {
|
|
const target = join(root, path);
|
|
mkdirSync(dirname(target), { recursive: true });
|
|
writeFileSync(target, content);
|
|
if (tracked) git("add", "--", path);
|
|
};
|
|
if (readme) write("README.md", "# Fixture");
|
|
const files = (dir: string, count: number) => { for (let i = 0; i < count; i++) write(`${dir}/file-${i}.ts`); };
|
|
const cli = (...args: string[]) => spawnSync(process.execPath, [resolve(import.meta.dir, "check-readme.ts"), `--root=${root}`, ...args], { encoding: "utf8" });
|
|
return { root, git, write, files, cli, result: (path: string) => checkReadmes(root).results.find(item => item.directory === path)! };
|
|
}
|
|
|
|
test("脚本与唯一同名 .test.* 合并,独立测试和资源文件不误配", () => {
|
|
expect(fileUnits(["foo.ts", "foo.test.ts", "install.sh", "install.test.ts", "build.mjs", "build.test.ts"])).toEqual([
|
|
["build.mjs", "build.test.ts"], ["foo.ts", "foo.test.ts"], ["install.sh", "install.test.ts"],
|
|
]);
|
|
expect(fileUnits(["foo.json", "foo.test.ts", "bar.test.ts", "foo.spec.ts"])).toHaveLength(4);
|
|
expect(fileUnits(["foo.ts", "foo.js", "foo.test.ts"])).toHaveLength(3);
|
|
expect(fileUnits(["README.md", "README.foo.md", "readme.markdown", "README", "foo.ts"])).toEqual([["foo.ts"]]);
|
|
});
|
|
|
|
test("根目录不受阈值影响,5 不要求,6 要求;测试对只算 1", () => {
|
|
const f = fixture(false);
|
|
expect(checkReadmes(f.root).results[0]?.status).toBe("missing");
|
|
f.write("README.md", "# Root");
|
|
f.files("src", 4);
|
|
f.write("src/example.ts");
|
|
f.write("src/example.test.ts");
|
|
expect(f.result("src")).toMatchObject({ weight: 5, required: false, status: "not-required" });
|
|
f.write("src/sixth.ts");
|
|
expect(f.result("src")).toMatchObject({ weight: 6, required: true, status: "missing", expectedReadme: "README.src.md" });
|
|
f.write("src/README.src.md", "# Source");
|
|
expect(f.result("src")).toMatchObject({ weight: 6, status: "ok" });
|
|
});
|
|
|
|
test("直接子目录各计 1,不累计后代文件,低权重父目录不阻止向下检查", () => {
|
|
const f = fixture();
|
|
f.files("parent/child", 8);
|
|
f.write("parent/other/example.ts");
|
|
expect(f.result("parent")).toMatchObject({ weight: 2, directoryWeight: 2, required: false });
|
|
expect(f.result("parent/child")).toMatchObject({ weight: 8, status: "missing" });
|
|
f.files("mixed", 4);
|
|
f.write("mixed/child/a.ts");
|
|
f.write("mixed/other/b.ts");
|
|
expect(f.result("mixed")).toMatchObject({ weight: 6, directoryWeight: 2, status: "missing" });
|
|
});
|
|
|
|
test("配对只在同一目录内进行", () => {
|
|
const f = fixture();
|
|
f.write("a/foo.ts");
|
|
f.write("b/foo.test.ts");
|
|
expect(f.result("a").fileUnits).toEqual([["foo.ts"]]);
|
|
expect(f.result("b").fileUnits).toEqual([["foo.test.ts"]]);
|
|
});
|
|
|
|
test("只检查 Git 跟踪成员;未跟踪、嵌套忽略内容不计数,已跟踪的忽略项仍计数", () => {
|
|
const f = fixture();
|
|
f.write(".gitignore", "ignored/\n");
|
|
f.write("src/.gitignore", "local/\n");
|
|
f.write("ignored/a.ts", "ignored", false);
|
|
f.write("src/local/a.ts", "ignored", false);
|
|
f.write("untracked/a.ts", "untracked", false);
|
|
f.files("src", 4);
|
|
expect(f.result("src").weight).toBe(5);
|
|
expect(checkReadmes(f.root).results.map(item => item.directory)).toEqual([".", "src"]);
|
|
f.write("ignored/tracked.ts", "tracked", false);
|
|
f.git("add", "-f", "--", "ignored/tracked.ts");
|
|
expect(f.result("ignored").weight).toBe(1);
|
|
});
|
|
|
|
test("校验真实大小写、错名、空文档和符号链接;检查不写文件", () => {
|
|
const f = fixture();
|
|
f.files("src", 6);
|
|
f.write("src/README.md", "# Wrong name");
|
|
expect(f.result("src").status).toBe("misnamed");
|
|
rmSync(join(f.root, "src/README.md"));
|
|
f.write("src/readme.src.md", "# Wrong case");
|
|
expect(f.result("src").status).toBe("misnamed");
|
|
rmSync(join(f.root, "src/readme.src.md"));
|
|
f.write("src/README.src.md", " \n\t ");
|
|
expect(f.result("src").status).toBe("empty");
|
|
rmSync(join(f.root, "src/README.src.md"));
|
|
symlinkSync(join(f.root, "README.md"), join(f.root, "src/README.src.md"));
|
|
expect(f.result("src").status).toBe("invalid-file");
|
|
const before = f.git("status", "--porcelain");
|
|
checkReadmes(f.root);
|
|
expect(f.git("status", "--porcelain")).toBe(before);
|
|
});
|
|
|
|
test("符号链接目录不展开,工作区中已删除的文件不计权重", () => {
|
|
const f = fixture();
|
|
f.files("src", 6);
|
|
rmSync(join(f.root, "src/file-0.ts"));
|
|
expect(f.result("src").weight).toBe(5);
|
|
mkdirSync(join(f.root, "outside"));
|
|
writeFileSync(join(f.root, "outside/secret.txt"), "fixture");
|
|
symlinkSync(join(f.root, "outside"), join(f.root, "link"));
|
|
f.git("add", "--", "link");
|
|
expect(checkReadmes(f.root).results.map(item => item.directory)).not.toContain("link");
|
|
});
|
|
|
|
test("CLI 支持 JSON、阈值与稳定退出码,非 Git 路径或无效参数返回 2", () => {
|
|
const f = fixture();
|
|
expect(f.cli().status).toBe(0);
|
|
f.files("src", 6);
|
|
const failed = f.cli("--json");
|
|
expect(failed.status).toBe(1);
|
|
expect(JSON.parse(failed.stdout)).toMatchObject({ threshold: 5, failed: 1 });
|
|
expect(f.cli("--threshold", "6").status).toBe(0);
|
|
expect(f.cli("--threshold=5").status).toBe(1);
|
|
for (const arg of ["--threshold=-1", "--threshold=1.5", "--threshold=9007199254740992", "--unknown", "--threshold"]) {
|
|
expect(f.cli("--json", arg).status).toBe(2);
|
|
}
|
|
const dir = mkdtempSync(join(tmpdir(), "hose-non-git-")); roots.push(dir);
|
|
const invalid = f.cli("--json", `--root=${dir}`);
|
|
expect(invalid.status).toBe(2);
|
|
expect(JSON.parse(invalid.stdout).error).toBeString();
|
|
expect(f.cli("--help").status).toBe(0);
|
|
});
|