@robhowley/pi-structured-return 1.5.0 → 1.6.1
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 +86 -63
- package/extensions/structured-return/src/config/registry.ts +92 -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/clang-text.test.ts +61 -0
- package/extensions/structured-return/src/parsers/clang-text.ts +59 -0
- 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/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/markdownlint-json.test.ts +76 -0
- package/extensions/structured-return/src/parsers/markdownlint-json.ts +73 -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/pyright-json.test.ts +112 -0
- package/extensions/structured-return/src/parsers/pyright-json.ts +81 -0
- 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/vale-json.test.ts +84 -0
- package/extensions/structured-return/src/parsers/vale-json.ts +80 -0
- package/package.json +8 -3
- package/skills/structured-return/SKILL.md +58 -6
|
@@ -0,0 +1,52 @@
|
|
|
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 "./jsonlint-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(), "jsonlint-test-"));
|
|
10
|
+
const stdoutPath = path.join(dir, "stdout");
|
|
11
|
+
const stderrPath = path.join(dir, "stderr");
|
|
12
|
+
fs.writeFileSync(stdoutPath, "");
|
|
13
|
+
fs.writeFileSync(stderrPath, stderr);
|
|
14
|
+
return {
|
|
15
|
+
command: "npx jsonlint src/config.json",
|
|
16
|
+
argv: ["npx", "jsonlint", "src/config.json"],
|
|
17
|
+
cwd,
|
|
18
|
+
artifactPaths: [],
|
|
19
|
+
stdoutPath,
|
|
20
|
+
stderrPath,
|
|
21
|
+
logPath: path.join(dir, "log"),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe("jsonlint-text parser", () => {
|
|
26
|
+
it("parse error with stack trace → strips stack, extracts line and expecting message", async () => {
|
|
27
|
+
const stderr = `Error: Parse error on line 1:
|
|
28
|
+
{"name": "test", missing: "quotes", }
|
|
29
|
+
-----------------^
|
|
30
|
+
Expecting 'STRING', got 'undefined'
|
|
31
|
+
at Object.parseError (jsonlint/lib/jsonlint.js:55:11)
|
|
32
|
+
at Object.parse (jsonlint/lib/jsonlint.js:132:22)
|
|
33
|
+
at parse (jsonlint/lib/cli.js:82:14)
|
|
34
|
+
at main (jsonlint/lib/cli.js:135:14)`;
|
|
35
|
+
const result = await parser.parse(makeCtx(stderr));
|
|
36
|
+
expect(result.status).toBe("fail");
|
|
37
|
+
expect(result.summary).toBe("parse error");
|
|
38
|
+
expect(result.failures).toHaveLength(1);
|
|
39
|
+
expect(result.failures![0].file).toBe("src/config.json");
|
|
40
|
+
expect(result.failures![0].line).toBe(1);
|
|
41
|
+
expect(result.failures![0].message).toBe("Expecting 'STRING', got 'undefined'");
|
|
42
|
+
// No stack trace
|
|
43
|
+
expect(result.failures![0].message).not.toContain("at Object");
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("valid JSON → status pass", async () => {
|
|
47
|
+
const result = await parser.parse(makeCtx(""));
|
|
48
|
+
expect(result.status).toBe("pass");
|
|
49
|
+
expect(result.summary).toBe("valid JSON");
|
|
50
|
+
expect(result.failures).toHaveLength(0);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { ParserModule, ParsedFailure } from "../types";
|
|
2
|
+
import { safeReadFile } from "./utils";
|
|
3
|
+
|
|
4
|
+
// Error: Parse error on line N:
|
|
5
|
+
const ERROR_HEADER = /^Error: Parse error on line (\d+):$/;
|
|
6
|
+
// Expecting 'X', got 'Y'
|
|
7
|
+
const EXPECTING_LINE = /^(Expecting .+)$/;
|
|
8
|
+
|
|
9
|
+
const parser: ParserModule = {
|
|
10
|
+
id: "jsonlint-text",
|
|
11
|
+
async parse(ctx) {
|
|
12
|
+
const combined = (safeReadFile(ctx.stdoutPath) + "\n" + safeReadFile(ctx.stderrPath)).trim();
|
|
13
|
+
if (!combined) {
|
|
14
|
+
return {
|
|
15
|
+
tool: "jsonlint",
|
|
16
|
+
status: "pass",
|
|
17
|
+
summary: "valid JSON",
|
|
18
|
+
failures: [],
|
|
19
|
+
logPath: ctx.logPath,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Derive filename from argv — use last non-flag arg since earlier positional
|
|
24
|
+
// args may be flag values (e.g. npx --yes jsonlint file.json).
|
|
25
|
+
const file = ctx.argv.filter((a) => !a.startsWith("-") && a !== "jsonlint" && a !== "npx").pop() ?? "input";
|
|
26
|
+
|
|
27
|
+
const lines = combined.split("\n");
|
|
28
|
+
const failures: ParsedFailure[] = [];
|
|
29
|
+
let line: number | undefined;
|
|
30
|
+
let message = "";
|
|
31
|
+
|
|
32
|
+
for (const l of lines) {
|
|
33
|
+
const headerMatch = l.match(ERROR_HEADER);
|
|
34
|
+
if (headerMatch) {
|
|
35
|
+
line = parseInt(headerMatch[1], 10);
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const expectMatch = l.match(EXPECTING_LINE);
|
|
39
|
+
if (expectMatch) {
|
|
40
|
+
message = expectMatch[1];
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (line !== undefined) {
|
|
46
|
+
failures.push({
|
|
47
|
+
id: `${file}:${line}`,
|
|
48
|
+
file,
|
|
49
|
+
line,
|
|
50
|
+
message: message || "parse error",
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
tool: "jsonlint",
|
|
56
|
+
status: failures.length > 0 ? "fail" : "pass",
|
|
57
|
+
summary: failures.length > 0 ? "parse error" : "valid JSON",
|
|
58
|
+
failures,
|
|
59
|
+
logPath: ctx.logPath,
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export default parser;
|
|
@@ -0,0 +1,76 @@
|
|
|
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 "./markdownlint-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(), "markdownlint-test-"));
|
|
10
|
+
const stdoutPath = path.join(dir, "stdout");
|
|
11
|
+
fs.writeFileSync(stdoutPath, stdout);
|
|
12
|
+
return {
|
|
13
|
+
command: "markdownlint --json .",
|
|
14
|
+
argv: ["markdownlint", "--json", "."],
|
|
15
|
+
cwd,
|
|
16
|
+
artifactPaths: [],
|
|
17
|
+
stdoutPath,
|
|
18
|
+
stderrPath: path.join(dir, "stderr"),
|
|
19
|
+
logPath: path.join(dir, "log"),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe("markdownlint-json parser", () => {
|
|
24
|
+
it("multiple issues → correct relative paths, rule codes, descriptions", async () => {
|
|
25
|
+
const cwd = "/project";
|
|
26
|
+
const stdout = JSON.stringify([
|
|
27
|
+
{
|
|
28
|
+
fileName: "/project/docs/README.md",
|
|
29
|
+
lineNumber: 1,
|
|
30
|
+
ruleNames: ["MD041", "first-line-heading"],
|
|
31
|
+
ruleDescription: "First line in a file should be a top-level heading",
|
|
32
|
+
errorDetail: null,
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
fileName: "/project/docs/README.md",
|
|
36
|
+
lineNumber: 3,
|
|
37
|
+
ruleNames: ["MD009", "no-trailing-spaces"],
|
|
38
|
+
ruleDescription: "Trailing spaces",
|
|
39
|
+
errorDetail: "Expected: 0 or 2; Actual: 3",
|
|
40
|
+
},
|
|
41
|
+
]);
|
|
42
|
+
const result = await parser.parse(makeCtx(stdout, cwd));
|
|
43
|
+
expect(result.status).toBe("fail");
|
|
44
|
+
expect(result.summary).toBe("2 lint errors");
|
|
45
|
+
expect(result.failures).toHaveLength(2);
|
|
46
|
+
expect(result.failures![0].file).toBe("docs/README.md");
|
|
47
|
+
expect(result.failures![0].rule).toBe("MD041");
|
|
48
|
+
expect(result.failures![1].message).toContain("Expected: 0 or 2; Actual: 3");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("no issues → status pass", async () => {
|
|
52
|
+
const result = await parser.parse(makeCtx(JSON.stringify([])));
|
|
53
|
+
expect(result.status).toBe("pass");
|
|
54
|
+
expect(result.failures).toHaveLength(0);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("empty stdout → no crash, status pass", async () => {
|
|
58
|
+
const result = await parser.parse(makeCtx(""));
|
|
59
|
+
expect(result.status).toBe("pass");
|
|
60
|
+
expect(result.failures).toHaveLength(0);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("single issue → singular 'error' in summary", async () => {
|
|
64
|
+
const stdout = JSON.stringify([
|
|
65
|
+
{
|
|
66
|
+
fileName: "/project/README.md",
|
|
67
|
+
lineNumber: 5,
|
|
68
|
+
ruleNames: ["MD032", "blanks-around-lists"],
|
|
69
|
+
ruleDescription: "Lists should be surrounded by blank lines",
|
|
70
|
+
errorDetail: null,
|
|
71
|
+
},
|
|
72
|
+
]);
|
|
73
|
+
const result = await parser.parse(makeCtx(stdout));
|
|
74
|
+
expect(result.summary).toBe("1 lint error");
|
|
75
|
+
});
|
|
76
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type { ParserModule, ParsedFailure } from "../types";
|
|
3
|
+
import { safeReadFile } from "./utils";
|
|
4
|
+
|
|
5
|
+
interface MarkdownlintItem {
|
|
6
|
+
fileName: string;
|
|
7
|
+
lineNumber: number;
|
|
8
|
+
ruleNames: string[];
|
|
9
|
+
ruleDescription: string;
|
|
10
|
+
errorDetail?: string | null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const parser: ParserModule = {
|
|
14
|
+
id: "markdownlint-json",
|
|
15
|
+
async parse(ctx) {
|
|
16
|
+
const stdout = safeReadFile(ctx.stdoutPath).trim();
|
|
17
|
+
if (!stdout) {
|
|
18
|
+
return {
|
|
19
|
+
tool: "markdownlint",
|
|
20
|
+
status: "pass",
|
|
21
|
+
summary: "no lint errors",
|
|
22
|
+
failures: [],
|
|
23
|
+
logPath: ctx.logPath,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let items: MarkdownlintItem[];
|
|
28
|
+
try {
|
|
29
|
+
items = JSON.parse(stdout);
|
|
30
|
+
} catch {
|
|
31
|
+
return {
|
|
32
|
+
tool: "markdownlint",
|
|
33
|
+
status: "error",
|
|
34
|
+
summary: "failed to parse markdownlint JSON output",
|
|
35
|
+
logPath: ctx.logPath,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!Array.isArray(items) || items.length === 0) {
|
|
40
|
+
return {
|
|
41
|
+
tool: "markdownlint",
|
|
42
|
+
status: "pass",
|
|
43
|
+
summary: "no lint errors",
|
|
44
|
+
failures: [],
|
|
45
|
+
logPath: ctx.logPath,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const failures: ParsedFailure[] = items.map((item) => {
|
|
50
|
+
const relPath = path.relative(ctx.cwd, item.fileName);
|
|
51
|
+
// Use the short MD code (first rule name, e.g. "MD041")
|
|
52
|
+
const rule = item.ruleNames[0];
|
|
53
|
+
const message = item.errorDetail ? `${item.ruleDescription}: ${item.errorDetail}` : item.ruleDescription;
|
|
54
|
+
return {
|
|
55
|
+
id: `${relPath}:${item.lineNumber}:${rule}`,
|
|
56
|
+
file: relPath,
|
|
57
|
+
line: item.lineNumber,
|
|
58
|
+
message,
|
|
59
|
+
rule,
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
tool: "markdownlint",
|
|
65
|
+
status: "fail",
|
|
66
|
+
summary: `${failures.length} lint error${failures.length !== 1 ? "s" : ""}`,
|
|
67
|
+
failures,
|
|
68
|
+
logPath: ctx.logPath,
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export default parser;
|
|
@@ -0,0 +1,117 @@
|
|
|
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 "./node-test-text";
|
|
6
|
+
import type { RunContext } from "../types";
|
|
7
|
+
|
|
8
|
+
function makeCtx(stdout: string, cwd = "/project"): RunContext {
|
|
9
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "node-test-text-"));
|
|
10
|
+
const stdoutPath = path.join(dir, "stdout");
|
|
11
|
+
const stderrPath = path.join(dir, "stderr");
|
|
12
|
+
fs.writeFileSync(stdoutPath, stdout);
|
|
13
|
+
fs.writeFileSync(stderrPath, "");
|
|
14
|
+
return {
|
|
15
|
+
command: "node --test",
|
|
16
|
+
argv: ["node", "--test"],
|
|
17
|
+
cwd,
|
|
18
|
+
artifactPaths: [],
|
|
19
|
+
stdoutPath,
|
|
20
|
+
stderrPath,
|
|
21
|
+
logPath: path.join(dir, "log"),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe("node-test-text parser", () => {
|
|
26
|
+
it("mixed pass/fail → correct summary, file:line, and assertion messages", async () => {
|
|
27
|
+
const cwd = "/project";
|
|
28
|
+
const stdout = `▶ math
|
|
29
|
+
✔ adds two numbers correctly (0.329ms)
|
|
30
|
+
✖ multiplies two numbers correctly (0.466ms)
|
|
31
|
+
✖ does not divide by zero (5.098ms)
|
|
32
|
+
✖ math (6.373ms)
|
|
33
|
+
ℹ tests 3
|
|
34
|
+
ℹ suites 1
|
|
35
|
+
ℹ pass 1
|
|
36
|
+
ℹ fail 2
|
|
37
|
+
ℹ cancelled 0
|
|
38
|
+
ℹ skipped 0
|
|
39
|
+
ℹ todo 0
|
|
40
|
+
ℹ duration_ms 68.049
|
|
41
|
+
|
|
42
|
+
✖ failing tests:
|
|
43
|
+
|
|
44
|
+
test at /project/src/test_math.mjs:9:3
|
|
45
|
+
✖ multiplies two numbers correctly (0.466ms)
|
|
46
|
+
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
|
|
47
|
+
|
|
48
|
+
12 !== 99
|
|
49
|
+
|
|
50
|
+
at TestContext.<anonymous> (file:///project/src/test_math.mjs:10:12)
|
|
51
|
+
at Test.runInAsyncScope (node:async_hooks:226:14)
|
|
52
|
+
|
|
53
|
+
test at /project/src/test_math.mjs:13:3
|
|
54
|
+
✖ does not divide by zero (5.098ms)
|
|
55
|
+
AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
|
|
56
|
+
|
|
57
|
+
assert.ok(isFinite(result))
|
|
58
|
+
|
|
59
|
+
at TestContext.<anonymous> (file:///project/src/test_math.mjs:15:12)`;
|
|
60
|
+
const result = await parser.parse(makeCtx(stdout, cwd));
|
|
61
|
+
expect(result.status).toBe("fail");
|
|
62
|
+
expect(result.summary).toBe("2 failed, 1 passed");
|
|
63
|
+
expect(result.failures).toHaveLength(2);
|
|
64
|
+
expect(result.failures![0].file).toBe("src/test_math.mjs");
|
|
65
|
+
expect(result.failures![0].line).toBe(9);
|
|
66
|
+
expect(result.failures![0].message).toContain("12 !== 99");
|
|
67
|
+
expect(result.failures![1].message).toContain("assert.ok(isFinite(result))");
|
|
68
|
+
// No stack traces in output
|
|
69
|
+
expect(result.failures![0].message).not.toContain("at Test");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("all passing → status pass, no failures", async () => {
|
|
73
|
+
const stdout = `▶ math
|
|
74
|
+
✔ adds correctly (0.2ms)
|
|
75
|
+
✔ math (1ms)
|
|
76
|
+
ℹ tests 1
|
|
77
|
+
ℹ suites 1
|
|
78
|
+
ℹ pass 1
|
|
79
|
+
ℹ fail 0
|
|
80
|
+
ℹ duration_ms 50.0`;
|
|
81
|
+
const result = await parser.parse(makeCtx(stdout));
|
|
82
|
+
expect(result.status).toBe("pass");
|
|
83
|
+
expect(result.summary).toBe("1 passed");
|
|
84
|
+
expect(result.failures).toHaveLength(0);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("non-assertion error (TypeError) → captures error message", async () => {
|
|
88
|
+
const cwd = "/project";
|
|
89
|
+
const stdout = `▶ suite
|
|
90
|
+
✖ throws type error (1ms)
|
|
91
|
+
✖ suite (2ms)
|
|
92
|
+
ℹ tests 1
|
|
93
|
+
ℹ suites 1
|
|
94
|
+
ℹ pass 0
|
|
95
|
+
ℹ fail 1
|
|
96
|
+
ℹ duration_ms 50.0
|
|
97
|
+
|
|
98
|
+
✖ failing tests:
|
|
99
|
+
|
|
100
|
+
test at /project/src/test.mjs:5:3
|
|
101
|
+
✖ throws type error (1ms)
|
|
102
|
+
TypeError: Cannot read properties of null (reading 'foo')
|
|
103
|
+
at TestContext.<anonymous> (file:///project/src/test.mjs:6:10)
|
|
104
|
+
at Test.runInAsyncScope (node:async_hooks:226:14)`;
|
|
105
|
+
const result = await parser.parse(makeCtx(stdout, cwd));
|
|
106
|
+
expect(result.status).toBe("fail");
|
|
107
|
+
expect(result.failures).toHaveLength(1);
|
|
108
|
+
expect(result.failures![0].message).toContain("Cannot read properties of null");
|
|
109
|
+
expect(result.failures![0].message).not.toContain("at Test");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("empty stdout → no crash, status pass", async () => {
|
|
113
|
+
const result = await parser.parse(makeCtx(""));
|
|
114
|
+
expect(result.status).toBe("pass");
|
|
115
|
+
expect(result.failures).toHaveLength(0);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type { ParserModule, ParsedFailure } from "../types";
|
|
3
|
+
import { safeReadFile } from "./utils";
|
|
4
|
+
|
|
5
|
+
// Summary lines: ℹ pass 1, ℹ fail 2
|
|
6
|
+
const SUMMARY_PASS = /^ℹ pass (\d+)/m;
|
|
7
|
+
const SUMMARY_FAIL = /^ℹ fail (\d+)/m;
|
|
8
|
+
|
|
9
|
+
// Failing test header: test at file:line:col
|
|
10
|
+
const TEST_AT_LINE = /^test at (.+?):(\d+):\d+/;
|
|
11
|
+
|
|
12
|
+
const parser: ParserModule = {
|
|
13
|
+
id: "node-test-text",
|
|
14
|
+
async parse(ctx) {
|
|
15
|
+
const combined = safeReadFile(ctx.stdoutPath) + safeReadFile(ctx.stderrPath);
|
|
16
|
+
if (!combined.trim()) {
|
|
17
|
+
return {
|
|
18
|
+
tool: "node:test",
|
|
19
|
+
status: "pass",
|
|
20
|
+
summary: "no tests found",
|
|
21
|
+
failures: [],
|
|
22
|
+
logPath: ctx.logPath,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const passMatch = combined.match(SUMMARY_PASS);
|
|
27
|
+
const failMatch = combined.match(SUMMARY_FAIL);
|
|
28
|
+
const passed = passMatch ? parseInt(passMatch[1], 10) : 0;
|
|
29
|
+
const failed = failMatch ? parseInt(failMatch[1], 10) : 0;
|
|
30
|
+
|
|
31
|
+
// Parse the "failing tests:" section
|
|
32
|
+
const failingSection = combined.split("✖ failing tests:")[1] ?? "";
|
|
33
|
+
const failures: ParsedFailure[] = [];
|
|
34
|
+
|
|
35
|
+
if (failingSection) {
|
|
36
|
+
// Split into per-test blocks by "test at" lines
|
|
37
|
+
const blocks = failingSection.split(/(?=^test at )/m).filter((b) => b.trim());
|
|
38
|
+
|
|
39
|
+
for (const block of blocks) {
|
|
40
|
+
const atMatch = block.match(TEST_AT_LINE);
|
|
41
|
+
const nameMatch = block.match(/^✖ (.+?) \(/m);
|
|
42
|
+
|
|
43
|
+
let file: string | undefined;
|
|
44
|
+
let line: number | undefined;
|
|
45
|
+
if (atMatch) {
|
|
46
|
+
file = path.relative(ctx.cwd, atMatch[1]);
|
|
47
|
+
line = parseInt(atMatch[2], 10);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const name = nameMatch ? nameMatch[1] : "unknown test";
|
|
51
|
+
|
|
52
|
+
// Extract meaningful error message — try specific patterns first, then generic
|
|
53
|
+
let message = name;
|
|
54
|
+
const strictMatch = block.match(/Expected values to be strictly equal:\s*\n\s*\n\s*(.+)/);
|
|
55
|
+
const falsyMatch = block.match(/The expression evaluated to a falsy value:\s*\n\s*\n\s*(.+)/);
|
|
56
|
+
if (strictMatch) {
|
|
57
|
+
message = `Expected values to be strictly equal: ${strictMatch[1].trim()}`;
|
|
58
|
+
} else if (falsyMatch) {
|
|
59
|
+
message = `The expression evaluated to a falsy value: ${falsyMatch[1].trim()}`;
|
|
60
|
+
} else {
|
|
61
|
+
// Match any Error type (AssertionError, TypeError, ReferenceError, etc.)
|
|
62
|
+
const errorMatch = block.match(/^\s+(\w*Error\b.*?)$/m);
|
|
63
|
+
if (errorMatch) message = errorMatch[1].replace(/\s*\[ERR_\w+\]:\s*/, ": ").replace(/^\w+Error: /, "");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
failures.push({
|
|
67
|
+
id: `${file}:${line}`,
|
|
68
|
+
file,
|
|
69
|
+
line,
|
|
70
|
+
message,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const parts: string[] = [];
|
|
76
|
+
if (failed > 0) parts.push(`${failed} failed`);
|
|
77
|
+
if (passed > 0) parts.push(`${passed} passed`);
|
|
78
|
+
const summary = parts.join(", ") || "tests completed";
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
tool: "node:test",
|
|
82
|
+
status: failed > 0 ? "fail" : "pass",
|
|
83
|
+
summary,
|
|
84
|
+
failures,
|
|
85
|
+
logPath: ctx.logPath,
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export default parser;
|
|
@@ -0,0 +1,83 @@
|
|
|
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 "./npm-audit-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(), "npm-audit-test-"));
|
|
10
|
+
const stdoutPath = path.join(dir, "stdout");
|
|
11
|
+
fs.writeFileSync(stdoutPath, stdout);
|
|
12
|
+
return {
|
|
13
|
+
command: "npm audit --json",
|
|
14
|
+
argv: ["npm", "audit", "--json"],
|
|
15
|
+
cwd,
|
|
16
|
+
artifactPaths: [],
|
|
17
|
+
stdoutPath,
|
|
18
|
+
stderrPath: path.join(dir, "stderr"),
|
|
19
|
+
logPath: path.join(dir, "log"),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe("npm-audit-json parser", () => {
|
|
24
|
+
it("vulnerabilities with advisories → extracts package name, severity, advisory titles", async () => {
|
|
25
|
+
const stdout = JSON.stringify({
|
|
26
|
+
auditReportVersion: 2,
|
|
27
|
+
vulnerabilities: {
|
|
28
|
+
lodash: {
|
|
29
|
+
name: "lodash",
|
|
30
|
+
severity: "high",
|
|
31
|
+
isDirect: true,
|
|
32
|
+
via: [
|
|
33
|
+
{ title: "Command Injection in lodash", severity: "high", url: "https://example.com" },
|
|
34
|
+
{ title: "ReDoS in lodash", severity: "moderate", url: "https://example.com" },
|
|
35
|
+
],
|
|
36
|
+
fixAvailable: true,
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
metadata: { vulnerabilities: { info: 0, low: 0, moderate: 0, high: 1, critical: 0, total: 1 } },
|
|
40
|
+
});
|
|
41
|
+
const result = await parser.parse(makeCtx(stdout));
|
|
42
|
+
expect(result.status).toBe("fail");
|
|
43
|
+
expect(result.summary).toBe("1 vulnerability (1 high)");
|
|
44
|
+
expect(result.failures).toHaveLength(1);
|
|
45
|
+
expect(result.failures![0].file).toBe("lodash");
|
|
46
|
+
expect(result.failures![0].rule).toBe("high");
|
|
47
|
+
expect(result.failures![0].message).toContain("Command Injection");
|
|
48
|
+
expect(result.failures![0].message).toContain("ReDoS");
|
|
49
|
+
// No URLs in output
|
|
50
|
+
expect(result.failures![0].message).not.toContain("https://");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("no vulnerabilities → status pass", async () => {
|
|
54
|
+
const stdout = JSON.stringify({
|
|
55
|
+
auditReportVersion: 2,
|
|
56
|
+
vulnerabilities: {},
|
|
57
|
+
metadata: { vulnerabilities: { info: 0, low: 0, moderate: 0, high: 0, critical: 0, total: 0 } },
|
|
58
|
+
});
|
|
59
|
+
const result = await parser.parse(makeCtx(stdout));
|
|
60
|
+
expect(result.status).toBe("pass");
|
|
61
|
+
expect(result.summary).toBe("no vulnerabilities found");
|
|
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("multiple severities → correct summary breakdown", async () => {
|
|
72
|
+
const stdout = JSON.stringify({
|
|
73
|
+
vulnerabilities: {
|
|
74
|
+
"pkg-a": { name: "pkg-a", severity: "critical", isDirect: true, via: [{ title: "RCE" }] },
|
|
75
|
+
"pkg-b": { name: "pkg-b", severity: "low", isDirect: false, via: [{ title: "Info leak" }] },
|
|
76
|
+
},
|
|
77
|
+
metadata: { vulnerabilities: { info: 0, low: 1, moderate: 0, high: 0, critical: 1, total: 2 } },
|
|
78
|
+
});
|
|
79
|
+
const result = await parser.parse(makeCtx(stdout));
|
|
80
|
+
expect(result.summary).toBe("2 vulnerabilities (1 critical, 1 low)");
|
|
81
|
+
expect(result.failures).toHaveLength(2);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { ParserModule, ParsedFailure } from "../types";
|
|
2
|
+
import { safeReadFile } from "./utils";
|
|
3
|
+
|
|
4
|
+
interface NpmAuditVia {
|
|
5
|
+
title?: string;
|
|
6
|
+
severity?: string;
|
|
7
|
+
url?: string;
|
|
8
|
+
name?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface NpmAuditVuln {
|
|
12
|
+
name: string;
|
|
13
|
+
severity: string;
|
|
14
|
+
isDirect: boolean;
|
|
15
|
+
via: Array<NpmAuditVia | string>;
|
|
16
|
+
fixAvailable?: boolean | { name: string; version: string };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface NpmAuditReport {
|
|
20
|
+
vulnerabilities?: Record<string, NpmAuditVuln>;
|
|
21
|
+
metadata?: {
|
|
22
|
+
vulnerabilities?: Record<string, number>;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const parser: ParserModule = {
|
|
27
|
+
id: "npm-audit-json",
|
|
28
|
+
async parse(ctx) {
|
|
29
|
+
const stdout = safeReadFile(ctx.stdoutPath).trim();
|
|
30
|
+
if (!stdout) {
|
|
31
|
+
return {
|
|
32
|
+
tool: "npm audit",
|
|
33
|
+
status: "pass",
|
|
34
|
+
summary: "no vulnerabilities found",
|
|
35
|
+
failures: [],
|
|
36
|
+
logPath: ctx.logPath,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let report: NpmAuditReport;
|
|
41
|
+
try {
|
|
42
|
+
report = JSON.parse(stdout);
|
|
43
|
+
} catch {
|
|
44
|
+
return {
|
|
45
|
+
tool: "npm audit",
|
|
46
|
+
status: "error",
|
|
47
|
+
summary: "failed to parse npm audit JSON output",
|
|
48
|
+
logPath: ctx.logPath,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const vulns = report.vulnerabilities ?? {};
|
|
53
|
+
const failures: ParsedFailure[] = [];
|
|
54
|
+
|
|
55
|
+
for (const [pkg, vuln] of Object.entries(vulns)) {
|
|
56
|
+
// Extract advisory titles from 'via' (skip string refs to other packages)
|
|
57
|
+
const titles = vuln.via.filter((v): v is NpmAuditVia => typeof v !== "string" && !!v.title).map((v) => v.title!);
|
|
58
|
+
const message = titles.length > 0 ? titles.join("; ") : `${vuln.severity} severity vulnerability`;
|
|
59
|
+
failures.push({
|
|
60
|
+
id: pkg,
|
|
61
|
+
file: pkg,
|
|
62
|
+
message,
|
|
63
|
+
rule: vuln.severity,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Build severity summary from metadata
|
|
68
|
+
const meta = report.metadata?.vulnerabilities ?? {};
|
|
69
|
+
const sevParts = ["critical", "high", "moderate", "low", "info"]
|
|
70
|
+
.filter((s) => meta[s])
|
|
71
|
+
.map((s) => `${meta[s]} ${s}`);
|
|
72
|
+
const total = meta.total ?? failures.length;
|
|
73
|
+
const summary =
|
|
74
|
+
total > 0
|
|
75
|
+
? `${total} vulnerabilit${total !== 1 ? "ies" : "y"} (${sevParts.join(", ")})`
|
|
76
|
+
: "no vulnerabilities found";
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
tool: "npm audit",
|
|
80
|
+
status: total > 0 ? "fail" : "pass",
|
|
81
|
+
summary,
|
|
82
|
+
failures,
|
|
83
|
+
logPath: ctx.logPath,
|
|
84
|
+
};
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export default parser;
|