diff --git a/README.md b/README.md index 3c3cf1e..496b27d 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,43 @@ If no filename is given, it will write the output to the console. **Description:** This will be used to output coverage reports. When you set a relative path, the directory is resolved against the `basePath`. +#### subdir +**Type:** String + +**Description**: This will be used in complement of the `coverageReporter.dir` +option to generate the full output directory path. By default, the output +directory is set to `./config.dir/BROWSER_NAME/`, this option allows you to +custom the second part. You can either pass a string or a function which will be +called with the `browser` passed as the only argument. + +```javascript +coverageReporter: { + dir: 'coverage', + subdir: '.' + // Would output the results into: .'/coverage/' +} +``` + +```javascript +coverageReporter: { + dir: 'coverage', + subdir: 'report' + // Would output the results into: .'/coverage/report/' +} +``` + +```javascript +coverageReporter: { + dir: 'coverage', + subdir: function(browser) { + // normalization process to keep a consistent browser name accross different + // OS + return browser.toLowerCase().split(/[ /-]/)[0]; + } + // Would output the results into: './coverage/firefox/' +} +``` + #### multiple reporters You can use multiple reporters, by providing array of options. diff --git a/lib/reporter.js b/lib/reporter.js index d08fa0e..7396afa 100644 --- a/lib/reporter.js +++ b/lib/reporter.js @@ -41,7 +41,6 @@ var CoverageReporter = function(rootConfig, helper, logger) { var log = logger.create('coverage'); var config = rootConfig.coverageReporter || {}; var basePath = rootConfig.basePath; - var outDir = helper.normalizeWinPath(path.resolve(basePath, config.dir || 'coverage')); var reporters = config.reporters; if (!helper.isDefined(reporters)) { @@ -63,6 +62,27 @@ var CoverageReporter = function(rootConfig, helper, logger) { } } + /** + * Generate the output directory from the `coverageReporter.dir` and + * `coverageReporter.subdir` options. + * + * @param {String} browserName - The browser name + * @param {String} dir - The given option + * @param {String|Function} subdir - The given option + * + * @return {String} - The output directory + */ + function generateOutputDir(browserName, dir, subdir) { + dir = dir || 'coverage'; + subdir = subdir || browserName; + + if (typeof subdir === 'function') { + subdir = subdir(browserName); + } + + return path.join(dir, subdir); + } + this.onRunStart = function(browsers) { collectors = Object.create(null); @@ -99,15 +119,17 @@ var CoverageReporter = function(rootConfig, helper, logger) { this.onRunComplete = function(browsers) { reporters.forEach(function(reporterConfig) { browsers.forEach(function(browser) { + var collector = collectors[browser.id]; if (collector) { pendingFileWritings++; - var reporterOutDir = helper.isDefined(reporterConfig.dir) ? helper.normalizeWinPath(path.resolve(basePath, reporterConfig.dir)) : outDir, - out = path.resolve(reporterOutDir, browser.name); - helper.mkdirIfNotExists(out, function() { - log.debug('Writing coverage to %s', out); + + var outputDir = helper.normalizeWinPath(path.resolve(basePath, generateOutputDir(browser.name, config.dir, config.subdir))); + + helper.mkdirIfNotExists(outputDir, function() { + log.debug('Writing coverage to %s', outputDir); var options = helper.merge({}, reporterConfig, { - dir : out, + dir : outputDir, sourceStore : new BasePathStore({ basePath : basePath }) @@ -121,6 +143,7 @@ var CoverageReporter = function(rootConfig, helper, logger) { writeEnd(); }); } + }); }); }; diff --git a/package.json b/package.json index 18495cd..d7b2eb2 100644 --- a/package.json +++ b/package.json @@ -29,17 +29,18 @@ }, "license": "MIT", "devDependencies": { - "mocks": "0.0.11", + "chai": "~1.7.2", "grunt": "~0.4.1", - "grunt-simple-mocha": "~0.4.0", + "grunt-auto-release": "~0.0.1", + "grunt-bump": "~0.0.7", "grunt-contrib-jshint": "~0.6.0", + "grunt-npm": "~0.0.2", + "grunt-simple-mocha": "~0.4.0", "karma": "~0.12.0", + "lodash": "^2.4.1", + "mocks": "0.0.11", "sinon": "~1.7.3", - "chai": "~1.7.2", - "sinon-chai": "~2.4.0", - "grunt-bump": "~0.0.7", - "grunt-npm": "~0.0.2", - "grunt-auto-release": "~0.0.1" + "sinon-chai": "~2.4.0" }, "contributors": [ "Friedel Ziegelmayer ", diff --git a/test/reporter.spec.coffee b/test/reporter.spec.coffee index e5b6077..63050f4 100644 --- a/test/reporter.spec.coffee +++ b/test/reporter.spec.coffee @@ -2,6 +2,7 @@ # lib/reporters/Coverage.js module #============================================================================== describe 'reporter', -> + _ = require 'lodash' events = require 'events' path = require 'path' istanbul = require 'istanbul' @@ -140,3 +141,42 @@ describe 'reporter', -> mockMkdir.getCall(0).args[1]() expect(mockReportCreate).to.have.been.called expect(mockWriteReport).to.have.been.called + + it 'should support a string for the subdir option', -> + customConfig = _.merge {}, rootConfig, + subdir: 'test' + coverageReporter: + subdir: 'test' + + reporter = new m.CoverageReporter customConfig, mockHelper, mockLogger + reporter.onRunStart() + browsers.forEach (b) -> reporter.onBrowserStart b + + reporter.onRunComplete browsers + expect(mockMkdir).to.have.been.calledTwice + dir = customConfig.coverageReporter.dir + subdir = customConfig.coverageReporter.subdir + expect(mockMkdir.getCall(0).args[0]).to.deep.equal path.resolve('/base', dir, subdir) + expect(mockMkdir.getCall(1).args[0]).to.deep.equal path.resolve('/base', dir, subdir) + mockMkdir.getCall(0).args[1]() + expect(mockReportCreate).to.have.been.called + expect(mockWriteReport).to.have.been.called + + it 'should support a function for the subdir option', -> + customConfig = _.merge {}, rootConfig, + subdir: 'test' + coverageReporter: + subdir: (browserName) -> browserName.toLowerCase().split(/[ /-]/)[0] + + reporter = new m.CoverageReporter customConfig, mockHelper, mockLogger + reporter.onRunStart() + browsers.forEach (b) -> reporter.onBrowserStart b + + reporter.onRunComplete browsers + expect(mockMkdir).to.have.been.calledTwice + dir = customConfig.coverageReporter.dir + expect(mockMkdir.getCall(0).args[0]).to.deep.equal path.resolve('/base', dir, 'chrome') + expect(mockMkdir.getCall(1).args[0]).to.deep.equal path.resolve('/base', dir, 'opera') + mockMkdir.getCall(0).args[1]() + expect(mockReportCreate).to.have.been.called + expect(mockWriteReport).to.have.been.called