From 31a9e9d01414faff6b9dce2466a6c492a95a949e Mon Sep 17 00:00:00 2001 From: Kat <00-kat@proton.me> Date: Fri, 20 Mar 2026 02:28:43 +1100 Subject: [PATCH] cmd/pkgserver: add JSON reporter to facilitate go test integration --- cmd/pkgserver/ui/static/test.ts | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/cmd/pkgserver/ui/static/test.ts b/cmd/pkgserver/ui/static/test.ts index 4e5476b..245756c 100644 --- a/cmd/pkgserver/ui/static/test.ts +++ b/cmd/pkgserver/ui/static/test.ts @@ -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"); + } +}