mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-11-16 20:45:33 -08:00
Add test command discovery and a check runner, and offer a test map in Init
AI Assisted
This commit is contained in:
1 parent
aecc3afb4c
commit
82298d256c
10 files changed
+706
-13
No files matched your search
@@ -24,6 +24,7 @@ The FIRST thing you do, before anything else in this file — before the Awarene
|
||||
- `node "<S>/agents-md.mjs" filename "<Section Name>"`: a new section file's name, link line and header.
|
||||
- `node "<S>/agent-files.mjs" detect` / `apply <id> ...`: Step 7.
|
||||
- `node "<S>/commit-msg.mjs" --subject "<subject>"`: the Session End commit.
|
||||
- `node "<S>/test-map.mjs" read` / `write --rows <file>` and `node "<S>/test-commands.mjs" discover`: the `## Test map` (Testing, Step 4); the rows file goes in the session folder or the OS temp folder, never the project.
|
||||
|
||||
---
|
||||
|
||||
@@ -138,7 +139,7 @@ Present the user with update options:
|
||||
> - **1. Commands** - Build, test, run, lint, how to run and view the app, or other CLI commands
|
||||
> - **2. Architecture** - How components interact, data flow, key patterns
|
||||
> - **3. Gotchas/Pitfalls** - Traps to avoid, non-obvious behaviors
|
||||
> - **4. Testing** - Test patterns, how to run specific tests, the test map and quick checks, fixtures
|
||||
> - **4. Testing** - Test patterns, how to run specific tests, the `## Test map` table and quick checks, fixtures
|
||||
> - **5. Environment/Config** - Setup quirks, env variables, configuration
|
||||
> - **6. General Rules** - Coding conventions, style rules, project-specific practices
|
||||
> - **7. Failure Log** - Record a mistake or correction in the Failure log
|
||||
@@ -160,7 +161,7 @@ Present the user with update options:
|
||||
| Commands | Purpose? Flags? Prerequisites? How to run and view the app? |
|
||||
| Architecture | Components? Interactions? Pattern? |
|
||||
| Gotchas | What was unexpected? Workaround? |
|
||||
| Testing | Commands? Test map and quick checks stale? Fixtures? Mocking? |
|
||||
| Testing | Commands? Test map `Paths` still match real files and `Run` commands still exist (`test-map.mjs read` problems, `test-commands.mjs discover`)? Quick checks stale? Fixtures? Mocking? No map but tests exist: offer to create one, written only on a yes through `test-map.mjs write`. |
|
||||
| Environment | Local/CI/deploy? Env vars/files? |
|
||||
| Rules | Project-wide or specific? Why? |
|
||||
| Failure Log | What went wrong? Correct behavior (imperative, one line)? Merge into an entry on the same topic if one exists; over 17 entries, propose what to prune (rule 7). |
|
||||
|
||||
@@ -26,7 +26,7 @@ The FIRST thing you do, before anything else in this file — before exploring t
|
||||
|
||||
## Content
|
||||
|
||||
1. **Commands**: Build, lint, test, run single test (and beside it a short **Test map**: `area -> tests or command` lines saying which tests cover which areas, plus a note of which checks are quick; omit it when the project has no tests), other common development tasks, and how to start the app and where to view it (URL or command), when it has one
|
||||
1. **Commands**: Build, lint, test, run single test (and beside it a note of which checks are quick), other common development tasks, and how to start the app and where to view it (URL or command), when it has one. Then the **`## Test map`** section: run `node "<S>/test-commands.mjs" discover` and `node "<S>/test-map.mjs" read`. When test commands are found and no map exists, propose a `## Test map` table (`| Area | Paths | Run | Shared |`, one row per area with its own run command, `Shared: yes` for helper, fixture and config areas) for the person to approve, and write it only on a yes with `test-map.mjs write --rows <file>` (the rows file in the session folder or the OS temp folder, never the project). In a progressive `AGENTS.md`, the heading holds one link to `.agents-docs/AGENTS-test-map.md` and the table goes there. Omit the map when the project has no tests.
|
||||
2. **Architecture**: High-level "big picture" structure requiring multi-file context to understand
|
||||
3. **How to Use This File**: A short paragraph explaining that sections below contain brief summaries and agents should follow the markdown links to `.agents-docs/` for full details — only read what's relevant to the current task.
|
||||
4. **Failure Log**: running list of mistakes and/or corrections that went wrong via the AI Agent at least once.
|
||||
|
||||
@@ -11,8 +11,8 @@
|
||||
|
||||
In this order:
|
||||
|
||||
1. `AGENTS.md`'s test map and its "run single test" command.
|
||||
2. Otherwise match by name, folder and imports of the changed files.
|
||||
1. `AGENTS.md`'s `## Test map` table (`Area`, `Paths`, `Run`, `Shared`; it may sit behind one link to `.agents-docs/AGENTS-test-map.md`), read with `test-map.mjs read` when the skill ships it, plus the "run single test" command.
|
||||
2. Otherwise match by name, folder and imports of the changed files; `test-match.mjs` does this when the skill ships it.
|
||||
|
||||
A runner's own related-tests flag may be used when `AGENTS.md` records it. It is never required.
|
||||
|
||||
@@ -22,7 +22,7 @@ Lint, a type check, a size check: run them at phase end only when the project or
|
||||
|
||||
## Shared ground
|
||||
|
||||
A test helper or fixture, build or test config, a widely imported module, a shared type or schema. Touching any of these is the moment to offer a wider run: say what was touched and why it reaches wide, and let the person choose.
|
||||
A test helper or fixture, build or test config, a widely imported module, a shared type or schema. A test map row marked `Shared: yes` is shared ground by definition. Touching any of these is the moment to offer a wider run: say what was touched and why it reaches wide, and let the person choose.
|
||||
|
||||
## Ask when unsure
|
||||
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
#!/usr/bin/env node
|
||||
// run-check.mjs: run one check command with a timeout and classify the result.
|
||||
//
|
||||
// Agent shells differ in their timeouts, none kill a Windows process tree, and
|
||||
// telling "the command is not there" from "the command failed" needs the exit
|
||||
// code and the spawn error. So one check runs here: passed, failed, timeout or
|
||||
// not-started, with the output's last lines. The full output goes only to
|
||||
// --log; nothing else is written.
|
||||
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { spawn, execFileSync } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { EXIT, fail, out, parseArgs, run, isDir } from "./common.mjs";
|
||||
|
||||
const USAGE = `
|
||||
run-check.mjs: run one command and classify its outcome. JSON on stdout.
|
||||
|
||||
node run-check.mjs --cmd <command> [--cwd <dir>] [--timeout <seconds>] [--log <file>]
|
||||
Runs the command through the shell in --cwd (relative to the current
|
||||
folder; default "."), stops it and every process it started after
|
||||
--timeout seconds (default 600, at most 86400), and prints
|
||||
{ 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)
|
||||
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
|
||||
|
||||
Exit: 0 whenever an outcome was classified · 2 bad arguments.
|
||||
`;
|
||||
|
||||
// One day: a longer timer overflows Node's 32-bit limit and fires at once.
|
||||
const MAX_TIMEOUT = 86400;
|
||||
const TAIL_LINES = 60;
|
||||
const LINE_CAP = 400;
|
||||
const NOT_RECOGNIZED = /is not recognized as an internal or external command/;
|
||||
|
||||
function killTree(child) {
|
||||
if (process.platform === "win32") {
|
||||
try {
|
||||
execFileSync("taskkill", ["/pid", String(child.pid), "/T", "/F"], { stdio: "ignore", windowsHide: true });
|
||||
} catch {}
|
||||
} else {
|
||||
try {
|
||||
process.kill(-child.pid, "SIGKILL");
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
|
||||
function tailOf(text) {
|
||||
const lines = text.split(/\r?\n/);
|
||||
if (lines.length && lines[lines.length - 1] === "") lines.pop();
|
||||
return lines.slice(-TAIL_LINES).map((l) => (l.length > LINE_CAP ? l.slice(0, LINE_CAP) : l));
|
||||
}
|
||||
|
||||
export function runCheck({ cmd, cwd, timeout, log }) {
|
||||
const shownCwd = cwd || ".";
|
||||
const abs = path.resolve(shownCwd);
|
||||
const logFile = log ? path.resolve(log) : null;
|
||||
const base = { ok: true, cmd, cwd: shownCwd };
|
||||
if (!isDir(abs)) {
|
||||
return Promise.resolve({ ...base, outcome: "not-started", reason: "cwd-missing", exit: null, signal: null, seconds: 0, tail: [], log: null });
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
const started = Date.now();
|
||||
let output = "";
|
||||
let stderrHead = "";
|
||||
let spawnError = null;
|
||||
let timedOut = false;
|
||||
let done = false;
|
||||
let logStream = null;
|
||||
if (logFile) {
|
||||
fs.mkdirSync(path.dirname(logFile), { recursive: true });
|
||||
logStream = fs.createWriteStream(logFile);
|
||||
}
|
||||
const child = spawn(cmd, { cwd: abs, shell: true, windowsHide: true, detached: process.platform !== "win32", stdio: ["ignore", "pipe", "pipe"] });
|
||||
const take = (chunk, isErr) => {
|
||||
const s = chunk.toString("utf8");
|
||||
output += s;
|
||||
// Keep only what the tail can still need, plus the start of stderr for
|
||||
// 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);
|
||||
};
|
||||
child.stdout.on("data", (c) => take(c, false));
|
||||
child.stderr.on("data", (c) => take(c, true));
|
||||
|
||||
const finish = (code, signal) => {
|
||||
if (done) return;
|
||||
done = true;
|
||||
clearTimeout(timer);
|
||||
clearTimeout(grace);
|
||||
// A process the kill missed must not keep this one alive through its pipes.
|
||||
child.stdout.destroy();
|
||||
child.stderr.destroy();
|
||||
child.unref();
|
||||
const seconds = Math.round((Date.now() - started) / 100) / 10;
|
||||
let outcome;
|
||||
let reason;
|
||||
if (spawnError) [outcome, reason] = ["not-started", "spawn-error"];
|
||||
else if (timedOut) outcome = "timeout";
|
||||
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));
|
||||
else resolve(result);
|
||||
};
|
||||
|
||||
let grace = null;
|
||||
const timer = setTimeout(() => {
|
||||
timedOut = true;
|
||||
killTree(child);
|
||||
// Wait for close, but never more than 5 seconds after the kill.
|
||||
grace = setTimeout(() => finish(null, "SIGKILL"), 5000);
|
||||
}, timeout * 1000);
|
||||
|
||||
child.on("error", (err) => {
|
||||
spawnError = err;
|
||||
// close may not follow a spawn error; give it a moment, then report.
|
||||
setTimeout(() => finish(null, null), 200);
|
||||
});
|
||||
child.on("close", (code, signal) => finish(code, signal));
|
||||
});
|
||||
}
|
||||
|
||||
async function main(argv) {
|
||||
const args = parseArgs(argv, { values: ["cmd", "cwd", "timeout", "log"] });
|
||||
if (args._.length) fail(EXIT.USAGE, "unexpected-argument", `Unexpected argument "${args._[0]}".`, "Pass the command with --cmd \"<command>\".");
|
||||
if (!args.cmd || !args.cmd.trim()) fail(EXIT.USAGE, "missing-cmd", "run-check needs --cmd <command>.", "Run with --help for usage.");
|
||||
const timeout = args.timeout === undefined ? 600 : Number(args.timeout);
|
||||
if (!Number.isFinite(timeout) || timeout <= 0 || timeout > MAX_TIMEOUT) {
|
||||
fail(EXIT.USAGE, "bad-timeout", `--timeout must be a number of seconds above 0 and at most ${MAX_TIMEOUT}, not "${args.timeout}".`, "Pass --timeout <seconds>.");
|
||||
}
|
||||
out(await runCheck({ cmd: args.cmd, cwd: args.cwd, timeout, log: args.log }));
|
||||
}
|
||||
|
||||
// Run as a script; imported, do nothing.
|
||||
const isMain = (() => {
|
||||
try {
|
||||
return fs.realpathSync(process.argv[1]) === fs.realpathSync(fileURLToPath(import.meta.url));
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
})();
|
||||
if (isMain) run(USAGE, main);
|
||||
@@ -0,0 +1,267 @@
|
||||
#!/usr/bin/env node
|
||||
// test-commands.mjs: find a project's test and check commands from its config.
|
||||
//
|
||||
// Reads package.json, workspace files and runner configs (go.mod, Cargo.toml,
|
||||
// pyproject.toml, *.csproj, Makefile, ...) and reports the whole-suite
|
||||
// command, one runner per folder, lint/type/build checks, the coverage
|
||||
// commands the project can already run, and the built-in runner to offer when
|
||||
// it has none. It only reads files: it never runs a command, installs anything
|
||||
// or touches the network. Which of these to run stays the agent's call, and
|
||||
// every command is shown to the person before it runs.
|
||||
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { EXIT, fail, out, parseArgs, run, readText, isDir } from "./common.mjs";
|
||||
import { matchGlob } from "./glob.mjs";
|
||||
|
||||
const USAGE = `
|
||||
test-commands.mjs: a project's test and check commands. JSON on stdout.
|
||||
|
||||
node test-commands.mjs discover [--root <dir>]
|
||||
{ ok, suite, runners, checks, coverage, builtin }
|
||||
suite { cmd, why } the whole-suite command, or null
|
||||
runners [{ cmd, cwd, covers[] }] one per folder with its own runner
|
||||
(workspaces, go.mod, Cargo.toml, Python configs, .NET, Makefile);
|
||||
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
|
||||
lcov or go-func
|
||||
builtin { language, cmd, others? } the language's own runner, only when
|
||||
there is no suite and no runner; otherwise null
|
||||
|
||||
Only reads files. Exit: 0 ok · 2 bad arguments · 3 no such folder.
|
||||
`;
|
||||
|
||||
const SKIP = new Set(["node_modules", ".git", "vendor", "dist", "build", "target", ".venv"]);
|
||||
const MAX_DEPTH = 4;
|
||||
const PLACEHOLDER = /no test specified/;
|
||||
const LANG_EXT = {
|
||||
node: [".js", ".mjs", ".cjs", ".jsx", ".ts", ".mts", ".cts", ".tsx"],
|
||||
python: [".py"],
|
||||
go: [".go"],
|
||||
rust: [".rs"],
|
||||
};
|
||||
const BUILTIN = { node: "node --test", python: "python -m unittest", go: "go test ./...", rust: "cargo test" };
|
||||
|
||||
function readJson(file) {
|
||||
const text = readText(file);
|
||||
if (text === null) return null;
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Every folder to depth 4 (forward-slash, "." for the root) with its file names, and source counts. */
|
||||
function walk(root) {
|
||||
const dirs = new Map();
|
||||
const counts = { node: 0, python: 0, go: 0, rust: 0 };
|
||||
const extLang = new Map(Object.entries(LANG_EXT).flatMap(([lang, exts]) => exts.map((e) => [e, lang])));
|
||||
const visit = (abs, rel, depth) => {
|
||||
let entries;
|
||||
try {
|
||||
entries = fs.readdirSync(abs, { withFileTypes: true });
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
const files = [];
|
||||
dirs.set(rel, files);
|
||||
for (const e of entries) {
|
||||
if (e.isDirectory()) {
|
||||
if (depth < MAX_DEPTH && !SKIP.has(e.name)) visit(path.join(abs, e.name), rel === "." ? e.name : `${rel}/${e.name}`, depth + 1);
|
||||
} else if (e.isFile()) {
|
||||
files.push(e.name);
|
||||
const lang = extLang.get(path.extname(e.name).toLowerCase());
|
||||
if (lang) counts[lang]++;
|
||||
}
|
||||
}
|
||||
};
|
||||
visit(root, ".", 0);
|
||||
return { dirs, counts };
|
||||
}
|
||||
|
||||
/** The package manager the lockfile names. */
|
||||
function packageManager(files) {
|
||||
if (files.includes("pnpm-lock.yaml")) return "pnpm";
|
||||
if (files.includes("yarn.lock")) return "yarn";
|
||||
if (files.includes("bun.lockb") || files.includes("bun.lock")) return "bun";
|
||||
return "npm";
|
||||
}
|
||||
|
||||
const realTest = (pkg) => {
|
||||
const t = pkg && pkg.scripts && pkg.scripts.test;
|
||||
return typeof t === "string" && t.trim() && !PLACEHOLDER.test(t) ? t : null;
|
||||
};
|
||||
const deps = (pkg) => ({ ...(pkg && pkg.dependencies), ...(pkg && pkg.devDependencies), ...(pkg && pkg.peerDependencies), ...(pkg && pkg.optionalDependencies) });
|
||||
const hasMakeTest = (text) => text !== null && /^test\s*:/m.test(text);
|
||||
const covers = (cwd) => (cwd === "." ? ["**"] : [`${cwd}/**`]);
|
||||
const ancestors = (rel) => {
|
||||
const list = rel === "." ? [] : ["."];
|
||||
const parts = rel === "." ? [] : rel.split("/");
|
||||
for (let i = 1; i < parts.length; i++) list.push(parts.slice(0, i).join("/"));
|
||||
return list;
|
||||
};
|
||||
|
||||
/** Workspace folders from package.json workspaces and pnpm-workspace.yaml, matched against the folders on disk. */
|
||||
function workspaceFolders(root, pkg, dirs) {
|
||||
let globs = [];
|
||||
const ws = pkg && pkg.workspaces;
|
||||
if (Array.isArray(ws)) globs = ws;
|
||||
else if (ws && Array.isArray(ws.packages)) globs = ws.packages;
|
||||
const pnpm = readText(path.join(root, "pnpm-workspace.yaml"));
|
||||
if (pnpm !== null) {
|
||||
let inPackages = false;
|
||||
for (const line of pnpm.split(/\r?\n/)) {
|
||||
if (/^packages\s*:/.test(line)) inPackages = true;
|
||||
else if (/^\S/.test(line)) inPackages = false;
|
||||
else if (inPackages) {
|
||||
const m = line.match(/^\s*-\s*['"]?([^'"#]+?)['"]?\s*(?:#.*)?$/);
|
||||
if (m) globs.push(m[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
globs = globs.filter((g) => typeof g === "string" && g.trim()).map((g) => g.trim().replace(/\/+$/, ""));
|
||||
const include = globs.filter((g) => !g.startsWith("!"));
|
||||
const exclude = globs.filter((g) => g.startsWith("!")).map((g) => g.slice(1));
|
||||
return [...dirs.keys()].filter((d) => d !== "." && include.some((g) => matchGlob(g, d)) && !exclude.some((g) => matchGlob(g, d))).sort();
|
||||
}
|
||||
|
||||
/** pytest is configured or listed in this folder's Python files. */
|
||||
function usesPytest(abs, files) {
|
||||
if (files.includes("pytest.ini")) return true;
|
||||
const pyproject = readText(path.join(abs, "pyproject.toml"));
|
||||
if (pyproject !== null && (/\[tool\.pytest/.test(pyproject) || /\bpytest\b/i.test(pyproject))) return true;
|
||||
const setup = readText(path.join(abs, "setup.cfg"));
|
||||
if (setup !== null && /\bpytest\b/i.test(setup)) return true;
|
||||
const tox = readText(path.join(abs, "tox.ini"));
|
||||
if (tox !== null && /\bpytest\b/i.test(tox)) return true;
|
||||
return files.some((f) => /^requirements.*\.txt$/i.test(f) && /^\s*pytest\b/im.test(readText(path.join(abs, f)) || ""));
|
||||
}
|
||||
|
||||
/** The Python files of this folder or a folder above it mention pytest-cov. */
|
||||
function usesPytestCov(root, cwd, dirs) {
|
||||
return [cwd, ...ancestors(cwd)].some((d) => {
|
||||
const files = dirs.get(d) || [];
|
||||
return ["pyproject.toml", "setup.cfg", "tox.ini", ...files.filter((f) => /^requirements.*\.txt$/i.test(f))]
|
||||
.some((f) => files.includes(f) && /pytest-cov/i.test(readText(path.join(root, d, f)) || ""));
|
||||
});
|
||||
}
|
||||
|
||||
export function discover(root) {
|
||||
const { dirs, counts } = walk(root);
|
||||
const rootFiles = dirs.get(".") || [];
|
||||
const pkg = readJson(path.join(root, "package.json"));
|
||||
const pm = packageManager(rootFiles);
|
||||
const runners = [];
|
||||
const coverage = [];
|
||||
const checks = [];
|
||||
const add = (list, item) => {
|
||||
if (!list.some((x) => x.cmd === item.cmd && x.cwd === item.cwd)) list.push(item);
|
||||
};
|
||||
|
||||
let suite = null;
|
||||
if (realTest(pkg)) suite = { cmd: `${pm} test`, why: "package.json scripts.test" };
|
||||
else if (hasMakeTest(readText(path.join(root, "Makefile")))) suite = { cmd: "make test", why: "Makefile test target" };
|
||||
|
||||
if (pkg && pkg.scripts) {
|
||||
const s = pkg.scripts;
|
||||
if (s.lint) checks.push({ kind: "lint", cmd: `${pm} run lint` });
|
||||
const types = ["typecheck", "type-check", "types", "tsc"].find((n) => s[n]);
|
||||
if (types) checks.push({ kind: "types", cmd: `${pm} run ${types}` });
|
||||
else if (rootFiles.includes("tsconfig.json")) checks.push({ kind: "types", cmd: "npx tsc --noEmit" });
|
||||
if (s.build) checks.push({ kind: "build", cmd: `${pm} run build` });
|
||||
} else if (rootFiles.includes("tsconfig.json")) checks.push({ kind: "types", cmd: "npx tsc --noEmit" });
|
||||
|
||||
// Node: the root package and each workspace with a test script.
|
||||
const nodeFolders = [{ cwd: ".", pkg }, ...workspaceFolders(root, pkg, dirs).map((cwd) => ({ cwd, pkg: readJson(path.join(root, cwd, "package.json")) }))];
|
||||
for (const { cwd, pkg: p } of nodeFolders) {
|
||||
const test = realTest(p);
|
||||
if (!test) continue;
|
||||
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" });
|
||||
}
|
||||
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" });
|
||||
}
|
||||
|
||||
// Nested runner configs, walked in folder order.
|
||||
const cargoWorkspaces = new Set();
|
||||
const solutions = new Set();
|
||||
for (const [cwd, files] of [...dirs].sort(([a], [b]) => (a === "." ? -1 : b === "." ? 1 : a.localeCompare(b)))) {
|
||||
const abs = path.join(root, cwd);
|
||||
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" });
|
||||
}
|
||||
if (files.includes("Cargo.toml")) {
|
||||
const cargo = readText(path.join(abs, "Cargo.toml")) || "";
|
||||
const isWorkspace = /^\[workspace\]/m.test(cargo);
|
||||
// A member of a Cargo workspace already runs from the workspace root.
|
||||
const inWorkspace = ancestors(cwd).some((a) => cargoWorkspaces.has(a));
|
||||
if (isWorkspace) cargoWorkspaces.add(cwd);
|
||||
if (!inWorkspace && (isWorkspace || /^\[package\]/m.test(cargo))) add(runners, { cmd: "cargo test", cwd, covers: covers(cwd) });
|
||||
}
|
||||
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" });
|
||||
} else add(runners, { cmd: "python -m unittest", cwd, covers: covers(cwd) });
|
||||
}
|
||||
const dotnet = files.some((f) => /\.(csproj|sln)$/i.test(f));
|
||||
// Projects under a solution already run from the solution's folder.
|
||||
if (dotnet && !ancestors(cwd).some((a) => solutions.has(a))) {
|
||||
if (files.some((f) => /\.sln$/i.test(f))) solutions.add(cwd);
|
||||
add(runners, { cmd: "dotnet test", cwd, covers: covers(cwd) });
|
||||
const prefix = cwd === "." ? "" : `${cwd}/`;
|
||||
const coverlet = [...dirs].some(([d, fs2]) => (cwd === "." || d === cwd || d.startsWith(prefix)) && fs2.some((f) => /\.csproj$/i.test(f) && /coverlet\.collector/i.test(readText(path.join(root, d, f)) || "")));
|
||||
if (coverlet) add(coverage, { runner: "dotnet", cmd: 'dotnet test --collect:"XPlat Code Coverage"', cwd, reads: "lcov" });
|
||||
}
|
||||
// make test is the folder's runner only when nothing else runs there.
|
||||
const hasNode = runners.slice(0, before).some((r) => r.cwd === cwd);
|
||||
if (runners.length === before && !hasNode && files.includes("Makefile") && hasMakeTest(readText(path.join(abs, "Makefile")))) {
|
||||
add(runners, { cmd: "make test", cwd, covers: covers(cwd) });
|
||||
}
|
||||
}
|
||||
|
||||
let builtin = null;
|
||||
if (!suite && !runners.length) {
|
||||
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;
|
||||
builtin = { language: first, cmd: BUILTIN[first] };
|
||||
if (rest.length) builtin.others = rest.map((language) => ({ language, cmd: BUILTIN[language] }));
|
||||
}
|
||||
}
|
||||
|
||||
return { ok: true, suite, runners, checks, coverage, builtin };
|
||||
}
|
||||
|
||||
async function main(argv) {
|
||||
const args = parseArgs(argv, { values: ["root"] });
|
||||
const cmd = args._[0];
|
||||
if (cmd !== "discover") fail(EXIT.USAGE, "unknown-command", cmd ? `Unknown command "${cmd}".` : "No command given.", "Commands: discover. Run with --help.");
|
||||
const root = path.resolve(args.root || ".");
|
||||
if (!isDir(root)) fail(EXIT.NOT_FOUND, "no-root", `No folder at ${root}.`, "Pass the project folder with --root, or run from the project root.");
|
||||
out(discover(root));
|
||||
}
|
||||
|
||||
// Run as a script; imported, do nothing.
|
||||
const isMain = (() => {
|
||||
try {
|
||||
return fs.realpathSync(process.argv[1]) === fs.realpathSync(fileURLToPath(import.meta.url));
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
})();
|
||||
if (isMain) run(USAGE, main);
|
||||
Reference in new issue
Block a user