Skip to content

Commit

Permalink
Merge pull request #1284 from guwirth/coverage-error-msg
Browse files Browse the repository at this point in the history
improve error messages:
  • Loading branch information
guwirth authored Nov 6, 2017
2 parents 48a1a0a + a96a2dc commit d6bab96
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
* {@inheritDoc}
*/
public class CxxCoverageSensor extends CxxReportSensor {

private static final Logger LOG = Loggers.get(CxxCoverageSensor.class);

// Configuration properties before SQ 6.2
Expand All @@ -73,13 +74,13 @@ public class CxxCoverageSensor extends CxxReportSensor {
// a) deprecate old feature ant style search
// b) support comma separated list of coverage files
//public static final String REPORT_PATHS_KEY = "coverage.reportPaths";

private final List<CoverageParser> parsers = new LinkedList<>();
private final CxxCoverageCache cache;
public static final String KEY = "Coverage";

/**
* {@inheritDoc}
*
* @param cache for all coverage data
* @param language for current analysis
* @param context for current file
Expand Down Expand Up @@ -110,16 +111,17 @@ public void execute(SensorContext context) {

/**
* {@inheritDoc}
*
* @param context for coverage analysis
* @param linesOfCodeByFile use for FORCE_ZERO_COVERAGE_KEY feature
* @param linesOfCodeByFile use for FORCE_ZERO_COVERAGE_KEY feature
*/
public void execute(SensorContext context, Map<InputFile, Set<Integer>> linesOfCodeByFile) {
Settings settings = context.settings();
String[] reportsKey = settings.getStringArray(getReportPathKey());
LOG.info("Searching coverage reports by path with basedir '{}' and search prop '{}'",
context.fileSystem().baseDir(), getReportPathKey());
LOG.info("Searching coverage reports by path with basedir '{}' and search prop '{}'",
context.fileSystem().baseDir(), getReportPathKey());
LOG.info("Searching for coverage reports '{}'", Arrays.toString(reportsKey));

Map<String, CoverageMeasures> coverageMeasures = null;
Map<String, CoverageMeasures> itCoverageMeasures = null;
Map<String, CoverageMeasures> overallCoverageMeasures = null;
Expand Down Expand Up @@ -151,8 +153,8 @@ public void execute(SensorContext context, Map<InputFile, Set<Integer>> linesOfC
LOG.debug("Parsing overall test coverage reports");
}
warnUsageOfDeprecatedProperty(settings, getOverallReportPathKey());
List<File> overallReports = getReports(settings,
context.fileSystem().baseDir(), getOverallReportPathKey());
List<File> overallReports = getReports(settings,
context.fileSystem().baseDir(), getOverallReportPathKey());
overallCoverageMeasures = processReports(context, overallReports, this.cache.overallCoverageCache());
saveMeasures(context, overallCoverageMeasures, CoverageType.OVERALL);
}
Expand All @@ -163,9 +165,9 @@ public void execute(SensorContext context, Map<InputFile, Set<Integer>> linesOfC
} else {
LOG.info("Zeroing coverage information for untouched files");
zeroMeasuresWithoutReports(context, coverageMeasures,
itCoverageMeasures,
overallCoverageMeasures,
linesOfCodeByFile);
itCoverageMeasures,
overallCoverageMeasures,
linesOfCodeByFile);
}
}
}
Expand All @@ -181,7 +183,7 @@ private void zeroMeasuresWithoutReports(
FileSystem fileSystem = context.fileSystem();
FilePredicates p = fileSystem.predicates();
Iterable<InputFile> inputFiles = fileSystem.inputFiles(p.and(p.hasType(InputFile.Type.MAIN),
p.hasLanguage(this.language.getKey())));
p.hasLanguage(this.language.getKey())));

for (InputFile inputFile : inputFiles) {
Set<Integer> linesOfCodeForFile = linesOfCode.get(inputFile);
Expand All @@ -201,8 +203,8 @@ private void zeroMeasuresWithoutReports(
}
}

