coverage.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import re
  2. import _common
  3. import lib.test_const as consts
  4. def get_coverage_filter_regexp(pattern, cache={}):
  5. return cache[pattern] if pattern in cache else cache.setdefault(pattern, re.compile(pattern))
  6. def should_be_covered(unit, filters):
  7. if unit.get("FORCE_COVERAGE_DISABLED") == "yes":
  8. return False
  9. if unit.get("FORCE_COVERAGE_ENABLED") == "yes":
  10. return True
  11. unit_path = _common.get_norm_unit_path(unit)
  12. return not any(pred(unit_path) for pred in filters)
  13. def get_cpp_coverage_filters(unit, filters=[]):
  14. # don`t calculate filters if it already was calculated
  15. if filters:
  16. return filters
  17. coverage_target_regexp = unit.get("COVERAGE_TARGET_REGEXP") or None
  18. coverage_exclude_regexp = unit.get("COVERAGE_EXCLUDE_REGEXP") or None
  19. if coverage_target_regexp:
  20. cov_re = get_coverage_filter_regexp(coverage_target_regexp)
  21. filters.append(lambda x: re.match(cov_re, x) is None)
  22. if coverage_exclude_regexp:
  23. cov_exclude_re = get_coverage_filter_regexp(coverage_exclude_regexp)
  24. filters.append(lambda x: re.match(cov_exclude_re, x) is not None)
  25. if unit.get("ENABLE_CONTRIB_COVERAGE") != "yes":
  26. paths_to_exclude = ("contrib",)
  27. filters.append(lambda x: x.startswith(paths_to_exclude))
  28. return filters
  29. def add_cpp_coverage_ldflags(unit):
  30. ldflags = unit.get("LDFLAGS")
  31. changed = False
  32. for flag in consts.COVERAGE_LDFLAGS:
  33. if flag not in ldflags:
  34. ldflags = ldflags + ' ' + flag
  35. changed = True
  36. if changed:
  37. unit.set(["LDFLAGS", ldflags])
  38. def add_cpp_coverage_cflags(unit):
  39. cflags = unit.get("CFLAGS")
  40. changed = False
  41. for flag in consts.COVERAGE_CFLAGS:
  42. if flag not in cflags:
  43. cflags = cflags + ' ' + flag
  44. changed = True
  45. if changed:
  46. unit.set(["CFLAGS", cflags])
  47. def onset_cpp_coverage_flags(unit):
  48. if unit.get("CLANG_COVERAGE") == "no":
  49. return
  50. filters = get_cpp_coverage_filters(unit)
  51. if should_be_covered(unit, filters):
  52. add_cpp_coverage_cflags(unit)
  53. add_cpp_coverage_ldflags(unit)