@robhowley/pi-structured-return 1.3.0 → 1.5.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 +79 -52
- package/extensions/structured-return/src/config/registry.test.ts +171 -0
- package/extensions/structured-return/src/config/registry.ts +99 -13
- 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/cargo-build.ts +2 -2
- package/extensions/structured-return/src/parsers/cargo-test.ts +2 -2
- package/extensions/structured-return/src/parsers/dbt-json.test.ts +172 -0
- package/extensions/structured-return/src/parsers/dbt-json.ts +131 -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/junit-xml.ts +2 -2
- 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/pylint-json.test.ts +120 -0
- package/extensions/structured-return/src/parsers/pylint-json.ts +64 -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/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/vitest-json.ts +2 -2
- package/package.json +1 -1
- package/skills/structured-return/SKILL.md +44 -0
|
@@ -0,0 +1,101 @@
|
|
|
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 "./mypy-json";
|
|
6
|
+
import type { RunContext } from "../types";
|
|
7
|
+
|
|
8
|
+
function makeCtx(stderr: string, cwd = "/project"): RunContext {
|
|
9
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "mypy-test-"));
|
|
10
|
+
const stderrPath = path.join(dir, "stderr");
|
|
11
|
+
fs.writeFileSync(stderrPath, stderr);
|
|
12
|
+
// mypy JSON goes to stderr; stdout is empty
|
|
13
|
+
const stdoutPath = path.join(dir, "stdout");
|
|
14
|
+
fs.writeFileSync(stdoutPath, "");
|
|
15
|
+
return {
|
|
16
|
+
command: "mypy type_check.py --output json",
|
|
17
|
+
argv: ["mypy", "type_check.py", "--output", "json"],
|
|
18
|
+
cwd,
|
|
19
|
+
artifactPaths: [],
|
|
20
|
+
stdoutPath,
|
|
21
|
+
stderrPath,
|
|
22
|
+
logPath: path.join(dir, "log"),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
describe("mypy-json parser", () => {
|
|
27
|
+
it("multiple type errors → correct file, line, message, rule, status fail", async () => {
|
|
28
|
+
const stderr = [
|
|
29
|
+
'{"file": "type_check.py", "line": 5, "column": 14, "message": "Incompatible types in assignment (expression has type \\"int\\", variable has type \\"str\\")", "hint": null, "code": "assignment", "severity": "error"}',
|
|
30
|
+
'{"file": "type_check.py", "line": 6, "column": 17, "message": "Argument 1 to \\"add\\" has incompatible type \\"str\\"; expected \\"int\\"", "hint": null, "code": "arg-type", "severity": "error"}',
|
|
31
|
+
].join("\n");
|
|
32
|
+
const result = await parser.parse(makeCtx(stderr));
|
|
33
|
+
expect(result.status).toBe("fail");
|
|
34
|
+
expect(result.summary).toBe("2 type errors");
|
|
35
|
+
expect(result.failures).toHaveLength(2);
|
|
36
|
+
expect(result.failures![0]).toMatchObject({
|
|
37
|
+
file: "type_check.py",
|
|
38
|
+
line: 5,
|
|
39
|
+
message: 'Incompatible types in assignment (expression has type "int", variable has type "str")',
|
|
40
|
+
rule: "assignment",
|
|
41
|
+
});
|
|
42
|
+
expect(result.failures![1]).toMatchObject({
|
|
43
|
+
file: "type_check.py",
|
|
44
|
+
line: 6,
|
|
45
|
+
message: 'Argument 1 to "add" has incompatible type "str"; expected "int"',
|
|
46
|
+
rule: "arg-type",
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("no errors (empty stderr) → status pass", async () => {
|
|
51
|
+
const result = await parser.parse(makeCtx(""));
|
|
52
|
+
expect(result.status).toBe("pass");
|
|
53
|
+
expect(result.summary).toBe("no type errors");
|
|
54
|
+
expect(result.failures).toHaveLength(0);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("absolute file paths → relativized to cwd", async () => {
|
|
58
|
+
const stderr =
|
|
59
|
+
'{"file": "/project/src/foo.py", "line": 10, "column": 5, "message": "Need type annotation", "hint": null, "code": "var-annotated", "severity": "error"}';
|
|
60
|
+
const result = await parser.parse(makeCtx(stderr, "/project"));
|
|
61
|
+
expect(result.failures![0].file).toBe("src/foo.py");
|
|
62
|
+
expect(result.failures![0].file).not.toContain("/project");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("note severity items are skipped as standalone failures", async () => {
|
|
66
|
+
const stderr = [
|
|
67
|
+
'{"file": "foo.py", "line": 5, "column": 1, "message": "Missing return statement", "hint": null, "code": "return", "severity": "error"}',
|
|
68
|
+
'{"file": "foo.py", "line": 5, "column": 1, "message": "See https://mypy.readthedocs.io/...", "hint": null, "code": null, "severity": "note"}',
|
|
69
|
+
].join("\n");
|
|
70
|
+
const result = await parser.parse(makeCtx(stderr));
|
|
71
|
+
expect(result.failures).toHaveLength(1);
|
|
72
|
+
expect(result.failures![0].rule).toBe("return");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("notes on the same line as an error are appended to the error message", async () => {
|
|
76
|
+
const stderr = [
|
|
77
|
+
'{"file": "foo.py", "line": 5, "column": 1, "message": "Missing return statement", "hint": null, "code": "return", "severity": "error"}',
|
|
78
|
+
'{"file": "foo.py", "line": 5, "column": 1, "message": "Did you forget to return a value?", "hint": null, "code": null, "severity": "note"}',
|
|
79
|
+
].join("\n");
|
|
80
|
+
const result = await parser.parse(makeCtx(stderr));
|
|
81
|
+
expect(result.failures).toHaveLength(1);
|
|
82
|
+
expect(result.failures![0].message).toBe("Missing return statement (Did you forget to return a value?)");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("hint field is included in message when present", async () => {
|
|
86
|
+
const stderr =
|
|
87
|
+
'{"file": "foo.py", "line": 3, "column": 1, "message": "Cannot find module", "hint": "Did you install types-requests?", "code": "import", "severity": "error"}';
|
|
88
|
+
const result = await parser.parse(makeCtx(stderr));
|
|
89
|
+
expect(result.failures![0].message).toBe("Cannot find module (Did you install types-requests?)");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("handles non-JSON lines gracefully (e.g. summary text mixed in)", async () => {
|
|
93
|
+
const stderr = [
|
|
94
|
+
'{"file": "foo.py", "line": 1, "column": 1, "message": "Unused import", "hint": null, "code": "unused-import", "severity": "error"}',
|
|
95
|
+
"Found 1 error in 1 file (checked 1 source file)",
|
|
96
|
+
].join("\n");
|
|
97
|
+
const result = await parser.parse(makeCtx(stderr));
|
|
98
|
+
expect(result.failures).toHaveLength(1);
|
|
99
|
+
expect(result.failures![0].rule).toBe("unused-import");
|
|
100
|
+
});
|
|
101
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type { ParserModule, ParsedFailure } from "../types";
|
|
3
|
+
import { safeReadFile } from "./utils";
|
|
4
|
+
|
|
5
|
+
interface MypyItem {
|
|
6
|
+
file: string;
|
|
7
|
+
line: number;
|
|
8
|
+
column: number;
|
|
9
|
+
message: string;
|
|
10
|
+
hint: string | null;
|
|
11
|
+
code: string | null;
|
|
12
|
+
severity: "error" | "note";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const parser: ParserModule = {
|
|
16
|
+
id: "mypy-json",
|
|
17
|
+
async parse(ctx) {
|
|
18
|
+
// mypy --output json writes JSON diagnostics to stderr (stdout gets the human summary).
|
|
19
|
+
const stderr = safeReadFile(ctx.stderrPath).trim();
|
|
20
|
+
if (!stderr) {
|
|
21
|
+
return {
|
|
22
|
+
tool: "mypy",
|
|
23
|
+
status: "pass",
|
|
24
|
+
summary: "no type errors",
|
|
25
|
+
failures: [],
|
|
26
|
+
logPath: ctx.logPath,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const items: MypyItem[] = [];
|
|
31
|
+
for (const line of stderr.split("\n")) {
|
|
32
|
+
const trimmed = line.trim();
|
|
33
|
+
if (!trimmed) continue;
|
|
34
|
+
try {
|
|
35
|
+
items.push(JSON.parse(trimmed) as MypyItem);
|
|
36
|
+
} catch {
|
|
37
|
+
// skip non-JSON lines (e.g. summary text on stderr)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const errors = items.filter((item) => item.severity === "error");
|
|
42
|
+
// Build a map of notes keyed by normalized relative path:line so lookups
|
|
43
|
+
// match regardless of whether mypy emits absolute or relative paths.
|
|
44
|
+
const relPathOf = (f: string) => (path.isAbsolute(f) ? path.relative(ctx.cwd, f) : f);
|
|
45
|
+
const notesByLocation = new Map<string, string[]>();
|
|
46
|
+
for (const item of items) {
|
|
47
|
+
if (item.severity === "note") {
|
|
48
|
+
const key = `${relPathOf(item.file)}:${item.line}`;
|
|
49
|
+
const notes = notesByLocation.get(key) ?? [];
|
|
50
|
+
notes.push(item.message);
|
|
51
|
+
notesByLocation.set(key, notes);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const failures: ParsedFailure[] = errors.map((item) => {
|
|
56
|
+
const relPath = relPathOf(item.file);
|
|
57
|
+
const key = `${relPath}:${item.line}`;
|
|
58
|
+
const notes = notesByLocation.get(key);
|
|
59
|
+
const hint = item.hint ?? (notes ? notes.join("; ") : undefined);
|
|
60
|
+
const message = hint ? `${item.message} (${hint})` : item.message;
|
|
61
|
+
return {
|
|
62
|
+
id: `${relPath}:${item.line}:${item.code ?? "error"}`,
|
|
63
|
+
file: relPath,
|
|
64
|
+
line: item.line,
|
|
65
|
+
message,
|
|
66
|
+
rule: item.code ?? undefined,
|
|
67
|
+
};
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
tool: "mypy",
|
|
72
|
+
status: failures.length > 0 ? "fail" : "pass",
|
|
73
|
+
summary: failures.length > 0 ? `${failures.length} type errors` : "no type errors",
|
|
74
|
+
failures,
|
|
75
|
+
logPath: ctx.logPath,
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export default parser;
|
|
@@ -0,0 +1,120 @@
|
|
|
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 "./pylint-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(), "pylint-test-"));
|
|
10
|
+
const stdoutPath = path.join(dir, "stdout");
|
|
11
|
+
fs.writeFileSync(stdoutPath, stdout);
|
|
12
|
+
return {
|
|
13
|
+
command: "pylint lint_check.py --output-format=json",
|
|
14
|
+
argv: ["pylint", "lint_check.py", "--output-format=json"],
|
|
15
|
+
cwd,
|
|
16
|
+
artifactPaths: [],
|
|
17
|
+
stdoutPath,
|
|
18
|
+
stderrPath: path.join(dir, "stderr"),
|
|
19
|
+
logPath: path.join(dir, "log"),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe("pylint-json parser", () => {
|
|
24
|
+
it("multiple lint errors → correct file, line, message, rule, status fail", async () => {
|
|
25
|
+
const stdout = JSON.stringify([
|
|
26
|
+
{
|
|
27
|
+
type: "warning",
|
|
28
|
+
module: "lint_check",
|
|
29
|
+
obj: "process_order",
|
|
30
|
+
line: 5,
|
|
31
|
+
column: 4,
|
|
32
|
+
endLine: 5,
|
|
33
|
+
endColumn: 12,
|
|
34
|
+
path: "lint_check.py",
|
|
35
|
+
symbol: "unused-variable",
|
|
36
|
+
message: "Unused variable 'discount'",
|
|
37
|
+
"message-id": "W0612",
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
type: "warning",
|
|
41
|
+
module: "lint_check",
|
|
42
|
+
obj: "",
|
|
43
|
+
line: 1,
|
|
44
|
+
column: 0,
|
|
45
|
+
endLine: 1,
|
|
46
|
+
endColumn: 9,
|
|
47
|
+
path: "lint_check.py",
|
|
48
|
+
symbol: "unused-import",
|
|
49
|
+
message: "Unused import os",
|
|
50
|
+
"message-id": "W0611",
|
|
51
|
+
},
|
|
52
|
+
]);
|
|
53
|
+
const result = await parser.parse(makeCtx(stdout));
|
|
54
|
+
expect(result.status).toBe("fail");
|
|
55
|
+
expect(result.summary).toBe("2 lint errors");
|
|
56
|
+
expect(result.failures).toHaveLength(2);
|
|
57
|
+
expect(result.failures![0]).toMatchObject({
|
|
58
|
+
file: "lint_check.py",
|
|
59
|
+
line: 5,
|
|
60
|
+
message: "Unused variable 'discount'",
|
|
61
|
+
rule: "W0612(unused-variable)",
|
|
62
|
+
});
|
|
63
|
+
expect(result.failures![1]).toMatchObject({
|
|
64
|
+
file: "lint_check.py",
|
|
65
|
+
line: 1,
|
|
66
|
+
message: "Unused import os",
|
|
67
|
+
rule: "W0611(unused-import)",
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("no errors (empty array) → status pass", async () => {
|
|
72
|
+
const result = await parser.parse(makeCtx("[]"));
|
|
73
|
+
expect(result.status).toBe("pass");
|
|
74
|
+
expect(result.summary).toBe("no lint errors");
|
|
75
|
+
expect(result.failures).toHaveLength(0);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("empty stdout → status pass", async () => {
|
|
79
|
+
const result = await parser.parse(makeCtx(""));
|
|
80
|
+
expect(result.status).toBe("pass");
|
|
81
|
+
expect(result.failures).toHaveLength(0);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("absolute file paths → relativized to cwd", async () => {
|
|
85
|
+
const stdout = JSON.stringify([
|
|
86
|
+
{
|
|
87
|
+
type: "convention",
|
|
88
|
+
module: "foo",
|
|
89
|
+
obj: "",
|
|
90
|
+
line: 1,
|
|
91
|
+
column: 0,
|
|
92
|
+
path: "/project/src/foo.py",
|
|
93
|
+
symbol: "missing-module-docstring",
|
|
94
|
+
message: "Missing module docstring",
|
|
95
|
+
"message-id": "C0114",
|
|
96
|
+
},
|
|
97
|
+
]);
|
|
98
|
+
const result = await parser.parse(makeCtx(stdout, "/project"));
|
|
99
|
+
expect(result.failures![0].file).toBe("src/foo.py");
|
|
100
|
+
expect(result.failures![0].file).not.toContain("/project");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("rule includes both message-id and symbol", async () => {
|
|
104
|
+
const stdout = JSON.stringify([
|
|
105
|
+
{
|
|
106
|
+
type: "convention",
|
|
107
|
+
module: "foo",
|
|
108
|
+
obj: "",
|
|
109
|
+
line: 1,
|
|
110
|
+
column: 0,
|
|
111
|
+
path: "foo.py",
|
|
112
|
+
symbol: "line-too-long",
|
|
113
|
+
message: "Line too long (120/100)",
|
|
114
|
+
"message-id": "C0301",
|
|
115
|
+
},
|
|
116
|
+
]);
|
|
117
|
+
const result = await parser.parse(makeCtx(stdout));
|
|
118
|
+
expect(result.failures![0].rule).toBe("C0301(line-too-long)");
|
|
119
|
+
});
|
|
120
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type { ParserModule, ParsedFailure } from "../types";
|
|
3
|
+
import { safeReadFile } from "./utils";
|
|
4
|
+
|
|
5
|
+
interface PylintItem {
|
|
6
|
+
type: string;
|
|
7
|
+
module: string;
|
|
8
|
+
obj: string;
|
|
9
|
+
line: number;
|
|
10
|
+
column: number;
|
|
11
|
+
path: string;
|
|
12
|
+
symbol: string;
|
|
13
|
+
message: string;
|
|
14
|
+
"message-id": string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const parser: ParserModule = {
|
|
18
|
+
id: "pylint-json",
|
|
19
|
+
async parse(ctx) {
|
|
20
|
+
const stdout = safeReadFile(ctx.stdoutPath).trim();
|
|
21
|
+
if (!stdout || stdout === "[]") {
|
|
22
|
+
return {
|
|
23
|
+
tool: "pylint",
|
|
24
|
+
status: "pass",
|
|
25
|
+
summary: "no lint errors",
|
|
26
|
+
failures: [],
|
|
27
|
+
logPath: ctx.logPath,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let items: PylintItem[];
|
|
32
|
+
try {
|
|
33
|
+
items = JSON.parse(stdout) as PylintItem[];
|
|
34
|
+
} catch {
|
|
35
|
+
return {
|
|
36
|
+
tool: "pylint",
|
|
37
|
+
status: "error",
|
|
38
|
+
summary: "failed to parse pylint JSON output",
|
|
39
|
+
logPath: ctx.logPath,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const failures: ParsedFailure[] = items.map((item) => {
|
|
44
|
+
const relPath = path.isAbsolute(item.path) ? path.relative(ctx.cwd, item.path) : item.path;
|
|
45
|
+
return {
|
|
46
|
+
id: `${relPath}:${item.line}:${item["message-id"]}`,
|
|
47
|
+
file: relPath,
|
|
48
|
+
line: item.line,
|
|
49
|
+
message: item.message,
|
|
50
|
+
rule: `${item["message-id"]}(${item.symbol})`,
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
tool: "pylint",
|
|
56
|
+
status: failures.length > 0 ? "fail" : "pass",
|
|
57
|
+
summary: failures.length > 0 ? `${failures.length} lint errors` : "no lint errors",
|
|
58
|
+
failures,
|
|
59
|
+
logPath: ctx.logPath,
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export default parser;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
1
|
import path from "node:path";
|
|
3
2
|
import type { ParserModule } from "../types";
|
|
3
|
+
import { safeReadFile } from "./utils";
|
|
4
4
|
|
|
5
5
|
interface RSpecException {
|
|
6
6
|
class: string;
|
|
@@ -40,7 +40,7 @@ function firstParagraph(message: string): string {
|
|
|
40
40
|
const parser: ParserModule = {
|
|
41
41
|
id: "rspec-json",
|
|
42
42
|
async parse(ctx) {
|
|
43
|
-
const stdout =
|
|
43
|
+
const stdout = safeReadFile(ctx.stdoutPath).trim();
|
|
44
44
|
const report = stdout ? (JSON.parse(stdout) as RSpecReport) : null;
|
|
45
45
|
if (!report) {
|
|
46
46
|
return {
|
|
@@ -0,0 +1,118 @@
|
|
|
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 "./rubocop-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(), "rubocop-test-"));
|
|
10
|
+
const stdoutPath = path.join(dir, "stdout");
|
|
11
|
+
fs.writeFileSync(stdoutPath, stdout);
|
|
12
|
+
return {
|
|
13
|
+
command: "rubocop lint_check.rb --format json",
|
|
14
|
+
argv: ["rubocop", "lint_check.rb", "--format", "json"],
|
|
15
|
+
cwd,
|
|
16
|
+
artifactPaths: [],
|
|
17
|
+
stdoutPath,
|
|
18
|
+
stderrPath: path.join(dir, "stderr"),
|
|
19
|
+
logPath: path.join(dir, "log"),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe("rubocop-json parser", () => {
|
|
24
|
+
it("multiple offenses → correct file, line, message, rule, status fail", async () => {
|
|
25
|
+
const stdout = JSON.stringify({
|
|
26
|
+
metadata: {},
|
|
27
|
+
files: [
|
|
28
|
+
{
|
|
29
|
+
path: "lint_check.rb",
|
|
30
|
+
offenses: [
|
|
31
|
+
{
|
|
32
|
+
severity: "warning",
|
|
33
|
+
message: "Lint/UselessAssignment: Useless assignment to variable - `discount`.",
|
|
34
|
+
cop_name: "Lint/UselessAssignment",
|
|
35
|
+
corrected: false,
|
|
36
|
+
location: { start_line: 2, start_column: 3, line: 2, column: 3 },
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
severity: "convention",
|
|
40
|
+
message:
|
|
41
|
+
"Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.",
|
|
42
|
+
cop_name: "Style/StringLiterals",
|
|
43
|
+
corrected: false,
|
|
44
|
+
location: { start_line: 6, start_column: 20, line: 6, column: 20 },
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
summary: { offense_count: 2 },
|
|
50
|
+
});
|
|
51
|
+
const result = await parser.parse(makeCtx(stdout));
|
|
52
|
+
expect(result.status).toBe("fail");
|
|
53
|
+
expect(result.summary).toBe("2 lint errors");
|
|
54
|
+
expect(result.failures).toHaveLength(2);
|
|
55
|
+
expect(result.failures![0]).toMatchObject({
|
|
56
|
+
file: "lint_check.rb",
|
|
57
|
+
line: 2,
|
|
58
|
+
message: "Useless assignment to variable - `discount`.",
|
|
59
|
+
rule: "Lint/UselessAssignment",
|
|
60
|
+
});
|
|
61
|
+
expect(result.failures![1].rule).toBe("Style/StringLiterals");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("no offenses → status pass", async () => {
|
|
65
|
+
const stdout = JSON.stringify({
|
|
66
|
+
metadata: {},
|
|
67
|
+
files: [{ path: "clean.rb", offenses: [] }],
|
|
68
|
+
summary: { offense_count: 0 },
|
|
69
|
+
});
|
|
70
|
+
const result = await parser.parse(makeCtx(stdout));
|
|
71
|
+
expect(result.status).toBe("pass");
|
|
72
|
+
expect(result.failures).toHaveLength(0);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("empty stdout → status pass", async () => {
|
|
76
|
+
const result = await parser.parse(makeCtx(""));
|
|
77
|
+
expect(result.status).toBe("pass");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("absolute file paths → relativized to cwd", async () => {
|
|
81
|
+
const stdout = JSON.stringify({
|
|
82
|
+
metadata: {},
|
|
83
|
+
files: [
|
|
84
|
+
{
|
|
85
|
+
path: "/project/src/foo.rb",
|
|
86
|
+
offenses: [
|
|
87
|
+
{ severity: "warning", message: "Lint/Foo: bar", cop_name: "Lint/Foo", location: { line: 1, column: 1 } },
|
|
88
|
+
],
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
summary: { offense_count: 1 },
|
|
92
|
+
});
|
|
93
|
+
const result = await parser.parse(makeCtx(stdout, "/project"));
|
|
94
|
+
expect(result.failures![0].file).toBe("src/foo.rb");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("strips cop_name prefix from message", async () => {
|
|
98
|
+
const stdout = JSON.stringify({
|
|
99
|
+
metadata: {},
|
|
100
|
+
files: [
|
|
101
|
+
{
|
|
102
|
+
path: "foo.rb",
|
|
103
|
+
offenses: [
|
|
104
|
+
{
|
|
105
|
+
severity: "convention",
|
|
106
|
+
message: "Style/FrozenStringLiteralComment: Missing frozen string literal comment.",
|
|
107
|
+
cop_name: "Style/FrozenStringLiteralComment",
|
|
108
|
+
location: { line: 1, column: 1 },
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
},
|
|
112
|
+
],
|
|
113
|
+
summary: { offense_count: 1 },
|
|
114
|
+
});
|
|
115
|
+
const result = await parser.parse(makeCtx(stdout));
|
|
116
|
+
expect(result.failures![0].message).toBe("Missing frozen string literal comment.");
|
|
117
|
+
});
|
|
118
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type { ParserModule, ParsedFailure } from "../types";
|
|
3
|
+
import { safeReadFile } from "./utils";
|
|
4
|
+
|
|
5
|
+
interface RubocopOffense {
|
|
6
|
+
severity: string;
|
|
7
|
+
message: string;
|
|
8
|
+
cop_name: string;
|
|
9
|
+
location: { line: number; column: number };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface RubocopFile {
|
|
13
|
+
path: string;
|
|
14
|
+
offenses: RubocopOffense[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface RubocopOutput {
|
|
18
|
+
files: RubocopFile[];
|
|
19
|
+
summary: { offense_count: number };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const parser: ParserModule = {
|
|
23
|
+
id: "rubocop-json",
|
|
24
|
+
async parse(ctx) {
|
|
25
|
+
const stdout = safeReadFile(ctx.stdoutPath).trim();
|
|
26
|
+
if (!stdout) {
|
|
27
|
+
return { tool: "rubocop", status: "pass", summary: "no lint errors", failures: [], logPath: ctx.logPath };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let data: RubocopOutput;
|
|
31
|
+
try {
|
|
32
|
+
data = JSON.parse(stdout) as RubocopOutput;
|
|
33
|
+
} catch {
|
|
34
|
+
return { tool: "rubocop", status: "error", summary: "failed to parse rubocop JSON output", logPath: ctx.logPath };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const failures: ParsedFailure[] = [];
|
|
38
|
+
for (const file of data.files) {
|
|
39
|
+
const relPath = path.isAbsolute(file.path) ? path.relative(ctx.cwd, file.path) : file.path;
|
|
40
|
+
for (const o of file.offenses) {
|
|
41
|
+
// Strip the "CopName: " prefix from the message if present.
|
|
42
|
+
// Escape special regex chars in cop_name (e.g. "." in Style/Foo.Bar).
|
|
43
|
+
const escapedCop = o.cop_name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
44
|
+
const msg = o.message.replace(new RegExp(`^${escapedCop}:\\s*`), "");
|
|
45
|
+
failures.push({
|
|
46
|
+
id: `${relPath}:${o.location.line}:${o.cop_name}`,
|
|
47
|
+
file: relPath,
|
|
48
|
+
line: o.location.line,
|
|
49
|
+
message: msg,
|
|
50
|
+
rule: o.cop_name,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
tool: "rubocop",
|
|
57
|
+
status: failures.length > 0 ? "fail" : "pass",
|
|
58
|
+
summary: failures.length > 0 ? `${failures.length} lint errors` : "no lint errors",
|
|
59
|
+
failures,
|
|
60
|
+
logPath: ctx.logPath,
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export default parser;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
1
|
import path from "node:path";
|
|
3
2
|
import type { ParserModule } from "../types";
|
|
3
|
+
import { safeReadFile } from "./utils";
|
|
4
4
|
|
|
5
5
|
interface RuffItem {
|
|
6
6
|
filename: string;
|
|
@@ -12,7 +12,7 @@ interface RuffItem {
|
|
|
12
12
|
const parser: ParserModule = {
|
|
13
13
|
id: "ruff-json",
|
|
14
14
|
async parse(ctx) {
|
|
15
|
-
const stdout =
|
|
15
|
+
const stdout = safeReadFile(ctx.stdoutPath).trim();
|
|
16
16
|
const items = stdout ? (JSON.parse(stdout) as RuffItem[]) : [];
|
|
17
17
|
const failures = (Array.isArray(items) ? items : []).map((item) => {
|
|
18
18
|
const relPath = path.relative(ctx.cwd, item.filename);
|