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