diff --git a/lib/cli.js b/lib/cli.js index 9d6c774c7..3a9e98823 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -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', ' 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 () { diff --git a/lib/init.js b/lib/init.js index 51e1eed7e..a257e05b2 100755 --- a/lib/init.js +++ b/lib/init.js @@ -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') @@ -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) 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..22a21de88 100644 --- a/lib/reporter.js +++ b/lib/reporter.js @@ -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. } } @@ -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 = { @@ -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) { @@ -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.') } }) diff --git a/lib/reporters/base.js b/lib/reporters/base.js index 9316c766e..28352c755 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,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) + } }) } @@ -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' @@ -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 diff --git a/lib/reporters/dots.js b/lib/reporters/dots.js index a0369cacf..e4aac0515 100644 --- a/lib/reporters/dots.js +++ b/lib/reporters/dots.js @@ -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 diff --git a/lib/reporters/dots_color.js b/lib/reporters/dots_color.js index ab42637cb..3da3ffdb3 100644 --- a/lib/reporters/dots_color.js +++ b/lib/reporters/dots_color.js @@ -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 diff --git a/lib/reporters/progress.js b/lib/reporters/progress.js index 9b5f21086..a20a166b8 100644 --- a/lib/reporters/progress.js +++ b/lib/reporters/progress.js @@ -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()) diff --git a/lib/reporters/progress_color.js b/lib/reporters/progress_color.js index 7d59f783e..bd607fa7b 100644 --- a/lib/reporters/progress_color.js +++ b/lib/reporters/progress_color.js @@ -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 diff --git a/lib/server.js b/lib/server.js index e7f496264..739ac8617 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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() diff --git a/package.json b/package.json index 86c58a339..49ec21278 100644 --- a/package.json +++ b/package.json @@ -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": "*", @@ -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": "*", diff --git a/test/e2e/mocharepoter.feature b/test/e2e/mocharepoter.feature new file mode 100644 index 000000000..92261509f --- /dev/null +++ b/test/e2e/mocharepoter.feature @@ -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 + """ \ No newline at end of file diff --git a/test/e2e/steps/core_steps.js b/test/e2e/steps/core_steps.js index f8bd2d6c4..de79e6f6d 100644 --- a/test/e2e/steps/core_steps.js +++ b/test/e2e/steps/core_steps.js @@ -154,7 +154,7 @@ module.exports = function coreSteps () { return callback() } - if (actualOutput.indexOf(expectedOutput) >= 0) { + if (actualOutput.indexOf(expectedOutput) === 0) { return callback() } diff --git a/test/e2e/support/mocha/plus.js b/test/e2e/support/mocha/plus.js new file mode 100644 index 000000000..d7fd9e84e --- /dev/null +++ b/test/e2e/support/mocha/plus.js @@ -0,0 +1,5 @@ +/* eslint-disable no-unused-vars */ +// Some code under test +function plus (a, b) { + return a + b +} diff --git a/test/e2e/support/mocha/test.js b/test/e2e/support/mocha/test.js new file mode 100644 index 000000000..2f0dde3ab --- /dev/null +++ b/test/e2e/support/mocha/test.js @@ -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) + }) +}) diff --git a/test/unit/reporters/base.spec.js b/test/unit/reporters/base.spec.js index 5c5b16496..e2c5dcd01 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,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)