|
| 1 | +from __future__ import print_function |
| 2 | +import sys |
| 3 | +import os |
| 4 | + |
| 5 | +SUMMARY_XML_FILENAME = "Summary.xml" |
| 6 | + |
| 7 | +# Note that this is python2 compatible, since that's currently what's installed on most CI images. |
| 8 | + |
| 9 | + |
| 10 | +def check_coverage(root_dir, min_percentage): |
| 11 | + # Walk the root directory looking for the summary file that |
| 12 | + # is output by ther code coverage checks. It's possible that |
| 13 | + # we'll need to refine this later in case there are multiple |
| 14 | + # such files. |
| 15 | + summary_xml = None |
| 16 | + for dirpath, _, filenames in os.walk(root_dir): |
| 17 | + if SUMMARY_XML_FILENAME in filenames: |
| 18 | + summary_xml = os.path.join(dirpath, SUMMARY_XML_FILENAME) |
| 19 | + break |
| 20 | + if not summary_xml: |
| 21 | + print("Couldn't find {} in root directory".format(SUMMARY_XML_FILENAME)) |
| 22 | + sys.exit(1) |
| 23 | + |
| 24 | + with open(summary_xml) as f: |
| 25 | + # Rather than try to parse the XML, just look for a line of the form |
| 26 | + # <Linecoverage>73.9</Linecoverage> |
| 27 | + lines = f.readlines() |
| 28 | + for l in lines: |
| 29 | + if "Linecoverage" in l: |
| 30 | + pct = l.replace("<Linecoverage>", "").replace("</Linecoverage>", "") |
| 31 | + pct = float(pct) |
| 32 | + if pct < min_percentage: |
| 33 | + print( |
| 34 | + "Coverage {} is below the min percentage of {}.".format( |
| 35 | + pct, min_percentage |
| 36 | + ) |
| 37 | + ) |
| 38 | + sys.exit(1) |
| 39 | + else: |
| 40 | + print( |
| 41 | + "Coverage {} is above the min percentage of {}.".format( |
| 42 | + pct, min_percentage |
| 43 | + ) |
| 44 | + ) |
| 45 | + sys.exit(0) |
| 46 | + |
| 47 | + # Couldn't find the results in the file. |
| 48 | + print("Couldn't find Linecoverage in summary file") |
| 49 | + sys.exit(1) |
| 50 | + |
| 51 | + |
| 52 | +def main(): |
| 53 | + root_dir = sys.argv[1] |
| 54 | + min_percent = float(sys.argv[2]) |
| 55 | + if min_percent > 0: |
| 56 | + # This allows us to set 0% coverage on 2018.4 |
| 57 | + check_coverage(root_dir, min_percent) |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + main() |
0 commit comments