2021-06-07 02:09:53 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-06-23 11:08:35 +02:00
|
|
|
/* eslint-disable no-console */
|
2022-04-19 06:47:18 +02:00
|
|
|
import { colors, ms as milliseconds } from 'playwright-core/lib/utilsBundle';
|
2022-02-11 17:33:56 +01:00
|
|
|
import { BaseReporter, formatTestTitle } from './base';
|
2022-04-06 23:57:14 +02:00
|
|
|
import type { FullConfig, FullResult, Suite, TestCase, TestResult, TestStep } from '../../types/testReporter';
|
2021-06-07 02:09:53 +02:00
|
|
|
|
2021-06-22 19:04:24 +02:00
|
|
|
// Allow it in the Visual Studio Code Terminal and the new Windows Terminal
|
|
|
|
|
const DOES_NOT_SUPPORT_UTF8_IN_TERMINAL = process.platform === 'win32' && process.env.TERM_PROGRAM !== 'vscode' && !process.env.WT_SESSION;
|
|
|
|
|
const POSITIVE_STATUS_MARK = DOES_NOT_SUPPORT_UTF8_IN_TERMINAL ? 'ok' : '✓';
|
|
|
|
|
const NEGATIVE_STATUS_MARK = DOES_NOT_SUPPORT_UTF8_IN_TERMINAL ? 'x' : '✘';
|
|
|
|
|
|
2021-06-07 02:09:53 +02:00
|
|
|
class ListReporter extends BaseReporter {
|
|
|
|
|
private _lastRow = 0;
|
2021-07-19 23:54:18 +02:00
|
|
|
private _testRows = new Map<TestCase, number>();
|
2021-06-07 02:09:53 +02:00
|
|
|
private _needNewLine = false;
|
2022-07-14 00:11:56 +02:00
|
|
|
private readonly _liveTerminal: string | boolean | undefined;
|
2021-08-18 01:41:36 +02:00
|
|
|
|
2021-10-16 04:18:56 +02:00
|
|
|
constructor(options: { omitFailures?: boolean } = {}) {
|
|
|
|
|
super(options);
|
2022-07-14 00:11:56 +02:00
|
|
|
this._liveTerminal = process.stdout.isTTY || !!process.env.PWTEST_TTY_WIDTH;
|
2021-08-18 01:41:36 +02:00
|
|
|
}
|
2021-06-07 02:09:53 +02:00
|
|
|
|
2021-11-03 16:25:16 +01:00
|
|
|
printsToStdio() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 16:11:18 +02:00
|
|
|
override onBegin(config: FullConfig, suite: Suite) {
|
2021-06-07 02:09:53 +02:00
|
|
|
super.onBegin(config, suite);
|
2021-11-03 19:17:23 +01:00
|
|
|
console.log(this.generateStartingMessage());
|
2021-06-07 02:09:53 +02:00
|
|
|
console.log();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-13 22:54:26 +02:00
|
|
|
onTestBegin(test: TestCase, result: TestResult) {
|
2022-07-14 00:11:56 +02:00
|
|
|
if (this._liveTerminal) {
|
2021-06-07 02:09:53 +02:00
|
|
|
if (this._needNewLine) {
|
|
|
|
|
this._needNewLine = false;
|
|
|
|
|
process.stdout.write('\n');
|
|
|
|
|
this._lastRow++;
|
|
|
|
|
}
|
2022-06-23 02:03:54 +02:00
|
|
|
const prefix = ' ';
|
|
|
|
|
const line = colors.gray(formatTestTitle(this.config, test)) + this._retrySuffix(result);
|
|
|
|
|
process.stdout.write(prefix + this.fitToScreen(line, prefix) + '\n');
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
this._testRows.set(test, this._lastRow++);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 16:11:18 +02:00
|
|
|
override onStdOut(chunk: string | Buffer, test?: TestCase, result?: TestResult) {
|
2021-08-12 01:44:19 +02:00
|
|
|
super.onStdOut(chunk, test, result);
|
2021-06-07 02:09:53 +02:00
|
|
|
this._dumpToStdio(test, chunk, process.stdout);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 16:11:18 +02:00
|
|
|
override onStdErr(chunk: string | Buffer, test?: TestCase, result?: TestResult) {
|
2021-08-12 01:44:19 +02:00
|
|
|
super.onStdErr(chunk, test, result);
|
2022-01-12 16:43:25 +01:00
|
|
|
this._dumpToStdio(test, chunk, process.stderr);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-18 01:41:36 +02:00
|
|
|
onStepBegin(test: TestCase, result: TestResult, step: TestStep) {
|
2022-07-14 00:11:56 +02:00
|
|
|
if (!this._liveTerminal)
|
2021-08-18 01:41:36 +02:00
|
|
|
return;
|
|
|
|
|
if (step.category !== 'test.step')
|
|
|
|
|
return;
|
2022-06-23 02:03:54 +02:00
|
|
|
this._updateTestLine(test, colors.gray(formatTestTitle(this.config, test, step)) + this._retrySuffix(result), ' ');
|
2021-08-18 01:41:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onStepEnd(test: TestCase, result: TestResult, step: TestStep) {
|
2022-07-14 00:11:56 +02:00
|
|
|
if (!this._liveTerminal)
|
2021-08-18 01:41:36 +02:00
|
|
|
return;
|
|
|
|
|
if (step.category !== 'test.step')
|
|
|
|
|
return;
|
2022-06-23 02:03:54 +02:00
|
|
|
this._updateTestLine(test, colors.gray(formatTestTitle(this.config, test, step.parent)) + this._retrySuffix(result), ' ');
|
2021-08-18 01:41:36 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-19 23:54:18 +02:00
|
|
|
private _dumpToStdio(test: TestCase | undefined, chunk: string | Buffer, stream: NodeJS.WriteStream) {
|
2021-06-07 02:09:53 +02:00
|
|
|
if (this.config.quiet)
|
|
|
|
|
return;
|
|
|
|
|
const text = chunk.toString('utf-8');
|
|
|
|
|
this._needNewLine = text[text.length - 1] !== '\n';
|
2022-07-14 00:11:56 +02:00
|
|
|
if (this._liveTerminal) {
|
2021-06-07 02:09:53 +02:00
|
|
|
const newLineCount = text.split('\n').length - 1;
|
|
|
|
|
this._lastRow += newLineCount;
|
|
|
|
|
}
|
|
|
|
|
stream.write(chunk);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 16:11:18 +02:00
|
|
|
override onTestEnd(test: TestCase, result: TestResult) {
|
2021-06-07 02:09:53 +02:00
|
|
|
super.onTestEnd(test, result);
|
|
|
|
|
|
|
|
|
|
const title = formatTestTitle(this.config, test);
|
2022-06-23 02:03:54 +02:00
|
|
|
let prefix = '';
|
2021-06-07 02:09:53 +02:00
|
|
|
let text = '';
|
|
|
|
|
if (result.status === 'skipped') {
|
2022-06-23 02:03:54 +02:00
|
|
|
prefix = colors.green(' - ');
|
|
|
|
|
// Do not show duration for skipped.
|
|
|
|
|
text = colors.cyan(title) + this._retrySuffix(result);
|
2021-06-07 02:09:53 +02:00
|
|
|
} else {
|
2021-06-22 19:04:24 +02:00
|
|
|
const statusMark = (' ' + (result.status === 'passed' ? POSITIVE_STATUS_MARK : NEGATIVE_STATUS_MARK)).padEnd(5);
|
2022-06-23 02:03:54 +02:00
|
|
|
if (result.status === test.expectedStatus) {
|
|
|
|
|
prefix = colors.green(statusMark);
|
|
|
|
|
text = colors.gray(title);
|
|
|
|
|
} else {
|
|
|
|
|
prefix = colors.red(statusMark);
|
|
|
|
|
text = colors.red(title);
|
|
|
|
|
}
|
|
|
|
|
text += this._retrySuffix(result) + colors.dim(` (${milliseconds(result.duration)})`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-14 00:11:56 +02:00
|
|
|
if (this._liveTerminal) {
|
2022-06-23 02:03:54 +02:00
|
|
|
this._updateTestLine(test, text, prefix);
|
2021-08-18 01:41:36 +02:00
|
|
|
} else {
|
|
|
|
|
if (this._needNewLine) {
|
|
|
|
|
this._needNewLine = false;
|
|
|
|
|
process.stdout.write('\n');
|
|
|
|
|
}
|
2022-06-23 02:03:54 +02:00
|
|
|
process.stdout.write(prefix + text);
|
2021-08-18 01:41:36 +02:00
|
|
|
process.stdout.write('\n');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-23 02:03:54 +02:00
|
|
|
private _updateTestLine(test: TestCase, line: string, prefix: string) {
|
2022-02-24 21:39:28 +01:00
|
|
|
if (process.env.PW_TEST_DEBUG_REPORTERS)
|
2022-06-23 02:03:54 +02:00
|
|
|
this._updateTestLineForTest(test, line, prefix);
|
2021-08-18 01:41:36 +02:00
|
|
|
else
|
2022-06-23 02:03:54 +02:00
|
|
|
this._updateTestLineForTTY(test, line, prefix);
|
2021-08-18 01:41:36 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-23 02:03:54 +02:00
|
|
|
private _updateTestLineForTTY(test: TestCase, line: string, prefix: string) {
|
2021-06-07 02:09:53 +02:00
|
|
|
const testRow = this._testRows.get(test)!;
|
|
|
|
|
// Go up if needed
|
2021-08-18 01:41:36 +02:00
|
|
|
if (testRow !== this._lastRow)
|
2021-06-07 02:09:53 +02:00
|
|
|
process.stdout.write(`\u001B[${this._lastRow - testRow}A`);
|
2021-12-18 06:07:04 +01:00
|
|
|
// Erase line, go to the start
|
|
|
|
|
process.stdout.write('\u001B[2K\u001B[0G');
|
2022-06-23 02:03:54 +02:00
|
|
|
process.stdout.write(prefix + this.fitToScreen(line, prefix));
|
2021-06-07 02:09:53 +02:00
|
|
|
// Go down if needed.
|
2021-08-18 01:41:36 +02:00
|
|
|
if (testRow !== this._lastRow)
|
|
|
|
|
process.stdout.write(`\u001B[${this._lastRow - testRow}E`);
|
2022-07-01 05:24:00 +02:00
|
|
|
if (process.env.PWTEST_TTY_WIDTH)
|
|
|
|
|
process.stdout.write('\n'); // For testing.
|
2021-08-18 01:41:36 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-18 06:07:04 +01:00
|
|
|
private _retrySuffix(result: TestResult) {
|
|
|
|
|
return (result.retry ? colors.yellow(` (retry #${result.retry})`) : '');
|
2021-09-08 02:42:17 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-23 02:03:54 +02:00
|
|
|
private _updateTestLineForTest(test: TestCase, line: string, prefix: string) {
|
2021-08-18 01:41:36 +02:00
|
|
|
const testRow = this._testRows.get(test)!;
|
2022-06-23 02:03:54 +02:00
|
|
|
process.stdout.write(testRow + ' : ' + prefix + line + '\n');
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-25 16:11:18 +02:00
|
|
|
override async onEnd(result: FullResult) {
|
2021-06-29 19:55:46 +02:00
|
|
|
await super.onEnd(result);
|
2021-06-07 02:09:53 +02:00
|
|
|
process.stdout.write('\n');
|
|
|
|
|
this.epilogue(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ListReporter;
|