Skip to content

Commit

Permalink
Prefer process.stdout.write in xUnit reporter.
Browse files Browse the repository at this point in the history
Falls back to `console.log` if `process.stdout` is unavailable.

Fixes mochajs#1068.

(This also seems to have picked up a completely unrelated change to another
file that hadn't yet made it into the mocha.js snapshot.)
  • Loading branch information
jeffparsons committed Jan 6, 2014
1 parent 74c0361 commit e923602
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
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');

This comment has been minimized.

Copy link
@jeffparsons

jeffparsons Jan 6, 2014

Author Owner

Hello. What are you doing here?

}
}
};
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

0 comments on commit e923602

Please sign in to comment.