mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-11-16 20:45:33 -08:00
Tighten PR Prep and Test after their first runs
AI Assisted
This commit is contained in:
1 parent
ff1c793633
commit
bf23d7eeea
22 files changed
+261
-109
No files matched your search
@@ -102,8 +102,13 @@ export function globToRegExp(glob) {
|
||||
return new RegExp(`^${out}$`);
|
||||
}
|
||||
|
||||
// Compiled globs, by glob text (null for an unsupported one): a map's few
|
||||
// globs are tested against every file in a run.
|
||||
const compiled = new Map();
|
||||
|
||||
/** Does the path match the glob? Unsupported globs match nothing. */
|
||||
export function matchGlob(glob, p) {
|
||||
if (unsupported(glob)) return false;
|
||||
return globToRegExp(glob).test(fold(toPosix(p)));
|
||||
if (!compiled.has(glob)) compiled.set(glob, unsupported(glob) ? null : globToRegExp(glob));
|
||||
const re = compiled.get(glob);
|
||||
return re !== null && re.test(fold(toPosix(p)));
|
||||
}
|
||||
@@ -23,7 +23,7 @@ run-check.mjs: run one command and classify its outcome. JSON on stdout.
|
||||
{ ok, cmd, cwd, outcome, exit, signal, seconds, tail, log }.
|
||||
outcome passed (exit 0) · failed (any other exit) · timeout ·
|
||||
not-started (with reason: spawn-error, command-not-found,
|
||||
cwd-missing)
|
||||
cwd-missing, log-unwritable)
|
||||
tail the output's last 60 lines, stdout and stderr interleaved,
|
||||
each cut at 400 characters
|
||||
log the file holding the full output, when --log was given
|
||||
@@ -71,9 +71,22 @@ export function runCheck({ cmd, cwd, timeout, log }) {
|
||||
let timedOut = false;
|
||||
let done = false;
|
||||
let logStream = null;
|
||||
let logFailed = false;
|
||||
if (logFile) {
|
||||
fs.mkdirSync(path.dirname(logFile), { recursive: true });
|
||||
logStream = fs.createWriteStream(logFile);
|
||||
// Opened before the run, so a log that cannot be written stops the
|
||||
// check up front instead of crashing it halfway with no JSON.
|
||||
let fd;
|
||||
try {
|
||||
fs.mkdirSync(path.dirname(logFile), { recursive: true });
|
||||
fd = fs.openSync(logFile, "w");
|
||||
} catch {
|
||||
resolve({ ...base, outcome: "not-started", reason: "log-unwritable", exit: null, signal: null, seconds: 0, tail: [], log: null });
|
||||
return;
|
||||
}
|
||||
logStream = fs.createWriteStream(null, { fd });
|
||||
logStream.on("error", () => {
|
||||
logFailed = true;
|
||||
});
|
||||
}
|
||||
const child = spawn(cmd, { cwd: abs, shell: true, windowsHide: true, detached: process.platform !== "win32", stdio: ["ignore", "pipe", "pipe"] });
|
||||
const take = (chunk, isErr) => {
|
||||
@@ -83,7 +96,7 @@ export function runCheck({ cmd, cwd, timeout, log }) {
|
||||
// the Windows "not recognized" check.
|
||||
if (output.length > 1_000_000) output = output.slice(-TAIL_LINES * LINE_CAP * 4);
|
||||
if (isErr && stderrHead.length < 65536) stderrHead += s;
|
||||
if (logStream) logStream.write(chunk);
|
||||
if (logStream && !logFailed) logStream.write(chunk);
|
||||
};
|
||||
child.stdout.on("data", (c) => take(c, false));
|
||||
child.stderr.on("data", (c) => take(c, true));
|
||||
@@ -105,8 +118,9 @@ export function runCheck({ cmd, cwd, timeout, log }) {
|
||||
else if (code === 0) outcome = "passed";
|
||||
else if (code === 127 || code === 9009 || NOT_RECOGNIZED.test(stderrHead)) [outcome, reason] = ["not-started", "command-not-found"];
|
||||
else outcome = "failed";
|
||||
const result = { ...base, outcome, ...(reason ? { reason } : {}), exit: typeof code === "number" ? code : null, signal: signal || null, seconds, tail: tailOf(output), log: logFile };
|
||||
if (logStream) logStream.end(() => resolve(result));
|
||||
const result = { ...base, outcome, ...(reason ? { reason } : {}), exit: typeof code === "number" ? code : null, signal: signal || null, seconds, tail: tailOf(output), log: logFailed ? null : logFile };
|
||||
// An errored stream never finishes, so it is not waited on.
|
||||
if (logStream && !logFailed) logStream.end(() => resolve(result));
|
||||
else resolve(result);
|
||||
};
|
||||
|
||||
|
||||
@@ -26,10 +26,12 @@ test-commands.mjs: a project's test and check commands. JSON on stdout.
|
||||
the root folder's is cwd "." and covers "**"
|
||||
checks [{ kind, cmd }] kind is lint, types or build
|
||||
coverage [{ runner, cmd, cwd, reads }] only for tools already present;
|
||||
<out> in cmd is a placeholder for the output file; reads is
|
||||
<out> in cmd (already in quotes, so a path with spaces
|
||||
works) is a placeholder for the output file; reads is
|
||||
lcov or go-func
|
||||
builtin { language, cmd, others? } the language's own runner, only when
|
||||
there is no suite and no runner; otherwise null
|
||||
builtin { language, cmd, others? } the language's own runner (node or
|
||||
python; Go and Rust always show as runners), only when there
|
||||
is no suite and no runner; otherwise null
|
||||
|
||||
Only reads files. Exit: 0 ok · 2 bad arguments · 3 no such folder.
|
||||
`;
|
||||
@@ -43,7 +45,8 @@ const LANG_EXT = {
|
||||
go: [".go"],
|
||||
rust: [".rs"],
|
||||
};
|
||||
const BUILTIN = { node: "node --test", python: "python -m unittest", go: "go test ./...", rust: "cargo test" };
|
||||
// Node and Python only: a go.mod or a Cargo.toml always gives a runner above.
|
||||
const BUILTIN = { node: "node --test", python: "python -m unittest" };
|
||||
|
||||
function readJson(file) {
|
||||
const text = readText(file);
|
||||
@@ -115,7 +118,11 @@ function workspaceFolders(root, pkg, dirs) {
|
||||
if (pnpm !== null) {
|
||||
let inPackages = false;
|
||||
for (const line of pnpm.split(/\r?\n/)) {
|
||||
if (/^packages\s*:/.test(line)) inPackages = true;
|
||||
const inline = line.match(/^packages\s*:\s*\[([^\]]*)\]/);
|
||||
if (inline) {
|
||||
globs.push(...inline[1].split(",").map((g) => g.trim().replace(/^['"]|['"]$/g, "")));
|
||||
inPackages = false;
|
||||
} else if (/^packages\s*:/.test(line)) inPackages = true;
|
||||
else if (/^\S/.test(line)) inPackages = false;
|
||||
else if (inPackages) {
|
||||
const m = line.match(/^\s*-\s*['"]?([^'"#]+?)['"]?\s*(?:#.*)?$/);
|
||||
@@ -183,7 +190,7 @@ export function discover(root) {
|
||||
add(runners, { cmd: `${pm} test`, cwd, covers: covers(cwd) });
|
||||
const d = deps(p);
|
||||
if (/\bnode\s+--test\b/.test(test)) {
|
||||
add(coverage, { runner: "node", cmd: "node --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=<out>", cwd, reads: "lcov" });
|
||||
add(coverage, { runner: "node", cmd: "node --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=\"<out>\"", cwd, reads: "lcov" });
|
||||
}
|
||||
if (d.jest || /\bjest\b/.test(test)) add(coverage, { runner: "jest", cmd: "npx jest --coverage --coverageReporters=lcov", cwd, reads: "lcov" });
|
||||
if (Object.keys(d).some((k) => k.startsWith("@vitest/coverage-"))) add(coverage, { runner: "vitest", cmd: "npx vitest run --coverage --coverage.reporter=lcov", cwd, reads: "lcov" });
|
||||
@@ -197,7 +204,7 @@ export function discover(root) {
|
||||
const before = runners.length;
|
||||
if (files.includes("go.mod")) {
|
||||
add(runners, { cmd: "go test ./...", cwd, covers: covers(cwd) });
|
||||
add(coverage, { runner: "go", cmd: "go test -coverprofile=<out> ./... && go tool cover -func=<out>", cwd, reads: "go-func" });
|
||||
add(coverage, { runner: "go", cmd: "go test -coverprofile=\"<out>\" ./... && go tool cover -func=\"<out>\"", cwd, reads: "go-func" });
|
||||
}
|
||||
if (files.includes("Cargo.toml")) {
|
||||
const cargo = readText(path.join(abs, "Cargo.toml")) || "";
|
||||
@@ -210,7 +217,7 @@ export function discover(root) {
|
||||
if (["pyproject.toml", "setup.cfg", "pytest.ini", "tox.ini"].some((f) => files.includes(f))) {
|
||||
if (usesPytest(abs, files)) {
|
||||
add(runners, { cmd: "python -m pytest", cwd, covers: covers(cwd) });
|
||||
if (usesPytestCov(root, cwd, dirs)) add(coverage, { runner: "pytest", cmd: "python -m pytest --cov --cov-report=lcov:<out>", cwd, reads: "lcov" });
|
||||
if (usesPytestCov(root, cwd, dirs)) add(coverage, { runner: "pytest", cmd: "python -m pytest --cov --cov-report=lcov:\"<out>\"", cwd, reads: "lcov" });
|
||||
} else add(runners, { cmd: "python -m unittest", cwd, covers: covers(cwd) });
|
||||
}
|
||||
const dotnet = files.some((f) => /\.(csproj|sln)$/i.test(f));
|
||||
@@ -234,8 +241,6 @@ export function discover(root) {
|
||||
const present = [];
|
||||
if (pkg || rootFiles.includes("package.json")) present.push("node");
|
||||
if (counts.python) present.push("python");
|
||||
if ([...dirs.values()].some((f) => f.includes("go.mod"))) present.push("go");
|
||||
if ([...dirs.values()].some((f) => f.includes("Cargo.toml"))) present.push("rust");
|
||||
if (present.length) {
|
||||
present.sort((a, b) => counts[b] - counts[a]);
|
||||
const [first, ...rest] = present;
|
||||
|
||||
@@ -156,21 +156,22 @@ function parseTable(lines, table) {
|
||||
}
|
||||
|
||||
/**
|
||||
* The first relative .md link in the section body. `docs` is the path under
|
||||
* .agents-docs/ when it points there, else `outside` holds the target.
|
||||
* The section body's link into .agents-docs/ (`docs`, the path under it);
|
||||
* with none, `outside` holds the first other relative .md link.
|
||||
*/
|
||||
function linkIn(lines, fenced, start, end) {
|
||||
let outside = null;
|
||||
for (let i = start; i < end; i++) {
|
||||
if (fenced[i]) continue;
|
||||
for (const m of lines[i].matchAll(/\]\(([^)\s]+)\)/g)) {
|
||||
const target = m[1].replace(/#.*$/, "");
|
||||
if (/^[a-z][a-z0-9+.-]*:/i.test(target) || !/\.md$/i.test(target)) continue;
|
||||
const norm = path.posix.normalize(toPosix(target));
|
||||
if (/^\.agents-docs\/[^/]+\.md$/i.test(norm)) return { docs: norm };
|
||||
return { outside: target };
|
||||
if (/^\.agents-docs\/.+\.md$/i.test(norm)) return { docs: norm };
|
||||
outside ??= target;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return outside === null ? null : { outside };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -222,6 +223,13 @@ function validRows(data) {
|
||||
if (typeof r.area !== "string" || !r.area.trim()) bad("has no area");
|
||||
if (typeof r.run !== "string" || !r.run.trim()) bad("has no run command");
|
||||
if (!Array.isArray(r.paths) || !r.paths.length || r.paths.some((p) => typeof p !== "string" || !p.trim())) bad("has no paths");
|
||||
// A backtick or line break would break the table; an unsupported glob
|
||||
// would be written, then dropped by the next read.
|
||||
if ([r.area, r.run, ...r.paths].some((s) => /[`\r\n]/.test(s))) bad("has a backtick or line break in a cell");
|
||||
for (const p of r.paths) {
|
||||
const why = unsupported(p.trim());
|
||||
if (why) bad(`has an unsupported glob "${p.trim()}": ${why}`);
|
||||
}
|
||||
return { area: r.area.trim(), paths: r.paths.map((p) => toPosix(p.trim())), run: r.run.trim(), shared: r.shared === true };
|
||||
});
|
||||
}
|
||||
@@ -254,7 +262,7 @@ function placeIn(text, table, { linked }) {
|
||||
let i = section.start;
|
||||
while (i < section.end && lines[i].trim() === "") i++;
|
||||
let at = section.start;
|
||||
if (i < section.end && !fenced[i] && !/^#/.test(lines[i])) {
|
||||
if (i < section.end && !fenced[i] && !/^#{1,6}(\s|$)/.test(lines[i])) {
|
||||
while (i < section.end && lines[i].trim() !== "") i++;
|
||||
at = i;
|
||||
}
|
||||
@@ -283,7 +291,7 @@ function write(root, rowsFile, dryRun) {
|
||||
const current = readText(file);
|
||||
next =
|
||||
current === null
|
||||
? ["# Test map", "", "> Part of [AGENTS.md](../AGENTS.md): which checks cover which files.", "", ...table, ""].join(eol(readText(path.join(root, "AGENTS.md")) || "\n"))
|
||||
? ["# Test map", "", `> Part of [AGENTS.md](${"../".repeat(map.source.split("/").length - 1)}AGENTS.md): which checks cover which files.`, "", ...table, ""].join(eol(readText(path.join(root, "AGENTS.md")) || "\n"))
|
||||
: placeIn(current, table, { linked: true });
|
||||
} else {
|
||||
file = path.join(root, "AGENTS.md");
|
||||
|
||||
@@ -37,7 +37,8 @@ test-match.mjs: match files to tests, both ways. JSON on stdout.
|
||||
node test-match.mjs nearest --file <f> [--root <dir>]
|
||||
Up to 3 existing test files to read for the project's test style.
|
||||
|
||||
Exit: 0 ok · 2 bad arguments · 3 not a git repository (gaps --scope branch).
|
||||
Exit: 0 ok · 2 bad arguments · 3 not a git repository, or review-scope.mjs
|
||||
not beside this script (gaps --scope branch).
|
||||
`;
|
||||
|
||||
/* ------------------------------------------------------------- files */
|
||||
@@ -143,6 +144,8 @@ function nameMatch(source, test) {
|
||||
if (ext === "py") return extOf(test) === "py" && (same(tname, `test_${stem}`) || same(tname, `${stem}_test`));
|
||||
if (ext === "go") return same(test, `${dirOf(source) ? `${dirOf(source)}/` : ""}${stem}_test.go`);
|
||||
if (ext === "cs" || ext === "java" || ext === "kt") return extOf(test) === ext && (same(tname, `${stem}Tests`) || same(tname, `${stem}Test`));
|
||||
const text = extOf(test);
|
||||
if (JS_EXT.has(ext) ? !JS_EXT.has(text) : text !== ext) return false;
|
||||
return same(tname, `${stem}.test`) || same(tname, `${stem}.spec`);
|
||||
}
|
||||
|
||||
@@ -373,42 +376,30 @@ function readStdin() {
|
||||
/* ------------------------------------------------------------------ gaps */
|
||||
|
||||
/**
|
||||
* The branch's changed files, relative to root: review-scope.mjs's list when
|
||||
* it ships beside this script, else the same git commands it runs.
|
||||
* The branch's changed files, relative to root, from review-scope.mjs, which
|
||||
* every skill that ships this script ships beside it. One list, never a copy
|
||||
* of its git commands that could drift from it.
|
||||
*/
|
||||
export function branchFiles(root, base) {
|
||||
const top = git(root, ["rev-parse", "--show-toplevel"]);
|
||||
if (top === null) fail(EXIT.NOT_FOUND, "not-a-repo", `${root} is not inside a git repository.`, "Use --scope all or --scope path, or ask which files to look at.");
|
||||
const prefix = (git(root, ["rev-parse", "--show-prefix"]) || "").trim();
|
||||
let list;
|
||||
const scope = path.join(HERE, "review-scope.mjs");
|
||||
if (fs.existsSync(scope)) {
|
||||
let stdout;
|
||||
try {
|
||||
stdout = execFileSync(process.execPath, [scope, "--root", root, ...(base ? ["--base", base] : [])], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], maxBuffer: 64 * 1024 * 1024 });
|
||||
} catch (err) {
|
||||
stdout = err.stdout;
|
||||
}
|
||||
let json;
|
||||
try {
|
||||
json = JSON.parse(stdout);
|
||||
} catch {
|
||||
fail(EXIT.CRASH, "review-scope-failed", "review-scope.mjs printed no JSON.", "Run review-scope.mjs on its own to see why.");
|
||||
}
|
||||
if (!json.ok) fail(EXIT.NOT_FOUND, json.error, json.message, json.next);
|
||||
list = json.files.map((f) => f.path);
|
||||
} else {
|
||||
const pick = base ? [base, `origin/${base}`] : ["main", "origin/main", "master", "origin/master"];
|
||||
const ref = pick.find((r) => git(root, ["rev-parse", "--verify", "--quiet", `${r}^{commit}`]) !== null);
|
||||
if (base && !ref) fail(EXIT.NOT_FOUND, "no-base", `No branch named ${base} (or origin/${base}).`, "Pass an existing --base, or omit it to use main / master.");
|
||||
const lines = (args) => (git(top.trim(), args) || "").split("\n").filter(Boolean);
|
||||
list = [
|
||||
...(ref ? lines(["diff", "--name-only", `${ref}...HEAD`]) : []),
|
||||
...lines(["diff", "--name-only", "--cached"]),
|
||||
...lines(["diff", "--name-only"]),
|
||||
...lines(["ls-files", "--others", "--exclude-standard"]),
|
||||
];
|
||||
if (!fs.existsSync(scope)) fail(EXIT.NOT_FOUND, "review-scope-missing", "review-scope.mjs is not beside test-match.mjs.", "Reinstall the skill, or use --scope all or --scope path.");
|
||||
let stdout;
|
||||
try {
|
||||
stdout = execFileSync(process.execPath, [scope, "--root", root, ...(base ? ["--base", base] : [])], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], maxBuffer: 64 * 1024 * 1024 });
|
||||
} catch (err) {
|
||||
stdout = err.stdout;
|
||||
}
|
||||
let json;
|
||||
try {
|
||||
json = JSON.parse(stdout);
|
||||
} catch {
|
||||
fail(EXIT.CRASH, "review-scope-failed", "review-scope.mjs printed no JSON.", "Run review-scope.mjs on its own to see why.");
|
||||
}
|
||||
if (!json.ok) fail(EXIT.NOT_FOUND, json.error, json.message, json.next);
|
||||
const list = json.files.map((f) => f.path);
|
||||
return [...new Set(list.map(toPosix))]
|
||||
.filter((f) => f.startsWith(prefix))
|
||||
.map((f) => f.slice(prefix.length))
|
||||
@@ -451,8 +442,9 @@ function gaps(root, { scope, sub, base }) {
|
||||
else {
|
||||
files = allFiles(root);
|
||||
if (scope === "path") {
|
||||
const p = toPosix(sub).replace(/\/+$/, "");
|
||||
files = files.filter((f) => fold(f) === fold(p) || fold(f).startsWith(`${fold(p)}/`));
|
||||
const p = path.posix.normalize(toPosix(sub)).replace(/\/+$/, "");
|
||||
// The project root (".") is every file.
|
||||
if (p !== "." && p !== "") files = files.filter((f) => fold(f) === fold(p) || fold(f).startsWith(`${fold(p)}/`));
|
||||
}
|
||||
}
|
||||
let sources = [...new Set(files)].filter(isSource).sort();
|
||||
@@ -560,7 +552,7 @@ async function main(argv) {
|
||||
fail(EXIT.USAGE, "bad-stdin", "--stdin needs review-scope.mjs JSON.", "Pipe review-scope.mjs output in, or pass --files.");
|
||||
}
|
||||
if (!json || !Array.isArray(json.files)) fail(EXIT.USAGE, "bad-stdin", "The JSON on stdin has no files list.", "Pipe review-scope.mjs output in, or pass --files.");
|
||||
files = json.files.map((f) => toPosix(f.path)).filter(Boolean);
|
||||
files = json.files.filter((f) => f && typeof f.path === "string" && f.path).map((f) => toPosix(f.path)).filter(Boolean);
|
||||
} else fail(EXIT.USAGE, "missing-files", "covers needs --files <a,b,...> or --stdin.", "Run with --help for usage.");
|
||||
out(covers(root, [...new Set(files)]));
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user