private void saveZeroValueForResource(InputFile inputFile, SensorContext context, CoverageType ctype,
@Nullable Set<Integer> linesOfCode) {
private void saveZeroValueForResource(InputFile inputFile, SensorContext context, CoverageType ctype,
@Nullable Set<Integer> linesOfCode) {
if (linesOfCode != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Zeroing {} coverage measures for file '{}'", ctype, inputFile.relativePath());
Expand All @@ -213,23 +215,22 @@ private void saveZeroValueForResource(InputFile inputFile, SensorContext context

try {
linesOfCode.forEach((Integer line) -> newCoverage.lineHits(line, 0));
} catch (RuntimeException ex) {
LOG.error("Cannot save Line Hits for Line '{}' : '{}', ignoring measure",
inputFile.relativePath(), ex);
} catch (RuntimeException ex) {
LOG.error("Cannot save coverage measure: '{}', ignoring measure", ex.getMessage());
CxxUtils.validateRecovery(ex, this.language);
}

try {
newCoverage.save();
} catch (RuntimeException ex) {
LOG.error("Cannot save measure '{}' : '{}', ignoring measure", inputFile.relativePath(), ex);
LOG.error("Cannot save coverage measure: '{}', ignoring measure", ex.getMessage());
CxxUtils.validateRecovery(ex, this.language);
}
}
}

private Map<String, CoverageMeasures> processReports(final SensorContext context, List<File> reports,
Map<String, Map<String, CoverageMeasures>> cacheCov) {
private Map<String, CoverageMeasures> processReports(final SensorContext context, List<File> reports,
Map<String, Map<String, CoverageMeasures>> cacheCov) {
Map<String, CoverageMeasures> measuresTotal = new HashMap<>();

for (File report : reports) {
Expand All @@ -239,7 +240,7 @@ private Map<String, CoverageMeasures> processReports(final SensorContext context
parseCoverageReport(parser, context, report, measuresTotal);
if (LOG.isDebugEnabled()) {
LOG.debug("cached measures for '{}' : current cache content data = '{}'", report.getAbsolutePath(),
cacheCov.size());
cacheCov.size());
}
cacheCov.put(report.getAbsolutePath(), measuresTotal);
// Only use first coverage parser which handles the data correctly
Expand Down Expand Up @@ -269,16 +270,16 @@ private Map<String, CoverageMeasures> processReports(final SensorContext context
* @return true if report was parsed and results are available otherwise false
*/
private static void parseCoverageReport(CoverageParser parser, final SensorContext context, File report,
Map<String, CoverageMeasures> measuresTotal) {
Map<String, CoverageMeasures> measuresTotal) {
Map<String, CoverageMeasures> measuresForReport = new HashMap<>();
try {
parser.processReport(context, report, measuresForReport);
} catch (XMLStreamException e) {
throw new EmptyReportException("Coverage report" + report + "cannot be parsed by" + parser, e);
throw new EmptyReportException("Coverage report" + report + "cannot be parsed by" + parser, e);
}

if (measuresForReport.isEmpty()) {
throw new EmptyReportException("Coverage report " + report + " result is empty (parsed by " + parser +")");
throw new EmptyReportException("Coverage report " + report + " result is empty (parsed by " + parser + ")");
}

measuresTotal.putAll(measuresForReport);
Expand All @@ -290,7 +291,7 @@ private void saveMeasures(SensorContext context,
CoverageType ctype) {
for (Map.Entry<String, CoverageMeasures> entry : coverageMeasures.entrySet()) {
String filePath = PathUtils.sanitize(entry.getKey());
if (filePath!= null) {
if (filePath != null) {
filePath = CxxUtils.normalizePathFull(filePath, context.fileSystem().baseDir().getAbsolutePath());
InputFile cxxFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasPath(filePath));
if (LOG.isDebugEnabled()) {
Expand All @@ -299,20 +300,20 @@ private void saveMeasures(SensorContext context,
if (cxxFile != null) {

NewCoverage newCoverage = context.newCoverage()
.onFile(cxxFile)
.ofType(ctype);
.onFile(cxxFile)
.ofType(ctype);

Collection<CoverageMeasure> measures = entry.getValue().getCoverageMeasures();
if (LOG.isDebugEnabled()) {
LOG.debug("Saving '{}' coverage measures for file '{}'", measures.size(), filePath);
}

measures.forEach((CoverageMeasure measure) -> checkCoverage(newCoverage, measure));

try {
newCoverage.save();
} catch(RuntimeException ex) {
LOG.error("Cannot save measure for file '{}' , ignoring measure. ", filePath, ex);
} catch (RuntimeException ex) {
LOG.error("Cannot save coverage measure: '{}' , ignoring measure. ", ex.getMessage());
CxxUtils.validateRecovery(ex, this.language);
}
LOG.info("Saved '{}' coverage measures for file '{}'", measures.size(), filePath);
Expand All @@ -339,9 +340,8 @@ private void checkCoverage(NewCoverage newCoverage, CoverageMeasure measure) {
try {
newCoverage.lineHits(measure.getLine(), measure.getHits());
newCoverage.conditions(measure.getLine(), measure.getConditions(), measure.getCoveredConditions());
} catch(RuntimeException ex) {
LOG.error("Cannot save Conditions Hits for Line '{}' , ignoring measure. ",
measure.getLine(), ex);
} catch (RuntimeException ex) {
LOG.error("Cannot save coverage measure: '{}', ignoring measure", ex.getMessage());
CxxUtils.validateRecovery(ex, this.language);
}
}
Expand All @@ -351,11 +351,11 @@ private void warnUsageOfDeprecatedProperty(Settings settings, String reportPathP
LOG.warn("Property '{}' is deprecated. Please use '{}' instead.", reportPathProperty, getReportPathKey());
}
}

@Override
protected String getSensorKey() {
return KEY;
}
}

@Override
public String getReportPathKey() {
Expand All @@ -372,11 +372,10 @@ protected String getITReportPathKey() {

protected String getOverallReportPathKey() {
return this.language.getPluginProperty(OVERALL_REPORT_PATH_KEY);
}
}

protected String getForceZeroCoverageKey() {
return this.language.getPluginProperty(FORCE_ZERO_COVERAGE_KEY);
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private void simpleMode(final SensorContext context, List<TestCase> testcases) {
.withValue(testsCount)
.save();
} catch(Exception ex) {
LOG.error("Cannot save measure TESTS : '{}', ignoring measure", ex);
LOG.error("Cannot save measure TESTS : '{}', ignoring measure", ex.getMessage());
CxxUtils.validateRecovery(ex, this.language);
}

Expand All @@ -172,7 +172,7 @@ private void simpleMode(final SensorContext context, List<TestCase> testcases) {
.withValue(testsErrors)
.save();
} catch(Exception ex) {
LOG.error("Cannot save measure TEST_ERRORS : '{}', ignoring measure", ex);
LOG.error("Cannot save measure TEST_ERRORS : '{}', ignoring measure", ex.getMessage());
CxxUtils.validateRecovery(ex, this.language);
}

Expand All @@ -183,7 +183,7 @@ private void simpleMode(final SensorContext context, List<TestCase> testcases) {
.withValue(testsFailures)
.save();
} catch(Exception ex) {
LOG.error("Cannot save measure TEST_FAILURES : '{}', ignoring measure", ex);
LOG.error("Cannot save measure TEST_FAILURES : '{}', ignoring measure", ex.getMessage());
CxxUtils.validateRecovery(ex, this.language);
}

Expand All @@ -194,7 +194,7 @@ private void simpleMode(final SensorContext context, List<TestCase> testcases) {
.withValue(testsSkipped)
.save();
} catch(Exception ex) {
LOG.error("Cannot save measure SKIPPED_TESTS : '{}', ignoring measure", ex);
LOG.error("Cannot save measure SKIPPED_TESTS : '{}', ignoring measure", ex.getMessage());
CxxUtils.validateRecovery(ex, this.language);
}

Expand All @@ -205,7 +205,7 @@ private void simpleMode(final SensorContext context, List<TestCase> testcases) {
.withValue(ParsingUtils.scaleValue(successDensity))
.save();
} catch(Exception ex) {
LOG.error("Cannot save measure TEST_SUCCESS_DENSITY : '{}', ignoring measure", ex);
LOG.error("Cannot save measure TEST_SUCCESS_DENSITY : '{}', ignoring measure", ex.getMessage());
CxxUtils.validateRecovery(ex, this.language);
}

Expand All @@ -216,7 +216,7 @@ private void simpleMode(final SensorContext context, List<TestCase> testcases) {
.withValue(testsTime)
.save();
} catch(Exception ex) {
LOG.error("Cannot save measure TEST_EXECUTION_TIME : '{}', ignoring measure", ex);
LOG.error("Cannot save measure TEST_EXECUTION_TIME : '{}', ignoring measure", ex.getMessage());
CxxUtils.validateRecovery(ex, this.language);
}
} else {
Expand Down

0 comments on commit d6bab96

Please sign in to comment.