Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Fix issue #2 compatibility with node v6+ #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# /!\ DEPRECATED

# karma-jenkins-reporter

> Reporter for the JUnit XML format for Jenkins.
Expand Down
119 changes: 71 additions & 48 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,95 @@ var path = require('path');
var fs = require('fs');
var builder = require('xmlbuilder');

// concatenate test suite(s) and test description by default
function defaultNameFormatter(browser, result) {
return result.suite.join(' ') + ' ' + result.description
}

var JenkinsReporter = function(baseReporterDecorator, config, logger, helper, formatError) {
var JenkinsReporter = function (baseReporterDecorator, config, logger, helper, formatError) {
var log = logger.create('reporter.jenkins');
var reporterConfig = config.jenkinsReporter || {};
var pkgName = reporterConfig.suite || '';
var featurePath = process.env.REPORT_FILE;
var outputFile = helper.normalizeWinPath(path.resolve(config.basePath, reporterConfig.outputFile
|| 'test-results.xml'));
if (featurePath != null) {
outputFile = path.join(process.env.PREFIX || '', featurePath);
var outputFile = helper.normalizeWinPath(path.resolve(config.basePath, reporterConfig.outputFile || 'test-results.xml'));
var useBrowserName = reporterConfig.userBrowserName
if (typeof useBrowserName === 'undefined') {
useBrowserName = true
}

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

baseReporterDecorator(this);

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

var initliazeXmlForBrowser = function(browser) {
var initializeXmlForBrowser = function (browser) {
var timestamp = (new Date()).toISOString().substr(0, 19);
var suite = suites[browser.id] = xml.ele('testsuite', {
name: browser.name,
'package': pkgName,
timestamp: timestamp,
id: 0,
hostname: os.hostname(),
make_target: process.env.MAKE_TARGET
'name': browser.name,
'package': pkgName,
'timestamp': timestamp,
'id': 0,
'hostname': os.hostname(),
'make_target': process.env.MAKE_TARGET
})

suite.ele('properties').ele('property', {
name: 'browser.fullName',
value: browser.fullName
});
suite.ele('properties').ele('property', {name: 'browser.fullName', value: browser.fullName});
};

this.onRunStart = function(browsers) {
var getClassName = function (browser, result) {
var browserName = browser.name.replace(/ /g, '_').replace(/\./g, '_') + '.'

return (useBrowserName
? browserName
: '') + (pkgName
? pkgName + '.'
: '') + result.suite[0]
}

this.onRunStart = function (browsers) {
suites = Object.create(null);
xml = builder.create('testsuites');

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

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

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

if (!suite) {
// This browser did not signal `onBrowserStart`. That happens
// if the browser timed out duging the start phase.
return;
if (!suite || !result) {
return // don't die if browser didn't start
}

var result = browser.lastResult;

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

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

this.onRunComplete = function() {
this.onRunComplete = function () {
var xmlToOutput = xml;

pendingFileWritings++;
helper.mkdirIfNotExists(path.dirname(outputFile), function() {
fs.writeFile(outputFile, xmlToOutput.end({pretty: true}), function(err) {
helper.mkdirIfNotExists(path.dirname(outputFile), function () {
fs.writeFile(outputFile, xmlToOutput.end({pretty: true}), function (err) {
if (err) {
log.warn('Cannot write xml\n\t' + err.message);
} else {
Expand All @@ -93,30 +108,38 @@ var JenkinsReporter = function(baseReporterDecorator, config, logger, helper, fo
allMessages.length = 0;
};

this.specSuccess = this.specSkipped = this.specFailure = function(browser, result) {
var classnameArray = path.dirname(featurePath).split('/');
if(reporterConfig.classnameSuffix != null) classnameArray.push(reporterConfig.classnameSuffix);
this.specSuccess = this.specSkipped = this.specFailure = function (browser, result) {
var testsuite = suites[browser.id]

if (!testsuite) {
return
}

var spec = suites[browser.id].ele('testcase', {
name: result.description, time: ((result.time || 0) / 1000),
classname: classnameArray.join('.'),
'package': (pkgName ? pkgName + ' ' : '') + browser.name,
var spec = testsuite.ele('testcase', {
name: defaultNameFormatter(browser, result),
time: ((result.time || 0) / 1000),
classname: getClassName(browser, result),
package: (pkgName
? pkgName + ' '
: '') + browser.name,
parentSuites: result.suite.join('|')
});
})

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;
} else {
Expand Down