Skip to content

Commit

Permalink
refactor: Standard code style and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jul 8, 2015
1 parent 2418163 commit c884155
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 111 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "standard"
}
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: node_js
node_js:
- "iojs"
- "0.12"
- "0.10"
- "0.8"

# Make sure we have new NPM.
before_install:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- npm install -g npm
- npm config set loglevel warn

before_script:
- npm install -g grunt-cli

script:
- grunt
29 changes: 0 additions & 29 deletions Gruntfile.coffee

This file was deleted.

5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# karma-junit-reporter

[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/karma-runner/karma-junit-reporter)
[![npm version](https://img.shields.io/npm/v/karma-junit-reporter.svg?style=flat-square)](https://www.npmjs.com/package/karma-junit-reporter) [![npm downloads](https://img.shields.io/npm/dm/karma-junit-reporter.svg?style=flat-square)](https://www.npmjs.com/package/karma-junit-reporter)

[![Build Status](https://img.shields.io/travis/karma-runner/karma-junit-reporter/master.svg?style=flat-square)](https://travis-ci.org/karma-runner/karma-junit-reporter) [![Dependency Status](https://img.shields.io/david/karma-runner/karma-junit-reporter.svg?style=flat-square)](https://david-dm.org/karma-runner/karma-junit-reporter) [![devDependency Status](https://img.shields.io/david/dev/karma-runner/karma-junit-reporter.svg?style=flat-square)](https://david-dm.org/karma-runner/karma-junit-reporter#info=devDependencies)

> Reporter for the JUnit XML format.
## Installation
Expand Down
38 changes: 38 additions & 0 deletions gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module.exports = function (grunt) {
grunt.initConfig({
pkgFile: 'package.json',
'npm-contributors': {
options: {
commitMessage: 'chore: update contributors'
}
},
bump: {
options: {
commitMessage: 'chore: release v%VERSION%',
pushTo: 'upstream',
commitFiles: [
'package.json'
]
}
},
eslint: {
target: [
'index.js',
'gruntfile.js'
]
}
})

require('load-grunt-tasks')(grunt)

grunt.registerTask('default', ['eslint'])

grunt.registerTask('release', 'Bump the version and publish to NPM.', function (type) {
grunt.task.run([
'npm-contributors',
'bump-only:' + (type || 'patch'),
'bump-commit',
'npm-publish'
])
})
}
155 changes: 77 additions & 78 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,134 +1,133 @@
var os = require('os');
var path = require('path');
var fs = require('fs');
var builder = require('xmlbuilder');
var os = require('os')
var path = require('path')
var fs = require('fs')
var builder = require('xmlbuilder')


var JUnitReporter = function(baseReporterDecorator, config, logger, helper, formatError) {
var log = logger.create('reporter.junit');
var reporterConfig = config.junitReporter || {};
var pkgName = reporterConfig.suite || '';
var JUnitReporter = function (baseReporterDecorator, config, logger, helper, formatError) {
var log = logger.create('reporter.junit')
var reporterConfig = config.junitReporter || {}
var pkgName = reporterConfig.suite || ''

if (!reporterConfig.outputDir) {
throw new Error('You must set an output directory for JUnitReporter via the outputDir config property');
throw new Error('You must set an output directory for JUnitReporter via the outputDir config property')
}

var outputDir = helper.normalizeWinPath(path.resolve(config.basePath, reporterConfig.outputDir)) + path.sep;
var suites;
var pendingFileWritings = 0;
var fileWritingFinished = function() {};
var allMessages = [];
var outputDir = helper.normalizeWinPath(path.resolve(config.basePath, reporterConfig.outputDir)) + path.sep
var suites
var pendingFileWritings = 0
var fileWritingFinished = function () {}
var allMessages = []

baseReporterDecorator(this);
baseReporterDecorator(this)

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

var initliazeXmlForBrowser = function(browser) {
var timestamp = (new Date()).toISOString().substr(0, 19);
var suite = suites[browser.id] = builder.create('testsuite');
var initliazeXmlForBrowser = function (browser) {
var timestamp = (new Date()).toISOString().substr(0, 19)
var suite = suites[browser.id] = builder.create('testsuite')
suite.att('name', browser.name)
.att('package', pkgName)
.att('timestamp', timestamp)
.att('id', 0)
.att('hostname', os.hostname());
.att('package', pkgName)
.att('timestamp', timestamp)
.att('id', 0)
.att('hostname', os.hostname())
suite.ele('properties')
.ele('property', {name: 'browser.fullName', value: browser.fullName});
};
.ele('property', {name: 'browser.fullName', value: browser.fullName})
}

var writeXmlForBrowser = function(browser) {
var outputFile = outputDir + browser.name.replace(/ /g, '_') + '.xml';
var xmlToOutput = suites[browser.id];
var writeXmlForBrowser = function (browser) {
var outputFile = outputDir + browser.name.replace(/ /g, '_') + '.xml'
var xmlToOutput = suites[browser.id]

pendingFileWritings++;
helper.mkdirIfNotExists(outputDir, function() {
fs.writeFile(outputFile, xmlToOutput.end({pretty: true}), function(err) {
pendingFileWritings++
helper.mkdirIfNotExists(outputDir, function () {
fs.writeFile(outputFile, xmlToOutput.end({pretty: true}), function (err) {
if (err) {
log.warn('Cannot write JUnit xml\n\t' + err.message);
log.warn('Cannot write JUnit xml\n\t' + err.message)
} else {
log.debug('JUnit results written to "%s".', outputFile);
log.debug('JUnit results written to "%s".', outputFile)
}

if (!--pendingFileWritings) {
fileWritingFinished();
fileWritingFinished()
}
});
});
};
})
})
}

this.onRunStart = function(browsers) {
suites = Object.create(null);
this.onRunStart = function (browsers) {
suites = Object.create(null)

// TODO(vojta): remove once we don't care about Karma 0.10
browsers.forEach(initliazeXmlForBrowser);
};
browsers.forEach(initliazeXmlForBrowser)
}

this.onBrowserStart = function(browser) {
initliazeXmlForBrowser(browser);
};
this.onBrowserStart = function (browser) {
initliazeXmlForBrowser(browser)
}

this.onBrowserComplete = function(browser) {
var suite = suites[browser.id];
this.onBrowserComplete = function (browser) {
var suite = suites[browser.id]

if (!suite) {
// This browser did not signal `onBrowserStart`. That happens
// if the browser timed out during the start phase.
return;
return
}

var result = browser.lastResult;
var result = browser.lastResult

suite.att('tests', result.total);
suite.att('errors', result.disconnected || result.error ? 1 : 0);
suite.att('failures', result.failed);
suite.att('time', (result.netTime || 0) / 1000);
suite.att('tests', result.total)
suite.att('errors', result.disconnected || result.error ? 1 : 0)
suite.att('failures', result.failed)
suite.att('time', (result.netTime || 0) / 1000)

if (result.disconnected) {
suite.ele('error').att('message', 'Browser disconnected');
suite.ele('error').att('message', 'Browser disconnected')
}

suite.ele('system-out').dat(allMessages.join() + '\n');
suite.ele('system-err');
suite.ele('system-out').dat(allMessages.join() + '\n')
suite.ele('system-err')

writeXmlForBrowser(browser);
};
writeXmlForBrowser(browser)
}

this.onRunComplete = function() {
suites = null;
allMessages.length = 0;
};
this.onRunComplete = function () {
suites = null
allMessages.length = 0
}

this.specSuccess = this.specSkipped = this.specFailure = function(browser, result) {
this.specSuccess = this.specSkipped = this.specFailure = function (browser, result) {
var spec = suites[browser.id].ele('testcase', {
name: result.description, time: ((result.time || 0) / 1000),
classname: (pkgName ? pkgName + ' ' : '') + result.suite.join(' ').replace(/\./g, '_')
});
})

if (result.skipped) {
spec.ele('skipped');
spec.ele('skipped')
}

if (!result.success) {
result.log.forEach(function(err) {
spec.ele('failure', {type: ''}, formatError(err));
});
result.log.forEach(function (err) {
spec.ele('failure', {type: ''}, formatError(err))
})
}
};
}

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

JUnitReporter.$inject = ['baseReporterDecorator', 'config', 'logger', 'helper', 'formatError'];
JUnitReporter.$inject = ['baseReporterDecorator', 'config', 'logger', 'helper', 'formatError']

// PUBLISH DI MODULE
module.exports = {
'reporter:junit': ['type', JUnitReporter]
};
}
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
},
"license": "MIT",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-npm": "~0.0.2",
"grunt-bump": "~0.0.7",
"grunt-auto-release": "~0.0.2"
"eslint": "^0.24.0",
"eslint-config-standard": "^3.4.1",
"grunt": "^0.4.1",
"grunt-bump": "^0.3.1",
"grunt-eslint": "^16.0.0",
"grunt-npm": "^0.0.2",
"load-grunt-tasks": "^3.2.0"
},
"contributors": [
"Friedel Ziegelmayer <[email protected]>",
Expand Down

0 comments on commit c884155

Please sign in to comment.