This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8189567
commit cf38534
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
(function (jacocoParse, Promise, Joi, logger, util, path) { | ||
'use strict'; | ||
|
||
var jacocoStringValidation = Joi.string().required(); | ||
var optionsValidation = Joi.object().keys().optional(); | ||
|
||
module.exports = { | ||
parse: function parseJacoco(pathPrefix, jacocoString, options) { | ||
return new Promise(function (resolve, reject) { | ||
logger.debug('Parsing Jacoco Data'); | ||
var validLcov = Joi.validate(jacocoString, jacocoStringValidation); | ||
var validOptions = Joi.validate(options, optionsValidation, { | ||
stripUnknown: true | ||
}); | ||
var validationError = validLcov.error || validOptions.error; | ||
|
||
if (validationError) { | ||
logger.error(validationError); | ||
return reject(validationError); | ||
} | ||
|
||
jacocoParse.parseContent(jacocoString, function (err, data) { | ||
if (err) { | ||
err = new Error(err); | ||
|
||
logger.error(err); | ||
return reject(err); | ||
} | ||
|
||
var result = { | ||
total: 0, | ||
fileReports: [] | ||
}; | ||
var totalLines = 0; | ||
var totalHits = 0; | ||
|
||
//TODO: Convert to reduce function | ||
data.forEach(function (stats) { | ||
var fileStats = { | ||
// The API expects the filenames to be relative to the project, ex. lib/reporter.js | ||
filename: stats.file ? pathPrefix + path.relative(process.cwd(), stats.file) : '', | ||
coverage: {} | ||
}; | ||
|
||
totalLines += stats.lines.found; | ||
totalHits += stats.lines.hit; | ||
|
||
// The API uses integers only, so convert accordingly. | ||
fileStats.total = Math.floor(util.safeDivision(stats.lines.hit, stats.lines.found) * 100); | ||
|
||
//TODO: Convert to reduce function | ||
stats.lines.details.forEach(function (detail) { | ||
// Codacy needs the 0s to know failed coverage data | ||
// We also can't have a negative number of hits on a line, so exclude those. | ||
if (detail.hit >= 0) { | ||
fileStats.coverage[detail.line] = detail.hit; | ||
} | ||
}); | ||
|
||
logger.trace('Successfully parsed ' + stats.file); | ||
result.fileReports.push(fileStats); | ||
}); | ||
|
||
// The API uses integers only, so convert accordingly. | ||
result.total = Math.floor(util.safeDivision(totalHits, totalLines) * 100); | ||
|
||
logger.debug('Successfully Parsed Jacoco Data'); | ||
|
||
resolve(result); | ||
}); | ||
}); | ||
} | ||
}; | ||
}(require('jacoco-parse'), require('bluebird'), require('joi'), require('../logger')(), require('../util'), require('path'))); |