From c884155ad1b6ee561d311253aff0e52a825e9519 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Wed, 8 Jul 2015 22:41:17 +0200 Subject: [PATCH] refactor: Standard code style and cleanup --- .eslintrc | 3 + .travis.yml | 19 ++++++ Gruntfile.coffee | 29 --------- README.md | 5 ++ gruntfile.js | 38 ++++++++++++ index.js | 155 +++++++++++++++++++++++------------------------ package.json | 11 ++-- 7 files changed, 149 insertions(+), 111 deletions(-) create mode 100644 .eslintrc create mode 100644 .travis.yml delete mode 100644 Gruntfile.coffee create mode 100644 gruntfile.js diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..e3578aa --- /dev/null +++ b/.eslintrc @@ -0,0 +1,3 @@ +{ + "extends": "standard" +} diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..24222e3 --- /dev/null +++ b/.travis.yml @@ -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 diff --git a/Gruntfile.coffee b/Gruntfile.coffee deleted file mode 100644 index a264a24..0000000 --- a/Gruntfile.coffee +++ /dev/null @@ -1,29 +0,0 @@ -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' - ] diff --git a/README.md b/README.md index 76a1304..875506e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/gruntfile.js b/gruntfile.js new file mode 100644 index 0000000..e585880 --- /dev/null +++ b/gruntfile.js @@ -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' + ]) + }) +} diff --git a/index.js b/index.js index 97125f1..8b3fd5e 100644 --- a/index.js +++ b/index.js @@ -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] -}; +} diff --git a/package.json b/package.json index de1d560..2833756 100644 --- a/package.json +++ b/package.json @@ -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 ",