Bug 1317019 - Fix object iteration and check if scriptName exists. r?jmaher
This patch first implements an iterator so that we can properly iterate over the elements of the object returned by the _getMethodNames function. Next, the recordTestCoverage function now checks to see if the file actually has methods before recording them. And finally, some formatting was done.
MozReview-Commit-ID: EPOeYlMBc0w
--- a/testing/modules/CoverageUtils.jsm
+++ b/testing/modules/CoverageUtils.jsm
@@ -94,75 +94,87 @@ CoverageCollector.prototype._getLinesCov
}
CoverageCollector.prototype._getUncoveredLines = function() {
let uncoveredLines = {};
this._scripts.forEach(s => {
let scriptName = s.url;
let scriptOffsets = s.getAllOffsets();
- if (!uncoveredLines[scriptName]){
+ if (!uncoveredLines[scriptName]) {
uncoveredLines[scriptName] = new Set();
}
// Get all lines in the script
scriptOffsets.forEach( function(element, index) {
- if (!element){
+ if (!element) {
return;
}
uncoveredLines[scriptName].add(index);
});
});
// For all covered lines, delete their entry
- for (let scriptName in this._allCoverage){
- for (let key in this._allCoverage[scriptName]){
+ for (let scriptName in this._allCoverage) {
+ for (let key in this._allCoverage[scriptName]) {
let [lineNumber, columnNumber, offset] = key.split('#');
uncoveredLines[scriptName].delete(parseInt(lineNumber, 10));
}
}
return uncoveredLines;
}
CoverageCollector.prototype._getMethodNames = function() {
let methodNames = {};
this._scripts.forEach(s => {
let method = s.displayName;
// If the method name is undefined, we return early
- if (!method){
+ if (!method) {
return;
}
let scriptName = s.url;
let tempMethodCov = [];
let scriptOffsets = s.getAllOffsets();
- if (!methodNames[scriptName]){
+ if (!methodNames[scriptName]) {
methodNames[scriptName] = {};
}
/**
* Get all lines contained within the method and
* push a record of the form:
* <method name> : <lines covered>
*/
- scriptOffsets.forEach(function (element, index){
- if (!element){
+ scriptOffsets.forEach( function (element, index) {
+ if (!element) {
return;
}
tempMethodCov.push(index);
});
methodNames[scriptName][method] = tempMethodCov;
});
return methodNames;
}
/**
+ * Implements an iterator for objects. It is
+ * used to iterate over the elements of the object obtained
+ * from the function _getMethodNames.
+ */
+Object.prototype[Symbol.iterator] = function * () {
+ for (var [key, value] of Object.entries(this)) {
+ yield [key, value];
+ }
+};
+
+
+/**
* Records lines covered since the last time coverage was recorded,
* associating them with the given test name. The result is written
* to a json file in a specified directory.
*/
CoverageCollector.prototype.recordTestCoverage = function (testName) {
dump("Collecting coverage for: " + testName + "\n");
let rawLines = this._getLinesCovered(testName);
let methods = this._getMethodNames();
@@ -175,25 +187,27 @@ CoverageCollector.prototype.recordTestCo
let rec = {
testUrl: testName,
sourceFile: scriptName,
methods: {},
covered: [],
uncovered: []
};
- for (let methodName in methods[scriptName]){
- rec.methods[methodName] = methods[scriptName][methodName];
+ if (typeof(methods[scriptName]) != 'undefined' && methods[scriptName] != null) {
+ for (let [methodName, methodLines] of methods[scriptName]) {
+ rec.methods[methodName] = methodLines;
+ }
}
for (let line of rawLines[scriptName]) {
rec.covered.push(line);
}
- for (let line of uncoveredLines[scriptName]){
+ for (let line of uncoveredLines[scriptName]) {
rec.uncovered.push(line);
}
result.push(rec);
}
let arr = this._encoder.encode(JSON.stringify(result, null, 2));
let path = this._prefix + '/' + 'jscov_' + Date.now() + '.json';
dump("Writing coverage to: " + path + "\n");