Skip to content

Commit

Permalink
Merge pull request #1902 from budde377/feature-fix-no-colors-to-run-c…
Browse files Browse the repository at this point in the history
…ommand

Feature fix no colors to run command
  • Loading branch information
dignifiedquire committed Feb 22, 2016
2 parents db4ada5 + 76cdd60 commit d3bec1e
Show file tree
Hide file tree
Showing 16 changed files with 157 additions and 41 deletions.
3 changes: 3 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ var describeRun = function () {
.describe('fail-on-empty-test-suite', 'Fail on empty test suite.')
.describe('no-fail-on-empty-test-suite', 'Do not fail on empty test suite.')
.describe('help', 'Print usage.')
.describe('log-level', '<disable | error | warn | info | debug> Level of logging.')
.describe('colors', 'Use colors when reporting and printing logs.')
.describe('no-colors', 'Do not use colors when reporting or printing logs.')
}

var describeStop = function () {
Expand Down
13 changes: 2 additions & 11 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var exec = require('child_process').exec

var helper = require('./helper')
var logger = require('./logger')
var constant = require('./constants')

var log = logger.create('init')

Expand Down Expand Up @@ -211,21 +210,13 @@ var processAnswers = function (answers, basePath, testMainFile) {
}

exports.init = function (config) {
var useColors = true
var logLevel = constant.LOG_INFO
logger.setupFromConfig(config)

var colorScheme = COLOR_SCHEME.ON

if (helper.isDefined(config.colors)) {
colorScheme = config.colors ? COLOR_SCHEME.ON : COLOR_SCHEME.OFF
useColors = config.colors
}

if (helper.isDefined(config.logLevel)) {
logLevel = config.logLevel
}

logger.setup(logLevel, useColors)

// need to be registered before creating readlineInterface
process.stdin.on('keypress', function (s, key) {
sm.onKeypress(key)
Expand Down
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
20 changes: 17 additions & 3 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var createErrorFormatter = function (basePath, emitter, SourceMapConsumer) {
original.line, original.column)
} catch (e) {
log.warn('SourceMap position not found for trace: %s', msg)
// Fall back to non-source-mapped formatting.
// Fall back to non-source-mapped formatting.
}
}

Expand All @@ -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 All @@ -101,6 +103,7 @@ var createReporters = function (names, config, emitter, injector) {
}

try {
log.debug('Trying to load reporter: %s', name)
reporters.push(injector.createChild([locals], ['reporter:' + name]).get('reporter:' + name))
} catch (e) {
if (e.message.indexOf('No provider for "reporter:' + name + '"') !== -1) {
Expand All @@ -109,6 +112,17 @@ var createReporters = function (names, config, emitter, injector) {
} else {
log.warn('Can not load "%s"!\n ' + e.stack, name)
}
return
}
var color_name = name + '_color'
if (names.indexOf(color_name) !== -1) {
return
}
try {
log.debug('Trying to load color-version of reporter: %s (%s)', name, color_name)
reporters.push(injector.createChild([locals], ['reporter:' + name + '_color']).get('reporter:' + name))
} catch (e) {
log.debug('Couldn\'t load color-version.')
}
})

Expand Down
19 changes: 12 additions & 7 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,14 @@ 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 (!helper.isDefined(self.EXCLUSIVELY_USE_COLORS) || adapter.colors === self.EXCLUSIVELY_USE_COLORS) {
return adapter(msg)
}
})
}

Expand Down Expand Up @@ -112,7 +117,7 @@ var BaseReporter = function (formatError, reportSlow, adapter) {
}

this.USE_COLORS = false

this.EXCLUSIVELY_USE_COLORS = undefined
this.LOG_SINGLE_BROWSER = '%s: %s\n'
this.LOG_MULTI_BROWSER = '%s %s: %s\n'

Expand All @@ -130,13 +135,13 @@ var BaseReporter = function (formatError, reportSlow, adapter) {
this.TOTAL_FAILED = 'TOTAL: %d FAILED, %d SUCCESS\n'
}

BaseReporter.decoratorFactory = function (formatError, reportSlow) {
BaseReporter.decoratorFactory = function (formatError, reportSlow, useColors) {
return function (self) {
BaseReporter.call(self, formatError, reportSlow)
BaseReporter.call(self, formatError, reportSlow, useColors)
}
}

BaseReporter.decoratorFactory.$inject = ['formatError', 'config.reportSlowerThan']
BaseReporter.decoratorFactory.$inject = ['formatError', 'config.reportSlowerThan', 'config.colors']

// PUBLISH
module.exports = BaseReporter
6 changes: 3 additions & 3 deletions lib/reporters/dots.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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

this.EXCLUSIVELY_USE_COLORS = false
this.onRunStart = function () {
this._browsers = []
this._dotsCount = 0
Expand Down
5 changes: 3 additions & 2 deletions lib/reporters/dots_color.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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)
this.EXCLUSIVELY_USE_COLORS = true
}

