merge-cobertura
Version:
Merges cobertura XML files
115 lines • 5.61 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeInputs = void 0;
const util_1 = require("./util");
const path = __importStar(require("path"));
const VERSION = '0.1';
function rewriteBasedir(originalBaseDir, classes) {
var _a;
return ((_a = classes === null || classes === void 0 ? void 0 : classes.map((jsonClass) => {
var _a, _b;
return ({
class: (_b = (_a = jsonClass.class) === null || _a === void 0 ? void 0 : _a.map((jsonInnerClass) => {
return Object.assign(Object.assign({}, jsonInnerClass), { filename: path.relative(process.cwd(), path.join(originalBaseDir, jsonInnerClass.filename)) });
})) !== null && _b !== void 0 ? _b : [],
});
})) !== null && _a !== void 0 ? _a : []);
}
function mergeInputs(inputs) {
const totalBranchesCovered = sumCoverageProperty(inputs, 'branches-covered');
const totalBranchesValid = sumCoverageProperty(inputs, 'branches-valid');
const totalLinesCovered = sumCoverageProperty(inputs, 'lines-covered');
const totalLinesValid = sumCoverageProperty(inputs, 'lines-valid');
const branchRate = (totalBranchesCovered / totalBranchesValid || 0).toString();
const lineRate = (totalLinesCovered / totalLinesValid || 0).toString();
const complexity = Math.max(...inputs.map((input) => parseInt(input.data.coverage.complexity, 10))).toString();
return {
coverage: {
'branch-rate': branchRate,
'branches-covered': totalBranchesCovered.toString(),
'branches-valid': totalBranchesValid.toString(),
complexity,
'line-rate': lineRate,
'lines-covered': totalLinesCovered.toString(),
'lines-valid': totalLinesValid.toString(),
version: VERSION,
timestamp: Date.now().toString(),
sources: [
{
source: [
{
$t: process.cwd(),
},
],
},
],
packages: [
{
package: (0, util_1.flatten)(inputs.map((input) => {
var _a, _b;
const originalBaseDir = (_b = (_a = input.data.coverage.sources) === null || _a === void 0 ? void 0 : _a[0].source[0].$t) !== null && _b !== void 0 ? _b : '';
return (0, util_1.flatten)(input.data.coverage.packages.map((packages) => {
if (packages.package) {
return packages.package.map((jsonPackage) => ({
name: jsonPackage.name === '.' ? input.packageName : `${input.packageName}.${jsonPackage.name}`,
'line-rate': jsonPackage['line-rate'],
'branch-rate': jsonPackage['branch-rate'],
complexity: jsonPackage.complexity,
classes: rewriteBasedir(originalBaseDir, jsonPackage.classes),
}));
}
else if (packages.class) {
return [
{
name: input.packageName,
'line-rate': lineRate,
'branch-rate': branchRate,
complexity,
classes: rewriteBasedir(originalBaseDir, packages.class.map((jsonClass) => ({
class: [jsonClass],
}))),
},
];
}
else if (packages.$t !== undefined) {
// No packages
return [];
}
else {
throw new Error('Unknown package format: ' + JSON.stringify(packages));
}
}));
})),
},
],
},
};
}
exports.mergeInputs = mergeInputs;
function sumCoverageProperty(inputs, property) {
return inputs.reduce((count, input) => count + parseInt(input.data.coverage[property], 10), 0);
}
//# sourceMappingURL=merge.js.map