2015-12-24 01:04:49 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["CoverageCollector"];
|
|
|
|
|
2017-06-24 00:08:04 +03:00
|
|
|
/* globals Debugger */
|
2019-01-17 21:18:31 +03:00
|
|
|
const { addDebuggerToGlobal } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/jsdebugger.jsm"
|
|
|
|
);
|
2017-09-16 21:06:17 +03:00
|
|
|
addDebuggerToGlobal(Cu.getGlobalForObject(this));
|
2015-12-24 01:04:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Records coverage for each test by way of the js debugger.
|
|
|
|
*/
|
2017-06-24 00:08:02 +03:00
|
|
|
var CoverageCollector = function(prefix) {
|
2015-12-24 01:04:49 +03:00
|
|
|
this._prefix = prefix;
|
|
|
|
this._dbg = new Debugger();
|
|
|
|
this._dbg.collectCoverageInfo = true;
|
|
|
|
this._dbg.addAllGlobalsAsDebuggees();
|
|
|
|
this._scripts = this._dbg.findScripts();
|
|
|
|
|
|
|
|
this._dbg.onNewScript = script => {
|
|
|
|
this._scripts.push(script);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Source -> coverage data;
|
|
|
|
this._allCoverage = {};
|
2017-06-21 19:28:52 +03:00
|
|
|
this._encoder = new TextEncoder();
|
|
|
|
|
2015-12-24 01:04:49 +03:00
|
|
|
this._testIndex = 0;
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2015-12-24 01:04:49 +03:00
|
|
|
|
2017-06-24 00:08:02 +03:00
|
|
|
CoverageCollector.prototype._getLinesCovered = function() {
|
2015-12-24 01:04:49 +03:00
|
|
|
let coveredLines = {};
|
|
|
|
let currentCoverage = {};
|
|
|
|
this._scripts.forEach(s => {
|
|
|
|
let scriptName = s.url;
|
|
|
|
let cov = s.getOffsetsCoverage();
|
|
|
|
if (!cov) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cov.forEach(covered => {
|
|
|
|
let { lineNumber, columnNumber, offset, count } = covered;
|
|
|
|
if (!count) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!currentCoverage[scriptName]) {
|
|
|
|
currentCoverage[scriptName] = {};
|
|
|
|
}
|
|
|
|
if (!this._allCoverage[scriptName]) {
|
|
|
|
this._allCoverage[scriptName] = {};
|
|
|
|
}
|
|
|
|
|
2017-06-24 00:08:02 +03:00
|
|
|
let key = [lineNumber, columnNumber, offset].join("#");
|
2015-12-24 01:04:49 +03:00
|
|
|
if (!currentCoverage[scriptName][key]) {
|
|
|
|
currentCoverage[scriptName][key] = count;
|
|
|
|
} else {
|
|
|
|
currentCoverage[scriptName][key] += count;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Covered lines are determined by comparing every offset mentioned as of the
|
|
|
|
// the completion of a test to the last time we measured coverage. If an
|
|
|
|
// offset in a line is novel as of this test, or a count has increased for
|
|
|
|
// any offset on a particular line, that line must have been covered.
|
|
|
|
for (let scriptName in currentCoverage) {
|
|
|
|
for (let key in currentCoverage[scriptName]) {
|
|
|
|
if (
|
|
|
|
!this._allCoverage[scriptName] ||
|
|
|
|
!this._allCoverage[scriptName][key] ||
|
|
|
|
this._allCoverage[scriptName][key] < currentCoverage[scriptName][key]
|
|
|
|
) {
|
2017-06-24 00:08:04 +03:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2017-06-24 00:08:02 +03:00
|
|
|
let [lineNumber, colNumber, offset] = key.split("#");
|
2015-12-24 01:04:49 +03:00
|
|
|
if (!coveredLines[scriptName]) {
|
|
|
|
coveredLines[scriptName] = new Set();
|
|
|
|
}
|
|
|
|
coveredLines[scriptName].add(parseInt(lineNumber, 10));
|
|
|
|
this._allCoverage[scriptName][key] = currentCoverage[scriptName][key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return coveredLines;
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2015-12-24 01:04:49 +03:00
|
|
|
|
2016-03-12 03:13:33 +03:00
|
|
|
CoverageCollector.prototype._getUncoveredLines = function() {
|
|
|
|
let uncoveredLines = {};
|
|
|
|
this._scripts.forEach(s => {
|
|
|
|
let scriptName = s.url;
|
|
|
|
let scriptOffsets = s.getAllOffsets();
|
|
|
|
|
2017-01-22 02:04:28 +03:00
|
|
|
if (!uncoveredLines[scriptName]) {
|
2016-03-12 03:13:33 +03:00
|
|
|
uncoveredLines[scriptName] = new Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all lines in the script
|
|
|
|
scriptOffsets.forEach(function(element, index) {
|
2017-01-22 02:04:28 +03:00
|
|
|
if (!element) {
|
2016-03-12 03:13:33 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
uncoveredLines[scriptName].add(index);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// For all covered lines, delete their entry
|
2017-01-22 02:04:28 +03:00
|
|
|
for (let scriptName in this._allCoverage) {
|
|
|
|
for (let key in this._allCoverage[scriptName]) {
|
2017-06-24 00:08:04 +03:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2017-06-24 00:08:02 +03:00
|
|
|
let [lineNumber, columnNumber, offset] = key.split("#");
|
2016-03-12 03:13:33 +03:00
|
|
|
uncoveredLines[scriptName].delete(parseInt(lineNumber, 10));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return uncoveredLines;
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2015-12-24 01:04:49 +03:00
|
|
|
|
2016-03-12 03:18:19 +03:00
|
|
|
CoverageCollector.prototype._getMethodNames = function() {
|
|
|
|
let methodNames = {};
|
|
|
|
this._scripts.forEach(s => {
|
|
|
|
let method = s.displayName;
|
|
|
|
// If the method name is undefined, we return early
|
2017-01-22 02:04:28 +03:00
|
|
|
if (!method) {
|
2016-03-12 03:18:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let scriptName = s.url;
|
|
|
|
let tempMethodCov = [];
|
|
|
|
let scriptOffsets = s.getAllOffsets();
|
|
|
|
|
2017-01-22 02:04:28 +03:00
|
|
|
if (!methodNames[scriptName]) {
|
2016-03-12 03:18:19 +03:00
|
|
|
methodNames[scriptName] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all lines contained within the method and
|
|
|
|
* push a record of the form:
|
|
|
|
* <method name> : <lines covered>
|
|
|
|
*/
|
2017-06-24 00:08:02 +03:00
|
|
|
scriptOffsets.forEach(function(element, index) {
|
2017-01-22 02:04:28 +03:00
|
|
|
if (!element) {
|
2016-03-12 03:18:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
tempMethodCov.push(index);
|
|
|
|
});
|
|
|
|
methodNames[scriptName][method] = tempMethodCov;
|
|
|
|
});
|
|
|
|
|
|
|
|
return methodNames;
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2016-03-12 03:18:19 +03:00
|
|
|
|
2015-12-24 01:04:49 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-06-24 00:08:02 +03:00
|
|
|
CoverageCollector.prototype.recordTestCoverage = function(testName) {
|
2017-04-29 17:47:24 +03:00
|
|
|
let ccov_scope = {};
|
2018-01-30 08:17:48 +03:00
|
|
|
const { OS } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/osfile.jsm",
|
|
|
|
ccov_scope
|
|
|
|
);
|
2017-04-29 17:47:24 +03:00
|
|
|
|
2015-12-24 01:04:49 +03:00
|
|
|
dump("Collecting coverage for: " + testName + "\n");
|
|
|
|
let rawLines = this._getLinesCovered(testName);
|
2016-03-12 03:21:36 +03:00
|
|
|
let methods = this._getMethodNames();
|
|
|
|
let uncoveredLines = this._getUncoveredLines();
|
2015-12-24 01:04:49 +03:00
|
|
|
let result = [];
|
2016-03-12 03:21:36 +03:00
|
|
|
let versionControlBlock = { version: 1.0 };
|
|
|
|
result.push(versionControlBlock);
|
|
|
|
|
2015-12-24 01:04:49 +03:00
|
|
|
for (let scriptName in rawLines) {
|
|
|
|
let rec = {
|
|
|
|
testUrl: testName,
|
|
|
|
sourceFile: scriptName,
|
2016-03-12 03:21:36 +03:00
|
|
|
methods: {},
|
|
|
|
covered: [],
|
|
|
|
uncovered: [],
|
2015-12-24 01:04:49 +03:00
|
|
|
};
|
2016-03-12 03:21:36 +03:00
|
|
|
|
2017-06-24 00:08:02 +03:00
|
|
|
if (
|
|
|
|
typeof methods[scriptName] != "undefined" &&
|
|
|
|
methods[scriptName] != null
|
2018-02-02 19:44:26 +03:00
|
|
|
) {
|
|
|
|
for (let [methodName, methodLines] of Object.entries(
|
|
|
|
methods[scriptName]
|
|
|
|
)) {
|
2017-01-22 02:04:28 +03:00
|
|
|
rec.methods[methodName] = methodLines;
|
|
|
|
}
|
2016-03-12 03:21:36 +03:00
|
|
|
}
|
|
|
|
|
2015-12-24 01:04:49 +03:00
|
|
|
for (let line of rawLines[scriptName]) {
|
|
|
|
rec.covered.push(line);
|
|
|
|
}
|
2016-03-12 03:21:36 +03:00
|
|
|
|
2017-01-22 02:04:28 +03:00
|
|
|
for (let line of uncoveredLines[scriptName]) {
|
2016-03-12 03:21:36 +03:00
|
|
|
rec.uncovered.push(line);
|
|
|
|
}
|
|
|
|
|
2015-12-24 01:04:49 +03:00
|
|
|
result.push(rec);
|
|
|
|
}
|
|
|
|
let arr = this._encoder.encode(JSON.stringify(result, null, 2));
|
2017-06-24 00:08:04 +03:00
|
|
|
let path = this._prefix + "/jscov_" + Date.now() + ".json";
|
2015-12-24 01:04:49 +03:00
|
|
|
dump("Writing coverage to: " + path + "\n");
|
2017-06-24 00:08:02 +03:00
|
|
|
return OS.File.writeAtomic(path, arr, { tmpPath: path + ".tmp" });
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2015-12-24 01:04:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tear down the debugger after all tests are complete.
|
|
|
|
*/
|
2017-06-24 00:08:02 +03:00
|
|
|
CoverageCollector.prototype.finalize = function() {
|
2015-12-24 01:04:49 +03:00
|
|
|
this._dbg.removeAllDebuggees();
|
|
|
|
this._dbg.enabled = false;
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|