4040import subprocess
4141import argparse
4242
43- from vcast_utils import dump , checkVectorCASTVersion , getVectorCASTEncoding
43+ from vcast_utils import dump , checkVectorCASTVersion , getVectorCASTEncoding , checkProjectResults
4444try :
4545 from safe_open import open
4646except :
4747 pass
4848
49+ try :
50+ import math
51+ INF = math .inf
52+ except Exception :
53+ INF = float ("inf" ) # Py2-compatible
4954encFmt = getVectorCASTEncoding ()
5055
5156fileList = []
@@ -116,30 +121,46 @@ def has_branches_covered(line):
116121
117122def get_function_name_line_number (file_path , function , initial_guess ):
118123
119- with open (file_path ,"rb" ) as fd :
124+ with open (file_path , "rb" ) as fd :
120125 lines = [line .decode (encFmt , "replace" ) for line in fd .readlines ()]
121-
122- line_number_closest_so_far = initial_guess ;
123- delta = 9999999999 ;
124126
125- # print(function, line_number_closest_so_far, delta, initial_guess)
126- for count , line in enumerate (reversed (lines [:initial_guess + 1 ])):
127- if function in line .replace (" " ,"" ):
127+ if initial_guess is None or initial_guess >= len (lines ):
128+ initial_guess = len (lines ) - 1
129+
130+ line_number_closest_so_far = initial_guess
131+ delta = INF
132+
133+ for count , line in enumerate (reversed (lines [:initial_guess + 1 ])):
134+ if function in line .replace (" " , "" ):
128135 line_num = initial_guess - count
129136 if abs (line_num - initial_guess ) < delta :
130137 line_number_closest_so_far = line_num
131138 delta = abs (line_num - initial_guess )
132- # print(function, line_number_closest_so_far, delta, initial_guess)
133-
134- # print(line_number_closest_so_far + 1,function)
135- return line_number_closest_so_far + 1 ## add one since python starts from 0
139+
140+ return line_number_closest_so_far + 1 # convert 0-based to 1-based
141+
136142
137143def runCoverageResultsMP (mpFile , verbose = False , testName = "" , source_root = "" ):
138144
139- vcproj = VCProjectApi (mpFile )
140- api = vcproj .project .cover_api
145+ with VCProjectApi (mpFile ) as vcproj :
146+
147+ anyLocalResults , anyImportedResults = checkProjectResults (vcproj )
148+
149+ if anyImportedResults :
150+ importedResultsError = " ** LCOV results does not processing imported results at this time\n \n "
151+ print (importedResultsError )
152+ return importedResultsError
153+
154+ if not anyLocalResults :
155+ localResultsError = " ** No local results in project to process\n \n "
156+ print (localResultsError )
157+ return localResultsError
158+
159+ api = vcproj .project .cover_api
160+
161+ results = runGcovResults (api , verbose = verbose , testName = vcproj .project .name , source_root = source_root )
141162
142- return runGcovResults ( api , verbose = verbose , testName = vcproj . project . name , source_root = source_root )
163+ return results
143164
144165def runGcovResults (api , verbose = False , testName = "" , source_root = "" ) :
145166
@@ -152,7 +173,7 @@ def runGcovResults(api, verbose = False, testName = "", source_root = "") :
152173 except :
153174 prj_dir = os .getcwd ().replace ("\\ " ,"/" ) + "/"
154175
155- # get a sorted listed of all the files with the proj directory stripped off
176+ # get a sorted listed of all the files with the proj directory stripped off
156177 for file in api .SourceFile .all ():
157178 if file .display_name == "" :
158179 continue
0 commit comments