forked from longtao.wu/droboticflow
145 lines
6.2 KiB
JavaScript
145 lines
6.2 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, copyFileSync, cpSync, rmSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join, resolve } from "node:path";
|
|
import { spawnSync } from "node:child_process";
|
|
import { createServer } from "node:http";
|
|
import { spawn } from "node:child_process";
|
|
const root = resolve(".");
|
|
const run = (file, args, cwd, env = {}) =>
|
|
spawnSync(process.execPath, [file, ...args], {
|
|
cwd,
|
|
encoding: "utf8",
|
|
env: { ...process.env, CI: "", HUSKY: "", ...env },
|
|
});
|
|
|
|
test("missing Husky wrappers are detected and installation produces a working pre-commit hook", () => {
|
|
const dir = mkdtempSync(join(tmpdir(), "hook-guard-"));
|
|
try {
|
|
mkdirSync(join(dir, "scripts/lib"), { recursive: true });
|
|
mkdirSync(join(dir, ".husky"));
|
|
mkdirSync(join(dir, "node_modules"));
|
|
copyFileSync(join(root, "scripts/check-git-hooks.mjs"), join(dir, "scripts/check-git-hooks.mjs"));
|
|
copyFileSync(join(root, "scripts/lib/git-hooks.mjs"), join(dir, "scripts/lib/git-hooks.mjs"));
|
|
cpSync(join(root, "node_modules/husky"), join(dir, "node_modules/husky"), { recursive: true });
|
|
spawnSync("git", ["init", "-q"], { cwd: dir });
|
|
writeFileSync(join(dir, ".husky/pre-commit"), "echo hook-was-executed\nexit 1\n");
|
|
const script = join(dir, "scripts/check-git-hooks.mjs");
|
|
assert.equal(run(script, [], dir).status, 1);
|
|
assert.equal(run(script, ["--install"], dir).status, 0);
|
|
assert.equal(run(script, [], dir).status, 0);
|
|
const hook = spawnSync(
|
|
"git",
|
|
[
|
|
"-c",
|
|
"user.name=Fixture",
|
|
"-c",
|
|
"user.email=fixture@example.invalid",
|
|
"commit",
|
|
"--allow-empty",
|
|
"-m",
|
|
"fixture",
|
|
],
|
|
{ cwd: dir, encoding: "utf8", env: { ...process.env, HUSKY: "" } },
|
|
);
|
|
assert.notEqual(hook.status, 0);
|
|
assert.match(hook.stdout + hook.stderr, /hook-was-executed/);
|
|
spawnSync("git", ["config", "core.hooksPath", "custom-hooks"], { cwd: dir });
|
|
assert.equal(run(script, ["--install"], dir).status, 1);
|
|
assert.equal(
|
|
spawnSync("git", ["config", "--get", "core.hooksPath"], { cwd: dir, encoding: "utf8" }).stdout.trim(),
|
|
"custom-hooks",
|
|
);
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("baseline gate keeps raw output for known failures, novel failures and runner crashes", () => {
|
|
const dir = mkdtempSync(join(tmpdir(), "baseline-logs-"));
|
|
try {
|
|
for (const name of ["scripts", "tests", "bin"]) mkdirSync(join(dir, name));
|
|
copyFileSync(join(root, "scripts/check-test-baseline.mjs"), join(dir, "scripts/check-test-baseline.mjs"));
|
|
const baseline = JSON.stringify({ frontend: ["fixture.test.mjs :: known"], go: ["fixture :: TestKnown"] });
|
|
writeFileSync(join(dir, "tests/known-failures.json"), baseline);
|
|
for (const [name, output, expected] of [
|
|
["known", "test at tests/frontend/fixture.test.mjs:1:1\n✖ known (1ms)\nassertion-details", 0],
|
|
["new", "test at tests/frontend/fixture.test.mjs:1:1\n✖ novel (1ms)\nnovel-assertion-details", 1],
|
|
["crash", "runner failed before producing tests", 1],
|
|
]) {
|
|
writeFileSync(
|
|
join(dir, "bin/npm"),
|
|
"#!/usr/bin/env node\nprocess.stdout.write(" +
|
|
JSON.stringify(output) +
|
|
'); process.stderr.write("stderr-details"); process.exit(1);\n',
|
|
{ mode: 0o755 },
|
|
);
|
|
const logDir = join(dir, name);
|
|
const result = run(join(dir, "scripts/check-test-baseline.mjs"), ["frontend", "--log-dir", logDir], dir, {
|
|
PATH: join(dir, "bin") + ":" + process.env.PATH,
|
|
});
|
|
assert.equal(result.status, expected, result.stderr);
|
|
assert.equal(readFileSync(join(logDir, "stdout.log"), "utf8"), output);
|
|
assert.equal(readFileSync(join(logDir, "stderr.log"), "utf8"), "stderr-details");
|
|
const summary = JSON.parse(readFileSync(join(logDir, "summary.json"), "utf8"));
|
|
assert.equal(summary.exitCode, 1);
|
|
assert.equal(summary.updateRequested, false);
|
|
assert.equal(readFileSync(join(dir, "tests/known-failures.json"), "utf8"), baseline);
|
|
}
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("local stack check rejects a missing or stale API version and accepts matching source", async () => {
|
|
const commit = spawnSync("git", ["rev-parse", "HEAD"], { encoding: "utf8" }).stdout.trim();
|
|
let apiCommit = "";
|
|
const server = createServer((req, res) => {
|
|
res.setHeader("Content-Type", "application/json");
|
|
res.end(
|
|
JSON.stringify(
|
|
req.url === "/__dev/status"
|
|
? { commit, dirty: false, root, apiTarget: "http://127.0.0.1:3001" }
|
|
: { status: "ok", buildCommit: apiCommit },
|
|
),
|
|
);
|
|
});
|
|
await new Promise((done) => server.listen(0, "127.0.0.1", done));
|
|
const url = "http://127.0.0.1:" + server.address().port;
|
|
const command = () =>
|
|
new Promise((done) => {
|
|
const child = spawn(process.execPath, ["scripts/local/check-dev-stack.mjs", url], { cwd: root });
|
|
let output = "";
|
|
child.stdout.on("data", (x) => (output += x));
|
|
child.on("close", (code) => done({ code, output }));
|
|
});
|
|
try {
|
|
assert.equal((await command()).code, 1);
|
|
apiCommit = "old-source";
|
|
assert.equal((await command()).code, 1);
|
|
apiCommit = commit;
|
|
const result = await command();
|
|
assert.equal(result.code, 0, result.output);
|
|
} finally {
|
|
await new Promise((done) => server.close(done));
|
|
}
|
|
});
|
|
|
|
test("Docker npm prepare has all hook bootstrap modules before dependency installation", () => {
|
|
const dir = mkdtempSync(join(tmpdir(), "docker-hook-prepare-"));
|
|
try {
|
|
const beforeInstall = readFileSync(join(root, "Dockerfile.api"), "utf8").split("RUN npm ci")[0];
|
|
for (const [, source, target] of beforeInstall.matchAll(/^COPY (scripts\/\S+) (\S+)$/gm)) {
|
|
const destination = join(dir, target);
|
|
mkdirSync(join(destination, ".."), { recursive: true });
|
|
cpSync(join(root, source), destination, { recursive: true });
|
|
}
|
|
const result = run(join(dir, "scripts/check-git-hooks.mjs"), [], dir);
|
|
assert.equal(result.status, 0, result.stderr);
|
|
assert.match(result.stdout, /Git hooks: skipped/);
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|