29
29
import java .util .Set ;
30
30
import java .util .stream .Collectors ;
31
31
import javax .xml .stream .XMLStreamException ;
32
+ import org .slf4j .Logger ;
33
+ import org .slf4j .LoggerFactory ;
32
34
import org .sonar .api .batch .fs .InputFile ;
33
35
import org .sonar .api .batch .sensor .Sensor ;
34
36
import org .sonar .api .batch .sensor .SensorContext ;
35
37
import org .sonar .api .batch .sensor .SensorDescriptor ;
36
38
import org .sonar .api .batch .sensor .coverage .NewCoverage ;
37
39
import org .sonar .api .config .Configuration ;
38
- import org .slf4j .Logger ;
39
- import org .slf4j .LoggerFactory ;
40
40
import org .sonar .plugins .python .EmptyReportException ;
41
41
import org .sonar .plugins .python .Python ;
42
+ import org .sonar .plugins .python .PythonReportSensor ;
42
43
import org .sonar .plugins .python .warnings .AnalysisWarningsWrapper ;
43
44
44
- import static org .sonar .plugins .python .PythonReportSensor .getReports ;
45
-
46
45
public class PythonCoverageSensor implements Sensor {
47
46
48
47
private static final Logger LOG = LoggerFactory .getLogger (PythonCoverageSensor .class );
@@ -73,24 +72,38 @@ public void execute(SensorContext context) {
73
72
74
73
warnDeprecatedPropertyUsage (config );
75
74
76
- HashSet <InputFile > filesCovered = new HashSet <>();
77
- List <File > reports = getCoverageReports (baseDir , config );
78
- if (!reports .isEmpty ()) {
79
- LOG .info ("Python test coverage" );
80
- for (File report : uniqueAbsolutePaths (reports )) {
81
- Map <InputFile , NewCoverage > coverageMeasures = parseReport (report , context );
82
- saveMeasures (coverageMeasures , filesCovered );
75
+ try {
76
+ HashSet <InputFile > filesCovered = new HashSet <>();
77
+ List <File > reports = getCoverageReports (baseDir , config );
78
+ if (!reports .isEmpty ()) {
79
+ LOG .info ("Python test coverage" );
80
+ for (File report : uniqueAbsolutePaths (reports )) {
81
+ importReport (context , report , filesCovered );
82
+ }
83
83
}
84
+ } catch (Exception e ) {
85
+ LOG .warn ("Cannot read coverage report, the following exception occurred: '{}'" , e .getMessage ());
86
+ analysisWarnings .addUnique ("An error occurred while trying to import coverage report(s)" );
87
+ }
88
+ }
89
+
90
+ private void importReport (SensorContext context , File report , HashSet <InputFile > filesCovered ) {
91
+ try {
92
+ Map <InputFile , NewCoverage > coverageMeasures = parseReport (report , context );
93
+ saveMeasures (coverageMeasures , filesCovered );
94
+ } catch (Exception e ) {
95
+ LOG .warn ("Cannot read coverage report '{}', the following exception occurred: '{}'" , report , e .getMessage ());
96
+ analysisWarnings .addUnique (String .format ("An error occurred while trying to import the coverage report: '%s'" , report ));
84
97
}
85
98
}
86
99
87
100
private List <File > getCoverageReports (String baseDir , Configuration config ) {
88
101
if (!config .hasKey (REPORT_PATHS_KEY )) {
89
- return getReports (config , baseDir , REPORT_PATHS_KEY , DEFAULT_REPORT_PATH , analysisWarnings );
102
+ return PythonReportSensor . getReports (config , baseDir , REPORT_PATHS_KEY , DEFAULT_REPORT_PATH , analysisWarnings );
90
103
}
91
104
92
105
return Arrays .stream (config .getStringArray (REPORT_PATHS_KEY ))
93
- .flatMap (path -> getReports (config , baseDir , REPORT_PATHS_KEY , path , analysisWarnings ).stream ())
106
+ .flatMap (path -> PythonReportSensor . getReports (config , baseDir , REPORT_PATHS_KEY , path , analysisWarnings ).stream ())
94
107
.toList ();
95
108
}
96
109
@@ -115,7 +128,8 @@ private Map<InputFile, NewCoverage> parseReport(File report, SensorContext conte
115
128
parser .parseReport (report , context , coverageMeasures );
116
129
if (!parser .errors ().isEmpty ()) {
117
130
String parseErrors = String .format (String .join ("%n" , parser .errors ()));
118
- analysisWarnings .addUnique (String .format ("The following error(s) occurred while trying to import coverage report:%n%s" , parseErrors ));
131
+ analysisWarnings .addUnique (String .format ("The following error(s) occurred while trying to import coverage report:%n%s" ,
132
+ parseErrors ));
119
133
}
120
134
} catch (EmptyReportException e ) {
121
135
analysisWarnings .addUnique (String .format ("The coverage report '%s' has been ignored because it seems to be empty." , report ));
0 commit comments