// PUBLISH
Expand Down
6 changes: 4 additions & 2 deletions lib/reporters/progress.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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.EXCLUSIVELY_USE_COLORS = false

this.writeCommonMsg = function (msg) {
this.write(this._remove() + msg + this._render())
Expand Down
5 changes: 3 additions & 2 deletions lib/reporters/progress_color.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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)
this.EXCLUSIVELY_USE_COLORS = true
}

// PUBLISH
Expand Down
8 changes: 1 addition & 7 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,11 @@ function createSocketIoServer (webServer, executor, config) {
return server
}

function setupLogger (level, colors) {
var logLevel = logLevel || constant.LOG_INFO
var logColors = helper.isDefined(colors) ? colors : true
logger.setup(logLevel, logColors, [constant.CONSOLE_APPENDER])
}

// Constructor
var Server = function (cliOptions, done) {
EventEmitter.call(this)

setupLogger(cliOptions.logLevel, cliOptions.colors)
logger.setupFromConfig(cliOptions)

this.log = logger.create()

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
"json3": "^3.3.2",
"karma-browserify": "^5.0.1",
"karma-browserstack-launcher": "^0.1.10",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "*",
"karma-coffee-preprocessor": "*",
"karma-commonjs": "*",
Expand All @@ -327,6 +328,7 @@
"karma-junit-reporter": "*",
"karma-live-preprocessor": "*",
"karma-mocha": "0.2.1",
"karma-mocha-reporter": "^1.2.0",
"karma-ng-scenario": "*",
"karma-phantomjs-launcher": "*",
"karma-qunit": "*",
Expand Down
48 changes: 48 additions & 0 deletions test/e2e/mocharepoter.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Feature: Mocha reporter
In order to use Karma
As a person who wants to write great tests
I want to be able to use the mocha reporter.

Scenario: Execute a test in PhantomJS with colors
Given a configuration with:
"""
files = ['mocha/plus.js', 'mocha/test.js'];
browsers = ['PhantomJS'];
frameworks = ['mocha', 'chai']
colors = true
plugins = [
'karma-jasmine',
'karma-phantomjs-launcher',
'karma-mocha-reporter',
'karma-mocha',
'karma-chai'
];
reporters = ['mocha'];
"""
When I start Karma
Then it passes with like:
"""
2 tests completed
"""

Scenario: Execute a test in PhantomJS with no-colors
Given a configuration with:
"""
files = ['mocha/plus.js', 'mocha/test.js'];
browsers = ['PhantomJS'];
frameworks = ['mocha', 'chai']
colors = false
plugins = [
'karma-jasmine',
'karma-phantomjs-launcher',
'karma-mocha-reporter',
'karma-mocha',
'karma-chai'
];
reporters = ['mocha'];
"""
When I start Karma
Then it passes with like:
"""
✔ 2 tests completed
"""
2 changes: 1 addition & 1 deletion test/e2e/steps/core_steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ module.exports = function coreSteps () {
return callback()
}

if (actualOutput.indexOf(expectedOutput) >= 0) {
if (actualOutput.indexOf(expectedOutput) === 0) {
return callback()
}

Expand Down
5 changes: 5 additions & 0 deletions test/e2e/support/mocha/plus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* eslint-disable no-unused-vars */
// Some code under test
function plus (a, b) {
return a + b
}
10 changes: 10 additions & 0 deletions test/e2e/support/mocha/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* globals plus */
describe('plus', function () {
it('should pass', function () {
expect(true).to.be.true
})

it('should work', function () {
expect(plus(1, 2)).to.equal(3)
})
})
42 changes: 41 additions & 1 deletion test/unit/reporters/base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand All @@ -28,6 +28,46 @@ 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.EXCLUSIVELY_USE_COLORS = false
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.EXCLUSIVELY_USE_COLORS = false
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)
reporter.EXCLUSIVELY_USE_COLORS = false
adapter.colors = false
reporter.write('some')
expect(adapter).to.have.been.calledWith('some')
return expect(anotherAdapter).to.not.have.been.called
})

it('should call all adapters if EXCLUSIVELY_USE_COLORS is undefined', function () {
var anotherAdapter = sinon.spy()
anotherAdapter.colors = true
reporter.adapters.push(anotherAdapter)
reporter.write('some')
expect(adapter).to.have.been.calledWith('some')
expect(anotherAdapter).to.have.been.calledWith('some')
})

it('should format', function () {
reporter.write('Success: %d Failure: %d', 10, 20)

Expand Down

0 comments on commit d3bec1e

Please sign in to comment.