Skip to content

Commit

Permalink
Introduce theme to use colors/safe
Browse files Browse the repository at this point in the history
  • Loading branch information
bcaudan committed Sep 20, 2020
1 parent cfa2cef commit 99112ba
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 54 deletions.
2 changes: 1 addition & 1 deletion docs/customize-output.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ For our example:
```node
var DisplayProcessor = require('jasmine-spec-reporter').DisplayProcessor;

function TimeProcessor(configuration) {
function TimeProcessor(configuration, theme) {
}

function getTime() {
Expand Down
5 changes: 3 additions & 2 deletions spec/helpers/test-processor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {Configuration} from "../../built/configuration";
import {DisplayProcessor} from "../../built/main";
import {Theme} from "../../src/theme";

export class TestProcessor extends DisplayProcessor {
private test: string;

constructor(configuration: Configuration) {
super(configuration);
constructor(configuration: Configuration, theme: Theme) {
super(configuration, theme);
this.test = configuration.customOptions.test;
}

Expand Down
2 changes: 1 addition & 1 deletion src/configuration-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function merge(template: any, override: any): Configuration {
&& Object.keys(override).indexOf(key) !== -1) {
result[key] = override[key];
if (key === "displayStacktrace" && typeof override[key] === "boolean") {
console.warn("WARN: jasmine-spec-reporter 'displayStacktrace' option supports value ('none', 'raw', 'pretty'), default to 'none'\n".yellow);
console.warn("WARN: jasmine-spec-reporter 'displayStacktrace' option supports value ('none', 'raw', 'pretty'), default to 'none'\n");
result[key] = StacktraceOption.NONE;
}
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/display-processor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {Configuration} from "./configuration";
import {CustomReporterResult} from "./spec-reporter";
import {Theme} from "./theme";
import SuiteInfo = jasmine.SuiteInfo;

export class DisplayProcessor {
protected configuration: Configuration;
protected theme: Theme;

constructor(configuration: Configuration) {
constructor(configuration: Configuration, theme: Theme) {
this.configuration = configuration;
this.theme = theme;
}

public displayJasmineStarted(info: SuiteInfo, log: string): string {
Expand Down
15 changes: 0 additions & 15 deletions src/display/colors-display.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/display/colors-theme.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/display/execution-display.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Configuration} from "../configuration";
import {DisplayProcessor} from "../display-processor";
import {CustomReporterResult, ExecutedSpecs} from "../spec-reporter";
import * as ColorsDisplay from "./colors-display";
import {Logger} from "./logger";

import SuiteInfo = jasmine.SuiteInfo;
Expand All @@ -23,7 +22,6 @@ export class ExecutionDisplay {

constructor(private configuration: Configuration, private logger: Logger, private specs: ExecutedSpecs,
displayProcessors: DisplayProcessor[]) {
ColorsDisplay.init(this.configuration);
this.hasCustomDisplaySpecStarted = ExecutionDisplay.hasCustomDisplaySpecStarted(displayProcessors);
}

Expand Down
11 changes: 6 additions & 5 deletions src/display/summary-display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import {Configuration} from "../configuration";
import {DisplayProcessor} from "../display-processor";
import {ExecutionMetrics} from "../execution-metrics";
import {CustomReporterResult, ExecutedSpecs} from "../spec-reporter";
import {Theme} from "../theme";
import {Logger} from "./logger";

export class SummaryDisplay {
constructor(private logger: Logger, private configuration: Configuration, private specs: ExecutedSpecs) {
constructor(private configuration: Configuration, private theme: Theme, private logger: Logger, private specs: ExecutedSpecs) {
}

public display(metrics: ExecutionMetrics) {
Expand All @@ -14,7 +15,7 @@ export class SummaryDisplay {
let status = "";
if (metrics.failedSpecs === 0 && metrics.globalErrors.length === 0) {
status = (metrics.totalSpecsDefined === metrics.executedSpecs) ?
" SUCCESS".successful : " INCOMPLETE".pending;
this.theme.successful(" SUCCESS") : this.theme.pending(" INCOMPLETE");
}
const failed = (metrics.failedSpecs > 0) ? ` (${metrics.failedSpecs} FAILED)` : "";
const pending = (metrics.pendingSpecs > 0) ? ` (${metrics.pendingSpecs} PENDING)` : "";
Expand All @@ -37,8 +38,8 @@ export class SummaryDisplay {
if (this.configuration.summary.displayPending && metrics.pendingSpecs > 0) {
this.pendingsSummary();
}
this.logger.log(execution + status + errors.failed + failed.failed
+ pending.pending + skipped.pending + duration + ".");
this.logger.log(execution + status + this.theme.failed(errors) + this.theme.failed(failed)
+ this.theme.pending(pending) + this.theme.pending(skipped) + duration + ".");

if (metrics.random) {
this.logger.log(`Randomized with seed ${metrics.seed}.`);
Expand Down Expand Up @@ -106,7 +107,7 @@ export class SummaryDisplay {
this.logger.log(`${index}) ${spec.fullName}`);
this.logger.increaseIndent();
const pendingReason = spec.pendingReason ? spec.pendingReason : "No reason given";
this.logger.log(pendingReason.pending);
this.logger.log(this.theme.pending(pendingReason));
this.logger.resetIndent();
}

Expand Down
2 changes: 1 addition & 1 deletion src/processors/default-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class DefaultProcessor extends DisplayProcessor {
private displayErrorMessages(spec: CustomReporterResult, stacktraceOption: StacktraceOption): string {
const logs: string[] = [];
for (const failedExpectation of spec.failedExpectations) {
logs.push("- ".failed + failedExpectation.message.failed);
logs.push(this.theme.failed("- ") + this.theme.failed(failedExpectation.message));
if (stacktraceOption === StacktraceOption.RAW && failedExpectation.stack) {
logs.push(this.configuration.stacktrace.filter(failedExpectation.stack));
}
Expand Down
6 changes: 3 additions & 3 deletions src/processors/pretty-stacktrace-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class PrettyStacktraceProcessor extends DisplayProcessor {
private displayErrorMessages(spec: CustomReporterResult) {
const logs: string[] = [];
for (const failedExpectation of spec.failedExpectations) {
logs.push("- ".failed + failedExpectation.message.failed);
logs.push(this.theme.failed("- ") + this.theme.failed(failedExpectation.message));
if (failedExpectation.stack) {
logs.push(this.prettifyStack(failedExpectation.stack));
}
Expand All @@ -40,7 +40,7 @@ export class PrettyStacktraceProcessor extends DisplayProcessor {
parseInt(columnNumber, 10)
);

logs.push(`${filename.prettyStacktraceFilename}:${lineNumber.prettyStacktraceLineNumber}:${columnNumber.prettyStacktraceColumnNumber}`);
logs.push(`${this.theme.prettyStacktraceFilename(filename)}:${this.theme.prettyStacktraceLineNumber(lineNumber)}:${this.theme.prettyStacktraceColumnNumber(columnNumber)}`);
logs.push(`${errorContext}\n`);
}
});
Expand All @@ -63,7 +63,7 @@ export class PrettyStacktraceProcessor extends DisplayProcessor {
logs.push(fileLines[i]);
}
if (i === errorLine) {
logs.push(" ".repeat(columnNb - 1) + "~".red);
logs.push(" ".repeat(columnNb - 1) + this.theme.prettyStacktraceError("~"));
}
}
return logs.join("\n");
Expand Down
6 changes: 3 additions & 3 deletions src/processors/spec-colors-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import {CustomReporterResult} from "../spec-reporter";

export class SpecColorsProcessor extends DisplayProcessor {
public displaySuccessfulSpec(spec: CustomReporterResult, log: string): string {
return log.successful;
return this.theme.successful(log);
}

public displayFailedSpec(spec: CustomReporterResult, log: string): string {
return log.failed;
return this.theme.failed(log);
}

public displayPendingSpec(spec: CustomReporterResult, log: string): string {
return log.pending;
return this.theme.pending(log);
}
}
23 changes: 13 additions & 10 deletions src/spec-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {SpecColorsProcessor} from "./processors/spec-colors-processor";
import {SpecDurationsProcessor} from "./processors/spec-durations-processor";
import {SpecPrefixesProcessor} from "./processors/spec-prefixes-processor";
import {SuiteNumberingProcessor} from "./processors/suite-numbering-processor";
import {Theme} from "./theme";

import CustomReporter = jasmine.CustomReporter;
import SuiteInfo = jasmine.SuiteInfo;
Expand All @@ -27,25 +28,25 @@ export interface ExecutedSpecs {
}

export class SpecReporter implements CustomReporter {
private static initProcessors(configuration: Configuration): DisplayProcessor[] {
private static initProcessors(configuration: Configuration, theme: Theme): DisplayProcessor[] {
const displayProcessors: DisplayProcessor[] = [
new DefaultProcessor(configuration),
new SpecPrefixesProcessor(configuration),
new SpecColorsProcessor(configuration),
new PrettyStacktraceProcessor(configuration)
new DefaultProcessor(configuration, theme),
new SpecPrefixesProcessor(configuration, theme),
new SpecColorsProcessor(configuration, theme),
new PrettyStacktraceProcessor(configuration, theme)
];

if (configuration.spec.displayDuration) {
displayProcessors.push(new SpecDurationsProcessor(configuration));
displayProcessors.push(new SpecDurationsProcessor(configuration, theme));
}

if (configuration.suite.displayNumber) {
displayProcessors.push(new SuiteNumberingProcessor(configuration));
displayProcessors.push(new SuiteNumberingProcessor(configuration, theme));
}

if (configuration.customProcessors) {
configuration.customProcessors.forEach((Processor: typeof DisplayProcessor) => {
displayProcessors.push(new Processor(configuration));
displayProcessors.push(new Processor(configuration, theme));
});
}

Expand All @@ -62,14 +63,16 @@ export class SpecReporter implements CustomReporter {
private summary: SummaryDisplay;
private metrics: ExecutionMetrics;
private configuration: Configuration;
private theme: Theme;

constructor(configuration?: Configuration) {
this.configuration = ConfigurationParser.parse(configuration);
const displayProcessors = SpecReporter.initProcessors(this.configuration);
this.theme = new Theme(this.configuration);
const displayProcessors = SpecReporter.initProcessors(this.configuration, this.theme);
const print = this.configuration.print;
this.logger = new Logger(displayProcessors, print);
this.display = new ExecutionDisplay(this.configuration, this.logger, this.specs, displayProcessors);
this.summary = new SummaryDisplay(this.logger, this.configuration, this.specs);
this.summary = new SummaryDisplay(this.configuration, this.theme, this.logger, this.specs);
this.metrics = new ExecutionMetrics();
}

Expand Down
58 changes: 58 additions & 0 deletions src/theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// tslint:disable-next-line:no-submodule-imports
import * as colors from "colors/safe";
import {Configuration} from "./configuration";

interface ColorsTheme {
successful(str: string): string;
failed(str: string): string;
pending(str: string): string;
prettyStacktraceFilename(str: string): string;
prettyStacktraceLineNumber(str: string): string;
prettyStacktraceColumnNumber(str: string): string;
prettyStacktraceError(str: string): string;
}

const colorsTheme = colors as unknown as ColorsTheme;

export class Theme {
constructor(configuration: Configuration) {
configuration.colors.enabled ? colors.enable() : colors.disable();
colors.setTheme({
failed: configuration.colors.failed,
pending: configuration.colors.pending,
successful: configuration.colors.successful,
prettyStacktraceFilename: configuration.colors.prettyStacktraceFilename,
prettyStacktraceLineNumber: configuration.colors.prettyStacktraceLineNumber,
prettyStacktraceColumnNumber: configuration.colors.prettyStacktraceColumnNumber,
prettyStacktraceError: configuration.colors.prettyStacktraceError,
});
}

successful(str: string) {
return colorsTheme.successful(str);
}

failed(str: string) {
return colorsTheme.failed(str);
}

pending(str: string) {
return colorsTheme.pending(str);
}

prettyStacktraceFilename(str: string) {
return colorsTheme.prettyStacktraceFilename(str);
}

prettyStacktraceLineNumber(str: string) {
return colorsTheme.prettyStacktraceLineNumber(str);
}

prettyStacktraceColumnNumber(str: string) {
return colorsTheme.prettyStacktraceColumnNumber(str);
}

prettyStacktraceError(str: string) {
return colorsTheme.prettyStacktraceError(str);
}
}

0 comments on commit 99112ba

Please sign in to comment.