Skip to content
Merged
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
31 changes: 2 additions & 29 deletions lib/Supervisor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require('fs');
const net = require('net');
const split = require('split');
const Report = require('./Report');
const XMLBuilder = require('./XMLBuilder');

/**
* @param { string } elmTestVersion
Expand Down Expand Up @@ -211,36 +212,8 @@ function run(elmTestVersion, pipeFilename, report, processes, dest, watch) {
if (report === 'junit') {
var xml = response.message;
var values = Array.from(results.values());

xml.testsuite.testcase = xml.testsuite.testcase.concat(values);

// The XmlBuilder by default does not remove characters that are
// invalid in XML, like backspaces. However, we can pass it an
// `invalidCharReplacement` option to tell it how to handle
// those characters, rather than crashing. In an attempt to
// retain useful information in the output, we try and output a
// hex-encoded unicode codepoint for the invalid character. For
// example, the start of a terminal escape (`\u{001B}` in Elm) will be output as a
// literal `\u{001B}`.
/** @type { (char: string) => string } */
var invalidCharReplacement = function (char) {
return (
'\\u{' +
(char.codePointAt(0) || 0).toString(16).padStart(4, '0') +
'}'
);
};

console.log(
// xmlbuilder takes 10–20 ms to load, and is only needed
// when using the junit report, so `require` it late.
require('xmlbuilder')
.create(xml, {
// @ts-expect-error The type annotation says that `invalidCharReplacement` should be a string, but we have always passed a function. This got noticed when migrating from Flow to TypeScript.
invalidCharReplacement: invalidCharReplacement,
})
.end()
);
console.log(XMLBuilder.toString(xml));
}
}

Expand Down
165 changes: 165 additions & 0 deletions lib/XMLBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/**
* We used to use `xmlbuilder@15.1.1` from npm to generate XML.
* However, we only used a tiny fraction of that package.
* This file implements only the bits we need.
*
* This function takes an object that describes XML.
* You can specify element names, attributes, text and nested elements.
*
* NOTE: Element names and attribute names are assumed to be valid.
* Attribute values and text are escaped.
*
* Example input:
*
* {
* "plain": "text content", // <plain>text content</plain>
* "rich": {
* "@my-attr": "my value", // <rich my-attr="my value">
* "nested": "text", // <nested>text</nested> (nested under <rich>)
* "child": [
* { name: "John" }, // <child><name>John</name></child> (nested under <rich>)
* { name: "Jane" }, // <child><name>Jane</name></child> (nested under <rich>)
* ],
* },
* }
*
* @typedef { { [key: string]: XmlValue } } Xml
* @typedef { string | number | Array<Xml> | Xml } XmlValue
*
* @param { Xml } xml
* @returns { string }
*/
function toString(xml) {
const string = Object.entries(xml)
.map(([key, value]) => {
if (key.startsWith('@')) {
throw new Error(
`Attributes cannot be set at the top level. Key: ${JSON.stringify(
key
)}`
);
}
return xmlValueToString(key, value);
})
.join('');
return '<?xml version="1.0"?>' + string;
}

/**
* @param { string } elementName
* @param { XmlValue } xmlValue
* @returns { string }
*/
function xmlValueToString(elementName, xmlValue) {
if (Array.isArray(xmlValue)) {
return xmlValue
.map((value) => xmlValueToString(elementName, value))
.join('');
}

const attributes =
typeof xmlValue === 'object'
? Object.entries(xmlValue).flatMap(([key, value]) => {
if (!key.startsWith('@')) {
return [];
}
const escapedValue = xmlValueToAttributeValue(key, value);
return `${key.slice(1)}="${escapedValue}"`;
})
: [];

const attributesString =
attributes.length === 0 ? '' : ' ' + attributes.join(' ');

const childrenString =
typeof xmlValue === 'object'
? Object.entries(xmlValue)
.flatMap(([key, value]) =>
key.startsWith('@') ? [] : xmlValueToString(key, value)
)
.join('')
: typeof xmlValue === 'string'
? escapeText(xmlValue)
: xmlValue.toString();

return childrenString === ''
? `<${elementName}${attributesString}/>`
: `<${elementName}${attributesString}>${childrenString}</${elementName}>`;
}

