1
0
forked from rosa/hakurei

cmd/pkgserver: add JSON reporter to facilitate go test integration

This commit is contained in:
Kat
2026-03-20 02:28:43 +11:00
parent 8cf2421f30
commit 31a9e9d014

View File

@@ -84,6 +84,7 @@ export interface TestResult {
}
export function run(reporter: Reporter) {
reporter.register(TESTS);
for (const suite of TESTS) {
for (const c of suite.children) runTests(reporter, [suite.name], c);
}
@@ -126,6 +127,7 @@ function extractExceptionString(e: any): string {
// Reporting
export interface Reporter {
register(suites: TestGroup[]): void
update(path: string[], result: TestResult): void;
finalize(): void;
}
@@ -149,6 +151,8 @@ export class StreamReporter implements Reporter {
this.counts = { successes: 0, failures: 0 };
}
register(suites: TestGroup[]) {}
update(path: string[], result: TestResult) {
if (path.length === 0) throw new RangeError("path is empty");
const pathStr = path.join(SEP);
@@ -206,6 +210,8 @@ export class StreamReporter implements Reporter {
}
export class DOMReporter implements Reporter {
register(suites: TestGroup[]) {}
update(path: string[], result: TestResult) {
if (path.length === 0) throw new RangeError("path is empty");
const counter = document.getElementById(result.success ? "successes" : "failures");
@@ -250,3 +256,32 @@ export class DOMReporter implements Reporter {
finalize() {}
}
interface GoNode {
name: string;
subtests?: GoNode[];
}
// Used to display results via `go test`, via some glue code from the Go side.
export class GoTestReporter implements Reporter {
// Convert a test tree into the one expected by the Go code.
static serialize(node: TestTree): GoNode {
if (!("children" in node)) return { name: node.name };
return {
name: node.name,
subtests: node.children.map(GoTestReporter.serialize),
};
}
register(suites: TestGroup[]) {
console.log(JSON.stringify(suites.map(GoTestReporter.serialize)));
}
update(path: string[], result: TestResult) {
console.log(JSON.stringify({ "path": path, "result": result }));
}
finalize() {
console.log("null");
}
}