From 287d0dba26eafd77b8d2a10458d3ceffd71a97a7 Mon Sep 17 00:00:00 2001 From: Christian Budde Christensen Date: Thu, 7 Jan 2016 21:01:13 +0100 Subject: [PATCH] feat(logging): Send color option to server Delay the decision on color/no-color reporter to write. This is done by letting the individual adapter decide the color and providing a default color to the base adapter. Closing #1067 --- lib/middleware/runner.js | 4 ++-- lib/reporter.js | 6 ++++-- lib/reporters/base.js | 14 ++++++++++---- lib/reporters/dots.js | 4 ++-- lib/reporters/dots_color.js | 4 ++-- lib/reporters/progress.js | 4 ++-- lib/reporters/progress_color.js | 4 ++-- test/unit/reporters/base.spec.js | 30 +++++++++++++++++++++++++++++- 8 files changed, 53 insertions(+), 17 deletions(-) diff --git a/lib/middleware/runner.js b/lib/middleware/runner.js index 06334fc44..7c0d13dbc 100644 --- a/lib/middleware/runner.js +++ b/lib/middleware/runner.js @@ -33,9 +33,10 @@ var createRunnerMiddleware = function (emitter, fileList, capturedBrowsers, repo response.write('Waiting for previous execution...\n') } + var data = request.body emitter.once('run_start', function () { var responseWrite = response.write.bind(response) - + responseWrite.colors = data.colors reporter.addAdapter(responseWrite) // clean up, close runner response @@ -46,7 +47,6 @@ var createRunnerMiddleware = function (emitter, fileList, capturedBrowsers, repo }) }) - var data = request.body log.debug('Setting client.args to ', data.args) config.client.args = data.args diff --git a/lib/reporter.js b/lib/reporter.js index dab5574cb..8c62cfed1 100644 --- a/lib/reporter.js +++ b/lib/reporter.js @@ -91,8 +91,10 @@ var createReporters = function (names, config, emitter, injector) { // TODO(vojta): instantiate all reporters through DI names.forEach(function (name) { if (['dots', 'progress'].indexOf(name) !== -1) { - var Cls = require('./reporters/' + name + (config.colors ? '_color' : '')) - return reporters.push(new Cls(errorFormatter, config.reportSlowerThan)) + var Cls = require('./reporters/' + name) + var ClsColor = require('./reporters/' + name + '_color') + reporters.push(new Cls(errorFormatter, config.reportSlowerThan, config.colors)) + return reporters.push(new ClsColor(errorFormatter, config.reportSlowerThan, config.colors)) } var locals = { diff --git a/lib/reporters/base.js b/lib/reporters/base.js index 9316c766e..9aaefc612 100644 --- a/lib/reporters/base.js +++ b/lib/reporters/base.js @@ -2,7 +2,7 @@ var util = require('util') var helper = require('../helper') -var BaseReporter = function (formatError, reportSlow, adapter) { +var BaseReporter = function (formatError, reportSlow, useColors, adapter) { this.adapters = [adapter || process.stdout.write.bind(process.stdout)] this.onRunStart = function () { @@ -46,9 +46,15 @@ var BaseReporter = function (formatError, reportSlow, adapter) { this.write = function () { var msg = util.format.apply(null, Array.prototype.slice.call(arguments)) - + var self = this this.adapters.forEach(function (adapter) { - adapter(msg) + if (!helper.isDefined(adapter.colors)) { + adapter.colors = useColors + } + + if (adapter.colors === self.USE_COLORS) { + return adapter(msg) + } }) } @@ -136,7 +142,7 @@ BaseReporter.decoratorFactory = function (formatError, reportSlow) { } } -BaseReporter.decoratorFactory.$inject = ['formatError', 'config.reportSlowerThan'] +BaseReporter.decoratorFactory.$inject = ['formatError', 'config.reportSlowerThan', 'config.colors'] // PUBLISH module.exports = BaseReporter diff --git a/lib/reporters/dots.js b/lib/reporters/dots.js index a0369cacf..f06b094af 100644 --- a/lib/reporters/dots.js +++ b/lib/reporters/dots.js @@ -1,7 +1,7 @@ var BaseReporter = require('./base') -var DotsReporter = function (formatError, reportSlow) { - BaseReporter.call(this, formatError, reportSlow) +var DotsReporter = function (formatError, reportSlow, useColors) { + BaseReporter.call(this, formatError, reportSlow, useColors) var DOTS_WRAP = 80 diff --git a/lib/reporters/dots_color.js b/lib/reporters/dots_color.js index ab42637cb..a583857ca 100644 --- a/lib/reporters/dots_color.js +++ b/lib/reporters/dots_color.js @@ -1,8 +1,8 @@ var DotsReporter = require('./dots') var BaseColorReporter = require('./base_color') -var DotsColorReporter = function (formatError, reportSlow) { - DotsReporter.call(this, formatError, reportSlow) +var DotsColorReporter = function (formatError, reportSlow, useColors) { + DotsReporter.call(this, formatError, reportSlow, useColors) BaseColorReporter.call(this) } diff --git a/lib/reporters/progress.js b/lib/reporters/progress.js index 9b5f21086..7a39d8053 100644 --- a/lib/reporters/progress.js +++ b/lib/reporters/progress.js @@ -1,7 +1,7 @@ var BaseReporter = require('./base') -var ProgressReporter = function (formatError, reportSlow) { - BaseReporter.call(this, formatError, reportSlow) +var ProgressReporter = function (formatError, reportSlow, useColors) { + BaseReporter.call(this, formatError, reportSlow, useColors) this.writeCommonMsg = function (msg) { this.write(this._remove() + msg + this._render()) diff --git a/lib/reporters/progress_color.js b/lib/reporters/progress_color.js index 7d59f783e..b27f2e7eb 100644 --- a/lib/reporters/progress_color.js +++ b/lib/reporters/progress_color.js @@ -1,8 +1,8 @@ var ProgressReporter = require('./progress') var BaseColorReporter = require('./base_color') -var ProgressColorReporter = function (formatError, reportSlow) { - ProgressReporter.call(this, formatError, reportSlow) +var ProgressColorReporter = function (formatError, reportSlow, useColors) { + ProgressReporter.call(this, formatError, reportSlow, useColors) BaseColorReporter.call(this) } diff --git a/test/unit/reporters/base.spec.js b/test/unit/reporters/base.spec.js index 5c5b16496..367a457ce 100644 --- a/test/unit/reporters/base.spec.js +++ b/test/unit/reporters/base.spec.js @@ -15,7 +15,7 @@ describe('reporter', function () { beforeEach(function () { adapter = sinon.spy() - reporter = new m.BaseReporter(null, null, adapter) + reporter = new m.BaseReporter(null, null, false, adapter) return reporter }) @@ -28,6 +28,34 @@ describe('reporter', function () { return expect(anotherAdapter).to.have.been.calledWith('some') }) + it('should omit adapters not using the right color', function () { + var anotherAdapter = sinon.spy() + anotherAdapter.colors = true + reporter.adapters.push(anotherAdapter) + reporter.write('some') + expect(adapter).to.have.been.calledWith('some') + return expect(anotherAdapter).to.not.have.been.called + }) + + it('should not call non-colored adapters when wrong default setting', function () { + var reporter = new m.BaseReporter(null, null, true, adapter) + var anotherAdapter = sinon.spy() + reporter.adapters.push(anotherAdapter) + reporter.write('some') + expect(adapter).to.not.have.been.called + return expect(anotherAdapter).to.not.have.been.called + }) + + it('should call colored adapters regardless of default setting', function () { + var reporter = new m.BaseReporter(null, null, true, adapter) + var anotherAdapter = sinon.spy() + reporter.adapters.push(anotherAdapter) + adapter.colors = false + reporter.write('some') + expect(adapter).to.have.been.calledWith('some') + return expect(anotherAdapter).to.not.have.been.called + }) + it('should format', function () { reporter.write('Success: %d Failure: %d', 10, 20)