Skip to content
This repository was archived by the owner on Feb 19, 2020. It is now read-only.

Commit d01a04e

Browse files
author
Rodrigo Fernandes
committed
Merge pull request #12 from codacy/fix-by-zero-division
Fix by zero division
2 parents a6e9ffe + a259091 commit d01a04e

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

lib/impl/lcov.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(function (lcovParse, Promise, Joi, logger, path) {
1+
(function (lcovParse, Promise, Joi, logger, util, path) {
22
'use strict';
33

44
var lcovStringValidation = Joi.string().required();
@@ -10,8 +10,8 @@
1010
logger.debug('Parsing Lcov Data');
1111
var validLcov = Joi.validate(lcovString, lcovStringValidation);
1212
var validOptions = Joi.validate(options, optionsValidation, {
13-
stripUnknown: true
14-
});
13+
stripUnknown: true
14+
});
1515
var validationError = validLcov.error || validOptions.error;
1616

1717
if (validationError) {
@@ -28,9 +28,9 @@
2828
}
2929

3030
var result = {
31-
total: 0,
32-
fileReports: []
33-
};
31+
total: 0,
32+
fileReports: []
33+
};
3434
var totalLines = 0;
3535
var totalHits = 0;
3636

@@ -46,7 +46,7 @@
4646
totalHits += stats.lines.hit;
4747

4848
// The API uses integers only, so convert accordingly.
49-
fileStats.total = Math.floor((stats.lines.hit / stats.lines.found) * 100);
49+
fileStats.total = Math.floor(util.safeDivision(stats.lines.hit, stats.lines.found) * 100);
5050

5151
//TODO: Convert to reduce function
5252
stats.lines.details.forEach(function (detail) {
@@ -62,7 +62,7 @@
6262
});
6363

6464
// The API uses integers only, so convert accordingly.
65-
result.total = Math.floor((totalHits / totalLines) * 100);
65+
result.total = Math.floor(util.safeDivision(totalHits, totalLines) * 100);
6666

6767
logger.debug('Successfully Parsed Lcov Data');
6868

@@ -71,4 +71,4 @@
7171
});
7272
}
7373
};
74-
}(require('lcov-parse'), require('bluebird'), require('joi'), require('../logger')(), require('path')));
74+
}(require('lcov-parse'), require('bluebird'), require('joi'), require('../logger')(), require('../util'), require('path')));

lib/util.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(function () {
2+
'use strict';
3+
4+
module.exports = {
5+
safeDivision: function (numerator, denominator) {
6+
if (denominator === 0 || isNaN(denominator)) {
7+
return 0;
8+
} else {
9+
return numerator / denominator;
10+
}
11+
}
12+
};
13+
14+
})();

test/lcov.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@
9393
return expect(parser.getParser('lcov').parse('', noStatsLcovData))
9494
.to.eventually.satisfy(function (data) {
9595
expect(JSON.stringify(data)).to.equal(JSON.stringify({
96-
total: null,
96+
total: 0,
9797
fileReports: [
9898
{
9999
filename: path.normalize('lib/reporter.js'),
100100
coverage: {},
101-
total: null
101+
total: 0
102102
}
103103
]
104104
}));
@@ -109,12 +109,12 @@
109109
return expect(parser.getParser('lcov').parse('', nadaLcovData))
110110
.to.eventually.satisfy(function (data) {
111111
expect(JSON.stringify(data)).to.equal(JSON.stringify({
112-
total: null,
112+
total: 0,
113113
fileReports: [
114114
{
115115
filename: '',
116116
coverage: {},
117-
total: null
117+
total: 0
118118
}
119119
]
120120
}));

0 commit comments

Comments
 (0)