blob: 84190ab8ae7ce85dce633408af6a3763cae119c1 [file] [log] [blame]
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const path = require('path');
const rootDir = path.resolve(__dirname, '..');
function byRule(messages) {
const rules = new Map();
for (const msg of messages) {
let msgList = rules.get(msg.ruleId);
if (!msgList) {
msgList = [];
rules.set(msg.ruleId, msgList);
}
msgList.push(msg);
}
return rules;
}
function formatMessage({ruleId, severity, message, line, column}, file) {
const tag = severity >= 2 ? 'error' : 'failure';
const type = severity >= 2 ? 'Error' : 'Warning';
return ` <${tag} message="${message.replace(/"/g, '&quot;')}" type="lint">
<![CDATA[${file}:${line}:${column} - ${type}: ${message}
(${ruleId})]]>
</${tag}>`;
}
// NB(sollyross): we *cannot* have a test suite per filename -- the way the
// test viewer displays suite names is as if they're java-style class names
// (x.y.ClassName), and therefore would only ever display `ts`, which is not
// helpful. otoh, testcase names are displayed in full
module.exports = function(results, context) {
const suites = results.map((file) => {
const fileName = path.relative(rootDir, file.filePath);
let cases = file.messages.map((inst) => formatMessage(inst, fileName));
return ` <testcase name="${fileName}" classname="eslint">
${cases.join("\n")}
</testcase>`
});
const numErrs = results.reduce(
(total, file) => total + file.messages.filter((msg) => msg.severity >= 2).length,
0
);
const numOther = results.reduce(
(total, file) => total + file.messages.filter((msg) => msg.severity == 1).length,
0
);
return `
<testsuites>
<testsuite name="eslint" tests="${results.length}" errors="${numErrs}" failures="${numOther}">
${suites.join("\n")}
</testsuite>
</testsuites>
`
};