@robhowley/pi-structured-return 1.7.2 → 1.7.3
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 +0 -1
- package/extensions/structured-return/src/index.test.ts +56 -1
- package/extensions/structured-return/src/index.ts +11 -1
- package/extensions/structured-return/src/parsers/junit-xml.test.ts +87 -0
- package/extensions/structured-return/src/parsers/junit-xml.ts +57 -54
- package/extensions/structured-return/src/types.ts +0 -1
- package/package.json +1 -1
- package/skills/structured-return/SKILL.md +16 -6
package/README.md
CHANGED
|
@@ -241,6 +241,5 @@ Every parser returns the same shape. The model always knows where to look.
|
|
|
241
241
|
| `summary` | `string` | One-line human+model readable result (`3 failed, 12 passed`) |
|
|
242
242
|
| `cwd` | `string` | Working directory — anchor for resolving relative paths in failures |
|
|
243
243
|
| `failures` | `{ id, file?, line?, message?, rule? }[]` | Per-failure details with relative file paths |
|
|
244
|
-
| `artifact` | `string?` | Path to the saved report file, if one was written |
|
|
245
244
|
| `logPath` | `string` | Path to full stdout+stderr log |
|
|
246
245
|
| `rawTail` | `string?` | Last 200 lines of log, included on fallback when no parser matched |
|
|
@@ -2,7 +2,7 @@ import fs from "node:fs";
|
|
|
2
2
|
import os from "node:os";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { describe, it, expect } from "vitest";
|
|
5
|
-
import { stripCdPrefix, formatResult, finalizeResult } from "./index";
|
|
5
|
+
import { stripCdPrefix, formatResult, finalizeResult, expandArtifactPaths } from "./index";
|
|
6
6
|
|
|
7
7
|
describe("stripCdPrefix", () => {
|
|
8
8
|
it("strips cd /path && prefix", () => {
|
|
@@ -123,6 +123,61 @@ describe("finalizeResult", () => {
|
|
|
123
123
|
});
|
|
124
124
|
});
|
|
125
125
|
|
|
126
|
+
describe("expandArtifactPaths", () => {
|
|
127
|
+
it("expands glob patterns to matching files (sorted)", () => {
|
|
128
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "expand-glob-"));
|
|
129
|
+
fs.writeFileSync(path.join(dir, "TEST-Bar.xml"), "<bar/>");
|
|
130
|
+
fs.writeFileSync(path.join(dir, "TEST-Foo.xml"), "<foo/>");
|
|
131
|
+
fs.writeFileSync(path.join(dir, "other.txt"), "ignore");
|
|
132
|
+
|
|
133
|
+
const result = expandArtifactPaths(["TEST-*.xml"], dir);
|
|
134
|
+
expect(result).toEqual([path.join(dir, "TEST-Bar.xml"), path.join(dir, "TEST-Foo.xml")]);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("keeps unmatched patterns as-is (parser handles missing files)", () => {
|
|
138
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "expand-nomatch-"));
|
|
139
|
+
const result = expandArtifactPaths(["reports/TEST-*.xml"], dir);
|
|
140
|
+
expect(result).toEqual([path.join(dir, "reports/TEST-*.xml")]);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("resolves relative paths against cwd", () => {
|
|
144
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "expand-rel-"));
|
|
145
|
+
const sub = path.join(dir, "build");
|
|
146
|
+
fs.mkdirSync(sub);
|
|
147
|
+
fs.writeFileSync(path.join(sub, "report.xml"), "<ok/>");
|
|
148
|
+
|
|
149
|
+
const result = expandArtifactPaths(["build/report.xml"], dir);
|
|
150
|
+
expect(result).toEqual([path.join(dir, "build/report.xml")]);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("passes through absolute paths unchanged", () => {
|
|
154
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "expand-abs-"));
|
|
155
|
+
fs.writeFileSync(path.join(dir, "report.xml"), "<ok/>");
|
|
156
|
+
|
|
157
|
+
const abs = path.join(dir, "report.xml");
|
|
158
|
+
const result = expandArtifactPaths([abs], "/some/other/cwd");
|
|
159
|
+
expect(result).toEqual([abs]);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("handles multiple patterns, some with globs and some without", () => {
|
|
163
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "expand-multi-"));
|
|
164
|
+
fs.writeFileSync(path.join(dir, "TEST-A.xml"), "a");
|
|
165
|
+
fs.writeFileSync(path.join(dir, "TEST-B.xml"), "b");
|
|
166
|
+
fs.writeFileSync(path.join(dir, "coverage.xml"), "c");
|
|
167
|
+
|
|
168
|
+
const result = expandArtifactPaths(["TEST-*.xml", "coverage.xml"], dir);
|
|
169
|
+
expect(result).toEqual([
|
|
170
|
+
path.join(dir, "TEST-A.xml"),
|
|
171
|
+
path.join(dir, "TEST-B.xml"),
|
|
172
|
+
path.join(dir, "coverage.xml"),
|
|
173
|
+
]);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("returns empty array for empty input", () => {
|
|
177
|
+
expect(expandArtifactPaths([], "/any")).toEqual([]);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
126
181
|
describe("formatResult safety net", () => {
|
|
127
182
|
it("surfaces log path and rawTail when failures detected but no details parsed", () => {
|
|
128
183
|
const result = formatResult({
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
+
import fs from "node:fs";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
import { randomUUID } from "node:crypto";
|
|
4
5
|
import { Type } from "@sinclair/typebox";
|
|
@@ -104,7 +105,7 @@ export default function structuredReturn(pi: ExtensionAPI) {
|
|
|
104
105
|
const argv = shellSplit(args.command);
|
|
105
106
|
const { stdout, stderr, exitCode } = await runCommand(args.command, cwd);
|
|
106
107
|
const logs = writeRunArtifacts(runDir, runId, stdout, stderr);
|
|
107
|
-
const artifactPaths = (args.artifactPaths ?? []
|
|
108
|
+
const artifactPaths = expandArtifactPaths(args.artifactPaths ?? [], cwd);
|
|
108
109
|
const runCtx: RunContext = {
|
|
109
110
|
command: args.command,
|
|
110
111
|
argv,
|
|
@@ -168,6 +169,15 @@ export function formatResult(result: ParsedResult): string {
|
|
|
168
169
|
return lines.join("\n");
|
|
169
170
|
}
|
|
170
171
|
|
|
172
|
+
/** Resolve and glob-expand artifact paths. Patterns that match nothing are kept as-is (parser handles missing files). */
|
|
173
|
+
export function expandArtifactPaths(raw: string[], cwd: string): string[] {
|
|
174
|
+
return raw.flatMap((p) => {
|
|
175
|
+
const resolved = path.isAbsolute(p) ? p : path.join(cwd, p);
|
|
176
|
+
const expanded = fs.globSync(resolved);
|
|
177
|
+
return expanded.length > 0 ? expanded.sort() : [resolved];
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
171
181
|
export function stripCdPrefix(command: string): string {
|
|
172
182
|
return command.replace(/^cd\s+\S+\s*&&\s*/, "");
|
|
173
183
|
}
|
|
@@ -20,6 +20,25 @@ function makeCtx(xml: string, cwd = "/project"): RunContext {
|
|
|
20
20
|
};
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
function makeMultiCtx(xmlFiles: Record<string, string>, cwd = "/project"): RunContext {
|
|
24
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "junit-multi-"));
|
|
25
|
+
const artifactPaths: string[] = [];
|
|
26
|
+
for (const [name, xml] of Object.entries(xmlFiles)) {
|
|
27
|
+
const p = path.join(dir, name);
|
|
28
|
+
fs.writeFileSync(p, xml);
|
|
29
|
+
artifactPaths.push(p);
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
command: "gradle test",
|
|
33
|
+
argv: ["gradle", "test"],
|
|
34
|
+
cwd,
|
|
35
|
+
artifactPaths,
|
|
36
|
+
stdoutPath: path.join(dir, "stdout"),
|
|
37
|
+
stderrPath: path.join(dir, "stderr"),
|
|
38
|
+
logPath: path.join(dir, "log"),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
23
42
|
const PASSING = (name: string, classname = "com.example.MyTest") =>
|
|
24
43
|
`<testcase name="${name}" classname="${classname}" time="0.001"/>`;
|
|
25
44
|
|
|
@@ -320,4 +339,72 @@ Received: 12
|
|
|
320
339
|
expect(result.failures).toHaveLength(2);
|
|
321
340
|
});
|
|
322
341
|
});
|
|
342
|
+
|
|
343
|
+
describe("multi-file artifact paths (Gradle-style)", () => {
|
|
344
|
+
it("aggregates results across multiple XML files", async () => {
|
|
345
|
+
const ctx = makeMultiCtx({
|
|
346
|
+
"TEST-com.example.FooTest.xml": `<testsuite name="com.example.FooTest" tests="2" failures="1" errors="0">
|
|
347
|
+
${PASSING("test_a")}
|
|
348
|
+
${FAILING("test_b", "com.example.FooTest", "expected 2 but was 1")}
|
|
349
|
+
</testsuite>`,
|
|
350
|
+
"TEST-com.example.BarTest.xml": `<testsuite name="com.example.BarTest" tests="3" failures="0" errors="0">
|
|
351
|
+
${PASSING("test_c")}
|
|
352
|
+
${PASSING("test_d")}
|
|
353
|
+
${PASSING("test_e")}
|
|
354
|
+
</testsuite>`,
|
|
355
|
+
});
|
|
356
|
+
const result = await parser.parse(ctx);
|
|
357
|
+
expect(result.status).toBe("fail");
|
|
358
|
+
expect(result.summary).toBe("1 failed, 4 passed");
|
|
359
|
+
expect(result.failures).toHaveLength(1);
|
|
360
|
+
expect(result.failures![0].message).toBe("expected 2 but was 1");
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it("all passing across multiple files → status pass", async () => {
|
|
364
|
+
const ctx = makeMultiCtx({
|
|
365
|
+
"TEST-FooTest.xml": `<testsuite name="FooTest" tests="2" failures="0" errors="0">
|
|
366
|
+
${PASSING("a")}
|
|
367
|
+
${PASSING("b")}
|
|
368
|
+
</testsuite>`,
|
|
369
|
+
"TEST-BarTest.xml": `<testsuite name="BarTest" tests="1" failures="0" errors="0">
|
|
370
|
+
${PASSING("c")}
|
|
371
|
+
</testsuite>`,
|
|
372
|
+
});
|
|
373
|
+
const result = await parser.parse(ctx);
|
|
374
|
+
expect(result.status).toBe("pass");
|
|
375
|
+
expect(result.summary).toBe("3 passed");
|
|
376
|
+
expect(result.failures).toHaveLength(0);
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
it("failures across multiple files are all collected", async () => {
|
|
380
|
+
const ctx = makeMultiCtx({
|
|
381
|
+
"TEST-FooTest.xml": `<testsuite name="FooTest" tests="2" failures="1" errors="0">
|
|
382
|
+
${PASSING("a")}
|
|
383
|
+
${FAILING("b", "FooTest", "foo broke")}
|
|
384
|
+
</testsuite>`,
|
|
385
|
+
"TEST-BarTest.xml": `<testsuite name="BarTest" tests="2" failures="0" errors="1">
|
|
386
|
+
${PASSING("c")}
|
|
387
|
+
${ERROR("d", "BarTest", "bar exploded")}
|
|
388
|
+
</testsuite>`,
|
|
389
|
+
});
|
|
390
|
+
const result = await parser.parse(ctx);
|
|
391
|
+
expect(result.status).toBe("fail");
|
|
392
|
+
expect(result.summary).toBe("2 failed, 2 passed");
|
|
393
|
+
expect(result.failures).toHaveLength(2);
|
|
394
|
+
expect(result.failures![0].message).toBe("foo broke");
|
|
395
|
+
expect(result.failures![1].message).toBe("bar exploded");
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it("skips empty artifact files gracefully", async () => {
|
|
399
|
+
const ctx = makeMultiCtx({
|
|
400
|
+
"TEST-FooTest.xml": `<testsuite name="FooTest" tests="1" failures="0" errors="0">
|
|
401
|
+
${PASSING("a")}
|
|
402
|
+
</testsuite>`,
|
|
403
|
+
"TEST-Empty.xml": "",
|
|
404
|
+
});
|
|
405
|
+
const result = await parser.parse(ctx);
|
|
406
|
+
expect(result.status).toBe("pass");
|
|
407
|
+
expect(result.summary).toBe("1 passed");
|
|
408
|
+
});
|
|
409
|
+
});
|
|
323
410
|
});
|
|
@@ -147,64 +147,68 @@ function resolveFile(tc: JUnitTestCase, suite: JUnitTestSuite, cwd: string): str
|
|
|
147
147
|
const parser: ParserModule = {
|
|
148
148
|
id: "junit-xml",
|
|
149
149
|
async parse(ctx) {
|
|
150
|
-
const
|
|
151
|
-
const xml = safeReadFile(artifactPath);
|
|
152
|
-
const doc = xmlParser.parse(xml) as JUnitDocument;
|
|
153
|
-
|
|
154
|
-
const suites: JUnitTestSuite[] = doc.testsuites?.testsuite ?? doc.testsuite ?? [];
|
|
150
|
+
const artifactSources = ctx.artifactPaths.length > 0 ? ctx.artifactPaths : [ctx.stdoutPath];
|
|
155
151
|
|
|
156
152
|
let totalTests = 0;
|
|
157
153
|
let totalFailed = 0;
|
|
158
154
|
const failures: ParsedFailure[] = [];
|
|
159
155
|
|
|
160
|
-
for (const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
(
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
!
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
if (
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
156
|
+
for (const artifactPath of artifactSources) {
|
|
157
|
+
const xml = safeReadFile(artifactPath);
|
|
158
|
+
if (!xml.trim()) continue;
|
|
159
|
+
const doc = xmlParser.parse(xml) as JUnitDocument;
|
|
160
|
+
|
|
161
|
+
const suites: JUnitTestSuite[] = doc.testsuites?.testsuite ?? doc.testsuite ?? [];
|
|
162
|
+
|
|
163
|
+
for (const suite of suites) {
|
|
164
|
+
totalTests += Number(suite.tests ?? 0);
|
|
165
|
+
totalFailed += Number(suite.failures ?? 0) + Number(suite.errors ?? 0);
|
|
166
|
+
|
|
167
|
+
for (const tc of suite.testcase ?? []) {
|
|
168
|
+
const rawProblem = tc.failure ?? tc.error;
|
|
169
|
+
if (!rawProblem) continue;
|
|
170
|
+
const problem: JUnitFailureOrError = typeof rawProblem === "string" ? { "#text": rawProblem } : rawProblem;
|
|
171
|
+
|
|
172
|
+
const bodyLocation = problem["#text"] ? parseBodyLocation(problem["#text"], tc.classname) : undefined;
|
|
173
|
+
const panicInfo =
|
|
174
|
+
!bodyLocation && suite["system-out"] && tc.name ? parsePanicInfo(suite["system-out"], tc.name) : undefined;
|
|
175
|
+
const file =
|
|
176
|
+
(tc.file ?? suite.file)
|
|
177
|
+
? resolveFile(tc, suite, ctx.cwd)
|
|
178
|
+
: (bodyLocation?.file ?? panicInfo?.file ?? resolveFile(tc, suite, ctx.cwd));
|
|
179
|
+
const line = tc.line !== undefined ? Number(tc.line) : (bodyLocation?.line ?? panicInfo?.line);
|
|
180
|
+
const id = [file, line, tc.name].filter(Boolean).join(":");
|
|
181
|
+
|
|
182
|
+
failures.push({
|
|
183
|
+
id: id || String(failures.length),
|
|
184
|
+
file,
|
|
185
|
+
line: Number.isNaN(line) ? undefined : line,
|
|
186
|
+
message: (() => {
|
|
187
|
+
const raw =
|
|
188
|
+
problem.message && problem.message.toLowerCase() !== "failed"
|
|
189
|
+
? problem.message
|
|
190
|
+
: (bodyLocation?.message ??
|
|
191
|
+
panicInfo?.message ??
|
|
192
|
+
problem.message ??
|
|
193
|
+
problem["#text"]?.trim().split("\n")[0]);
|
|
194
|
+
if (!raw) return undefined;
|
|
195
|
+
const decoded = decodeXmlEntities(raw);
|
|
196
|
+
// Append assertion diff lines (Expected/Received/Actual) from the body when not already present.
|
|
197
|
+
// Covers Jest-style failures where the diff follows the error type line in #text.
|
|
198
|
+
if (
|
|
199
|
+
problem["#text"] &&
|
|
200
|
+
!decoded.includes("Expected") &&
|
|
201
|
+
!decoded.includes("Received") &&
|
|
202
|
+
!decoded.includes("Actual")
|
|
203
|
+
) {
|
|
204
|
+
const diff = extractAssertionDiff(problem["#text"]);
|
|
205
|
+
if (diff) return `${decoded}\n${diff}`;
|
|
206
|
+
}
|
|
207
|
+
return decoded;
|
|
208
|
+
})(),
|
|
209
|
+
rule: problem.type,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
208
212
|
}
|
|
209
213
|
}
|
|
210
214
|
|
|
@@ -215,7 +219,6 @@ const parser: ParserModule = {
|
|
|
215
219
|
status: totalFailed > 0 ? "fail" : "pass",
|
|
216
220
|
summary: totalFailed > 0 ? `${totalFailed} failed, ${passed} passed` : `${passed} passed`,
|
|
217
221
|
failures,
|
|
218
|
-
artifact: ctx.artifactPaths[0],
|
|
219
222
|
logPath: ctx.logPath,
|
|
220
223
|
};
|
|
221
224
|
},
|
package/package.json
CHANGED
|
@@ -139,21 +139,31 @@ Prefer better output at the source.
|
|
|
139
139
|
|
|
140
140
|
JUnit XML is the de facto standard output format across the JVM ecosystem and many others — Maven, Gradle, pytest (`--junitxml`), Go (`go-junit-report`), .NET (`--logger trx` with conversion), Jest (`jest-junit`), and more. If a tool can emit JUnit XML, `junit-xml` covers it without a custom parser.
|
|
141
141
|
|
|
142
|
+
`artifactPaths` supports glob patterns — the parser aggregates results across all matching files. Use globs for tools that write one XML per test class (Gradle, Maven/Surefire). Use a single path for tools that write one consolidated file (pytest, Jest, etc.).
|
|
143
|
+
|
|
142
144
|
- `structured_return({ command: "[tool] [args] --junitxml=.tmp/report.xml", parseAs: "junit-xml", artifactPaths: [".tmp/report.xml"] })` — pytest, nose2, and any other tool that writes to a file via `--junitxml`
|
|
143
145
|
- `structured_return({ command: "go test [any go test args] 2>&1 | go-junit-report > .tmp/report.xml", parseAs: "junit-xml", artifactPaths: [".tmp/report.xml"] })` — Go requires `go-junit-report` (`go install github.com/jstemmer/go-junit-report/v2@latest`); pipe `go test -v` output through it
|
|
144
|
-
- `structured_return({ command: "gradle test", parseAs: "junit-xml", artifactPaths: ["build/test-results/test/TEST-*.xml"] })` — Gradle writes one XML per test class; pass all matching paths
|
|
145
146
|
|
|
146
|
-
####
|
|
147
|
+
#### Gradle
|
|
147
148
|
|
|
148
|
-
|
|
149
|
+
Gradle writes one XML file per test class — there is no single-file option. Always use a glob pattern.
|
|
149
150
|
|
|
150
|
-
- `structured_return({ command: "
|
|
151
|
+
- `structured_return({ command: "gradle test", parseAs: "junit-xml", artifactPaths: ["build/test-results/test/TEST-*.xml"] })` — full test suite; results aggregated across all matched files
|
|
152
|
+
- `structured_return({ command: "gradle test --tests 'com.example.FooTest'", parseAs: "junit-xml", artifactPaths: ["build/test-results/test/TEST-*.xml"] })` — single class; glob still works (matches the one file)
|
|
153
|
+
- For multi-module projects: `structured_return({ command: "gradle :module:test", parseAs: "junit-xml", artifactPaths: ["module/build/test-results/test/TEST-*.xml"] })` — scope the glob to the module's build dir
|
|
151
154
|
|
|
152
155
|
#### Maven
|
|
153
156
|
|
|
154
|
-
|
|
157
|
+
Maven Surefire writes one XML per test class — same as Gradle. Always use a glob pattern.
|
|
158
|
+
|
|
159
|
+
- `structured_return({ command: "mvn test", parseAs: "junit-xml", artifactPaths: ["target/surefire-reports/TEST-*.xml"] })` — full test suite; any maven args (e.g. `-pl module`, `-Dtest=ClassName`) go before `test`
|
|
160
|
+
- For multi-module projects: `structured_return({ command: "mvn test -pl module", parseAs: "junit-xml", artifactPaths: ["module/target/surefire-reports/TEST-*.xml"] })` — scope the glob to the module
|
|
155
161
|
|
|
156
|
-
|
|
162
|
+
#### .NET / xUnit
|
|
163
|
+
|
|
164
|
+
`dotnet test` requires the `JunitXml.TestLogger` package (`dotnet add package JunitXml.TestLogger`). Pass the logger inline — no config file changes needed. Add a namespace to your test class or the logger will fall back to `UnknownNamespace.UnknownType` for classnames.
|
|
165
|
+
|
|
166
|
+
- `structured_return({ command: "dotnet test --logger \"junit;LogFilePath=.tmp/report.xml\"", parseAs: "junit-xml", artifactPaths: [".tmp/report.xml"] })` — any dotnet test args (project path, `--filter`, etc.) go before `--logger`
|
|
157
167
|
|
|
158
168
|
#### Jest
|
|
159
169
|
|