forked from rosa/hakurei
cmd/pkgserver: add JSON reporter to facilitate go test integration
This commit is contained in:
@@ -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);
|
||||
}
|
||||
@@ -122,6 +123,7 @@ function extractExceptionString(e: any): string {
|
||||
// Reporting
|
||||
|
||||
export interface Reporter {
|
||||
register(suites: TestGroup[]): void
|
||||
update(path: string[], result: TestResult): void;
|
||||
finalize(): void;
|
||||
}
|
||||
@@ -145,6 +147,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);
|
||||
@@ -202,6 +206,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");
|
||||
@@ -246,3 +252,31 @@ 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 {
|
||||
return {
|
||||
name: node.name,
|
||||
subtests: "children" in node ? node.children.map(GoTestReporter.serialize) : null,
|
||||
};
|
||||
}
|
||||
|
||||
register(suites: TestGroup[]) {
|
||||
console.log(JSON.stringify(suites.map(GoTestReporter.serialize)));
|
||||
}
|
||||
|
||||
update(path: string[], result: TestResult) {
|
||||
console.log(JSON.stringify({ path: path, ...result }));
|
||||
}
|
||||
|
||||
finalize() {
|
||||
console.log("null");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user