Summary: -- Reviewers: #reviewers Subscribers: jenkins-user Differential Revision: https://hub.sealcode.org/D1606
43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
import fs from "fs";
|
|
import xml2js from "xml2js";
|
|
|
|
function convertCoberturaToArcanistJSON(coberturaXml) {
|
|
const parser = new xml2js.Parser();
|
|
return parser.parseStringPromise(coberturaXml).then((result) => {
|
|
const coverage = {};
|
|
|
|
const classes = result.coverage.packages[0].package.flatMap(
|
|
(pkg) => pkg.classes[0].class
|
|
);
|
|
|
|
for (const cls of classes) {
|
|
const filePath = cls.$.filename;
|
|
const lines = cls.lines[0].line;
|
|
|
|
const lineCoverage = [];
|
|
let maxLine = 0;
|
|
|
|
for (const line of lines) {
|
|
const num = parseInt(line.$.number, 10);
|
|
const hits = parseInt(line.$.hits, 10);
|
|
if (num > maxLine) maxLine = num;
|
|
lineCoverage[num] = hits > 0 ? "C" : "U";
|
|
}
|
|
|
|
// Fill gaps with 'N'
|
|
for (let i = 1; i <= maxLine; i++) {
|
|
if (!lineCoverage[i]) lineCoverage[i] = "N";
|
|
}
|
|
|
|
const coverageStr = lineCoverage.slice(1).join("");
|
|
coverage[filePath] = coverageStr;
|
|
}
|
|
return coverage;
|
|
});
|
|
}
|
|
|
|
const coberturaXml = fs.readFileSync("coverage/cobertura-coverage.xml", "utf8");
|
|
convertCoberturaToArcanistJSON(coberturaXml).then((json) => {
|
|
console.log(JSON.stringify(json));
|
|
});
|