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

fix: ensure that the xunit reporter emits well-formed XML #5001

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var util = require('util');
var he = require('he');

const MOCHA_ID_PROP_NAME = '__mocha_id__';
const RESTRICTED_CHAR = /[\x01-\x08\x0B-\x0C\x0E-\x1F\x7F-\x84\x86-\x9F]/g;

/**
* Inherit the prototype methods from one constructor into another.
Expand All @@ -35,6 +36,7 @@ exports.inherits = util.inherits;
* @return {string}
*/
exports.escape = function (html) {
html = html.toString().replaceAll(RESTRICTED_CHAR, "�");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 this looks like a duplicate of #4527. @malloryavvir do you have thoughts on https://github.com/mochajs/mocha/pull/4527/files#r1511131215 ?

return he.encode(String(html), {useNamedReferences: false});
};

Expand Down
39 changes: 39 additions & 0 deletions test/reporters/xunit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,45 @@ describe('XUnit reporter', function () {
expect(expectedWrite, 'to be', expectedTag);
});

it('should write well-formed XML', function () {
var xunit = new XUnit(runner);
var inputMessage = '\x1Bsome message';
expectedMessage = '�some message';
var expectedTest = {
state: STATE_FAILED,
title: expectedTitle,
parent: {
fullTitle: function () {
return expectedClassName;
}
},
duration: 1000,
err: {
actual: 'foo',
expected: 'bar',
message: inputMessage,
stack: expectedStack
}
};

xunit.test.call(fakeThis, expectedTest);
sinon.restore();

var expectedTag =
'<testcase classname="' +
expectedClassName +
'" name="' +
expectedTitle +
'" time="1"><failure>' +
expectedMessage +
'\n' +
expectedDiff +
'\n' +
expectedStack +
'</failure></testcase>';
expect(expectedWrite, 'to be', expectedTag);
});

it('should handle non-string diff values', function () {
var runner = new EventEmitter();
createStatsCollector(runner);
Expand Down