junit-postprocess.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. import os
  3. import glob
  4. import argparse
  5. import xml.etree.ElementTree as ET
  6. from mute_utils import mute_target, update_suite_info, MutedTestCheck
  7. from junit_utils import add_junit_property
  8. def case_iterator(root):
  9. for case in root.findall("testcase"):
  10. cls, method = case.attrib["classname"], case.attrib["name"]
  11. yield case, cls, method
  12. def attach_filename(testcase, filename):
  13. shardname = os.path.splitext(filename)[0]
  14. add_junit_property(testcase, "shard", shardname)
  15. def postprocess_junit(is_mute_test, folder, dry_run):
  16. for fn in glob.glob(os.path.join(folder, "*.xml")):
  17. tree = ET.parse(fn)
  18. root = tree.getroot()
  19. total_muted = 0
  20. for suite in root.findall("testsuite"):
  21. muted_cnt = 0
  22. for case, cls, method in case_iterator(suite):
  23. attach_filename(case, os.path.basename(fn))
  24. if is_mute_test(cls, method):
  25. if mute_target(case):
  26. print(f"mute {cls}::{method}")
  27. muted_cnt += 1
  28. if muted_cnt:
  29. update_suite_info(suite, n_skipped=muted_cnt, n_remove_failures=muted_cnt)
  30. total_muted += muted_cnt
  31. if total_muted:
  32. update_suite_info(root, n_skipped=total_muted, n_remove_failures=total_muted)
  33. print(f"{'(dry-run) ' if dry_run else ''}patch {fn}")
  34. if not dry_run:
  35. tree.write(fn, xml_declaration=True, encoding="UTF-8")
  36. def main():
  37. parser = argparse.ArgumentParser()
  38. parser.add_argument("--filter-file", required=True)
  39. parser.add_argument("--dry-run", action="store_true", default=False)
  40. parser.add_argument("yunit_path")
  41. args = parser.parse_args()
  42. if not os.path.isdir(args.yunit_path):
  43. print(f"{args.yunit_path} is not a directory, exit")
  44. raise SystemExit(-1)
  45. # FIXME: add gtest filter file ?
  46. is_mute_test = MutedTestCheck(args.filter_file)
  47. if not is_mute_test.has_rules:
  48. print("nothing to mute")
  49. return
  50. postprocess_junit(is_mute_test, args.yunit_path, args.dry_run)
  51. if __name__ == "__main__":
  52. main()