From 341ec5f319e8d756995beb746a7cf879caadff8f Mon Sep 17 00:00:00 2001 From: Tom Lauwaerts Date: Tue, 16 Dec 2025 11:31:36 +0100 Subject: [PATCH] Fix reporter customization --- src/framework/Framework.ts | 12 +-------- src/reporter/Reporter.ts | 54 ++++++++++++++++++++++++-------------- src/reporter/Style.ts | 4 +++ tests/examples/example.ts | 5 ++-- 4 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/framework/Framework.ts b/src/framework/Framework.ts index 8c359db..46d5818 100644 --- a/src/framework/Framework.ts +++ b/src/framework/Framework.ts @@ -56,11 +56,9 @@ export class Framework { public runs: number = 1; - private outputStyle: StyleType = StyleType.plain; - private scheduled: Suite[] = []; - public readonly reporter: Reporter = new Reporter(styling(this.outputStyle)); + public readonly reporter: Reporter = new Reporter(); private constructor() { } @@ -73,14 +71,6 @@ export class Framework { return this.scheduled; } - public style(style: StyleType): void { - this.outputStyle = style; - } - - public styling(): StyleType { - return this.outputStyle; - } - public async sequential(suites: Suite[]) { this.scheduled.concat(suites); this.reporter.general(); diff --git a/src/reporter/Reporter.ts b/src/reporter/Reporter.ts index c88c643..c3e8a8c 100644 --- a/src/reporter/Reporter.ts +++ b/src/reporter/Reporter.ts @@ -1,7 +1,7 @@ import {SuiteResult} from './Results'; import {Archiver} from '../framework/Archiver'; -import {Style} from './Style'; -import {Verbosity} from './index'; +import {Style, styling as styleMap} from './Style'; +import {StyleType, Verbosity} from './index'; import {version} from '../../package.json'; import {green, red, yellow} from 'ansi-colors'; import {Outcome, SilentDescriber} from './describers/Describer'; @@ -39,32 +39,44 @@ export class Reporter { private archiver: Archiver; - private readonly style: Style; + private design: Style; - private readonly verbosity: Verbosity; + private verboseness: Verbosity; - constructor(style: Style, verbosity: Verbosity = Verbosity.normal) { - this.style = style; - this.verbosity = verbosity; + constructor(style: StyleType = StyleType.plain, verbosity: Verbosity = Verbosity.normal) { + this.design = styleMap(style); + this.verboseness = verbosity; this.archiver = new Archiver(`${process.env.TESTFILE?.replace('.asserts.wast', '.wast') ?? 'suite'}.${Date.now()}.log`); this.archiver.set('date', new Date(Date.now()).toISOString()); } private indent(override?: number) { - return indent(override ?? this.indentationLevel, this.style.indentation); + return indent(override ?? this.indentationLevel, this.design.indentation); + } + + style(type: StyleType) { + this.design = styleMap(type); + } + + styling(): StyleType { + return this.design.type; + } + + verbosity(level: Verbosity) { + this.verboseness = level; } general() { - console.log(this.indent() + this.style.colors.highlight(this.style.bullet) + this.style.colors.highlight('latch.') + this.style.emph(' General information')); + console.log(this.indent() + this.design.colors.highlight(this.design.bullet) + this.design.colors.highlight('latch.') + this.design.emph(' General information')); // console.log(blue(`${this.indent()}===================`)); - console.log(this.indent() + ' '.repeat(2) + this.style.emph('version') + ' '.repeat(5) + version); - console.log(this.indent() + ' '.repeat(2) + this.style.emph('archive') + ' '.repeat(5) + this.archiver.archive); + console.log(this.indent() + ' '.repeat(2) + this.design.emph('version') + ' '.repeat(5) + version); + console.log(this.indent() + ' '.repeat(2) + this.design.emph('archive') + ' '.repeat(5) + this.archiver.archive); console.log(); } report(suiteResult: SuiteResult) { this.suites.push(suiteResult); - const report: string[] = describer(this.verbosity, suiteResult).describe(this.style); + const report: string[] = describer(this.verboseness, suiteResult).describe(this.design); for (const line of report) { console.log(this.indent() + line); @@ -88,7 +100,7 @@ export class Reporter { this.archiver.set('skipped scenarios', skipped); this.archiver.set('failed scenarios', failing); - console.log(this.indent() + this.style.colors.highlight(this.style.bullet) + this.style.colors.highlight('results.') + this.style.emph(' Overview')); + console.log(this.indent() + this.design.colors.highlight(this.design.bullet) + this.design.colors.highlight('results.') + this.design.emph(' Overview')); console.log(); this.indentationLevel += 1; @@ -106,12 +118,12 @@ export class Reporter { const len: number = 12; const pss = [`${sc} passing`, `${passing} passing`, `${psa} passing`] - console.log(this.indent() + this.style.emph('Test suites:') + ' '.repeat(len - pss[0].length) + this.style.emph((sc === tl ? green : red)(pss[0])) + `, ${tl} total` + this.style.emph(` (${time.toFixed(0)}ms)`)); - if (this.verbosity > Verbosity.minimal) { - console.log(this.indent() + this.style.emph('Scenarios:') + - ' '.repeat(2 + len - pss[1].length) + this.style.emph((passing === scs.length ? green : red)(pss[1])) + - (skipped > 0 ? ', ' + this.style.emph(yellow(`${skipped} skipped`)) : '') + `, ${scs.length} total`); - console.log(this.indent() + this.style.emph('Actions:') + ' '.repeat(4 + len - pss[2].length) + this.style.emph((passing === scs.length ? green : red)(pss[2])) + (timeouts > 0 ? `, ${timeouts} timeouts` : '') + `, ${total} total`); + console.log(this.indent() + this.design.emph('Test suites:') + ' '.repeat(len - pss[0].length) + this.design.emph((sc === tl ? green : red)(pss[0])) + `, ${tl} total` + this.design.emph(` (${time.toFixed(0)}ms)`)); + if (this.verboseness > Verbosity.minimal) { + console.log(this.indent() + this.design.emph('Scenarios:') + + ' '.repeat(2 + len - pss[1].length) + this.design.emph((passing === scs.length ? green : red)(pss[1])) + + (skipped > 0 ? ', ' + this.design.emph(yellow(`${skipped} skipped`)) : '') + `, ${scs.length} total`); + console.log(this.indent() + this.design.emph('Actions:') + ' '.repeat(4 + len - pss[2].length) + this.design.emph((passing === scs.length ? green : red)(pss[2])) + (timeouts > 0 ? `, ${timeouts} timeouts` : '') + `, ${total} total`); } this.indentationLevel -= 1; @@ -119,6 +131,10 @@ export class Reporter { this.archiver.write(); } + info(text: string) { + this.output += `info: ${text}\n`; + } + error(text: string) { this.output += `error: ${text}\n`; } diff --git a/src/reporter/Style.ts b/src/reporter/Style.ts index 395023d..02fa079 100644 --- a/src/reporter/Style.ts +++ b/src/reporter/Style.ts @@ -36,6 +36,8 @@ export function styling(type: StyleType): Style { // strategy pattern export interface Style { + readonly type: StyleType; + indentation: number; bullet: string; @@ -48,6 +50,7 @@ export interface Style { } export class Plain implements Style { + type = StyleType.plain; indentation = 2; bullet = '● '; end = ''; @@ -71,6 +74,7 @@ export class Plain implements Style { } export class GitHub extends Plain { + type = StyleType.github; bullet = '::group::'; end = '::endgroup::'; } diff --git a/tests/examples/example.ts b/tests/examples/example.ts index 2648b58..7911aa7 100644 --- a/tests/examples/example.ts +++ b/tests/examples/example.ts @@ -6,14 +6,12 @@ import { Invoker, Kind, Message, - OutofPlaceSpecification, Step, - Target, + Verbosity, WASM } from '../../src/index'; import dump = Message.dump; import stepOver = Message.stepOver; -import step = Message.step; const framework = Framework.getImplementation(); @@ -94,4 +92,5 @@ debug.test({ steps: [DUMP] }); +framework.reporter.verbosity(Verbosity.debug); framework.analyse([spec, debug]);