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 */
|
2021-06-07 02:09:53 +02:00
|
|
|
import colors from 'colors/safe';
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
import milliseconds from 'ms';
|
|
|
|
|
import { BaseReporter, formatTestTitle } from './base';
|
2021-07-19 23:54:18 +02:00
|
|
|
import { FullConfig, FullResult, Suite, TestCase, TestResult } 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;
|
|
|
|
|
|
|
|
|
|
onBegin(config: FullConfig, suite: Suite) {
|
|
|
|
|
super.onBegin(config, suite);
|
|
|
|
|
console.log();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 23:54:18 +02:00
|
|
|
onTestBegin(test: TestCase) {
|
2021-06-07 02:09:53 +02:00
|
|
|
if (process.stdout.isTTY) {
|
|
|
|
|
if (this._needNewLine) {
|
|
|
|
|
this._needNewLine = false;
|
|
|
|
|
process.stdout.write('\n');
|
|
|
|
|
this._lastRow++;
|
|
|
|
|
}
|
2021-08-12 01:44:19 +02:00
|
|
|
process.stdout.write(' ' + colors.gray(formatTestTitle(this.config, test)) + '\n');
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
this._testRows.set(test, this._lastRow++);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 01:44:19 +02:00
|
|
|
onStdOut(chunk: string | Buffer, test?: TestCase, result?: TestResult) {
|
|
|
|
|
super.onStdOut(chunk, test, result);
|
2021-06-07 02:09:53 +02:00
|
|
|
this._dumpToStdio(test, chunk, process.stdout);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 01:44:19 +02:00
|
|
|
onStdErr(chunk: string | Buffer, test?: TestCase, result?: TestResult) {
|
|
|
|
|
super.onStdErr(chunk, test, result);
|
2021-06-07 02:09:53 +02:00
|
|
|
this._dumpToStdio(test, chunk, process.stdout);
|
|
|
|
|
}
|
|
|
|
|
|
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';
|
|
|
|
|
if (process.stdout.isTTY) {
|
|
|
|
|
const newLineCount = text.split('\n').length - 1;
|
|
|
|
|
this._lastRow += newLineCount;
|
|
|
|
|
}
|
|
|
|
|
stream.write(chunk);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 23:54:18 +02:00
|
|
|
onTestEnd(test: TestCase, result: TestResult) {
|
2021-06-07 02:09:53 +02:00
|
|
|
super.onTestEnd(test, result);
|
|
|
|
|
|
|
|
|
|
const duration = colors.dim(` (${milliseconds(result.duration)})`);
|
|
|
|
|
const title = formatTestTitle(this.config, test);
|
|
|
|
|
let text = '';
|
|
|
|
|
if (result.status === 'skipped') {
|
2021-08-12 01:44:19 +02:00
|
|
|
text = colors.green(' - ') + colors.cyan(title);
|
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);
|
2021-06-07 02:09:53 +02:00
|
|
|
if (result.status === test.expectedStatus)
|
|
|
|
|
text = '\u001b[2K\u001b[0G' + colors.green(statusMark) + colors.gray(title) + duration;
|
|
|
|
|
else
|
2021-08-12 01:44:19 +02:00
|
|
|
text = '\u001b[2K\u001b[0G' + colors.red(statusMark + title) + duration;
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const testRow = this._testRows.get(test)!;
|
|
|
|
|
// Go up if needed
|
|
|
|
|
if (process.stdout.isTTY && testRow !== this._lastRow)
|
|
|
|
|
process.stdout.write(`\u001B[${this._lastRow - testRow}A`);
|
|
|
|
|
// Erase line
|
|
|
|
|
if (process.stdout.isTTY)
|
|
|
|
|
process.stdout.write('\u001B[2K');
|
|
|
|
|
if (!process.stdout.isTTY && this._needNewLine) {
|
|
|
|
|
this._needNewLine = false;
|
|
|
|
|
process.stdout.write('\n');
|
|
|
|
|
}
|
|
|
|
|
process.stdout.write(text);
|
|
|
|
|
// Go down if needed.
|
|
|
|
|
if (testRow !== this._lastRow) {
|
|
|
|
|
if (process.stdout.isTTY)
|
|
|
|
|
process.stdout.write(`\u001B[${this._lastRow - testRow}E`);
|
|
|
|
|
else
|
|
|
|
|
process.stdout.write('\n');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 19:55:46 +02:00
|
|
|
async onEnd(result: FullResult) {
|
|
|
|
|
await super.onEnd(result);
|
2021-06-07 02:09:53 +02:00
|
|
|
process.stdout.write('\n');
|
|
|
|
|
this.epilogue(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ListReporter;
|