apigeelint
Version:
Node module and tool to lint a bundle for an Apigee API Proxy or sharedflow.
85 lines (66 loc) • 2.36 kB
JavaScript
/**
* @fileoverview jUnit Reporter
* @author Jamund Ferguson
*/
;
const xmlEscape = require("../util/xml-escape");
/*
* There is no schema for junit-style XML output. The best we have is
* https://github.com/testmoapp/junitxml?tab=readme-ov-file#example.
**/
//------------------------------------------------------------------------------
// Helper Functions
//------------------------------------------------------------------------------
/**
* Returns the severity of warning or error
* @param {Object} message message object to examine
* @returns {string} severity level
* @private
*/
function getMessageType(message) {
if (message.fatal || message.severity === 2) {
return "Error";
}
return "Warning";
}
//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
module.exports = function (results) {
let output = "";
output += '<?xml version="1.0" encoding="utf-8"?>\n';
output += "<testsuites>\n";
results.forEach((result) => {
const messages = result.messages;
if (messages.length) {
output +=
`<testsuite package="apigeelint" time="0" ` +
`tests="${messages.length}" errors="${messages.length}" ` +
`name="${xmlEscape(result.filePath)}">\n`;
}
messages.forEach((message) => {
const type = message.fatal ? "error" : "failure";
// The start of the <testcase> element
output += `<testcase time="0" name="apigeelint.${message.ruleId || "unknown"}"`;
output += ` file="${xmlEscape(result.filePath)}"`;
// Add a line number
output += ` line="${message.line || 0}"`;
// The end of the <testcase> element
output += `>`;
output += `<${type} message="${xmlEscape(message.message || "")}">`;
output += "<![CDATA[";
output += `line ${message.line || 0}, col `;
output += `${message.column || 0}, ${getMessageType(message)}`;
output += ` - ${xmlEscape(message.message || "")}`;
output += message.ruleId ? ` (${message.ruleId})` : "";
output += "]]>";
output += `</${type}>`;
output += "</testcase>\n";
});
if (messages.length) {
output += "</testsuite>\n";
}
});
output += "</testsuites>\n";
return output;
};