merge_coverage.py 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/env python3
  2. """Merge set/count coverage profiles by source block, rejecting mismatched layouts."""
  3. import argparse
  4. from pathlib import Path
  5. def merge(paths):
  6. blocks = {}
  7. for path in paths:
  8. lines = Path(path).read_text().splitlines()
  9. if not lines or lines[0] not in ("mode: set", "mode: count", "mode: atomic"):
  10. raise ValueError(f"invalid profile header: {path}")
  11. for line in lines[1:]:
  12. location, statements, count = line.rsplit(" ", 2)
  13. statements, count = int(statements), int(count)
  14. previous = blocks.get(location)
  15. if previous is not None and previous[0] != statements:
  16. raise ValueError(f"source layout changed: {location}")
  17. blocks[location] = (statements, max(int(count > 0), previous[1] if previous else 0))
  18. return "mode: set\n" + "".join(f"{loc} {n} {covered}\n" for loc, (n, covered) in sorted(blocks.items()))
  19. def main():
  20. parser = argparse.ArgumentParser(description=__doc__)
  21. parser.add_argument("profiles", nargs="+")
  22. parser.add_argument("--output", required=True)
  23. arguments = parser.parse_args()
  24. Path(arguments.output).write_text(merge(arguments.profiles))
  25. if __name__ == "__main__":
  26. main()