@robhowley/pi-structured-return 1.4.0 → 1.6.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 +108 -74
- package/extensions/structured-return/src/config/registry.test.ts +171 -0
- package/extensions/structured-return/src/config/registry.ts +183 -14
- package/extensions/structured-return/src/index.test.ts +54 -0
- package/extensions/structured-return/src/index.ts +20 -9
- package/extensions/structured-return/src/parsers/ava-text.test.ts +116 -0
- package/extensions/structured-return/src/parsers/ava-text.ts +161 -0
- package/extensions/structured-return/src/parsers/bandit-json.test.ts +90 -0
- package/extensions/structured-return/src/parsers/bandit-json.ts +78 -0
- package/extensions/structured-return/src/parsers/black-text.test.ts +67 -0
- package/extensions/structured-return/src/parsers/black-text.ts +74 -0
- package/extensions/structured-return/src/parsers/cargo-build.ts +2 -2
- package/extensions/structured-return/src/parsers/cargo-test.ts +2 -2
- package/extensions/structured-return/src/parsers/clang-text.test.ts +61 -0
- package/extensions/structured-return/src/parsers/clang-text.ts +59 -0
- package/extensions/structured-return/src/parsers/dbt-json.ts +2 -2
- package/extensions/structured-return/src/parsers/dotnet-build-text.test.ts +85 -0
- package/extensions/structured-return/src/parsers/dotnet-build-text.ts +48 -0
- package/extensions/structured-return/src/parsers/eslint-json.ts +2 -2
- package/extensions/structured-return/src/parsers/go-test-json.test.ts +119 -0
- package/extensions/structured-return/src/parsers/go-test-json.ts +137 -0
- package/extensions/structured-return/src/parsers/hadolint-json.test.ts +73 -0
- package/extensions/structured-return/src/parsers/hadolint-json.ts +55 -0
- package/extensions/structured-return/src/parsers/htmlhint-json.test.ts +72 -0
- package/extensions/structured-return/src/parsers/htmlhint-json.ts +72 -0
- package/extensions/structured-return/src/parsers/isort-text.test.ts +58 -0
- package/extensions/structured-return/src/parsers/isort-text.ts +64 -0
- package/extensions/structured-return/src/parsers/javac-text.test.ts +75 -0
- package/extensions/structured-return/src/parsers/javac-text.ts +81 -0
- package/extensions/structured-return/src/parsers/jsonlint-text.test.ts +52 -0
- package/extensions/structured-return/src/parsers/jsonlint-text.ts +64 -0
- package/extensions/structured-return/src/parsers/junit-xml.ts +2 -2
- package/extensions/structured-return/src/parsers/markdownlint-json.test.ts +76 -0
- package/extensions/structured-return/src/parsers/markdownlint-json.ts +73 -0
- package/extensions/structured-return/src/parsers/minitest-text.ts +2 -2
- package/extensions/structured-return/src/parsers/mocha-json.test.ts +108 -0
- package/extensions/structured-return/src/parsers/mocha-json.ts +77 -0
- package/extensions/structured-return/src/parsers/mypy-json.test.ts +101 -0
- package/extensions/structured-return/src/parsers/mypy-json.ts +80 -0
- package/extensions/structured-return/src/parsers/node-test-text.test.ts +117 -0
- package/extensions/structured-return/src/parsers/node-test-text.ts +90 -0
- package/extensions/structured-return/src/parsers/npm-audit-json.test.ts +83 -0
- package/extensions/structured-return/src/parsers/npm-audit-json.ts +88 -0
- package/extensions/structured-return/src/parsers/prettier-text.test.ts +76 -0
- package/extensions/structured-return/src/parsers/prettier-text.ts +63 -0
- package/extensions/structured-return/src/parsers/pylint-json.test.ts +120 -0
- package/extensions/structured-return/src/parsers/pylint-json.ts +64 -0
- package/extensions/structured-return/src/parsers/pyright-json.test.ts +112 -0
- package/extensions/structured-return/src/parsers/pyright-json.ts +81 -0
- package/extensions/structured-return/src/parsers/rspec-json.ts +2 -2
- package/extensions/structured-return/src/parsers/rubocop-json.test.ts +118 -0
- package/extensions/structured-return/src/parsers/rubocop-json.ts +65 -0
- package/extensions/structured-return/src/parsers/ruff-json.ts +2 -2
- package/extensions/structured-return/src/parsers/shellcheck-json.test.ts +110 -0
- package/extensions/structured-return/src/parsers/shellcheck-json.ts +61 -0
- package/extensions/structured-return/src/parsers/stylelint-json.test.ts +120 -0
- package/extensions/structured-return/src/parsers/stylelint-json.ts +68 -0
- package/extensions/structured-return/src/parsers/swiftc-text.test.ts +73 -0
- package/extensions/structured-return/src/parsers/swiftc-text.ts +47 -0
- package/extensions/structured-return/src/parsers/tail-fallback.ts +2 -2
- package/extensions/structured-return/src/parsers/tidy-text.test.ts +74 -0
- package/extensions/structured-return/src/parsers/tidy-text.ts +67 -0
- package/extensions/structured-return/src/parsers/tsc-text.test.ts +81 -0
- package/extensions/structured-return/src/parsers/tsc-text.ts +47 -0
- package/extensions/structured-return/src/parsers/unittest-text.test.ts +126 -0
- package/extensions/structured-return/src/parsers/unittest-text.ts +116 -0
- package/extensions/structured-return/src/parsers/utils.ts +43 -0
- package/extensions/structured-return/src/parsers/vale-json.test.ts +84 -0
- package/extensions/structured-return/src/parsers/vale-json.ts +80 -0
- package/extensions/structured-return/src/parsers/vitest-json.ts +2 -2
- package/package.json +1 -1
- package/skills/structured-return/SKILL.md +93 -2
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
1
4
|
import { describe, it, expect } from "vitest";
|
|
2
5
|
import { stripCdPrefix, formatResult, finalizeResult } from "./index";
|
|
3
6
|
|
|
@@ -95,4 +98,55 @@ describe("finalizeResult", () => {
|
|
|
95
98
|
);
|
|
96
99
|
expect(result.cwd).toBe("/project");
|
|
97
100
|
});
|
|
101
|
+
|
|
102
|
+
it("appends rawTail when parser reports failures but extracts no details", () => {
|
|
103
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "finalize-test-"));
|
|
104
|
+
const logPath = path.join(dir, "combined.log");
|
|
105
|
+
fs.writeFileSync(logPath, "line1\nline2\nFAIL: something broke\n");
|
|
106
|
+
const result = finalizeResult(
|
|
107
|
+
{ tool: "ava", status: "fail", summary: "2 failed, 1 passed", failures: [] },
|
|
108
|
+
1,
|
|
109
|
+
logPath,
|
|
110
|
+
"/project"
|
|
111
|
+
);
|
|
112
|
+
expect(result.rawTail).toContain("FAIL: something broke");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("does not overwrite rawTail when parser already set it", () => {
|
|
116
|
+
const result = finalizeResult(
|
|
117
|
+
{ tool: "dbt", status: "fail", summary: "1 error", failures: [], rawTail: "SELECT * FROM ..." },
|
|
118
|
+
1,
|
|
119
|
+
"/nonexistent/log",
|
|
120
|
+
"/project"
|
|
121
|
+
);
|
|
122
|
+
expect(result.rawTail).toBe("SELECT * FROM ...");
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
describe("formatResult safety net", () => {
|
|
127
|
+
it("surfaces log path and rawTail when failures detected but no details parsed", () => {
|
|
128
|
+
const result = formatResult({
|
|
129
|
+
tool: "ava",
|
|
130
|
+
exitCode: 1,
|
|
131
|
+
status: "fail",
|
|
132
|
+
summary: "2 failed, 1 passed",
|
|
133
|
+
failures: [],
|
|
134
|
+
logPath: "/tmp/ava-run.log",
|
|
135
|
+
rawTail: "FAIL: test_multiply\nexpected 99, got 12",
|
|
136
|
+
});
|
|
137
|
+
expect(result).toContain("log: /tmp/ava-run.log");
|
|
138
|
+
expect(result).toContain("expected 99, got 12");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("does not surface log path when failures have details", () => {
|
|
142
|
+
const result = formatResult({
|
|
143
|
+
tool: "ava",
|
|
144
|
+
exitCode: 1,
|
|
145
|
+
status: "fail",
|
|
146
|
+
summary: "1 failed",
|
|
147
|
+
failures: [{ id: "test1", file: "test.js", line: 5, message: "boom" }],
|
|
148
|
+
logPath: "/tmp/ava-run.log",
|
|
149
|
+
});
|
|
150
|
+
expect(result).not.toContain("log:");
|
|
151
|
+
});
|
|
98
152
|
});
|
|
@@ -8,6 +8,7 @@ import type { ObservedRunArgs, ParsedResult, RunContext } from "./types";
|
|
|
8
8
|
import { ensureRunDir, writeRunArtifacts } from "./storage/log-store";
|
|
9
9
|
import { loadProjectConfig } from "./config/project-config";
|
|
10
10
|
import { resolveParser, listParsers } from "./config/registry";
|
|
11
|
+
import { safeReadFile } from "./parsers/utils";
|
|
11
12
|
|
|
12
13
|
export default function structuredReturn(pi: ExtensionAPI) {
|
|
13
14
|
pi.registerCommand("sr-parsers", {
|
|
@@ -41,19 +42,14 @@ export default function structuredReturn(pi: ExtensionAPI) {
|
|
|
41
42
|
},
|
|
42
43
|
});
|
|
43
44
|
|
|
44
|
-
pi.on("before_agent_start", async (event) => {
|
|
45
|
-
return {
|
|
46
|
-
systemPrompt:
|
|
47
|
-
event.systemPrompt +
|
|
48
|
-
"\n\nWhen running lint, test, build, or other shell commands, load and follow the `structured-return` skill before choosing how to invoke them.",
|
|
49
|
-
};
|
|
50
|
-
});
|
|
51
|
-
|
|
52
45
|
pi.registerTool({
|
|
53
46
|
name: "structured_return",
|
|
54
47
|
label: "Structured Return",
|
|
55
48
|
description:
|
|
56
49
|
"Run a command, store full logs, apply an explicit or registered parser when available, and fall back to tail + log path.",
|
|
50
|
+
promptGuidelines: [
|
|
51
|
+
"Prefer structured_return over bash for test suites, linters, type checkers, and build commands - it returns compact results. Check the structured-return skill for the right flags and parseAs value for each tool.",
|
|
52
|
+
],
|
|
57
53
|
parameters: Type.Object({
|
|
58
54
|
command: Type.String(),
|
|
59
55
|
cwd: Type.Optional(Type.String()),
|
|
@@ -112,6 +108,12 @@ export function formatResult(result: ParsedResult): string {
|
|
|
112
108
|
lines.push(` ${location} ${msgLines[0]}${rule}`);
|
|
113
109
|
for (const extra of msgLines.slice(1)) lines.push(` ${extra}`);
|
|
114
110
|
}
|
|
111
|
+
// If the parser detected failures but couldn't extract details, surface the
|
|
112
|
+
// log path and raw tail so the model has a path forward instead of a dead end.
|
|
113
|
+
if (result.status === "fail" && (result.failures ?? []).length === 0) {
|
|
114
|
+
if (result.logPath) lines.push(`log: ${result.logPath}`);
|
|
115
|
+
if (result.rawTail) lines.push(result.rawTail);
|
|
116
|
+
}
|
|
115
117
|
return lines.join("\n");
|
|
116
118
|
}
|
|
117
119
|
|
|
@@ -157,5 +159,14 @@ export function finalizeResult(
|
|
|
157
159
|
logPath,
|
|
158
160
|
};
|
|
159
161
|
}
|
|
160
|
-
|
|
162
|
+
const finalized: ParsedResult = { ...result, exitCode, cwd, logPath };
|
|
163
|
+
// Safety net: if the parser reports failures but couldn't extract any details
|
|
164
|
+
// (e.g., tool output format changed), append the log tail so the model isn't
|
|
165
|
+
// left with "2 failed" and nothing actionable.
|
|
166
|
+
if (finalized.status === "fail" && (finalized.failures ?? []).length === 0 && !finalized.rawTail) {
|
|
167
|
+
const log = safeReadFile(logPath);
|
|
168
|
+
const lines = log.split(/\r?\n/);
|
|
169
|
+
finalized.rawTail = lines.slice(-200).join("\n");
|
|
170
|
+
}
|
|
171
|
+
return finalized;
|
|
161
172
|
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { describe, it, expect } from "vitest";
|
|
5
|
+
import parser from "./ava-text";
|
|
6
|
+
import type { RunContext } from "../types";
|
|
7
|
+
|
|
8
|
+
function makeCtx(stderr: string, cwd = "/project"): RunContext {
|
|
9
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "ava-test-"));
|
|
10
|
+
const stderrPath = path.join(dir, "stderr");
|
|
11
|
+
const stdoutPath = path.join(dir, "stdout");
|
|
12
|
+
const logPath = path.join(dir, "log");
|
|
13
|
+
fs.writeFileSync(stderrPath, stderr);
|
|
14
|
+
fs.writeFileSync(stdoutPath, "");
|
|
15
|
+
fs.writeFileSync(logPath, stderr);
|
|
16
|
+
return {
|
|
17
|
+
command: "npx ava test_math.js --no-color",
|
|
18
|
+
argv: ["npx", "ava", "test_math.js", "--no-color"],
|
|
19
|
+
cwd,
|
|
20
|
+
artifactPaths: [],
|
|
21
|
+
stdoutPath,
|
|
22
|
+
stderrPath,
|
|
23
|
+
logPath,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const FAILURE_OUTPUT = `
|
|
28
|
+
✔ adds two numbers correctly
|
|
29
|
+
✘ [fail]: multiplies two numbers correctly
|
|
30
|
+
✘ [fail]: does not divide by zero Error thrown in test
|
|
31
|
+
─
|
|
32
|
+
|
|
33
|
+
multiplies two numbers correctly
|
|
34
|
+
|
|
35
|
+
test_math.js:8
|
|
36
|
+
|
|
37
|
+
7: test('multiplies two numbers correctly', t => {
|
|
38
|
+
8: t.is(3 * 4, 99);
|
|
39
|
+
9: });
|
|
40
|
+
|
|
41
|
+
Difference (- actual, + expected):
|
|
42
|
+
|
|
43
|
+
- 12
|
|
44
|
+
+ 99
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
does not divide by zero
|
|
49
|
+
|
|
50
|
+
Error thrown in test:
|
|
51
|
+
|
|
52
|
+
TypeError {
|
|
53
|
+
message: 'Cannot read properties of null (reading \\'value\\')',
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
TypeError: Cannot read properties of null (reading 'value')
|
|
57
|
+
at /project/benchmarks/test-runners/ava/test_math.js:12:29
|
|
58
|
+
at Test.callFn (file:///project/node_modules/ava/lib/test.js:525:26)
|
|
59
|
+
at Test.run (file:///project/node_modules/ava/lib/test.js:534:33)
|
|
60
|
+
|
|
61
|
+
─
|
|
62
|
+
|
|
63
|
+
2 tests failed
|
|
64
|
+
1 test passed
|
|
65
|
+
`;
|
|
66
|
+
|
|
67
|
+
const SUCCESS_OUTPUT = `
|
|
68
|
+
✔ adds two numbers correctly
|
|
69
|
+
─
|
|
70
|
+
|
|
71
|
+
1 test passed
|
|
72
|
+
`;
|
|
73
|
+
|
|
74
|
+
describe("ava-text parser", () => {
|
|
75
|
+
it("parses assertion failure with expected/actual and file:line", async () => {
|
|
76
|
+
const result = await parser.parse(makeCtx(FAILURE_OUTPUT));
|
|
77
|
+
expect(result.status).toBe("fail");
|
|
78
|
+
expect(result.summary).toBe("2 failed, 1 passed");
|
|
79
|
+
expect(result.failures).toHaveLength(2);
|
|
80
|
+
|
|
81
|
+
const assertion = result.failures![0];
|
|
82
|
+
expect(assertion.id).toBe("multiplies two numbers correctly");
|
|
83
|
+
expect(assertion.file).toBe("test_math.js");
|
|
84
|
+
expect(assertion.line).toBe(8);
|
|
85
|
+
expect(assertion.message).toBe("expected 99, got 12");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("parses runtime error with message and file:line from stack", async () => {
|
|
89
|
+
const result = await parser.parse(makeCtx(FAILURE_OUTPUT));
|
|
90
|
+
const runtime = result.failures![1];
|
|
91
|
+
expect(runtime.id).toBe("does not divide by zero");
|
|
92
|
+
expect(runtime.file).toBe("benchmarks/test-runners/ava/test_math.js");
|
|
93
|
+
expect(runtime.line).toBe(12);
|
|
94
|
+
expect(runtime.message).toBe("Cannot read properties of null (reading \\'value\\')");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("all passing → status pass", async () => {
|
|
98
|
+
const result = await parser.parse(makeCtx(SUCCESS_OUTPUT));
|
|
99
|
+
expect(result.status).toBe("pass");
|
|
100
|
+
expect(result.summary).toBe("1 passed");
|
|
101
|
+
expect(result.failures).toHaveLength(0);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("empty stderr → status error", async () => {
|
|
105
|
+
const result = await parser.parse(makeCtx(""));
|
|
106
|
+
expect(result.status).toBe("error");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("file paths are relative to cwd", async () => {
|
|
110
|
+
const result = await parser.parse(makeCtx(FAILURE_OUTPUT));
|
|
111
|
+
const runtime = result.failures![1];
|
|
112
|
+
// absolute /project/benchmarks/... becomes relative to /project
|
|
113
|
+
expect(runtime.file).not.toMatch(/^\//);
|
|
114
|
+
expect(runtime.file).toBe("benchmarks/test-runners/ava/test_math.js");
|
|
115
|
+
});
|
|
116
|
+
});
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type { ParserModule, ParsedFailure } from "../types";
|
|
3
|
+
import { extractJsStackLocation, safeReadFile } from "./utils";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Parses AVA's default text output (--no-color).
|
|
7
|
+
* Reads stderrPath — AVA writes all output (results + diagnostics) to stderr.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// Summary line: "2 tests failed" or "3 tests passed"
|
|
11
|
+
const SUMMARY_RE = /(\d+) tests? (passed|failed)/g;
|
|
12
|
+
|
|
13
|
+
// Failure header in the results area: "✘ [fail]: <name>"
|
|
14
|
+
const FAIL_MARKER_RE = /^\s*✘\s+\[fail\]:\s+(.+?)(?:\s+Error thrown in test)?$/;
|
|
15
|
+
|
|
16
|
+
const parser: ParserModule = {
|
|
17
|
+
id: "ava-text",
|
|
18
|
+
async parse(ctx) {
|
|
19
|
+
const stderr = safeReadFile(ctx.stderrPath);
|
|
20
|
+
if (!stderr.trim()) {
|
|
21
|
+
return { tool: "ava", status: "error", summary: "no output", logPath: ctx.logPath };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Extract pass/fail counts from summary lines at the end
|
|
25
|
+
let passed = 0;
|
|
26
|
+
let failed = 0;
|
|
27
|
+
let m: RegExpExecArray | null;
|
|
28
|
+
while ((m = SUMMARY_RE.exec(stderr)) !== null) {
|
|
29
|
+
const count = parseInt(m[1], 10);
|
|
30
|
+
if (m[2] === "passed") passed = count;
|
|
31
|
+
else failed = count;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (passed === 0 && failed === 0) {
|
|
35
|
+
return { tool: "ava", status: "error", summary: "could not parse ava output", logPath: ctx.logPath };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const failures = parseFailureBlocks(stderr, ctx.cwd);
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
tool: "ava",
|
|
42
|
+
status: failed > 0 ? "fail" : "pass",
|
|
43
|
+
summary: failed > 0 ? `${failed} failed, ${passed} passed` : `${passed} passed`,
|
|
44
|
+
failures,
|
|
45
|
+
logPath: ctx.logPath,
|
|
46
|
+
};
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export default parser;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* AVA's failure detail section lives between the first and last ─ separators.
|
|
54
|
+
* Each failure block starts with the test name (matching a name from the ✘ lines),
|
|
55
|
+
* followed by either a file:line (assertion) or "Error thrown in test:" (runtime).
|
|
56
|
+
*
|
|
57
|
+
* Strategy: collect failed test names from ✘ lines, then split the detail section
|
|
58
|
+
* by those names to get per-failure blocks.
|
|
59
|
+
*/
|
|
60
|
+
function parseFailureBlocks(output: string, cwd: string): ParsedFailure[] {
|
|
61
|
+
const lines = output.split("\n");
|
|
62
|
+
|
|
63
|
+
// Collect failed test names from ✘ markers
|
|
64
|
+
const failedNames: string[] = [];
|
|
65
|
+
for (const line of lines) {
|
|
66
|
+
const fm = FAIL_MARKER_RE.exec(line);
|
|
67
|
+
if (fm) failedNames.push(fm[1]);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (failedNames.length === 0) return [];
|
|
71
|
+
|
|
72
|
+
// Find the detail section between first and last ─ separators
|
|
73
|
+
const sepIndices: number[] = [];
|
|
74
|
+
for (let i = 0; i < lines.length; i++) {
|
|
75
|
+
if (lines[i].match(/^\s*─\s*$/)) sepIndices.push(i);
|
|
76
|
+
}
|
|
77
|
+
if (sepIndices.length < 2) return [];
|
|
78
|
+
|
|
79
|
+
const detailStart = sepIndices[0] + 1;
|
|
80
|
+
const detailEnd = sepIndices[sepIndices.length - 1];
|
|
81
|
+
const detailLines = lines.slice(detailStart, detailEnd);
|
|
82
|
+
|
|
83
|
+
// Split detail lines into blocks by test name
|
|
84
|
+
const blocks: Array<{ name: string; lines: string[] }> = [];
|
|
85
|
+
let currentBlock: { name: string; lines: string[] } | null = null;
|
|
86
|
+
|
|
87
|
+
for (const line of detailLines) {
|
|
88
|
+
const trimmed = line.trim();
|
|
89
|
+
if (failedNames.includes(trimmed)) {
|
|
90
|
+
if (currentBlock) blocks.push(currentBlock);
|
|
91
|
+
currentBlock = { name: trimmed, lines: [] };
|
|
92
|
+
} else if (currentBlock) {
|
|
93
|
+
currentBlock.lines.push(line);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (currentBlock) blocks.push(currentBlock);
|
|
97
|
+
|
|
98
|
+
// Parse each block
|
|
99
|
+
return blocks.map((block) => parseBlock(block.name, block.lines, cwd));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function parseBlock(name: string, lines: string[], cwd: string): ParsedFailure {
|
|
103
|
+
// Determine type by looking for "Error thrown in test:"
|
|
104
|
+
const isRuntimeError = lines.some((l) => l.trim() === "Error thrown in test:");
|
|
105
|
+
|
|
106
|
+
if (isRuntimeError) {
|
|
107
|
+
return parseRuntimeError(name, lines, cwd);
|
|
108
|
+
}
|
|
109
|
+
return parseAssertionFailure(name, lines, cwd);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function parseAssertionFailure(name: string, lines: string[], cwd: string): ParsedFailure {
|
|
113
|
+
let file: string | undefined;
|
|
114
|
+
let line: number | undefined;
|
|
115
|
+
let actual: string | undefined;
|
|
116
|
+
let expected: string | undefined;
|
|
117
|
+
|
|
118
|
+
for (const l of lines) {
|
|
119
|
+
const trimmed = l.trim();
|
|
120
|
+
|
|
121
|
+
// file:line (standalone, e.g. "test_math.js:8")
|
|
122
|
+
if (!file) {
|
|
123
|
+
const flm = trimmed.match(/^(.+\.[a-z]{1,4}):(\d+)$/);
|
|
124
|
+
if (flm) {
|
|
125
|
+
file = path.relative(cwd, path.resolve(cwd, flm[1]));
|
|
126
|
+
line = parseInt(flm[2], 10);
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Diff values: "- 12" (actual), "+ 99" (expected)
|
|
132
|
+
if (trimmed.startsWith("- ") && actual === undefined) {
|
|
133
|
+
actual = trimmed.slice(2);
|
|
134
|
+
} else if (trimmed.startsWith("+ ") && expected === undefined) {
|
|
135
|
+
expected = trimmed.slice(2);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const message = actual !== undefined && expected !== undefined ? `expected ${expected}, got ${actual}` : undefined;
|
|
140
|
+
|
|
141
|
+
return { id: name, file, line, message };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function parseRuntimeError(name: string, lines: string[], cwd: string): ParsedFailure {
|
|
145
|
+
let message: string | undefined;
|
|
146
|
+
|
|
147
|
+
for (const l of lines) {
|
|
148
|
+
const trimmed = l.trim();
|
|
149
|
+
// Extract message from "message: '...'" inside the error object
|
|
150
|
+
if (!message) {
|
|
151
|
+
const msgMatch = trimmed.match(/^message:\s*'(.+)',?$/);
|
|
152
|
+
if (msgMatch) message = msgMatch[1];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Extract file:line from the first user stack frame
|
|
157
|
+
const loc = extractJsStackLocation(lines.join("\n"));
|
|
158
|
+
const file = loc.file ? path.relative(cwd, path.resolve(cwd, loc.file)) : undefined;
|
|
159
|
+
|
|
160
|
+
return { id: name, file, line: loc.line, message };
|
|
161
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { describe, it, expect } from "vitest";
|
|
5
|
+
import parser from "./bandit-json";
|
|
6
|
+
import type { RunContext } from "../types";
|
|
7
|
+
|
|
8
|
+
function makeCtx(stdout: string, cwd = "/project"): RunContext {
|
|
9
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bandit-test-"));
|
|
10
|
+
const stdoutPath = path.join(dir, "stdout");
|
|
11
|
+
fs.writeFileSync(stdoutPath, stdout);
|
|
12
|
+
return {
|
|
13
|
+
command: "bandit -f json .",
|
|
14
|
+
argv: ["bandit", "-f", "json", "."],
|
|
15
|
+
cwd,
|
|
16
|
+
artifactPaths: [],
|
|
17
|
+
stdoutPath,
|
|
18
|
+
stderrPath: path.join(dir, "stderr"),
|
|
19
|
+
logPath: path.join(dir, "log"),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe("bandit-json parser", () => {
|
|
24
|
+
it("multiple issues → correct severity summary, relative paths, rule includes test_id and test_name", async () => {
|
|
25
|
+
const cwd = "/project";
|
|
26
|
+
const stdout = JSON.stringify({
|
|
27
|
+
results: [
|
|
28
|
+
{
|
|
29
|
+
filename: "/project/src/app.py",
|
|
30
|
+
line_number: 1,
|
|
31
|
+
issue_text: "Consider possible security implications associated with the subprocess module.",
|
|
32
|
+
issue_severity: "LOW",
|
|
33
|
+
issue_confidence: "HIGH",
|
|
34
|
+
test_id: "B404",
|
|
35
|
+
test_name: "blacklist",
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
filename: "/project/src/app.py",
|
|
39
|
+
line_number: 4,
|
|
40
|
+
issue_text: "subprocess call with shell=True identified, security issue.",
|
|
41
|
+
issue_severity: "HIGH",
|
|
42
|
+
issue_confidence: "HIGH",
|
|
43
|
+
test_id: "B602",
|
|
44
|
+
test_name: "subprocess_popen_with_shell_equals_true",
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
});
|
|
48
|
+
const result = await parser.parse(makeCtx(stdout, cwd));
|
|
49
|
+
expect(result.status).toBe("fail");
|
|
50
|
+
expect(result.summary).toBe("2 issues (1 high, 1 low)");
|
|
51
|
+
expect(result.failures).toHaveLength(2);
|
|
52
|
+
expect(result.failures![0].file).toBe("src/app.py");
|
|
53
|
+
expect(result.failures![0].rule).toBe("B404:blacklist");
|
|
54
|
+
expect(result.failures![1].rule).toBe("B602:subprocess_popen_with_shell_equals_true");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("no issues → status pass, empty failures", async () => {
|
|
58
|
+
const stdout = JSON.stringify({ results: [] });
|
|
59
|
+
const result = await parser.parse(makeCtx(stdout));
|
|
60
|
+
expect(result.status).toBe("pass");
|
|
61
|
+
expect(result.summary).toBe("no security issues");
|
|
62
|
+
expect(result.failures).toHaveLength(0);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("empty stdout → no crash, status pass", async () => {
|
|
66
|
+
const result = await parser.parse(makeCtx(""));
|
|
67
|
+
expect(result.status).toBe("pass");
|
|
68
|
+
expect(result.failures).toHaveLength(0);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("single high severity issue → singular 'issue' in summary", async () => {
|
|
72
|
+
const stdout = JSON.stringify({
|
|
73
|
+
results: [
|
|
74
|
+
{
|
|
75
|
+
filename: "/project/danger.py",
|
|
76
|
+
line_number: 10,
|
|
77
|
+
issue_text: "Possible hardcoded password.",
|
|
78
|
+
issue_severity: "HIGH",
|
|
79
|
+
issue_confidence: "MEDIUM",
|
|
80
|
+
test_id: "B105",
|
|
81
|
+
test_name: "hardcoded_password_string",
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
});
|
|
85
|
+
const result = await parser.parse(makeCtx(stdout));
|
|
86
|
+
expect(result.status).toBe("fail");
|
|
87
|
+
expect(result.summary).toBe("1 issue (1 high)");
|
|
88
|
+
expect(result.failures).toHaveLength(1);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type { ParserModule, ParsedFailure } from "../types";
|
|
3
|
+
import { safeReadFile } from "./utils";
|
|
4
|
+
|
|
5
|
+
interface BanditResult {
|
|
6
|
+
filename: string;
|
|
7
|
+
line_number: number;
|
|
8
|
+
issue_text: string;
|
|
9
|
+
issue_severity: string;
|
|
10
|
+
test_id: string;
|
|
11
|
+
test_name: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface BanditOutput {
|
|
15
|
+
results?: BanditResult[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const parser: ParserModule = {
|
|
19
|
+
id: "bandit-json",
|
|
20
|
+
async parse(ctx) {
|
|
21
|
+
const stdout = safeReadFile(ctx.stdoutPath).trim();
|
|
22
|
+
if (!stdout) {
|
|
23
|
+
return {
|
|
24
|
+
tool: "bandit",
|
|
25
|
+
status: "pass",
|
|
26
|
+
summary: "no security issues",
|
|
27
|
+
failures: [],
|
|
28
|
+
logPath: ctx.logPath,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let output: BanditOutput;
|
|
33
|
+
try {
|
|
34
|
+
output = JSON.parse(stdout);
|
|
35
|
+
} catch {
|
|
36
|
+
return {
|
|
37
|
+
tool: "bandit",
|
|
38
|
+
status: "error",
|
|
39
|
+
summary: "failed to parse bandit JSON output",
|
|
40
|
+
logPath: ctx.logPath,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const results = output.results ?? [];
|
|
45
|
+
const failures: ParsedFailure[] = results.map((r) => {
|
|
46
|
+
const relPath = path.relative(ctx.cwd, r.filename);
|
|
47
|
+
return {
|
|
48
|
+
id: `${relPath}:${r.line_number}:${r.test_id}`,
|
|
49
|
+
file: relPath,
|
|
50
|
+
line: r.line_number,
|
|
51
|
+
message: r.issue_text,
|
|
52
|
+
rule: `${r.test_id}:${r.test_name}`,
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const bySeverity: Record<string, number> = {};
|
|
57
|
+
for (const r of results) {
|
|
58
|
+
const sev = r.issue_severity.toLowerCase();
|
|
59
|
+
bySeverity[sev] = (bySeverity[sev] ?? 0) + 1;
|
|
60
|
+
}
|
|
61
|
+
const sevParts = ["high", "medium", "low"].filter((s) => bySeverity[s]).map((s) => `${bySeverity[s]} ${s}`);
|
|
62
|
+
|
|
63
|
+
const summary =
|
|
64
|
+
results.length > 0
|
|
65
|
+
? `${results.length} issue${results.length !== 1 ? "s" : ""} (${sevParts.join(", ")})`
|
|
66
|
+
: "no security issues";
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
tool: "bandit",
|
|
70
|
+
status: failures.length > 0 ? "fail" : "pass",
|
|
71
|
+
summary,
|
|
72
|
+
failures,
|
|
73
|
+
logPath: ctx.logPath,
|
|
74
|
+
};
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export default parser;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { describe, it, expect } from "vitest";
|
|
5
|
+
import parser from "./black-text";
|
|
6
|
+
import type { RunContext } from "../types";
|
|
7
|
+
|
|
8
|
+
function makeCtx(stdout: string, stderr = "", cwd = "/project"): RunContext {
|
|
9
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "black-test-"));
|
|
10
|
+
const stdoutPath = path.join(dir, "stdout");
|
|
11
|
+
const stderrPath = path.join(dir, "stderr");
|
|
12
|
+
fs.writeFileSync(stdoutPath, stdout);
|
|
13
|
+
fs.writeFileSync(stderrPath, stderr);
|
|
14
|
+
return {
|
|
15
|
+
command: "black --check .",
|
|
16
|
+
argv: ["black", "--check", "."],
|
|
17
|
+
cwd,
|
|
18
|
+
artifactPaths: [],
|
|
19
|
+
stdoutPath,
|
|
20
|
+
stderrPath,
|
|
21
|
+
logPath: path.join(dir, "log"),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe("black-text parser", () => {
|
|
26
|
+
it("files needing reformat → strips emoji, lists files, status fail", async () => {
|
|
27
|
+
const cwd = "/project";
|
|
28
|
+
const stderr = `would reformat /project/src/app.py
|
|
29
|
+
would reformat /project/src/utils.py
|
|
30
|
+
|
|
31
|
+
Oh no! 💥 💔 💥
|
|
32
|
+
2 files would be reformatted.`;
|
|
33
|
+
const result = await parser.parse(makeCtx("", stderr, cwd));
|
|
34
|
+
expect(result.status).toBe("fail");
|
|
35
|
+
expect(result.summary).toBe("2 files would be reformatted");
|
|
36
|
+
expect(result.failures).toHaveLength(2);
|
|
37
|
+
expect(result.failures![0].file).toBe("src/app.py");
|
|
38
|
+
expect(result.failures![1].file).toBe("src/utils.py");
|
|
39
|
+
expect(result.failures![0].message).toBe("would reformat");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("all formatted → status pass", async () => {
|
|
43
|
+
const stderr = `All done! ✨ 🍰 ✨
|
|
44
|
+
3 files would be left unchanged.`;
|
|
45
|
+
const result = await parser.parse(makeCtx("", stderr));
|
|
46
|
+
expect(result.status).toBe("pass");
|
|
47
|
+
expect(result.summary).toBe("all files formatted");
|
|
48
|
+
expect(result.failures).toHaveLength(0);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("format errors → captured with message", async () => {
|
|
52
|
+
const stderr = `error: cannot format /project/bad.py: Cannot parse: 1:0:
|
|
53
|
+
Oh no! 💥 💔 💥
|
|
54
|
+
1 file would fail to reformat.`;
|
|
55
|
+
const result = await parser.parse(makeCtx("", stderr));
|
|
56
|
+
expect(result.status).toBe("fail");
|
|
57
|
+
expect(result.failures).toHaveLength(1);
|
|
58
|
+
expect(result.failures![0].file).toBe("bad.py");
|
|
59
|
+
expect(result.failures![0].message).toContain("Cannot parse");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("empty output → no crash, status pass", async () => {
|
|
63
|
+
const result = await parser.parse(makeCtx(""));
|
|
64
|
+
expect(result.status).toBe("pass");
|
|
65
|
+
expect(result.failures).toHaveLength(0);
|
|
66
|
+
});
|
|
67
|
+
});
|