Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80606 views
1
/**
2
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
3
*
4
* This source code is licensed under the BSD-style license found in the
5
* LICENSE file in the root directory of this source tree. An additional grant
6
* of patent rights can be found in the PATENTS file in the same directory.
7
*/
8
'use strict';
9
10
var CoverageInstrumentor = require('cover/instrument').Instrumentor;
11
var fs = require('graceful-fs');
12
13
var COVERAGE_TEMPLATE_PATH = require.resolve('./coverageTemplate');
14
15
var _memoizedCoverageTemplate = null;
16
function _getCoverageTemplate() {
17
if (_memoizedCoverageTemplate === null) {
18
_memoizedCoverageTemplate = require('lodash.template')(
19
fs.readFileSync(COVERAGE_TEMPLATE_PATH, 'utf8')
20
);
21
}
22
return _memoizedCoverageTemplate;
23
}
24
25
function CoverageCollector(sourceText) {
26
this._coverageDataStore = {};
27
this._instrumentedSourceText = null;
28
this._instrumentor = new CoverageInstrumentor();
29
this._origSourceText = sourceText;
30
}
31
32
CoverageCollector.prototype.getCoverageDataStore = function() {
33
return this._coverageDataStore;
34
};
35
36
CoverageCollector.prototype.getInstrumentedSource = function(storageVarName) {
37
if (this._instrumentedSourceText === null) {
38
this._instrumentedSourceText = _getCoverageTemplate()({
39
instrumented: this._instrumentor,
40
coverageStorageVar: storageVarName,
41
source: this._instrumentor.instrument(this._origSourceText)
42
});
43
}
44
return this._instrumentedSourceText;
45
};
46
47
CoverageCollector.prototype.extractRuntimeCoverageInfo = function() {
48
var instrumentationInfo = this._instrumentor.objectify();
49
var coverageInfo = {
50
coveredSpans: [],
51
uncoveredSpans: [],
52
sourceText: this._origSourceText
53
};
54
55
var nodeIndex;
56
57
// Find all covered spans
58
for (nodeIndex in this._coverageDataStore.nodes) {
59
coverageInfo.coveredSpans.push(instrumentationInfo.nodes[nodeIndex].loc);
60
}
61
62
// Find all definitely uncovered spans
63
for (nodeIndex in instrumentationInfo.nodes) {
64
if (!this._coverageDataStore.nodes.hasOwnProperty(nodeIndex)) {
65
coverageInfo.uncoveredSpans.push(
66
instrumentationInfo.nodes[nodeIndex].loc
67
);
68
}
69
}
70
71
return coverageInfo;
72
};
73
74
module.exports = CoverageCollector;
75
76