From b11c1298f0af221e11da91f388411c2146bb4641 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Thu, 20 Feb 2020 22:49:12 -0800 Subject: [PATCH] address comments --- utils/testrunner/Matchers.js | 22 +++++----- utils/testrunner/Reporter.js | 77 +++++++++++++++++----------------- utils/testrunner/TestRunner.js | 4 +- utils/testrunner/utils.js | 2 +- 4 files changed, 53 insertions(+), 52 deletions(-) diff --git a/utils/testrunner/Matchers.js b/utils/testrunner/Matchers.js index 55976230dd..b9273ca509 100644 --- a/utils/testrunner/Matchers.js +++ b/utils/testrunner/Matchers.js @@ -14,8 +14,8 @@ * limitations under the License. */ -const {getLocation} = require('./utils.js'); -const c = require('colors/safe'); +const {getCallerLocation} = require('./utils.js'); +const colors = require('colors/safe'); const Diff = require('text-diff'); class Matchers { @@ -36,11 +36,11 @@ class Matchers { }; class MatchError extends Error { - constructor(message, formatter = null) { + constructor(message, formatter) { super(message); this.name = this.constructor.name; this.formatter = formatter; - this.location = getLocation(__filename); + this.location = getCallerLocation(__filename); Error.captureStackTrace(this, this.constructor); } } @@ -67,7 +67,7 @@ class Expect { } function defaultFormatter(received) { - return `Received: ${c.red(JSON.stringify(received))}`; + return `Received: ${colors.red(JSON.stringify(received))}`; } function stringFormatter(received, expected) { @@ -76,9 +76,9 @@ function stringFormatter(received, expected) { diff.cleanupSemantic(result); const highlighted = result.map(([type, text]) => { if (type === -1) - return c.bgRed(text); + return colors.bgRed(text); if (type === 1) - return c.bgGreen.black(text); + return colors.bgGreen.black(text); return text; }).join(''); const output = [ @@ -89,7 +89,7 @@ function stringFormatter(received, expected) { if (expected[i] !== received[i]) { const padding = ' '.repeat('Expected: '.length); const firstDiffCharacter = '~'.repeat(i) + '^'; - output.push(c.red(padding + firstDiffCharacter)); + output.push(colors.red(padding + firstDiffCharacter)); break; } } @@ -133,9 +133,9 @@ function objectFormatter(received, expected) { const highlighted = result.map(([type, text]) => { const lines = doDecodeLines(text); if (type === -1) - return lines.map(line => '- ' + c.bgRed(line)); + return lines.map(line => '- ' + colors.bgRed(line)); if (type === 1) - return lines.map(line => '+ ' + c.bgGreen.black(line)); + return lines.map(line => '+ ' + colors.bgGreen.black(line)); return lines.map(line => ' ' + line); }).flat().join('\n'); return `Received:\n${highlighted}`; @@ -147,7 +147,7 @@ function toBeFormatter(received, expected) { } return [ `Expected: ${JSON.stringify(expected)}`, - `Received: ${c.red(JSON.stringify(received))}`, + `Received: ${colors.red(JSON.stringify(received))}`, ].join('\n'); } diff --git a/utils/testrunner/Reporter.js b/utils/testrunner/Reporter.js index d758f5abe0..986c05d3a7 100644 --- a/utils/testrunner/Reporter.js +++ b/utils/testrunner/Reporter.js @@ -15,7 +15,7 @@ */ const fs = require('fs'); -const c = require('colors'); +const colors = require('colors'); const {MatchError} = require('./Matchers.js'); class Reporter { @@ -44,16 +44,17 @@ class Reporter { this._testCounter = 0; this._timestamp = Date.now(); const allTests = this._runner.tests(); - if (allTests.length === runnableTests.length) - console.log(`Running all ${c.yellow(runnableTests.length)} tests on ${c.yellow(this._runner.parallel())} worker${this._runner.parallel() > 1 ? 's' : ''}:\n`); - else - console.log(`Running ${c.yellow(runnableTests.length)} focused tests out of total ${c.yellow(allTests.length)} on ${c.yellow(this._runner.parallel())} worker${this._runner.parallel() > 1 ? 's' : ''}:\n`); + if (allTests.length === runnableTests.length) { + console.log(`Running all ${colors.yellow(runnableTests.length)} tests on ${colors.yellow(this._runner.parallel())} worker${this._runner.parallel() > 1 ? 's' : ''}:\n`); + } else { + console.log(`Running ${colors.yellow(runnableTests.length)} focused tests out of total ${colors.yellow(allTests.length)} on ${colors.yellow(this._runner.parallel())} worker${this._runner.parallel() > 1 ? 's' : ''}:\n`); + } } _printTermination(result, message, error) { - console.log(c.red(`## ${result.toUpperCase()} ##`)); + console.log(colors.red(`## ${result.toUpperCase()} ##`)); console.log('Message:'); - console.log(` ${c.red(message)}`); + console.log(` ${colors.red(message)}`); if (error && error.stack) { console.log('Stack:'); console.log(padLines(error.stack, 2)); @@ -65,21 +66,21 @@ class Reporter { const {isRunning, test} = this._workersState.get(workerId); let description = ''; if (isRunning) - description = c.yellow('RUNNING'); + description = colors.yellow('RUNNING'); else if (test.result === 'ok') - description = c.green('OK'); + description = colors.green('OK'); else if (test.result === 'skipped') - description = c.yellow('SKIPPED'); + description = colors.yellow('SKIPPED'); else if (test.result === 'failed') - description = c.red('FAILED'); + description = colors.red('FAILED'); else if (test.result === 'crashed') - description = c.red('CRASHED'); + description = colors.red('CRASHED'); else if (test.result === 'timedout') - description = c.red('TIMEDOUT'); + description = colors.red('TIMEDOUT'); else if (test.result === 'terminated') - description = c.magenta('TERMINATED'); + description = colors.magenta('TERMINATED'); else - description = c.red(''); + description = colors.red(''); console.log(` ${workerId}: [${description}] ${test.fullName} (${formatLocation(test.location)})`); } console.log(''); @@ -118,7 +119,7 @@ class Reporter { } if (this._showSkippedTests < skippedTests.length) { console.log(''); - console.log(`... and ${c.yellow(skippedTests.length - this._showSkippedTests)} more skipped tests ...`); + console.log(`... and ${colors.yellow(skippedTests.length - this._showSkippedTests)} more skipped tests ...`); } } @@ -132,7 +133,7 @@ class Reporter { for (let i = 0; i < slowTests.length; ++i) { const test = slowTests[i]; const duration = test.endTimestamp - test.startTimestamp; - console.log(` (${i + 1}) ${c.yellow((duration / 1000) + 's')} - ${test.fullName} (${formatLocation(test.location)})`); + console.log(` (${i + 1}) ${colors.yellow((duration / 1000) + 's')} - ${test.fullName} (${formatLocation(test.location)})`); } } @@ -141,18 +142,18 @@ class Reporter { const okTestsLength = executedTests.length - failedTests.length - skippedTests.length; let summaryText = ''; if (failedTests.length || skippedTests.length) { - const summary = [`ok - ${c.green(okTestsLength)}`]; + const summary = [`ok - ${colors.green(okTestsLength)}`]; if (failedTests.length) - summary.push(`failed - ${c.red(failedTests.length)}`); + summary.push(`failed - ${colors.red(failedTests.length)}`); if (skippedTests.length) - summary.push(`skipped - ${c.yellow(skippedTests.length)}`); + summary.push(`skipped - ${colors.yellow(skippedTests.length)}`); summaryText = ` (${summary.join(', ')})`; } console.log(`\nRan ${executedTests.length}${summaryText} of ${tests.length} test${tests.length > 1 ? 's' : ''}`); const milliseconds = Date.now() - this._timestamp; const seconds = milliseconds / 1000; - console.log(`Finished in ${c.yellow(seconds)} seconds`); + console.log(`Finished in ${colors.yellow(seconds)} seconds`); } _onTestStarted(test, workerId) { @@ -166,36 +167,36 @@ class Reporter { this._printVerboseTestResult(this._testCounter, test, workerId); } else { if (test.result === 'ok') - process.stdout.write(c.green('.')); + process.stdout.write(colors.green('.')); else if (test.result === 'skipped') - process.stdout.write(c.yellow('*')); + process.stdout.write(colors.yellow('*')); else if (test.result === 'failed') - process.stdout.write(c.red('F')); + process.stdout.write(colors.red('F')); else if (test.result === 'crashed') - process.stdout.write(c.red('C')); + process.stdout.write(colors.red('C')); else if (test.result === 'terminated') - process.stdout.write(c.magenta('.')); + process.stdout.write(colors.magenta('.')); else if (test.result === 'timedout') - process.stdout.write(c.red('T')); + process.stdout.write(colors.red('T')); } } _printVerboseTestResult(resultIndex, test, workerId = undefined) { let prefix = `${resultIndex})`; if (this._runner.parallel() > 1 && workerId !== undefined) - prefix += ' ' + c.gray(`[worker = ${workerId}]`); + prefix += ' ' + colors.gray(`[worker = ${workerId}]`); if (test.result === 'ok') { - console.log(`${prefix} ${c.green('[OK]')} ${test.fullName} (${formatLocation(test.location)})`); + console.log(`${prefix} ${colors.green('[OK]')} ${test.fullName} (${formatLocation(test.location)})`); } else if (test.result === 'terminated') { - console.log(`${prefix} ${c.magenta('[TERMINATED]')} ${test.fullName} (${formatLocation(test.location)})`); + console.log(`${prefix} ${colors.magenta('[TERMINATED]')} ${test.fullName} (${formatLocation(test.location)})`); } else if (test.result === 'crashed') { - console.log(`${prefix} ${c.red('[CRASHED]')} ${test.fullName} (${formatLocation(test.location)})`); + console.log(`${prefix} ${colors.red('[CRASHED]')} ${test.fullName} (${formatLocation(test.location)})`); } else if (test.result === 'skipped') { - console.log(`${prefix} ${c.yellow('[SKIP]')} ${test.fullName} (${formatLocation(test.location)})`); + console.log(`${prefix} ${colors.yellow('[SKIP]')} ${test.fullName} (${formatLocation(test.location)})`); } else if (test.result === 'timedout') { - console.log(`${prefix} ${c.red(`[TIMEOUT ${test.timeout}ms]`)} ${test.fullName} (${formatLocation(test.location)})`); + console.log(`${prefix} ${colors.red(`[TIMEOUT ${test.timeout}ms]`)} ${test.fullName} (${formatLocation(test.location)})`); } else if (test.result === 'failed') { - console.log(`${prefix} ${c.red('[FAIL]')} ${test.fullName} (${formatLocation(test.location)})`); + console.log(`${prefix} ${colors.red('[FAIL]')} ${test.fullName} (${formatLocation(test.location)})`); if (test.error instanceof MatchError) { let lines = this._filePathToLines.get(test.error.location.filePath); if (!lines) { @@ -212,13 +213,13 @@ class Reporter { const FROM = Math.max(test.location.lineNumber - 1, lineNumber - 5); const snippet = lines.slice(FROM, lineNumber).map((line, index) => ` ${(FROM + index + 1 + '').padStart(lineNumberLength, ' ')} | ${line}`).join('\n'); const pointer = ` ` + ' '.repeat(lineNumberLength) + ' ' + '~'.repeat(test.error.location.columnNumber - 1) + '^'; - console.log('\n' + snippet + '\n' + c.grey(pointer) + '\n'); + console.log('\n' + snippet + '\n' + colors.grey(pointer) + '\n'); } console.log(padLines(test.error.formatter(), 4)); console.log(''); } else { console.log(' Message:'); - console.log(` ${c.red(test.error.message || test.error)}`); + console.log(` ${colors.red(test.error.message || test.error)}`); if (test.error.stack) { console.log(' Stack:'); let stack = test.error.stack; @@ -227,7 +228,7 @@ class Reporter { if (match) { const [, line, column] = match; const fileName = `${test.location.fileName}:${line}:${column}`; - stack = stack.substring(0, match.index) + stack.substring(match.index).replace(fileName, c.yellow(fileName)); + stack = stack.substring(0, match.index) + stack.substring(match.index).replace(fileName, colors.yellow(fileName)); } console.log(padLines(stack, 4)); } @@ -243,7 +244,7 @@ class Reporter { function formatLocation(location) { if (!location) return ''; - return c.yellow(`${location.fileName}:${location.lineNumber}:${location.columnNumber}`); + return colors.yellow(`${location.fileName}:${location.lineNumber}:${location.columnNumber}`); } function padLines(text, spaces = 0) { diff --git a/utils/testrunner/TestRunner.js b/utils/testrunner/TestRunner.js index 2f018b4e8e..74793fc994 100644 --- a/utils/testrunner/TestRunner.js +++ b/utils/testrunner/TestRunner.js @@ -23,7 +23,7 @@ const Multimap = require('./Multimap'); const fs = require('fs'); const {SourceMapSupport} = require('./SourceMapSupport'); const debug = require('debug'); -const {getLocation} = require('./utils'); +const {getCallerLocation} = require('./utils'); const INFINITE_TIMEOUT = 2147483647; @@ -42,7 +42,7 @@ class UserCallback { }); this.timeout = timeout; - this.location = getLocation(__filename); + this.location = getCallerLocation(__filename); } async run(...args) { diff --git a/utils/testrunner/utils.js b/utils/testrunner/utils.js index 43e655646f..9934a7d7be 100644 --- a/utils/testrunner/utils.js +++ b/utils/testrunner/utils.js @@ -1,7 +1,7 @@ const path = require('path'); module.exports = { - getLocation: function(filename) { + getCallerLocation: function(filename) { const error = new Error(); const stackFrames = error.stack.split('\n').slice(1); // Find first stackframe that doesn't point to this file.