@maniumek/suwgate 0.19.0 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -19,6 +19,17 @@ pnpm add -D --save-exact @maniumek/suwgate
19
19
  pnpm installs a version only once it is a day old (`minimumReleaseAge`). To take a new suwgate the day it is published, exclude the
20
20
  package by name in `pnpm-workspace.yaml`: `minimumReleaseAgeExclude: ["@maniumek/suwgate"]`.
21
21
 
22
+ Once per machine, so that `suwgate` is a command in every repository:
23
+
24
+ ```sh
25
+ npm install --global @maniumek/suwgate
26
+ ```
27
+
28
+ The command on the machine is only a way in: started in a repository, it hands over to the version that repository pins, which is
29
+ the one CI runs, so the verdict is the same in both places. A repository that pins a version and has not installed it gets a
30
+ refusal with the folder to run `pnpm install` in, never a verdict from another version. Hooks keep calling the repository's own
31
+ binary by its path, so they work on a machine without the global command.
32
+
22
33
  ## Quick start
23
34
 
24
35
  ```sh
@@ -252,7 +263,7 @@ The plugin has two agents, each with its own clean context. `/suwgate:ship` star
252
263
  | `suwgate:shipper` | shell, files, `Agent`, `Skill` | takes the change to a pull request: `suwgate ship`, push, CI, and starts the simplifier and the suwagent skills |
253
264
  | `suwgate:simplifier` | shell, files | makes the change simpler without changing its behaviour; commits it, never pushes |
254
265
 
255
- The plugin describes suwgate 0.19.0; a repository on an older package lacks `stats`, `doctor`, `dashboard` and `garden-ran`, and the skills say so instead of working around it.
266
+ The plugin describes suwgate 0.21.0; a repository on an older package lacks `stats`, `doctor`, `dashboard` and `garden-ran`, and the skills say so instead of working around it.
256
267
 
257
268
  ## Configuration
258
269
 
package/dist/cli.js CHANGED
@@ -1,7 +1,9 @@
1
1
  #!/usr/bin/env node
2
- import { readdirSync, readFileSync } from "node:fs";
2
+ import { spawnSync } from "node:child_process";
3
+ import { readdirSync, readFileSync, realpathSync } from "node:fs";
3
4
  import { homedir } from "node:os";
4
5
  import path from "node:path";
6
+ import { fileURLToPath } from "node:url";
5
7
  import { parseArgs } from "node:util";
6
8
  import { check, step } from "./gate/check.js";
7
9
  import { dev, realEnvironment } from "./dev/index.js";
@@ -18,7 +20,7 @@ import { PROFILES } from "./gate/contract.js";
18
20
  import { changedFiles, repositoryFiles, repositoryRoot } from "./gate/repository.js";
19
21
  import { criterionIds } from "./profiles/criteriaRan.js";
20
22
  import { guards, SHARED_RULES, writeSharedRules } from "./guards/index.js";
21
- import { discoverApiIn, dotnetGuards, holdoutStep, stepsFor, verdict } from "./profiles/index.js";
23
+ import { discoverApiIn, dotnetGuards, failedTests, holdoutStep, stepsFor, verdict } from "./profiles/index.js";
22
24
  import { readTimes, reportLocation } from "./profiles/reports.js";
23
25
  import { doctor } from "./session/doctor.js";
24
26
  import { lastGarden, parseFound, recordGarden } from "./session/gardenRecord.js";
@@ -27,6 +29,7 @@ import { guardPr } from "./session/guardPr.js";
27
29
  import { guardEdit } from "./session/guardEdit.js";
28
30
  import { preCommit, runTool } from "./session/preCommit.js";
29
31
  import { prePush } from "./session/prePush.js";
32
+ import { findOwnInstall } from "./session/ownInstall.js";
30
33
  import { LEFT_TO_CI, ship } from "./session/ship.js";
31
34
  import { stop } from "./session/stop.js";
32
35
  const USAGE = `usage: suwgate check [--quick] [--verbose] | suwgate step [<id> [--only] [--verbose] [-- <args>]] | suwgate hook <guard-edit|guard-pr|stop|pre-commit|pre-push> | suwgate ship | suwgate stats [--days <n>] [--json] | suwgate doctor | suwgate context | suwgate dashboard [--days <n>] [--no-ci] [--out <path> | --serve [--port <n>] | --json] | suwgate garden-ran <kind>=<count>... | suwgate dev <command> | suwgate agents-shared | suwgate --help | --version
