fix(reporters): truncate long test titles from the start (#15052)

Most useful information is at the end - test name, current step, retry.
We truncate the repetitive project + suites at the start.
This commit is contained in:
Dmitry Gozman 2022-06-22 17:03:54 -07:00 committed by GitHub
parent 141093a1cd
commit a46aaee6e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 51 deletions

View file

@ -106,13 +106,13 @@ export class BaseReporter implements Reporter {
this.result = result; this.result = result;
} }
protected fitToScreen(line: string, suffix?: string): string { protected fitToScreen(line: string, prefix?: string): string {
const ttyWidth = this._ttyWidthForTest || process.stdout.columns || 0; const ttyWidth = this._ttyWidthForTest || process.stdout.columns || 0;
if (!ttyWidth) { if (!ttyWidth) {
// Guard against the case where we cannot determine available width. // Guard against the case where we cannot determine available width.
return line; return line;
} }
return fitToWidth(line, ttyWidth, suffix); return fitToWidth(line, ttyWidth, prefix);
} }
protected generateStartingMessage() { protected generateStartingMessage() {
@ -428,26 +428,34 @@ function monotonicTime(): number {
return seconds * 1000 + (nanoseconds / 1000000 | 0); return seconds * 1000 + (nanoseconds / 1000000 | 0);
} }
const ansiRegex = new RegExp('[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))', 'g'); const ansiRegex = new RegExp('([\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~])))', 'g');
export function stripAnsiEscapes(str: string): string { export function stripAnsiEscapes(str: string): string {
return str.replace(ansiRegex, ''); return str.replace(ansiRegex, '');
} }
// Leaves enough space for the "suffix" to also fit. // Leaves enough space for the "prefix" to also fit.
function fitToWidth(line: string, width: number, suffix?: string): string { function fitToWidth(line: string, width: number, prefix?: string): string {
const suffixLength = suffix ? stripAnsiEscapes(suffix).length : 0; const prefixLength = prefix ? stripAnsiEscapes(prefix).length : 0;
width -= suffixLength; width -= prefixLength;
if (line.length <= width) if (line.length <= width)
return line; return line;
let m;
let ansiLen = 0; // Even items are plain text, odd items are control sequences.
ansiRegex.lastIndex = 0; const parts = line.split(ansiRegex);
while ((m = ansiRegex.exec(line)) !== null) { const taken: string[] = [];
const visibleLen = m.index - ansiLen; for (let i = parts.length - 1; i >= 0; i--) {
if (visibleLen >= width) if (i % 2) {
break; // Include all control sequences to preserve formatting.
ansiLen += m[0].length; taken.push(parts[i]);
} else {
let part = parts[i].substring(parts[i].length - width);
if (part.length < parts[i].length && part.length > 0) {
// Add ellipsis if we are truncating.
part = '\u2026' + part.substring(1);
}
taken.push(part);
width -= part.length;
}
} }
// Truncate and reset all colors. return taken.reverse().join('');
return line.substr(0, width + ansiLen) + '\u001b[0m';
} }

View file

@ -62,13 +62,14 @@ class LineReporter extends BaseReporter {
override onTestEnd(test: TestCase, result: TestResult) { override onTestEnd(test: TestCase, result: TestResult) {
super.onTestEnd(test, result); super.onTestEnd(test, result);
++this._current; ++this._current;
const retriesSuffix = this.totalTestCount < this._current ? ` (retries)` : ``; const retriesPrefix = this.totalTestCount < this._current ? ` (retries)` : ``;
const title = `[${this._current}/${this.totalTestCount}]${retriesSuffix} ${formatTestTitle(this.config, test)}`; const prefix = `[${this._current}/${this.totalTestCount}]${retriesPrefix} `;
const suffix = result.retry ? ` (retry #${result.retry})` : ''; const currentRetrySuffix = result.retry ? colors.yellow(` (retry #${result.retry})`) : '';
const title = formatTestTitle(this.config, test) + currentRetrySuffix;
if (process.env.PW_TEST_DEBUG_REPORTERS) if (process.env.PW_TEST_DEBUG_REPORTERS)
process.stdout.write(`${title + suffix}\n`); process.stdout.write(`${prefix + title}\n`);
else else
process.stdout.write(`\u001B[1A\u001B[2K${this.fitToScreen(title, suffix) + colors.yellow(suffix)}\n`); process.stdout.write(`\u001B[1A\u001B[2K${prefix + this.fitToScreen(title, prefix)}\n`);
if (!this.willRetry(test) && (test.outcome() === 'flaky' || test.outcome() === 'unexpected')) { if (!this.willRetry(test) && (test.outcome() === 'flaky' || test.outcome() === 'unexpected')) {
if (!process.env.PW_TEST_DEBUG_REPORTERS) if (!process.env.PW_TEST_DEBUG_REPORTERS)

View file

@ -52,9 +52,9 @@ class ListReporter extends BaseReporter {
process.stdout.write('\n'); process.stdout.write('\n');
this._lastRow++; this._lastRow++;
} }
const line = ' ' + colors.gray(formatTestTitle(this.config, test)); const prefix = ' ';
const suffix = this._retrySuffix(result); const line = colors.gray(formatTestTitle(this.config, test)) + this._retrySuffix(result);
process.stdout.write(this.fitToScreen(line, suffix) + suffix + '\n'); process.stdout.write(prefix + this.fitToScreen(line, prefix) + '\n');
} }
this._testRows.set(test, this._lastRow++); this._testRows.set(test, this._lastRow++);
} }
@ -74,7 +74,7 @@ class ListReporter extends BaseReporter {
return; return;
if (step.category !== 'test.step') if (step.category !== 'test.step')
return; return;
this._updateTestLine(test, ' ' + colors.gray(formatTestTitle(this.config, test, step)), this._retrySuffix(result)); this._updateTestLine(test, colors.gray(formatTestTitle(this.config, test, step)) + this._retrySuffix(result), ' ');
} }
onStepEnd(test: TestCase, result: TestResult, step: TestStep) { onStepEnd(test: TestCase, result: TestResult, step: TestStep) {
@ -82,7 +82,7 @@ class ListReporter extends BaseReporter {
return; return;
if (step.category !== 'test.step') if (step.category !== 'test.step')
return; return;
this._updateTestLine(test, ' ' + colors.gray(formatTestTitle(this.config, test, step.parent)), this._retrySuffix(result)); this._updateTestLine(test, colors.gray(formatTestTitle(this.config, test, step.parent)) + this._retrySuffix(result), ' ');
} }
private _dumpToStdio(test: TestCase | undefined, chunk: string | Buffer, stream: NodeJS.WriteStream) { private _dumpToStdio(test: TestCase | undefined, chunk: string | Buffer, stream: NodeJS.WriteStream) {
@ -100,48 +100,52 @@ class ListReporter extends BaseReporter {
override onTestEnd(test: TestCase, result: TestResult) { override onTestEnd(test: TestCase, result: TestResult) {
super.onTestEnd(test, result); super.onTestEnd(test, result);
let duration = colors.dim(` (${milliseconds(result.duration)})`);
const title = formatTestTitle(this.config, test); const title = formatTestTitle(this.config, test);
let prefix = '';
let text = ''; let text = '';
if (result.status === 'skipped') { if (result.status === 'skipped') {
text = colors.green(' - ') + colors.cyan(title); prefix = colors.green(' - ');
duration = ''; // Do not show duration for skipped. // Do not show duration for skipped.
text = colors.cyan(title) + this._retrySuffix(result);
} else { } else {
const statusMark = (' ' + (result.status === 'passed' ? POSITIVE_STATUS_MARK : NEGATIVE_STATUS_MARK)).padEnd(5); const statusMark = (' ' + (result.status === 'passed' ? POSITIVE_STATUS_MARK : NEGATIVE_STATUS_MARK)).padEnd(5);
if (result.status === test.expectedStatus) if (result.status === test.expectedStatus) {
text = colors.green(statusMark) + colors.gray(title); prefix = colors.green(statusMark);
else text = colors.gray(title);
text = colors.red(statusMark + title); } else {
prefix = colors.red(statusMark);
text = colors.red(title);
}
text += this._retrySuffix(result) + colors.dim(` (${milliseconds(result.duration)})`);
} }
const suffix = this._retrySuffix(result) + duration;
if (this._liveTerminal) { if (this._liveTerminal) {
this._updateTestLine(test, text, suffix); this._updateTestLine(test, text, prefix);
} else { } else {
if (this._needNewLine) { if (this._needNewLine) {
this._needNewLine = false; this._needNewLine = false;
process.stdout.write('\n'); process.stdout.write('\n');
} }
process.stdout.write(text + suffix); process.stdout.write(prefix + text);
process.stdout.write('\n'); process.stdout.write('\n');
} }
} }
private _updateTestLine(test: TestCase, line: string, suffix: string) { private _updateTestLine(test: TestCase, line: string, prefix: string) {
if (process.env.PW_TEST_DEBUG_REPORTERS) if (process.env.PW_TEST_DEBUG_REPORTERS)
this._updateTestLineForTest(test, line, suffix); this._updateTestLineForTest(test, line, prefix);
else else
this._updateTestLineForTTY(test, line, suffix); this._updateTestLineForTTY(test, line, prefix);
} }
private _updateTestLineForTTY(test: TestCase, line: string, suffix: string) { private _updateTestLineForTTY(test: TestCase, line: string, prefix: string) {
const testRow = this._testRows.get(test)!; const testRow = this._testRows.get(test)!;
// Go up if needed // Go up if needed
if (testRow !== this._lastRow) if (testRow !== this._lastRow)
process.stdout.write(`\u001B[${this._lastRow - testRow}A`); process.stdout.write(`\u001B[${this._lastRow - testRow}A`);
// Erase line, go to the start // Erase line, go to the start
process.stdout.write('\u001B[2K\u001B[0G'); process.stdout.write('\u001B[2K\u001B[0G');
process.stdout.write(this.fitToScreen(line, suffix) + suffix); process.stdout.write(prefix + this.fitToScreen(line, prefix));
// Go down if needed. // Go down if needed.
if (testRow !== this._lastRow) if (testRow !== this._lastRow)
process.stdout.write(`\u001B[${this._lastRow - testRow}E`); process.stdout.write(`\u001B[${this._lastRow - testRow}E`);
@ -151,9 +155,9 @@ class ListReporter extends BaseReporter {
return (result.retry ? colors.yellow(` (retry #${result.retry})`) : ''); return (result.retry ? colors.yellow(` (retry #${result.retry})`) : '');
} }
private _updateTestLineForTest(test: TestCase, line: string, suffix: string) { private _updateTestLineForTest(test: TestCase, line: string, prefix: string) {
const testRow = this._testRows.get(test)!; const testRow = this._testRows.get(test)!;
process.stdout.write(testRow + ' : ' + line + suffix + '\n'); process.stdout.write(testRow + ' : ' + prefix + line + '\n');
} }
override async onEnd(result: FullResult) { override async onEnd(result: FullResult) {

View file

@ -113,7 +113,7 @@ test('should truncate long test names', async ({ runInlineTest }) => {
`, `,
'a.test.ts': ` 'a.test.ts': `
const { test } = pwt; const { test } = pwt;
test('fails very long name', async ({}) => { test('failure in very long name', async ({}) => {
expect(1).toBe(0); expect(1).toBe(0);
}); });
test('passes', async ({}) => { test('passes', async ({}) => {
@ -126,12 +126,12 @@ test('should truncate long test names', async ({ runInlineTest }) => {
}, { reporter: 'list', retries: 0 }, { PWTEST_TTY_WIDTH: 50 }); }, { reporter: 'list', retries: 0 }, { PWTEST_TTY_WIDTH: 50 });
const text = stripAnsi(result.output); const text = stripAnsi(result.output);
expect(text).toContain(`${NEGATIVE_STATUS_MARK} [foo] a.test.ts:6:7 fails very`); expect(text).toContain(`${NEGATIVE_STATUS_MARK} …st.ts:6:7 failure in very long name (`);
expect(text).not.toContain(`${NEGATIVE_STATUS_MARK} [foo] a.test.ts:6:7 fails very long name (`); expect(text).not.toContain(`${NEGATIVE_STATUS_MARK} est.ts:6:7 fails very long name (`);
expect(text).toContain(`${POSITIVE_STATUS_MARK} [foo] a.test.ts:9:7 passes (`); expect(text).toContain(`${POSITIVE_STATUS_MARK} [foo] a.test.ts:9:7 passes (`);
expect(text).toContain(`${POSITIVE_STATUS_MARK} [foo] a.test.ts:11:7 passes 2 long`); expect(text).toContain(`${POSITIVE_STATUS_MARK} a.test.ts:11:7 passes 2 long name (`);
expect(text).not.toContain(`${POSITIVE_STATUS_MARK} [foo] a.test.ts:11:7 passes 2 long name (`); expect(text).not.toContain(`${POSITIVE_STATUS_MARK} ] a.test.ts:11:7 passes 2 long name (`);
expect(text).toContain(`- [foo] a.test.ts:13:12 skipped very long n`); expect(text).toContain(`- …] a.test.ts:13:12 skipped very long nam`);
expect(text).not.toContain(`- [foo] a.test.ts:13:12 skipped very long na`); expect(text).not.toContain(`- …o] a.test.ts:13:12 skipped very long nam`);
expect(result.exitCode).toBe(1); expect(result.exitCode).toBe(1);
}); });