merge_coverage.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python3
  2. """Merges gcovr JSON reports of different builds (the host's coverage build and the portable one that ran in the VMs) by
  3. line: a line counts when any report ran it, only lines the first report knows are kept (other compilers split inlined code
  4. differently). Prints the per file difference and the totals.
  5. Usage: merge_coverage.py <base.json> <more.json>... [--top N]"""
  6. import json, sys
  7. args = [a for a in sys.argv[1:] if not a.startswith('--')]
  8. top = int(sys.argv[sys.argv.index('--top') + 1]) if '--top' in sys.argv else 25
  9. if '--top' in sys.argv:
  10. args.remove(sys.argv[sys.argv.index('--top') + 1])
  11. def load(path):
  12. return {f['file']: {l['line_number']: l['count'] for l in f['lines']} for f in json.load(open(path))['files']}
  13. base = load(args[0])
  14. merged = {f: dict(lines) for f, lines in base.items()}
  15. for path in args[1:]:
  16. for f, lines in load(path).items():
  17. if f in merged:
  18. for number, count in lines.items():
  19. if number in merged[f]:
  20. merged[f][number] += count
  21. rows = []
  22. total = [0, 0, 0]
  23. for f, lines in base.items():
  24. before = sum(1 for c in lines.values() if c == 0)
  25. after = sum(1 for c in merged[f].values() if c == 0)
  26. total[0] += len(lines)
  27. total[1] += before
  28. total[2] += after
  29. if before != after:
  30. rows.append((before - after, f.rsplit('/', 1)[-1], before, after))
  31. for row in sorted(rows, reverse=True)[:top]:
  32. print(*row)
  33. print('lines', total[0], 'missed before', total[1], 'after', total[2])