Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature adding no colors to run command #1791

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,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 describeCompletion = 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
24 changes: 24 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ var setup = function (level, colors, appenders) {
})
}

// Setup the logger by passing in the config object. The function sets the
// `colors` and `logLevel` if they are defined. It takes two arguments:
//
// setupFromConfig(config, appenders)
//
// * `config`: *Object* The configuration object.
// * `appenders`: *Array* This will be passed as appenders to log4js
// to allow for fine grained configuration of log4js. For more information
// see https://github.com/nomiddlename/log4js-node.
var setupFromConfig = function (config, appenders) {
var useColors = true
var logLevel = constant.LOG_INFO

if (helper.isDefined(config.colors)) {
useColors = config.colors
}

if (helper.isDefined(config.logLevel)) {
logLevel = config.logLevel
}
setup(logLevel, useColors, appenders)
}

// Create a new logger. There are two optional arguments
// * `name`, which defaults to `karma` and
// If the `name = 'socket.io'` this will create a special wrapper
Expand All @@ -60,3 +83,4 @@ var create = function (name, level) {

exports.create = create
exports.setup = setup
exports.setupFromConfig = setupFromConfig
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
5 changes: 4 additions & 1 deletion lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ var parseExitCode = function (buffer, defaultCode, failOnEmptyTestSuite) {

// TODO(vojta): read config file (port, host, urlRoot)
exports.run = function (config, done) {
logger.setupFromConfig(config)

done = helper.isFunction(done) ? done : process.exit
config = cfg.parseConfig(config.configFile, config)

Expand Down Expand Up @@ -71,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
}))
}
8 changes: 1 addition & 7 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,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
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