/**
* @param { string } key
* @param { XmlValue } xmlValue
* @returns { string }
*/
function xmlValueToAttributeValue(key, xmlValue) {
switch (typeof xmlValue) {
case 'string':
return escapeAttributeValue(xmlValue);
case 'number':
return xmlValue.toString();
default:
throw new Error(
`Attribute values must be strings or numbers. Key: ${JSON.stringify(
key
)}. Value: ${JSON.stringify(xmlValue)}`
);
}
}

// https://github.com/oozcitak/xmlbuilder-js/blob/ce625aeb9f52d1f75d5d94260794d3b5fd74a8b1/src/XMLStringifier.coffee#L119
const invalidCharRegex =
// eslint-disable-next-line no-control-regex
/[\0-\x08\x0B\f\x0E-\x1F\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/g;

/**
* @param { string } string
* @returns { string }
*/
function escapeAttributeValue(string) {
return (
string
.replace(invalidCharRegex, invalidCharReplacement)
// https://github.com/oozcitak/xmlbuilder-js/blob/ce625aeb9f52d1f75d5d94260794d3b5fd74a8b1/src/XMLStringifier.coffee#L179-L184
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/"/g, '&quot;')
.replace(/\t/g, '&#x9;')
.replace(/\n/g, '&#xA;')
.replace(/\r/g, '&#xD;')
);
}

/**
* @param { string } string
* @returns { string }
*/
function escapeText(string) {
return (
string
.replace(invalidCharRegex, invalidCharReplacement)
// https://github.com/oozcitak/xmlbuilder-js/blob/ce625aeb9f52d1f75d5d94260794d3b5fd74a8b1/src/XMLStringifier.coffee#L166-L169
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/\r/g, '&#xD;')
);
}

/**
* Some characters are invalid in XML, like backspaces. In an attempt to
* retain useful information in the output, we try and output a
* hex-encoded unicode codepoint for the invalid character. For
* example, the start of a terminal escape (`\u{001B}` in Elm) will be output as a
* literal `\u{001B}`.
*
* @param { string } char
* @returns { string }
*/
function invalidCharReplacement(char) {
return `\\u{${(char.codePointAt(0) || 0).toString(16).padStart(4, '0')}}`;
}

module.exports = {
toString,
};
16 changes: 1 addition & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
"elm-solve-deps-wasm": "^1.0.2 || ^2.0.0",
"split": "^1.0.1",
"tinyglobby": "^0.2.10",
"which": "^2.0.2",
"xmlbuilder": "^15.1.1"
"which": "^2.0.2"
},
"devDependencies": {
"@eslint/js": "9.20.0",
Expand Down
10 changes: 10 additions & 0 deletions tests/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ describe('flags', () => {
path.join('tests', 'Passing', 'One.elm'),
]);

assert.strictEqual(
runResult.stdout.replace(/time="[^"]+"/g, `time="1337"`),
'<?xml version="1.0"?><testsuite name="elm-test" package="elm-test" tests="1" failures="0" errors="0" time="1337"><testcase classname="Passing.One" name="this should pass" time="1337"/></testsuite>\n'
);

xml2js.parseString(runResult.stdout, (err, data) => {
if (err) throw err;

Expand All @@ -338,6 +343,11 @@ describe('flags', () => {
path.join('tests', 'Failing', 'One.elm'),
]);

assert.strictEqual(
runResult.stdout.replace(/time="[^"]+"/g, `time="1337"`),
'<?xml version="1.0"?><testsuite name="elm-test" package="elm-test" tests="1" failures="1" errors="0" time="1337"><testcase classname="Failing.One" name="intentional failure" time="1337"><failure>This should fail!</failure></testcase></testsuite>\n'
);

xml2js.parseString(runResult.stdout, (err, data) => {
if (err) throw err;

Expand Down