Skip to content

Commit

Permalink
chore: split output clients by capabilities and base dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Feldman committed Dec 23, 2024
1 parent 1c8e6f0 commit fe12c7e
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 133 deletions.
209 changes: 146 additions & 63 deletions packages/playwright/src/reporters/base.ts

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions packages/playwright/src/reporters/dot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

import { colors, BaseReporter, formatError } from './base';
import { TerminalReporter } from './base';
import type { FullResult, TestCase, TestResult, Suite, TestError } from '../../types/testReporter';

class DotReporter extends BaseReporter {
class DotReporter extends TerminalReporter {
private _counter = 0;

override onBegin(suite: Suite) {
Expand Down Expand Up @@ -45,23 +45,23 @@ class DotReporter extends BaseReporter {
}
++this._counter;
if (result.status === 'skipped') {
process.stdout.write(colors.yellow('°'));
process.stdout.write(this.screen.colors.yellow('°'));
return;
}
if (this.willRetry(test)) {
process.stdout.write(colors.gray('×'));
process.stdout.write(this.screen.colors.gray('×'));
return;
}
switch (test.outcome()) {
case 'expected': process.stdout.write(colors.green('·')); break;
case 'unexpected': process.stdout.write(colors.red(result.status === 'timedOut' ? 'T' : 'F')); break;
case 'flaky': process.stdout.write(colors.yellow('±')); break;
case 'expected': process.stdout.write(this.screen.colors.green('·')); break;
case 'unexpected': process.stdout.write(this.screen.colors.red(result.status === 'timedOut' ? 'T' : 'F')); break;
case 'flaky': process.stdout.write(this.screen.colors.yellow('±')); break;
}
}

override onError(error: TestError): void {
super.onError(error);
console.log('\n' + formatError(error, colors.enabled).message);
console.log('\n' + this.formatError(error).message);
this._counter = 0;
}

Expand Down
19 changes: 12 additions & 7 deletions packages/playwright/src/reporters/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { ms as milliseconds } from 'playwright-core/lib/utilsBundle';
import path from 'path';
import { BaseReporter, colors, formatError, formatResultFailure, formatRetry, formatTestHeader, formatTestTitle, stripAnsiEscapes } from './base';
import { TerminalReporter, formatResultFailure, formatRetry, noColors, stripAnsiEscapes } from './base';
import type { TestCase, FullResult, TestError } from '../../types/testReporter';

type GitHubLogType = 'debug' | 'notice' | 'warning' | 'error';
Expand Down Expand Up @@ -56,9 +56,14 @@ class GitHubLogger {
}
}

export class GitHubReporter extends BaseReporter {
export class GitHubReporter extends TerminalReporter {
githubLogger = new GitHubLogger();

constructor(options: { omitFailures?: boolean } = {}) {
super(options);
this.screen.colors = noColors;
}

printsToStdio() {
return false;
}
Expand All @@ -69,7 +74,7 @@ export class GitHubReporter extends BaseReporter {
}

override onError(error: TestError) {
const errorMessage = formatError(error, false).message;
const errorMessage = this.formatError(error).message;
this.githubLogger.error(errorMessage);
}

Expand Down Expand Up @@ -100,10 +105,10 @@ export class GitHubReporter extends BaseReporter {

private _printFailureAnnotations(failures: TestCase[]) {
failures.forEach((test, index) => {
const title = formatTestTitle(this.config, test);
const header = formatTestHeader(this.config, test, { indent: ' ', index: index + 1, mode: 'error' });
const title = this.formatTestTitle(test);
const header = this.formatTestHeader(test, { indent: ' ', index: index + 1, mode: 'error' });
for (const result of test.results) {
const errors = formatResultFailure(test, result, ' ', colors.enabled);
const errors = formatResultFailure(this.screen, test, result, ' ');
for (const error of errors) {
const options: GitHubLogOptions = {
file: workspaceRelativePath(error.location?.file || test.location.file),
Expand All @@ -113,7 +118,7 @@ export class GitHubReporter extends BaseReporter {
options.line = error.location.line;
options.col = error.location.column;
}
const message = [header, ...formatRetry(result), error.message].join('\n');
const message = [header, ...formatRetry(this.screen, result), error.message].join('\n');
this.githubLogger.error(message, options);
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/playwright/src/reporters/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { open } from 'playwright-core/lib/utilsBundle';
import { colors, open } from 'playwright-core/lib/utilsBundle';
import { MultiMap, getPackageManagerExecCommand } from 'playwright-core/lib/utils';
import fs from 'fs';
import path from 'path';
Expand All @@ -23,7 +23,7 @@ import { Transform } from 'stream';
import { codeFrameColumns } from '../transform/babelBundle';
import type * as api from '../../types/testReporter';
import { HttpServer, assert, calculateSha1, copyFileAndMakeWritable, gracefullyProcessExitDoNotHang, removeFolders, sanitizeForFilePath, toPosixPath } from 'playwright-core/lib/utils';
import { colors, formatError, formatResultFailure, stripAnsiEscapes } from './base';
import { formatError, formatResultFailure, internalScreen, stripAnsiEscapes } from './base';
import { resolveReporterOutputPath } from '../util';
import type { Metadata } from '../../types/test';
import type { ZipFile } from 'playwright-core/lib/zipBundle';
Expand Down Expand Up @@ -297,7 +297,7 @@ class HtmlBuilder {
files: [...data.values()].map(e => e.testFileSummary),
projectNames: projectSuites.map(r => r.project()!.name),
stats: { ...[...data.values()].reduce((a, e) => addStats(a, e.testFileSummary.stats), emptyStats()) },
errors: topLevelErrors.map(error => formatError(error, true).message),
errors: topLevelErrors.map(error => formatError(internalScreen, error).message),
};
htmlReport.files.sort((f1, f2) => {
const w1 = f1.stats.unexpected * 1000 + f1.stats.flaky;
Expand Down Expand Up @@ -506,7 +506,7 @@ class HtmlBuilder {
startTime: result.startTime.toISOString(),
retry: result.retry,
steps: dedupeSteps(result.steps).map(s => this._createTestStep(s)),
errors: formatResultFailure(test, result, '', true).map(error => error.message),
errors: formatResultFailure(internalScreen, test, result, '').map(error => error.message),
status: result.status,
attachments: this._serializeAttachments([
...result.attachments,
Expand Down
4 changes: 2 additions & 2 deletions packages/playwright/src/reporters/internalReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import fs from 'fs';
import { codeFrameColumns } from '../transform/babelBundle';
import type { FullConfig, TestCase, TestError, TestResult, FullResult, TestStep } from '../../types/testReporter';
import { Suite } from '../common/test';
import { colors, prepareErrorStack, relativeFilePath } from './base';
import { internalScreen, prepareErrorStack, relativeFilePath } from './base';
import type { ReporterV2 } from './reporterV2';
import { monotonicTime } from 'playwright-core/lib/utils';
import { Multiplexer } from './multiplexer';
Expand Down Expand Up @@ -125,7 +125,7 @@ function addLocationAndSnippetToError(config: FullConfig, error: TestError, file
const codeFrame = codeFrameColumns(source, { start: location }, { highlightCode: true });
// Convert /var/folders to /private/var/folders on Mac.
if (!file || fs.realpathSync(file) !== location.file) {
tokens.push(colors.gray(` at `) + `${relativeFilePath(config, location.file)}:${location.line}`);
tokens.push(internalScreen.colors.gray(` at `) + `${relativeFilePath(internalScreen, config, location.file)}:${location.line}`);
tokens.push('');
}
tokens.push(codeFrame);
Expand Down
4 changes: 2 additions & 2 deletions packages/playwright/src/reporters/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import fs from 'fs';
import path from 'path';
import type { FullConfig, TestCase, Suite, TestResult, TestError, TestStep, FullResult, Location, JSONReport, JSONReportSuite, JSONReportSpec, JSONReportTest, JSONReportTestResult, JSONReportTestStep, JSONReportError } from '../../types/testReporter';
import { formatError, prepareErrorStack, resolveOutputFile } from './base';
import { formatError, nonTerminalScreen, prepareErrorStack, resolveOutputFile } from './base';
import { MultiMap, toPosixPath } from 'playwright-core/lib/utils';
import { getProjectId } from '../common/config';
import type { ReporterV2 } from './reporterV2';
Expand Down Expand Up @@ -222,7 +222,7 @@ class JSONReporter implements ReporterV2 {
}

private _serializeError(error: TestError): JSONReportError {
return formatError(error, true);
return formatError(nonTerminalScreen, error);
}

private _serializeTestStep(step: TestStep): JSONReportTestStep {
Expand Down
4 changes: 2 additions & 2 deletions packages/playwright/src/reporters/junit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import fs from 'fs';
import path from 'path';
import type { FullConfig, FullResult, Suite, TestCase } from '../../types/testReporter';
import { formatFailure, resolveOutputFile, stripAnsiEscapes } from './base';
import { formatFailure, nonTerminalScreen, resolveOutputFile, stripAnsiEscapes } from './base';
import { getAsBooleanFromENV } from 'playwright-core/lib/utils';
import type { ReporterV2 } from './reporterV2';

Expand Down Expand Up @@ -188,7 +188,7 @@ class JUnitReporter implements ReporterV2 {
message: `${path.basename(test.location.file)}:${test.location.line}:${test.location.column} ${test.title}`,
type: 'FAILURE',
},
text: stripAnsiEscapes(formatFailure(this.config, test))
text: stripAnsiEscapes(formatFailure(nonTerminalScreen, this.config, test))
});
}

Expand Down
14 changes: 7 additions & 7 deletions packages/playwright/src/reporters/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

import { colors, BaseReporter, formatError, formatFailure, formatTestTitle } from './base';
import { TerminalReporter } from './base';
import type { TestCase, Suite, TestResult, FullResult, TestStep, TestError } from '../../types/testReporter';

class LineReporter extends BaseReporter {
class LineReporter extends TerminalReporter {
private _current = 0;
private _failures = 0;
private _lastTest: TestCase | undefined;
Expand Down Expand Up @@ -50,7 +50,7 @@ class LineReporter extends BaseReporter {
stream.write(`\u001B[1A\u001B[2K`);
if (test && this._lastTest !== test) {
// Write new header for the output.
const title = colors.dim(formatTestTitle(this.config, test));
const title = this.screen.colors.dim(this.formatTestTitle(test));
stream.write(this.fitToScreen(title) + `\n`);
this._lastTest = test;
}
Expand Down Expand Up @@ -82,16 +82,16 @@ class LineReporter extends BaseReporter {
if (!this.willRetry(test) && (test.outcome() === 'flaky' || test.outcome() === 'unexpected' || result.status === 'interrupted')) {
if (!process.env.PW_TEST_DEBUG_REPORTERS)
process.stdout.write(`\u001B[1A\u001B[2K`);
console.log(formatFailure(this.config, test, ++this._failures));
console.log(this.formatFailure(test, ++this._failures));
console.log();
}
}

private _updateLine(test: TestCase, result: TestResult, step?: TestStep) {
const retriesPrefix = this.totalTestCount < this._current ? ` (retries)` : ``;
const prefix = `[${this._current}/${this.totalTestCount}]${retriesPrefix} `;
const currentRetrySuffix = result.retry ? colors.yellow(` (retry #${result.retry})`) : '';
const title = formatTestTitle(this.config, test, step) + currentRetrySuffix;
const currentRetrySuffix = result.retry ? this.screen.colors.yellow(` (retry #${result.retry})`) : '';
const title = this.formatTestTitle(test, step) + currentRetrySuffix;
if (process.env.PW_TEST_DEBUG_REPORTERS)
process.stdout.write(`${prefix + title}\n`);
else
Expand All @@ -101,7 +101,7 @@ class LineReporter extends BaseReporter {
override onError(error: TestError): void {
super.onError(error);

const message = formatError(error, colors.enabled).message + '\n';
const message = this.formatError(error).message + '\n';
if (!process.env.PW_TEST_DEBUG_REPORTERS && this._didBegin)
process.stdout.write(`\u001B[1A\u001B[2K`);
process.stdout.write(message);
Expand Down
Loading

0 comments on commit fe12c7e

Please sign in to comment.