Last active
December 5, 2021 20:11
-
-
Save R0Wi/19fa8a508d32948049267248c3c11cb1 to your computer and use it in GitHub Desktop.
Node script for merging different Clover XML coverage files from different sources like PHPUnit and Jest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @copyright Copyright (c) 2021 Robin Windey <[email protected]> | |
* | |
* @author Robin Windey <[email protected]> | |
* | |
* @license GNU AGPL version 3 or any later version | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU Affero General Public License as | |
* published by the Free Software Foundation, either version 3 of the | |
* License, or (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU Affero General Public License for more details. | |
* | |
* You should have received a copy of the GNU Affero General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
* | |
* The latest source code is available at <https://gist.github.com/R0Wi/19fa8a508d32948049267248c3c11cb1> | |
* | |
* This script merges the coverage data from multiple Clover-XML-files. | |
* It can be useful to merge coverage files from different sources like | |
* PHPUnit (PHP) and jest (JavaScript) for example. | |
* | |
* Example: | |
* Assume you have created 3 different coverage files in clover format: | |
* phpunit -c phpunit.xml --coverage-clover coverage_unittests.xml | |
* phpunit -c phpunit.integration.xml --coverage-clover coverage_integrationtests.xml | |
* jest --coverage | |
* Usage: | |
* Install dependencies via | |
* npm install | |
* Then run the script: | |
* node mergeCoverage.js -i coverage_unittests.xml -i coverage_integrationtests.xml -i coverage_jest.xml -o mergedCoverage.json | |
* | |
* Parameters: | |
* i: input files in Clover XML format | |
* o: output file in JSON format | |
*/ | |
"use strict"; | |
// Imports | |
const commandLineArgs = require('command-line-args'); | |
const fs = require("fs"); | |
const clover = require("@cvrg-report/clover-json"); | |
const merge = require('@connectis/coverage-merger'); | |
// Read commandline arguments | |
const optionDefinitions = [ | |
{ name: 'inputs', alias: 'i', type: String, multiple: true }, | |
{ name: 'output', alias: 'o', type: String, multiple: false } | |
] | |
const options = commandLineArgs(optionDefinitions) | |
// Input clover files | |
const coverageFiles = options.inputs; | |
// Output JSON file for merged coverage | |
const resultFile = options.output; | |
const allResults = []; | |
const defaultObj = { | |
found: 0, | |
hit: 0, | |
details: [] | |
}; | |
// Reads a single coverage file into a JSON object array | |
function readCoverage(file) { | |
return new Promise(function (resolve) { | |
clover.parseFile(file) | |
.then(resultArr => allResults.push(resultArr)) | |
.catch(function (err) { | |
console.error(err); | |
process.exit(1); | |
}) | |
.then(() => resolve()); | |
}); | |
} | |
// Wait for all files to be read, merge them and write to file | |
Promise.all(coverageFiles.map(file => readCoverage(file))) | |
.then(() => { | |
const concatResults = allResults.flatMap(arr => arr.map(r => { | |
// Fix for missing branches/lines/functions info in PHPUnit | |
r.branches = r.branches ?? defaultObj; | |
r.function = r.function ?? defaultObj; | |
r.lines = r.lines ?? defaultObj; | |
return r; | |
})); | |
const mergedResults = merge.mergeByFile(concatResults); | |
fs.writeFileSync(resultFile, JSON.stringify(mergedResults, null, 2)); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "merge_clover", | |
"version": "1.0.0", | |
"description": "", | |
"main": "mergeCoverage.js", | |
"dependencies": { | |
"@connectis/coverage-merger": "^1.1.0", | |
"@cvrg-report/clover-json": "^0.3.2", | |
"command-line-args": "^5.2.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment