@@ -44,8 +44,7 @@ def start_coverage_collection() -> bool:
4444 import coverage as coverage_module
4545 except ImportError :
4646 logger .warning (
47- "Coverage requested but 'coverage' package is not installed. "
48- "Install it with: pip install coverage"
47+ "Coverage requested but 'coverage' package is not installed. Install it with: pip install coverage"
4948 )
5049 return False
5150
@@ -108,38 +107,39 @@ def take_coverage_snapshot(baseline: bool = False) -> dict:
108107 _cov_instance .stop ()
109108 coverage = {}
110109
111- if baseline :
112- data = _cov_instance .get_data ()
113- for filename in data .measured_files ():
114- if not _is_user_file (filename ):
115- continue
116- try :
117- _ , statements , _ , missing , _ = _cov_instance .analysis2 (filename )
118- missing_set = set (missing )
119- lines_map = {}
120- for line in statements :
121- lines_map [str (line )] = 0 if line in missing_set else 1
122- branch_data = _get_branch_data (data , filename )
123- if lines_map :
124- coverage [filename ] = {"lines" : lines_map , ** branch_data }
125- except Exception as e :
126- logger .debug (f"Failed to analyze { filename } : { e } " )
127- continue
128- else :
129- data = _cov_instance .get_data ()
130- for filename in data .measured_files ():
131- if not _is_user_file (filename ):
132- continue
133- lines = data .lines (filename )
134- if lines :
135- branch_data = _get_branch_data (data , filename )
136- coverage [filename ] = {
137- "lines" : {str (line ): 1 for line in lines },
138- ** branch_data ,
139- }
140-
141- _cov_instance .erase ()
142- _cov_instance .start ()
110+ try :
111+ if baseline :
112+ data = _cov_instance .get_data ()
113+ for filename in data .measured_files ():
114+ if not _is_user_file (filename ):
115+ continue
116+ try :
117+ _ , statements , _ , missing , _ = _cov_instance .analysis2 (filename )
118+ missing_set = set (missing )
119+ lines_map = {}
120+ for line in statements :
121+ lines_map [str (line )] = 0 if line in missing_set else 1
122+ branch_data = _get_branch_data (data , filename )
123+ if lines_map :
124+ coverage [filename ] = {"lines" : lines_map , ** branch_data }
125+ except Exception as e :
126+ logger .debug (f"Failed to analyze { filename } : { e } " )
127+ continue
128+ else :
129+ data = _cov_instance .get_data ()
130+ for filename in data .measured_files ():
131+ if not _is_user_file (filename ):
132+ continue
133+ lines = data .lines (filename )
134+ if lines :
135+ branch_data = _get_branch_data (data , filename )
136+ coverage [filename ] = {
137+ "lines" : {str (line ): 1 for line in lines },
138+ ** branch_data ,
139+ }
140+ finally :
141+ _cov_instance .erase ()
142+ _cov_instance .start ()
143143
144144 return coverage
145145
@@ -166,6 +166,9 @@ def _get_branch_data(data, filename: str) -> dict:
166166 if not data .has_arcs ():
167167 return {"totalBranches" : 0 , "coveredBranches" : 0 , "branches" : {}}
168168
169+ if _cov_instance is None :
170+ return {"totalBranches" : 0 , "coveredBranches" : 0 , "branches" : {}}
171+
169172 analysis = _cov_instance ._analyze (filename )
170173 numbers = analysis .numbers
171174
0 commit comments