1
0
forked from rosa/hakurei

cmd/pkgserver: serialize TestController methods to go test

This commit is contained in:
Kat
2026-03-20 03:48:31 +11:00
parent 31a9e9d014
commit ff2db48f8c

View File

@@ -34,17 +34,26 @@ function checkDuplicates(parent: string, names: { name: string }[]) {
class FailNowSentinel {} class FailNowSentinel {}
export type Journal = ({ method: "fail" } | { method: "log", message: string })[];
function formatJournal(journal: Journal): string {
return journal
.filter((e) => e.method === "log")
.map((e) => e.message)
.join("\n");
}
export class TestController { export class TestController {
#logBuf: string[]; journal: Journal;
#failed: boolean; #failed: boolean;
constructor() { constructor() {
this.#logBuf = []; this.journal = [];
this.#failed = false; this.#failed = false;
} }
fail() { fail() {
this.#failed = true; this.#failed = true;
this.journal.push({ method: "fail" });
} }
failed(): boolean { failed(): boolean {
@@ -57,7 +66,7 @@ export class TestController {
} }
log(message: string) { log(message: string) {
this.#logBuf.push(message); this.journal.push({ method: "log", message });
} }
error(message: string) { error(message: string) {
@@ -69,10 +78,6 @@ export class TestController {
this.log(message); this.log(message);
this.failNow(); this.failNow();
} }
getLog(): string {
return this.#logBuf.join("\n");
}
} }
// ============================================================================= // =============================================================================
@@ -80,7 +85,7 @@ export class TestController {
export interface TestResult { export interface TestResult {
success: boolean; success: boolean;
output: string; journal: Journal;
} }
export function run(reporter: Reporter) { export function run(reporter: Reporter) {
@@ -103,13 +108,11 @@ function runTests(reporter: Reporter, parents: string[], node: TestTree) {
node.test(controller); node.test(controller);
} catch (e) { } catch (e) {
if (!(e instanceof FailNowSentinel)) { if (!(e instanceof FailNowSentinel)) {
controller.fail();
excStr = extractExceptionString(e); excStr = extractExceptionString(e);
} }
} }
const log = controller.getLog(); if (excStr !== undefined) controller.error(excStr);
const output = (log && excStr) ? `${log}\n${excStr}` : `${log}${excStr ?? ''}`; reporter.update(path, { success: !controller.failed(), journal: controller.journal });
reporter.update(path, { success: !controller.failed(), output });
} }
function extractExceptionString(e: any): string { function extractExceptionString(e: any): string {
@@ -196,10 +199,11 @@ export class StreamReporter implements Reporter {
#writeOutput(test: { name: string } & TestResult, prefix: string, nested: boolean) { #writeOutput(test: { name: string } & TestResult, prefix: string, nested: boolean) {
let output = ""; let output = "";
if (test.output) { let logOutput = formatJournal(test.journal);
const lines = test.output.split("\n"); if (logOutput) {
const lines = logOutput.split("\n");
if (lines.length <= 1) { if (lines.length <= 1) {
output = `: ${test.output}`; output = `: ${logOutput}`;
} else { } else {
const padding = nested ? " " : " "; const padding = nested ? " " : " ";
output = ":\n" + lines.map((line) => padding + line).join("\n"); output = ":\n" + lines.map((line) => padding + line).join("\n");
@@ -243,9 +247,10 @@ export class DOMReporter implements Reporter {
} }
const p = document.createElement("p"); const p = document.createElement("p");
p.classList.add("test-desc"); p.classList.add("test-desc");
if (result.output) { const logOutput = formatJournal(result.journal);
if (logOutput) {
const pre = document.createElement("pre"); const pre = document.createElement("pre");
pre.appendChild(document.createTextNode(result.output)); pre.appendChild(document.createTextNode(logOutput));
p.appendChild(pre); p.appendChild(pre);
} else { } else {
p.classList.add("italic"); p.classList.add("italic");
@@ -266,7 +271,7 @@ interface GoNode {
export class GoTestReporter implements Reporter { export class GoTestReporter implements Reporter {
// Convert a test tree into the one expected by the Go code. // Convert a test tree into the one expected by the Go code.
static serialize(node: TestTree): GoNode { static serialize(node: TestTree): GoNode {
if (!("children" in node)) return { name: node.name }; if (!("children" in node)) return { name: node.name, subtests: null };
return { return {
name: node.name, name: node.name,
subtests: node.children.map(GoTestReporter.serialize), subtests: node.children.map(GoTestReporter.serialize),
@@ -278,7 +283,7 @@ export class GoTestReporter implements Reporter {
} }
update(path: string[], result: TestResult) { update(path: string[], result: TestResult) {
console.log(JSON.stringify({ "path": path, "result": result })); console.log(JSON.stringify({ "path": path, ...result }));
} }
finalize() { finalize() {