Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Tabuenca committed Oct 1, 2013
0 parents commit 1eeaf00
Show file tree
Hide file tree
Showing 10 changed files with 445 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.idea
.iml
*.swp
*.~
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.*
junit-schema.xsd
Gruntfile.coffee
CONTRIBUTING.md
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Please see the [Contributing to Karma] guide for information on contributing to this project.

[Contributing to Karma]: https://github.com/karma-runner/karma/blob/master/CONTRIBUTING.md
29 changes: 29 additions & 0 deletions Gruntfile.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = (grunt) ->

# Project configuration.
grunt.initConfig
pkgFile: 'package.json'

'npm-contributors':
options:
commitMessage: 'chore: update contributors'

bump:
options:
commitMessage: 'chore: release v%VERSION%'
pushTo: 'upstream'

'auto-release':
options:
checkTravisBuild: false

grunt.loadNpmTasks 'grunt-npm'
grunt.loadNpmTasks 'grunt-bump'
grunt.loadNpmTasks 'grunt-auto-release'

grunt.registerTask 'release', 'Bump the version and publish to NPM.', (type) ->
grunt.task.run [
'npm-contributors',
"bump:#{type||'patch'}",
'npm-publish'
]
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License

Copyright (C) 2011-2013 Vojta Jína and contributors.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# karma-html-reporter

> Reporter that formats results in HTML similar to jasmine.
## Installation

The easiest way is to keep `karma-html-reporter` as a devDependency in your `package.json`.
```json
{
"devDependencies": {
"karma": "~0.10",
"karma-html-reporter": "~0.1"
}
}
```

You can simple do it by:
```bash
npm install karma-html-reporter --save-dev
```

## Configuration
```js
// karma.conf.js
module.exports = function(config) {
config.set({
reporters: ['progress', 'html'],

// the default configuration
htmlReporter: {
outputDir: 'karma_html',
templatePath: __dirname+'/jasmine_template.html'
}
});
};
```

You can pass list of reporters as a CLI argument too:
```bash
karma start --reporters html,dots
```

----

For more information on Karma see the [homepage].


[homepage]: http://karma-runner.github.com
179 changes: 179 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
var os = require('os');
var path = require('path');
var fs = require('fs');
var _ = require('lodash');
var mu = require('mu2');


var HtmlReporter = function(baseReporterDecorator, config, emitter, logger, helper, formatError) {
config = config || {};
var pkgName = config.suite;
var log = logger.create('reporter.html');

var browserResults = {};
var allMessages = [];
var pendingFileWritings = 0;
var fileWritingFinished = function() {
};

baseReporterDecorator(this);

this.adapters = [function(msg) {
allMessages.push(msg);
}];

this.onRunStart = function(browsers) {
var browserInfo;
var timestamp = (new Date()).toISOString().substr(0, 19);
allMessages = [];
browsers.forEach(function(browser) {
browserInfo = browserResults[browser.id] = {
browserName : browser.name,
browserFullName : browser.fullName,
'package' : pkgName,
timestamp : timestamp,
hostname : os.hostname(),
suites : {}
};
});
};

this.onBrowserComplete = function(browser) {
var browserResult = browserResults[browser.id];
browserResult.results = browser.lastResult;
browserResult.output = allMessages;
};

this.onRunComplete = function(browsers) {
pendingFileWritings = browsers.length
browsers.forEach(function(browser) {
var results = browserResults[browser.id]

prepareResults(results);
//console.log(JSON.stringify(results,null, 4));
var outputDir = config.outputDir || 'karma_html';
var templatePath = config.templatePath || __dirname + "/jasmine_template.html";
var template = mu.compileAndRender(templatePath, results);
template.pause();
var reportFile = outputDir + '/' + results.browserName + '/index.html';
var writeStream;
helper.mkdirIfNotExists(path.dirname(reportFile), function() {

writeStream = fs.createWriteStream(reportFile, function(err) {
if (err) {
log.warn('Cannot write HTML Report\n\t' + err.message);
} else {
log.debug('HTML report written to "%s".', reportFile);
}


});
template.pipe(writeStream);
template.resume();
});
template.on("end", function() {
if (!--pendingFileWritings) {
fileWritingFinished();
}
template = null;
});

});
}; //HtmlReporter


this.specSuccess = this.specSkipped = this.specFailure = function(browser, result) {
var suite = getOrCreateSuite(browser, result);
suite.specs.push(result);
};

// wait for writing all the xml files, before exiting
emitter.on('exit', function(done) {
if (pendingFileWritings) {
fileWritingFinished = done;
} else {
done();
}
});

function getOrCreateSuite(browser, result) {
var suites = browserResults[browser.id].suites;
var suiteKey = result.suite.join(" ");
if (suites[suiteKey] === undefined) {
return suites[suiteKey] = { specs : [] };
}
else {
return suites[suiteKey];
}
}

function prepareResults(browser) {
browser.suites = suitesToArray(browser.suites);
var results = browser.results;
results.hasSuccess = results.success > 0;
results.hasFailed = results.failed > 0;
results.hasSkipped = results.skipped > 0;
browser.failedSuites = getFailedSuites(browser.suites);
return browser;
}

function suitesToArray(suites) {
return _.map(suites, function(suite, suiteName) {
var specs = transformSpecs(suite.specs);
var overallState = getOverallState(specs);
return { name : suiteName, state : overallState, specs : transformSpecs(suite.specs)};
});
}

function transformSpecs(specs) {
return _.map(specs, function(spec) {
var newSpec = _.clone(spec);
if (spec.skipped) {
newSpec.state = "skipped";
}
else if (spec.success) {
newSpec.state = "passed";
}
else {
newSpec.state = "failed";
}
return newSpec;
});
}

function getOverallState(specs) {
if (_.any(specs, function(spec) {
return spec.state === "failed"
})) {
return "failed";
}
else {
return "passed";
}
}

function getFailedSuites(suites) {
return _.filter(suites,function(suite) {
return suite.state === "failed"
}).map(function(suite) {
var newSuite = _.clone(suite);
newSuite.specs = getFailedSpecs(suite.specs);
return newSuite;
});
}

function getFailedSpecs(specs) {
return _.filter(specs, function(spec) {
return spec.state === "failed";
});
}


};

HtmlReporter.$inject = ['baseReporterDecorator', 'config.htmlReporter', 'emitter', 'logger', 'helper', 'formatError'];

// PUBLISH DI MODULE
module.exports = {
'reporter:html' : ['type', HtmlReporter]
};
Loading

0 comments on commit 1eeaf00

Please sign in to comment.