Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer process.stdout.write in xUnit reporter. #1102

Closed
wants to merge 1 commit into from
Closed
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
35 changes: 30 additions & 5 deletions lib/reporters/xunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function XUnit(runner) {
});

runner.on('end', function(){
console.log(tag('testsuite', {
writeLine(tag('testsuite', {
name: 'Mocha Tests'
, tests: stats.tests
, failures: stats.failures
Expand All @@ -60,7 +60,7 @@ function XUnit(runner) {
}, false));

tests.forEach(test);
console.log('</testsuite>');
writeLine('</testsuite>');
});
}

Expand All @@ -84,11 +84,11 @@ function test(test) {
if ('failed' == test.state) {
var err = test.err;
attrs.message = escape(err.message);
console.log(tag('testcase', attrs, false, tag('failure', attrs, false, cdata(err.stack))));
writeLine(tag('testcase', attrs, false, tag('failure', attrs, false, cdata(err.stack))));
} else if (test.pending) {
console.log(tag('testcase', attrs, false, tag('skipped', {}, true)));
writeLine(tag('testcase', attrs, false, tag('skipped', {}, true)));
} else {
console.log(tag('testcase', attrs, true) );
writeLine(tag('testcase', attrs, true) );
}
}

Expand Down Expand Up @@ -117,3 +117,28 @@ function tag(name, attrs, close, content) {
function cdata(str) {
return '<![CDATA[' + escape(str) + ']]>';
}

/**
* Write to standard output if available (through Node.js), or otherwise
* to `console.log`.
*
* NOTE: Beware of the differences between `console.log` and
* `process.stdout.write`--most importantly that `console.log`
* on some browsers may format its arguments a la `printf`,
* whereas `process.stdout.write` expects a string to print
* verbatim as its first argument. Fortunately most browsers
* (at least Firefox and Chrome) print the first argument
* verbatim as long as there are no others, so this is unlikely
* to be a problem in practice.
*/

var writeLine;
if ((typeof process != 'undefined') && (typeof process.stdout != 'undefined')) {
writeLine = function(str) {
process.stdout.write(str + '\n');
};
} else {
writeLine = function(str) {
console.log(str);
};
}
36 changes: 30 additions & 6 deletions mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -1977,7 +1977,7 @@ exports.cursor = {
exports.cursor.deleteLine();
exports.cursor.beginningOfLine();
} else {
process.stdout.write('\n');
process.stdout.write('\r');
}
}
};
Expand Down Expand Up @@ -4018,7 +4018,7 @@ function XUnit(runner) {
});

runner.on('end', function(){
console.log(tag('testsuite', {
writeLine(tag('testsuite', {
name: 'Mocha Tests'
, tests: stats.tests
, failures: stats.failures
Expand All @@ -4029,7 +4029,7 @@ function XUnit(runner) {
}, false));

tests.forEach(test);
console.log('</testsuite>');
writeLine('</testsuite>');
});
}

Expand Down Expand Up @@ -4057,11 +4057,11 @@ function test(test) {
if ('failed' == test.state) {
var err = test.err;
attrs.message = escape(err.message);
console.log(tag('testcase', attrs, false, tag('failure', attrs, false, cdata(err.stack))));
writeLine(tag('testcase', attrs, false, tag('failure', attrs, false, cdata(err.stack))));
} else if (test.pending) {
console.log(tag('testcase', attrs, false, tag('skipped', {}, true)));
writeLine(tag('testcase', attrs, false, tag('skipped', {}, true)));
} else {
console.log(tag('testcase', attrs, true) );
writeLine(tag('testcase', attrs, true) );
}
}

Expand Down Expand Up @@ -4091,6 +4091,30 @@ function cdata(str) {
return '<![CDATA[' + escape(str) + ']]>';
}

/**
* Write to standard output if available (through Node.js), or otherwise
* to `console.log`.
*
* NOTE: Beware of the differences between `console.log` and
* `process.stdout.write`--most importantly that `console.log`
* on some browsers may format its arguments a la `printf`,
* whereas `process.stdout.write` expects a string to print
* verbatim as its first argument. Fortunately most browsers
* (at least Firefox and Chrome) print the first argument
* verbatim as long as there are no others, so this is unlikely
* to be a problem in practice.
*/

var writeLine;
if ((typeof process != 'undefined') && (typeof process.stdout != 'undefined')) {
writeLine = function(str) {
process.stdout.write(str + '\n');
};
} else {
writeLine = function(str) {
console.log(str);
};
}
}); // module: reporters/xunit.js

require.register("runnable.js", function(module, exports, require){
Expand Down