-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(testrunner): introduce pytest-style reporter
- Loading branch information
1 parent
2b3a1ae
commit 90aff69
Showing
9 changed files
with
278 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
import colors from 'colors/safe'; | ||
import milliseconds from 'ms'; | ||
import * as path from 'path'; | ||
import { Test, Suite, Configuration } from '../test'; | ||
import { BaseReporter } from './base'; | ||
import { RunnerConfig } from '../runnerConfig'; | ||
|
||
const cursorPrevLine = '\u001B[F'; | ||
const eraseLine = '\u001B[2K' | ||
|
||
type Row = { | ||
id: string; | ||
relativeFile: string; | ||
configuration: string; | ||
ordinal: number; | ||
track: string[]; | ||
total: number; | ||
failed: boolean; | ||
startTime: number; | ||
finishTime: number; | ||
}; | ||
|
||
export class PytestReporter extends BaseReporter { | ||
private _rows = new Map<string, Row>(); | ||
private _suiteIds = new Map<Suite, string>(); | ||
private _lastOrdinal = 0; | ||
private _total: number; | ||
private _processed = 0; | ||
private _totalRows: number; | ||
private _failed = false; | ||
private _throttler = new Throttler(250, () => this._repaint()); | ||
|
||
onBegin(config: RunnerConfig, rootSuite: Suite) { | ||
super.onBegin(config, rootSuite); | ||
this._total = rootSuite.total(); | ||
|
||
// We get job rows and up to 3 buffer rows for green suites. | ||
const jobs = Math.min(config.jobs, rootSuite.suites.length); | ||
this._totalRows = jobs + Math.min(jobs, 4); | ||
for (let i = 0; i < this._totalRows; ++i) | ||
process.stdout.write('\n'); | ||
|
||
for (const s of rootSuite.suites) { | ||
const relativeFile = path.relative(this.config.testDir, s.file); | ||
const configurationString = serializeConfiguration(s.configuration); | ||
const id = relativeFile + `::[${configurationString}]`; | ||
this._suiteIds.set(s, id); | ||
const row = { | ||
id, | ||
relativeFile, | ||
configuration: configurationString, | ||
ordinal: this._lastOrdinal++, | ||
track: [], | ||
total: s.total(), | ||
failed: false, | ||
startTime: 0, | ||
finishTime: 0, | ||
}; | ||
this._rows.set(id, row); | ||
} | ||
} | ||
|
||
onTest(test: Test) { | ||
super.onTest(test); | ||
const row = this._rows.get(this._id(test)); | ||
if (!row.startTime) | ||
row.startTime = Date.now(); | ||
} | ||
|
||
onPending(test: Test) { | ||
super.onPending(test); | ||
this._append(test, colors.yellow('∘')); | ||
} | ||
|
||
onPass(test: Test) { | ||
super.onPass(test); | ||
this._append(test, colors.green('✓')); | ||
} | ||
|
||
onFail(test: Test) { | ||
super.onFail(test); | ||
const title = test.duration >= test.timeout ? colors.red('T') : colors.red('F'); | ||
const row = this._append(test, title); | ||
row.failed = true; | ||
this._failed = true; | ||
} | ||
|
||
onEnd() { | ||
super.onEnd(); | ||
this._repaint(); | ||
if (this._failed) | ||
this.epilogue(); | ||
} | ||
|
||
private _append(test: Test, s: string): Row { | ||
++this._processed; | ||
const testId = this._id(test); | ||
const row = this._rows.get(testId); | ||
row.track.push(s); | ||
if (row.track.length === row.total) | ||
row.finishTime = Date.now(); | ||
this._throttler.schedule(); | ||
return row; | ||
} | ||
|
||
private _repaint() { | ||
const rowList = [...this._rows.values()]; | ||
const running = rowList.filter(r => r.startTime && !r.finishTime); | ||
const finished = rowList.filter(r => r.finishTime).sort((a, b) => b.finishTime - a.finishTime); | ||
const finishedToPrint = finished.slice(0, this._totalRows - running.length - 1); | ||
const lines = []; | ||
for (const row of finishedToPrint.concat(running)) { | ||
const remaining = row.total - row.track.length; | ||
const remainder = '·'.repeat(remaining); | ||
let title = row.relativeFile; | ||
if (row.finishTime) { | ||
if (row.failed) | ||
title = colors.red(row.relativeFile); | ||
else | ||
title = colors.green(row.relativeFile); | ||
} | ||
const configuration = ` [${colors.gray(row.configuration)}]`; | ||
lines.push(' ' + title + configuration + ' ' + row.track.join('') + colors.gray(remainder)); | ||
} | ||
|
||
const status = []; | ||
if (this._total - this._processed) | ||
status.push(colors.cyan(`${this._total - this._processed} remaining`)); | ||
if (this.passes.length) | ||
status.push(colors.green(`${this.passes.length} passed`)); | ||
if (this.pending.length) | ||
status.push(colors.yellow(`${this.pending.length} skipped`)); | ||
if (this.failures.length) | ||
status.push(colors.red(`${this.failures.length} failed`)); | ||
if (this.timeouts.length) | ||
status.push(colors.red(`${this.timeouts.length} timed out`)); | ||
status.push(colors.dim(`(${milliseconds(Date.now() - this.startTime)})`)); | ||
lines.push(''); | ||
lines.push(' ' + status.join(' ')); | ||
|
||
process.stdout.write((cursorPrevLine + eraseLine).repeat(this._totalRows)); | ||
process.stdout.write(lines.join('\n')); | ||
process.stdout.write('\n'.repeat(this._totalRows - lines.length + 1)); | ||
} | ||
|
||
private _id(test: Test): string { | ||
for (let suite = test.suite; suite; suite = suite.parent) { | ||
if (this._suiteIds.has(suite)) | ||
return this._suiteIds.get(suite); | ||
} | ||
return ''; | ||
} | ||
} | ||
|
||
function serializeConfiguration(configuration: Configuration): string { | ||
const tokens = []; | ||
for (const { name, value } of configuration) | ||
tokens.push(`${name}=${value}`); | ||
return tokens.join(', '); | ||
} | ||
|
||
class Throttler { | ||
private _timeout: number; | ||
private _callback: () => void; | ||
private _lastFire = 0; | ||
private _timer: NodeJS.Timeout | null = null; | ||
|
||
constructor(timeout: number, callback: () => void) { | ||
this._timeout = timeout; | ||
this._callback = callback; | ||
} | ||
|
||
schedule() { | ||
const time = Date.now(); | ||
const timeRemaining = this._lastFire + this._timeout - time; | ||
if (timeRemaining <= 0) { | ||
this._fire(); | ||
return; | ||
} | ||
if (!this._timer) | ||
this._timer = setTimeout(() => this._fire(), timeRemaining); | ||
} | ||
|
||
private _fire() { | ||
this._timer = null; | ||
this._lastFire = Date.now(); | ||
this._callback(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.