coverage_check.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import argparse
  12. import xmltodict
  13. import sys
  14. import subprocess
  15. def check_total_coverage(coverage_file, threshold=80):
  16. """Check the total project coverage."""
  17. with open(coverage_file) as f:
  18. data = xmltodict.parse(f.read())
  19. total_coverage = float(data['coverage']['@line-rate']) * 100
  20. print(f"Total Coverage: {total_coverage:.2f}%")
  21. if total_coverage < threshold:
  22. print(f"Total project coverage is below {threshold}%: {total_coverage:.2f}%")
  23. sys.exit(1)
  24. def check_changed_files_coverage(coverage_file, changed_files, threshold=80):
  25. """Check the coverage of changed files."""
  26. with open(coverage_file) as f:
  27. data = xmltodict.parse(f.read())
  28. # Handle multiple packages in the coverage report
  29. packages = data["coverage"]["packages"]["package"]
  30. if not isinstance(packages, list):
  31. packages = [packages]
  32. # Extract coverage data for all files
  33. files = {}
  34. for package in packages:
  35. classes = package["classes"]["class"]
  36. if not isinstance(classes, list):
  37. classes = [classes]
  38. for cls in classes:
  39. files[cls["@filename"]] = float(cls["@line-rate"]) * 100
  40. qty = 0
  41. sum_coverage = 0
  42. for file in changed_files:
  43. if file in files:
  44. coverage = files[file]
  45. print(f"Coverage for {file}: {coverage:.2f}%")
  46. sum_coverage += coverage
  47. qty += 1
  48. else:
  49. print(f"No coverage data found for {file}")
  50. if sum_coverage/qty < threshold:
  51. print(f"Coverage for changed files is below {threshold}%: {sum_coverage/qty:.2f}%")
  52. sys.exit(1)
  53. print(f"Coverage for changed files: {sum_coverage/qty:.2f}%")
  54. def get_changed_files(base_branch):
  55. """Get the list of changed Python files in the pull request."""
  56. try:
  57. result = subprocess.run(
  58. ['git', 'diff', '--name-only', f"origin/{base_branch}", '--', '*.py'],
  59. capture_output=True,
  60. text=True,
  61. check=True,
  62. )
  63. changed_files = [
  64. file.replace("taipy/", "") for file in result.stdout.strip().splitlines() if not file.startswith(('tests/', 'tools/'))
  65. ]
  66. return changed_files
  67. except subprocess.CalledProcessError as e:
  68. print(f"Error fetching changed files: {e}")
  69. sys.exit(1)
  70. if __name__ == '__main__':
  71. parser = argparse.ArgumentParser(description="Coverage check script.")
  72. parser.add_argument('command', choices=['check-total', 'check-changed'], help="Command to execute")
  73. parser.add_argument('--coverage-file', default='coverage.xml', help="Path to the coverage XML file")
  74. parser.add_argument('--threshold', type=float, default=80, help="Coverage threshold percentage")
  75. parser.add_argument('--base-branch', help="Base branch for comparing changed files")
  76. args = parser.parse_args()
  77. if args.command == 'check-total':
  78. check_total_coverage(args.coverage_file, args.threshold)
  79. elif args.command == 'check-changed':
  80. if not args.base_branch:
  81. print("Error: --base-branch is required for check-changed")
  82. sys.exit(1)
  83. changed_files = get_changed_files(args.base_branch)
  84. if not changed_files:
  85. print("No relevant Python files changed.")
  86. sys.exit(0)
  87. check_changed_files_coverage(args.coverage_file, changed_files, args.threshold)