Skip to content

Commit

Permalink
feat(logging): Send color option to server
Browse files Browse the repository at this point in the history
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 karma-runner#1067
  • Loading branch information
budde377 committed Jan 20, 2016
1 parent 2d29165 commit 486c4f3
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/middleware/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
6 changes: 4 additions & 2 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
14 changes: 10 additions & 4 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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)
}
})
}

Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/reporters/dots.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/reporters/dots_color.js
Original file line number Diff line number Diff line change
@@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions lib/reporters/progress.js
Original file line number Diff line number Diff line change
@@ -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())
Expand Down
4 changes: 2 additions & 2 deletions lib/reporters/progress_color.js
Original file line number Diff line number Diff line change
@@ -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)
}

Expand Down
3 changes: 2 additions & 1 deletion lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ exports.run = function (config, done) {
removedFiles: config.removedFiles,
changedFiles: config.changedFiles,
addedFiles: config.addedFiles,
refresh: config.refresh
refresh: config.refresh,
colors: config.colors
}))
}
30 changes: 29 additions & 1 deletion test/unit/reporters/base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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
})

Expand All @@ -29,6 +29,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)

Expand Down

0 comments on commit 486c4f3

Please sign in to comment.