@@ -92,7 +95,7 @@ Every step has a ceiling (SUWGATE_STEP_LIMIT seconds overrides them all). A step
92
95
  and a SKIP fails the gate when CI is set. Exit code: 0 green, 1 red, 2 a usage or suwgate.yml problem.`;
93
96
  const DEFAULT_PORT = 4780;
94
97
  const PORT_FALLBACKS = 20;
95
- const catalog = { guards, dotnetGuards, stepsFor, holdoutStep };
98
+ const catalog = { guards, dotnetGuards, stepsFor, holdoutStep, failedTests };
96
99
  function version() {
97
100
  const manifest = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
98
101
  return manifest.version;
@@ -202,7 +205,29 @@ async function runStep(argv) {
202
205
  (result.exitCode === 0 ? console.log : console.error)(result.text);
203
206
  return result.exitCode;
204
207
  }
208
+ const HANDED_OVER = "SUWGATE_OWN";
209
+ function ownVersionFirst(argv) {
210
+ if (process.env[HANDED_OVER] === "1")
211
+ return null;
212
+ const root = repositoryRoot(process.cwd());
213
+ if (root === null)
214
+ return null;
215
+ const own = findOwnInstall(root);
216
+ if (own.cli === null) {
217
+ if (own.declaredIn === null)
218
+ return null;
219
+ console.error(`suwgate: this repository pins its own suwgate in ${path.join(path.relative(root, own.declaredIn), "package.json")}, and it is not installed: run pnpm install there. Its version, not the one on this machine, is the one CI runs.`);
220
+ return 2;
221
+ }
222
+ if (realpathSync(own.cli) === realpathSync(fileURLToPath(import.meta.url)))
223
+ return null;
224
+ const handed = spawnSync(process.execPath, [own.cli, ...argv], { stdio: "inherit", env: { ...process.env, [HANDED_OVER]: "1" } });
225
+ return handed.status ?? 1;
226
+ }
205
227
  async function main(argv) {
228
+ const handed = ownVersionFirst(argv);
229
+ if (handed !== null)
230
+ return handed;
206
231
  if (argv[0] === "dev")
207
232
  return dev(argv.slice(1), realEnvironment(discoverApi, slowTestsSteps));
208
233
  if (argv[0] === "step")
@@ -7,6 +7,7 @@ export interface Stopper {
7
7
  last: string;
8
8
  branches: string[];
9
9
  open: string[];
10
+ tests: string[];
10
11
  }
11
12
  export interface BranchSummary {
12
13
  branch: string;
@@ -44,7 +44,8 @@ export function stoppers(history, days, now) {
44
44
  const key = `${kind}:${id}`;
45
45
  const own = found.get(key);
46
46
  if (own === undefined) {
47
- found.set(key, { kind, id, runs: 1, ci: run.ci ? 1 : 0, last: run.at, branches: [run.branch], open: [] });
47
+ const tests = kind === "step" ? (run.steps.find((step) => step.id === id)?.failed ?? []) : [];
48
+ found.set(key, { kind, id, runs: 1, ci: run.ci ? 1 : 0, last: run.at, branches: [run.branch], open: [], tests });
48
49
  }
49
50
  else {
50
51
  own.runs += 1;
@@ -71,7 +71,15 @@ async function run(command, prepared, options) {
71
71
  const began = performance.now();
72
72
  const keep = options.runFolder !== undefined;
73
73
  const runFolder = options.runFolder ?? mkdtempSync(path.join(tmpdir(), "suwgate-run-"));
74
- const results = await runSteps(steps, { ...options.env, SUWGATE_ROOT: root, SUWGATE_QUICK: options.quick ? "1" : "", SUWGATE_RUN: runFolder }, prepared.stepLimit).finally(() => {
74
+ const failedTests = new Map();
75
+ const results = await runSteps(steps, { ...options.env, SUWGATE_ROOT: root, SUWGATE_QUICK: options.quick ? "1" : "", SUWGATE_RUN: runFolder }, prepared.stepLimit)
76
+ .then((finished) => {
77
+ for (const result of finished.filter((one) => one.status === "fail")) {
78
+ failedTests.set(result.step.id, options.catalog.failedTests?.(runFolder, result.step.id) ?? []);
79
+ }
80
+ return finished;
81
+ })
82
+ .finally(() => {
75
83
  if (!keep)
76
84
  rmSync(runFolder, { recursive: true, force: true });
77
85
  });
@@ -93,6 +101,7 @@ async function run(command, prepared, options) {
93
101
  ...options.recorded,
94
102
  root,
95
103
  results,
104
+ failedTests,
96
105
  passed,
97
106
  elapsed: (performance.now() - began) / 1000,
98
107
  overBudget: quickLimit !== null && totalSeconds > quickLimit && results.every((result) => result.status !== "fail"),
@@ -111,7 +120,15 @@ function runRecord(input) {
111
120
  verdict: input.passed ? "green" : "red",
112
121
  seconds: tenths(input.elapsed),
113
122
  overBudget: input.overBudget,
114
- steps: input.results.map((result) => ({ id: result.step.id, verdict: result.status, seconds: tenths(result.elapsed ?? 0) })),
123
+ steps: input.results.map((result) => {
124
+ const failed = input.failedTests.get(result.step.id) ?? [];
125
+ return {
126
+ id: result.step.id,
127
+ verdict: result.status,
128
+ seconds: tenths(result.elapsed ?? 0),
129
+ ...(failed.length === 0 ? {} : { failed }),
130
+ };
131
+ }),
115
132
  ran: tally?.ran ?? [],
116
133
  guards: tally?.failed ?? {},
117
134
  excepted: tally?.excepted ?? {},
@@ -45,6 +45,7 @@ export interface Catalog {
45
45
  dotnetGuards: readonly DotnetGuard[];
46
46
  stepsFor(profiles: readonly Profile[]): readonly Step[];
47
47
  holdoutStep?(holdout: Holdout): Step;
48
+ failedTests?(run: string, step: string): string[];
48
49
  }
49
50
  export type Loaded = {
50
51
  config: Config;
@@ -106,18 +106,28 @@ export function hookRows(read, hooksPath) {
106
106
  return { id, where, wired };
107
107
  });
108
108
  }
109
+ const TESTS_NAMED = 5;
110
+ function brokenItems(stopper) {
111
+ const where = `still red on ${stopper.open.join(", ")}`;
112
+ if (stopper.tests.length === 0) {
113
+ return [
114
+ { kind: "broken", text: `${stopper.id} stopped the gate ${stopper.runs} time${stopper.runs === 1 ? "" : "s"}`, detail: where },
115
+ ];
116
+ }
117
+ const named = stopper.tests
118
+ .slice(0, TESTS_NAMED)
119
+ .map((test) => ({ kind: "broken", text: test, detail: `${stopper.id} · ${where}` }));
120
+ const rest = stopper.tests.length - named.length;
121
+ return rest === 0
122
+ ? named
123
+ : [...named, { kind: "broken", text: `${rest} more failed test${rest === 1 ? "" : "s"} of ${stopper.id}`, detail: where }];
124
+ }
109
125
  export function attentionOf(wall, stopped, hooks) {
110
126
  return [
111
127
  ...wall
112
128
  .filter((check) => check.state === "broken")
113
129
  .map((check) => ({ kind: "wall", text: check.name, detail: "is not in place" })),
114
- ...stopped
115
- .filter((stopper) => stopper.open.length > 0)
116
- .map((stopper) => ({
117
- kind: "broken",
118
- text: `${stopper.id} stopped the gate ${stopper.runs} time${stopper.runs === 1 ? "" : "s"}`,
119
- detail: `still red on ${stopper.open.join(", ")}`,
120
- })),
130
+ ...stopped.filter((stopper) => stopper.open.length > 0).flatMap(brokenItems),
121
131
  ...hooks
122
132
  .filter((hook) => !hook.wired)
123
133
  .map((hook) => ({ kind: "have", text: `the ${hook.id} hook is not wired`, detail: `in ${hook.where}` })),
@@ -5,6 +5,7 @@ interface StepRecord {
5
5
  id: string;
6
6
  verdict: "pass" | "fail" | "skip";
7
7
  seconds: number;
8
+ failed?: string[];
8
9
  }
9
10
  export interface RunRecord {
10
11
  at: string;
@@ -69,7 +69,8 @@ function counts(value) {
69
69
  }
70
70
  function isStep(value) {
71
71
  const step = value;
72
- return (typeof step?.id === "string" &&
72
+ return ((step?.failed === undefined || (Array.isArray(step.failed) && step.failed.every((name) => typeof name === "string"))) &&
73
+ typeof step?.id === "string" &&
73
74
  typeof step.verdict === "string" &&
74
75
  STEP_VERDICTS.includes(step.verdict) &&
75
76
  typeof step.seconds === "number");
@@ -2,5 +2,6 @@ import { type Profile, type Step } from "../gate/contract.ts";
2
2
  export { discoverApiIn } from "./contract.ts";
3
3
  export { dotnetGuards } from "./dotnetGuards.ts";
4
4
  export { holdoutStep } from "./holdout.ts";
5
+ export { failedTests } from "./reports.ts";
5
6
  export { verdict } from "./verdict.ts";
6
7
  export declare function stepsFor(profiles: readonly Profile[]): Step[];
@@ -9,6 +9,7 @@ import { webSteps } from "./web.js";
9
9
  export { discoverApiIn } from "./contract.js";
10
10
  export { dotnetGuards } from "./dotnetGuards.js";
11
11
  export { holdoutStep } from "./holdout.js";
12
+ export { failedTests } from "./reports.js";
12
13
  export { verdict } from "./verdict.js";
13
14
  const BY_PROFILE = {
14
15
  web: [...nodeSteps("web"), ...webSteps, ...contractSteps],
@@ -51,5 +51,6 @@ export declare function playwrightTimes(report: PlaywrightReport): TestTime[];
51
51
  export declare function xunitTimes(xml: string): TestTime[];
52
52
  export declare function files(location: string, extension: string): string[];
53
53
  export declare function readRun(format: ReportFormat, location: string): TestRun | null;
54
+ export declare function failedTests(run: string, step: string): string[];
54
55
  export declare function readTimes(format: ReportFormat, location: string): TestTime[] | null;
55
56
  export {};
@@ -112,6 +112,45 @@ export function readRun(format, location) {
112
112
  const report = JSON.parse(readFileSync(location, "utf8"));
113
113
  return format === "vitest" ? vitestRun(report) : playwrightRun(report);
114
114
  }
115
+ const FAILED_SHOWN = 20;
116
+ const NAME_LENGTH = 200;
117
+ function vitestFailed(report) {
118
+ return (report.testResults ?? [])
119
+ .flatMap((file) => file.assertionResults ?? [])
120
+ .filter((assertion) => assertion.status === "failed")
121
+ .map((assertion) => assertion.fullName ?? "");
122
+ }
123
+ function playwrightFailed(report) {
124
+ return (report.suites ?? [])
125
+ .flatMap((suite) => specsOf(suite, []))
126
+ .filter((spec) => spec.statuses.some((status) => status === "unexpected"))
127
+ .map((spec) => spec.title);
128
+ }
129
+ function xunitFailed(xml) {
130
+ return Array.from(xml.matchAll(XUNIT_TEST), (tag) => tag[1] ?? "")
131
+ .filter((attributes) => /\bresult="Fail"/.test(attributes))
132
+ .map((attributes) => unescaped(/\bname="([^"]*)"/.exec(attributes)?.[1] ?? ""));
133
+ }
134
+ function parsed(file) {
135
+ if (!existsSync(file))
136
+ return {};
137
+ try {
138
+ return JSON.parse(readFileSync(file, "utf8")) ?? {};
139
+ }
140
+ catch {
141
+ return {};
142
+ }
143
+ }
144
+ export function failedTests(run, step) {
145
+ return [
146
+ ...vitestFailed(parsed(reportLocation(run, step, "vitest"))),
147
+ ...playwrightFailed(parsed(reportLocation(run, step, "playwright"))),
148
+ ...files(reportLocation(run, step, "xunit"), ".xml").flatMap((report) => xunitFailed(readFileSync(report, "utf8"))),
149
+ ]
150
+ .filter((name) => name !== "")
151
+ .slice(0, FAILED_SHOWN)
152
+ .map((name) => name.slice(0, NAME_LENGTH));
153
+ }
115
154
  export function readTimes(format, location) {
116
155
  if (format === "xunit") {
117
156
  const reports = files(location, ".xml");
@@ -0,0 +1,5 @@
1
+ export interface OwnInstall {
2
+ cli: string | null;
3
+ declaredIn: string | null;
4
+ }
5
+ export declare function findOwnInstall(root: string): OwnInstall;
@@ -0,0 +1,38 @@
1
+ import { existsSync, readdirSync, readFileSync } from "node:fs";
2
+ import path from "node:path";
3
+ const PACKAGE = "@maniumek/suwgate";
4
+ function manifest(file) {
5
+ try {
6
+ return JSON.parse(readFileSync(file, "utf8"));
7
+ }
8
+ catch {
9
+ return null;
10
+ }
11
+ }
12
+ function declares(folder) {
13
+ const own = manifest(path.join(folder, "package.json"));
14
+ return ["dependencies", "devDependencies"].some((field) => {
15
+ const listed = own?.[field];
16
+ return typeof listed === "object" && listed !== null && PACKAGE in listed;
17
+ });
18
+ }
19
+ function cliOf(folder) {
20
+ const installed = path.join(folder, "node_modules", PACKAGE);
21
+ const bin = manifest(path.join(installed, "package.json"))?.["bin"];
22
+ const entry = typeof bin === "object" && bin !== null ? bin["suwgate"] : bin;
23
+ if (typeof entry !== "string")
24
+ return null;
25
+ const cli = path.join(installed, entry);
26
+ return existsSync(cli) ? cli : null;
27
+ }
28
+ function folders(root) {
29
+ const inner = readdirSync(root, { withFileTypes: true })
30
+ .filter((entry) => entry.isDirectory() && !entry.name.startsWith(".") && entry.name !== "node_modules")
31
+ .map((entry) => path.join(root, entry.name));
32
+ return [root, ...inner.toSorted()];
33
+ }
34
+ export function findOwnInstall(root) {
35
+ const declaring = folders(root).filter(declares);
36
+ const cli = declaring.map(cliOf).find((found) => found !== null) ?? null;
37
+ return { cli, declaredIn: declaring[0] ?? null };
38
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maniumek/suwgate",
3
- "version": "0.19.0",
3
+ "version": "0.21.0",
4
4
  "description": "One quality gate for every repository: suwgate check runs the steps of the repository's profiles and its guards, configured in suwgate.yml",
5
5
  "license": "MIT",
6
6
  "author": "Manomenu",