index.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Copyright 2012-2015, Yahoo Inc.
  3. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  4. */
  5. 'use strict';
  6. const { ReportBase } = require('istanbul-lib-report');
  7. class LcovOnlyReport extends ReportBase {
  8. constructor(opts) {
  9. super();
  10. opts = opts || {};
  11. this.file = opts.file || 'lcov.info';
  12. this.projectRoot = opts.projectRoot || process.cwd();
  13. this.contentWriter = null;
  14. }
  15. onStart(root, context) {
  16. this.contentWriter = context.writer.writeFile(this.file);
  17. }
  18. onDetail(node) {
  19. const fc = node.getFileCoverage();
  20. const writer = this.contentWriter;
  21. const functions = fc.f;
  22. const functionMap = fc.fnMap;
  23. const lines = fc.getLineCoverage();
  24. const branches = fc.b;
  25. const branchMap = fc.branchMap;
  26. const summary = node.getCoverageSummary();
  27. const path = require('path');
  28. writer.println('TN:');
  29. const fileName = path.relative(this.projectRoot, fc.path);
  30. writer.println('SF:' + fileName);
  31. Object.values(functionMap).forEach(meta => {
  32. // Some versions of the instrumenter in the wild populate 'loc'
  33. // but not 'decl':
  34. const decl = meta.decl || meta.loc;
  35. writer.println('FN:' + [decl.start.line, meta.name].join(','));
  36. });
  37. writer.println('FNF:' + summary.functions.total);
  38. writer.println('FNH:' + summary.functions.covered);
  39. Object.entries(functionMap).forEach(([key, meta]) => {
  40. const stats = functions[key];
  41. writer.println('FNDA:' + [stats, meta.name].join(','));
  42. });
  43. Object.entries(lines).forEach(entry => {
  44. writer.println('DA:' + entry.join(','));
  45. });
  46. writer.println('LF:' + summary.lines.total);
  47. writer.println('LH:' + summary.lines.covered);
  48. Object.entries(branches).forEach(([key, branchArray]) => {
  49. const meta = branchMap[key];
  50. if (meta) {
  51. const { line } = meta.loc.start;
  52. branchArray.forEach((b, i) => {
  53. writer.println('BRDA:' + [line, key, i, b].join(','));
  54. });
  55. } else {
  56. console.warn('Missing coverage entries in', fileName, key);
  57. }
  58. });
  59. writer.println('BRF:' + summary.branches.total);
  60. writer.println('BRH:' + summary.branches.covered);
  61. writer.println('end_of_record');
  62. }
  63. onEnd() {
  64. this.contentWriter.close();
  65. }
  66. }
  67. module.exports = LcovOnlyReport;