This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreport.js
168 lines (157 loc) · 4.64 KB
/
report.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict'
const debug = require('debug')('cov8:report')
const fs = require('fs')
const path = require('path')
const libCoverage = require('istanbul-lib-coverage')
const CoverageMap = require('istanbul-lib-coverage/lib/coverage-map').CoverageMap
const libReport = require('istanbul-lib-report')
const reports = require('istanbul-reports')
const v8ToIstanbul = require('v8-to-istanbul')
const uuid = require('uuid')
module.exports = class Report {
/**
* Construct new report instance
* @param {String} directory Coverage directory
* @param {Array} [reporters] List of reporters
*/
constructor (directory, reporters = []) {
debug(`Init new report handler with reporters: [${reporters.join(', ')}] on directory ${directory}`)
this.reporters = reporters
this.directory = directory
this.reportsPaths = path.resolve(this.directory, './tmp')
debug(`reportsPaths=${this.reportsPaths}`)
}
/**
* Store coverage into coverage.map
* @param {Object} result V8 result
*/
store (result) {
debug('Try to store reports')
const reportsList = this.getReports(result)
debug('Store reports')
reportsList.forEach((report) => {
fs.writeFileSync(path.join(this.reportsPaths, `${uuid.v4()}.json`), JSON.stringify(report))
})
debug('Reports stored')
}
/**
* Get reports from V8
* @param {Object} result V8 result
*/
getReports (reports) {
debug(`Format ${reports.length} reports`)
return reports.map((report) => {
const reportFormatted = v8ToIstanbul(report.url)
reportFormatted.applyCoverage(report.functions)
return reportFormatted.toIstanbul()
})
}
/**
* Retrieve previous reports from previous test
*/
getPreviousReports () {
debug('Try to get previous map')
let reports = []
fs.readdirSync(this.reportsPaths).map((reportName) => {
let report = null
try {
report = JSON.parse(fs.readFileSync(path.join(this.reportsPaths, reportName)))
} catch (err) {
return debug(`Got an error on reading a report: ${err.message}`)
}
reports.push(report)
})
debug('Previous reports got')
return reports
}
/**
* Merge previous map with reports
* @param {CoverageMap} map
* @param {Array} reports
*/
mergeMap (map, reports) {
debug('Merge map with reports')
reports.forEach((report) => {
this.mergeReport(map, report)
})
return map
}
/**
* Merge previous map with one report
* @param {CoverageMap} map
* @param {Object} report
*/
mergeReport (map, report) {
debug('Merge 1 report to map')
let sourceMap
if (report instanceof CoverageMap) {
sourceMap = report
} else {
sourceMap = new CoverageMap(report)
}
Object.keys(sourceMap.data).forEach((k) => {
let fc = sourceMap.data[k]
if (map.data[k]) {
this.mergeReportData(map.data[k], fc)
} else {
map.data[k] = fc
}
})
}
/**
* Merge previous map with one report data
* @param {CoverageMap} map
* @param {Object} report data
*/
mergeReportData (map, reportData) {
debug('Merge 1 report data to map')
Object.keys(reportData.branchMap).forEach(function (k) {
if (!map.data.branchMap[k]) {
map.data.branchMap[k] = reportData.branchMap[k]
}
})
Object.keys(reportData.fnMap).forEach(function (k) {
if (!map.data.fnMap[k]) {
map.data.fnMap[k] = reportData.fnMap[k]
}
})
Object.keys(reportData.statementMap).forEach(function (k) {
if (!map.data.statementMap[k]) {
map.data.statementMap[k] = reportData.statementMap[k]
}
})
Object.keys(reportData.s).forEach(function (k) {
map.data.s[k] += reportData.s[k]
})
Object.keys(reportData.f).forEach(function (k) {
map.data.f[k] += reportData.f[k]
})
Object.keys(reportData.b).forEach(function (k) {
let retArray = map.data.b[k]
let secondArray = reportData.b[k]
if (!retArray) {
map.data.b[k] = secondArray
return
}
for (let i = 0; i < retArray.length; i += 1) {
retArray[i] += secondArray[i]
}
})
}
/**
* Generate report with coverage map and istanbul report
*/
generateReport () {
debug('Try to generate report')
const reportsList = this.getPreviousReports()
const map = this.mergeMap(libCoverage.createCoverageMap({}), reportsList)
const context = libReport.createContext({
dir: this.directory
})
const tree = libReport.summarizers.pkg(map)
this.reporters.forEach((reporter) => {
debug(`Generate report for reporter ${reporter}`)
tree.visit(reports.create(reporter), context)
})
}
}