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

Commit 17aa67b

Browse files
committed
fix division by zero
1 parent 64ec952 commit 17aa67b

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lib/impl/lcov.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(function (lcovParse, Q, Joi, logger, path) {
1+
(function (lcovParse, Q, Joi, logger, util, path) {
22
'use strict';
33
var lcovStringValidation = Joi.string().required(),
44
optionsValidation = Joi.object().keys().optional();
@@ -45,7 +45,9 @@
4545
totalHits += stats.lines.hit;
4646

4747
// The API uses integers only, so convert accordingly.
48-
fileStats.total = Math.floor((stats.lines.hit / stats.lines.found) * 100);
48+
console.log("pilas" + stats.lines.hit + '/' + stats.lines.found);
49+
50+
fileStats.total = Math.floor(util.safeDivision(stats.lines.hit, stats.lines.found) * 100);
4951

5052
//TODO: Convert to reduce function
5153
stats.lines.details.forEach(function (detail) {
@@ -61,7 +63,7 @@
6163
});
6264

6365
// The API uses integers only, so convert accordingly.
64-
result.total = Math.floor((totalHits / totalLines) * 100);
66+
result.total = Math.floor(util.safeDivision(totalHits, totalLines) * 100);
6567

6668
logger.debug('Successfully Parsed Lcov Data');
6769

@@ -71,4 +73,4 @@
7173
return deferred.promise;
7274
}
7375
};
74-
}(require('lcov-parse'), require('q'), require('joi'), require('../logger')(), require('path')));
76+
}(require('lcov-parse'), require('q'), 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+
})();

0 commit comments

Comments
 (0)