report.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. const Exclude = require('test-exclude')
  2. const libCoverage = require('istanbul-lib-coverage')
  3. const libReport = require('istanbul-lib-report')
  4. const reports = require('istanbul-reports')
  5. const { readdirSync, readFileSync, statSync } = require('fs')
  6. const { isAbsolute, resolve, extname } = require('path')
  7. const { pathToFileURL, fileURLToPath } = require('url')
  8. const getSourceMapFromFile = require('./source-map-from-file')
  9. // TODO: switch back to @c88/v8-coverage once patch is landed.
  10. const v8toIstanbul = require('v8-to-istanbul')
  11. const isCjsEsmBridgeCov = require('./is-cjs-esm-bridge')
  12. const util = require('util')
  13. const debuglog = util.debuglog('c8')
  14. class Report {
  15. constructor ({
  16. exclude,
  17. extension,
  18. excludeAfterRemap,
  19. include,
  20. reporter,
  21. reportsDirectory,
  22. tempDirectory,
  23. watermarks,
  24. omitRelative,
  25. wrapperLength,
  26. resolve: resolvePaths,
  27. all,
  28. src,
  29. allowExternal = false,
  30. skipFull,
  31. excludeNodeModules
  32. }) {
  33. this.reporter = reporter
  34. this.reportsDirectory = reportsDirectory
  35. this.tempDirectory = tempDirectory
  36. this.watermarks = watermarks
  37. this.resolve = resolvePaths
  38. this.exclude = new Exclude({
  39. exclude: exclude,
  40. include: include,
  41. extension: extension,
  42. relativePath: !allowExternal,
  43. excludeNodeModules: excludeNodeModules
  44. })
  45. this.excludeAfterRemap = excludeAfterRemap
  46. this.shouldInstrumentCache = new Map()
  47. this.omitRelative = omitRelative
  48. this.sourceMapCache = {}
  49. this.wrapperLength = wrapperLength
  50. this.all = all
  51. this.src = this._getSrc(src)
  52. this.skipFull = skipFull
  53. }
  54. _getSrc (src) {
  55. if (typeof src === 'string') {
  56. return [src]
  57. } else if (Array.isArray(src)) {
  58. return src
  59. } else {
  60. return [process.cwd()]
  61. }
  62. }
  63. async run () {
  64. const context = libReport.createContext({
  65. dir: this.reportsDirectory,
  66. watermarks: this.watermarks,
  67. coverageMap: await this.getCoverageMapFromAllCoverageFiles()
  68. })
  69. for (const _reporter of this.reporter) {
  70. reports.create(_reporter, {
  71. skipEmpty: false,
  72. skipFull: this.skipFull,
  73. maxCols: process.stdout.columns || 100
  74. }).execute(context)
  75. }
  76. }
  77. async getCoverageMapFromAllCoverageFiles () {
  78. // the merge process can be very expensive, and it's often the case that
  79. // check-coverage is called immediately after a report. We memoize the
  80. // result from getCoverageMapFromAllCoverageFiles() to address this
  81. // use-case.
  82. if (this._allCoverageFiles) return this._allCoverageFiles
  83. const map = libCoverage.createCoverageMap()
  84. const v8ProcessCov = this._getMergedProcessCov()
  85. const resultCountPerPath = new Map()
  86. const possibleCjsEsmBridges = new Map()
  87. for (const v8ScriptCov of v8ProcessCov.result) {
  88. try {
  89. const sources = this._getSourceMap(v8ScriptCov)
  90. const path = resolve(this.resolve, v8ScriptCov.url)
  91. const converter = v8toIstanbul(path, this.wrapperLength, sources, (path) => {
  92. if (this.excludeAfterRemap) {
  93. return !this._shouldInstrument(path)
  94. }
  95. })
  96. await converter.load()
  97. if (resultCountPerPath.has(path)) {
  98. resultCountPerPath.set(path, resultCountPerPath.get(path) + 1)
  99. } else {
  100. resultCountPerPath.set(path, 0)
  101. }
  102. if (isCjsEsmBridgeCov(v8ScriptCov)) {
  103. possibleCjsEsmBridges.set(converter, {
  104. path,
  105. functions: v8ScriptCov.functions
  106. })
  107. } else {
  108. converter.applyCoverage(v8ScriptCov.functions)
  109. map.merge(converter.toIstanbul())
  110. }
  111. } catch (err) {
  112. debuglog(`file: ${v8ScriptCov.url} error: ${err.stack}`)
  113. }
  114. }
  115. for (const [converter, { path, functions }] of possibleCjsEsmBridges) {
  116. if (resultCountPerPath.get(path) <= 1) {
  117. converter.applyCoverage(functions)
  118. map.merge(converter.toIstanbul())
  119. }
  120. }
  121. this._allCoverageFiles = map
  122. return this._allCoverageFiles
  123. }
  124. /**
  125. * Returns source-map and fake source file, if cached during Node.js'
  126. * execution. This is used to support tools like ts-node, which transpile
  127. * using runtime hooks.
  128. *
  129. * Note: requires Node.js 13+
  130. *
  131. * @return {Object} sourceMap and fake source file (created from line #s).
  132. * @private
  133. */
  134. _getSourceMap (v8ScriptCov) {
  135. const sources = {}
  136. const sourceMapAndLineLengths = this.sourceMapCache[pathToFileURL(v8ScriptCov.url).href]
  137. if (sourceMapAndLineLengths) {
  138. // See: https://github.com/nodejs/node/pull/34305
  139. if (!sourceMapAndLineLengths.data) return
  140. sources.sourceMap = {
  141. sourcemap: sourceMapAndLineLengths.data
  142. }
  143. if (sourceMapAndLineLengths.lineLengths) {
  144. let source = ''
  145. sourceMapAndLineLengths.lineLengths.forEach(length => {
  146. source += `${''.padEnd(length, '.')}\n`
  147. })
  148. sources.source = source
  149. }
  150. }
  151. return sources
  152. }
  153. /**
  154. * Returns the merged V8 process coverage.
  155. *
  156. * The result is computed from the individual process coverages generated
  157. * by Node. It represents the sum of their counts.
  158. *
  159. * @return {ProcessCov} Merged V8 process coverage.
  160. * @private
  161. */
  162. _getMergedProcessCov () {
  163. const { mergeProcessCovs } = require('@bcoe/v8-coverage')
  164. const v8ProcessCovs = []
  165. const fileIndex = new Set() // Set<string>
  166. for (const v8ProcessCov of this._loadReports()) {
  167. if (this._isCoverageObject(v8ProcessCov)) {
  168. if (v8ProcessCov['source-map-cache']) {
  169. Object.assign(this.sourceMapCache, this._normalizeSourceMapCache(v8ProcessCov['source-map-cache']))
  170. }
  171. v8ProcessCovs.push(this._normalizeProcessCov(v8ProcessCov, fileIndex))
  172. }
  173. }
  174. if (this.all) {
  175. const emptyReports = []
  176. v8ProcessCovs.unshift({
  177. result: emptyReports
  178. })
  179. const workingDirs = this.src
  180. const { extension } = this.exclude
  181. for (const workingDir of workingDirs) {
  182. this.exclude.globSync(workingDir).forEach((f) => {
  183. const fullPath = resolve(workingDir, f)
  184. if (!fileIndex.has(fullPath)) {
  185. const ext = extname(fullPath)
  186. if (extension.includes(ext)) {
  187. const stat = statSync(fullPath)
  188. const sourceMap = getSourceMapFromFile(fullPath)
  189. if (sourceMap) {
  190. this.sourceMapCache[pathToFileURL(fullPath)] = { data: sourceMap }
  191. }
  192. emptyReports.push({
  193. scriptId: 0,
  194. url: resolve(fullPath),
  195. functions: [{
  196. functionName: '(empty-report)',
  197. ranges: [{
  198. startOffset: 0,
  199. endOffset: stat.size,
  200. count: 0
  201. }],
  202. isBlockCoverage: true
  203. }]
  204. })
  205. }
  206. }
  207. })
  208. }
  209. }
  210. return mergeProcessCovs(v8ProcessCovs)
  211. }
  212. /**
  213. * Make sure v8ProcessCov actually contains coverage information.
  214. *
  215. * @return {boolean} does it look like v8ProcessCov?
  216. * @private
  217. */
  218. _isCoverageObject (maybeV8ProcessCov) {
  219. return maybeV8ProcessCov && Array.isArray(maybeV8ProcessCov.result)
  220. }
  221. /**
  222. * Returns the list of V8 process coverages generated by Node.
  223. *
  224. * @return {ProcessCov[]} Process coverages generated by Node.
  225. * @private
  226. */
  227. _loadReports () {
  228. const reports = []
  229. for (const file of readdirSync(this.tempDirectory)) {
  230. try {
  231. reports.push(JSON.parse(readFileSync(
  232. resolve(this.tempDirectory, file),
  233. 'utf8'
  234. )))
  235. } catch (err) {
  236. debuglog(`${err.stack}`)
  237. }
  238. }
  239. return reports
  240. }
  241. /**
  242. * Normalizes a process coverage.
  243. *
  244. * This function replaces file URLs (`url` property) by their corresponding
  245. * system-dependent path and applies the current inclusion rules to filter out
  246. * the excluded script coverages.
  247. *
  248. * The result is a copy of the input, with script coverages filtered based
  249. * on their `url` and the current inclusion rules.
  250. * There is no deep cloning.
  251. *
  252. * @param v8ProcessCov V8 process coverage to normalize.
  253. * @param fileIndex a Set<string> of paths discovered in coverage
  254. * @return {v8ProcessCov} Normalized V8 process coverage.
  255. * @private
  256. */
  257. _normalizeProcessCov (v8ProcessCov, fileIndex) {
  258. const result = []
  259. for (const v8ScriptCov of v8ProcessCov.result) {
  260. // https://github.com/nodejs/node/pull/35498 updates Node.js'
  261. // builtin module filenames:
  262. if (/^node:/.test(v8ScriptCov.url)) {
  263. v8ScriptCov.url = `${v8ScriptCov.url.replace(/^node:/, '')}.js`
  264. }
  265. if (/^file:\/\//.test(v8ScriptCov.url)) {
  266. try {
  267. v8ScriptCov.url = fileURLToPath(v8ScriptCov.url)
  268. fileIndex.add(v8ScriptCov.url)
  269. } catch (err) {
  270. debuglog(`${err.stack}`)
  271. continue
  272. }
  273. }
  274. if ((!this.omitRelative || isAbsolute(v8ScriptCov.url))) {
  275. if (this.excludeAfterRemap || this._shouldInstrument(v8ScriptCov.url)) {
  276. result.push(v8ScriptCov)
  277. }
  278. }
  279. }
  280. return { result }
  281. }
  282. /**
  283. * Normalizes a V8 source map cache.
  284. *
  285. * This function normalizes file URLs to a system-independent format.
  286. *
  287. * @param v8SourceMapCache V8 source map cache to normalize.
  288. * @return {v8SourceMapCache} Normalized V8 source map cache.
  289. * @private
  290. */
  291. _normalizeSourceMapCache (v8SourceMapCache) {
  292. const cache = {}
  293. for (const fileURL of Object.keys(v8SourceMapCache)) {
  294. cache[pathToFileURL(fileURLToPath(fileURL)).href] = v8SourceMapCache[fileURL]
  295. }
  296. return cache
  297. }
  298. /**
  299. * this.exclude.shouldInstrument with cache
  300. *
  301. * @private
  302. * @return {boolean}
  303. */
  304. _shouldInstrument (filename) {
  305. const cacheResult = this.shouldInstrumentCache.get(filename)
  306. if (cacheResult !== undefined) {
  307. return cacheResult
  308. }
  309. const result = this.exclude.shouldInstrument(filename)
  310. this.shouldInstrumentCache.set(filename, result)
  311. return result
  312. }
  313. }
  314. module.exports = function (opts) {
  315. return new Report(opts)
  316. }