|
| 1 | +(function (jacocoParse, Promise, Joi, logger, util, path) { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + var jacocoStringValidation = Joi.string().required(); |
| 5 | + var optionsValidation = Joi.object().keys().optional(); |
| 6 | + |
| 7 | + module.exports = { |
| 8 | + parse: function parseJacoco(pathPrefix, jacocoString, options) { |
| 9 | + return new Promise(function (resolve, reject) { |
| 10 | + logger.debug('Parsing Jacoco Data'); |
| 11 | + var nonEmptyReport = Joi.validate(jacocoString, jacocoStringValidation); |
| 12 | + var validOptions = Joi.validate(options, optionsValidation, { |
| 13 | + stripUnknown: true |
| 14 | + }); |
| 15 | + var validationError = nonEmptyReport.error || validOptions.error; |
| 16 | + |
| 17 | + if (validationError) { |
| 18 | + logger.error(validationError); |
| 19 | + return reject(validationError); |
| 20 | + } |
| 21 | + |
| 22 | + jacocoParse.parseContent(jacocoString, function (err, data) { |
| 23 | + if (err) { |
| 24 | + err = new Error('Failed to parse jacoco report: ' + err); |
| 25 | + |
| 26 | + logger.error(err); |
| 27 | + return reject(err); |
| 28 | + } |
| 29 | + |
| 30 | + var result = { |
| 31 | + total: 0, |
| 32 | + fileReports: [] |
| 33 | + }; |
| 34 | + var totalLines = 0; |
| 35 | + var totalHits = 0; |
| 36 | + |
| 37 | + //TODO: Convert to reduce function |
| 38 | + data.forEach(function (stats) { |
| 39 | + var fileStats = { |
| 40 | + // The API expects the filenames to be relative to the project, ex. lib/reporter.js |
| 41 | + filename: stats.file ? pathPrefix + path.relative(process.cwd(), stats.file) : '', |
| 42 | + coverage: {} |
| 43 | + }; |
| 44 | + |
| 45 | + totalLines += stats.lines.found; |
| 46 | + totalHits += stats.lines.hit; |
| 47 | + |
| 48 | + // The API uses integers only, so convert accordingly. |
| 49 | + fileStats.total = Math.floor(util.safeDivision(stats.lines.hit, stats.lines.found) * 100); |
| 50 | + |
| 51 | + //TODO: Convert to reduce function |
| 52 | + stats.lines.details.forEach(function (detail) { |
| 53 | + // Codacy needs the 0s to know failed coverage data |
| 54 | + // We also can't have a negative number of hits on a line, so exclude those. |
| 55 | + if (detail.hit >= 0) { |
| 56 | + fileStats.coverage[detail.line] = detail.hit; |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + logger.trace('Successfully parsed ' + stats.file); |
| 61 | + result.fileReports.push(fileStats); |
| 62 | + }); |
| 63 | + |
| 64 | + // The API uses integers only, so convert accordingly. |
| 65 | + result.total = Math.floor(util.safeDivision(totalHits, totalLines) * 100); |
| 66 | + |
| 67 | + logger.debug('Successfully Parsed Jacoco Data'); |
| 68 | + |
| 69 | + resolve(result); |
| 70 | + }); |
| 71 | + }); |
| 72 | + } |
| 73 | + }; |
| 74 | +}(require('jacoco-parse'), require('bluebird'), require('joi'), require('../logger')(), require('../util'), require('path'))); |
0 commit comments