From b1456b75751fef08a1709075971045368d8367c6 Mon Sep 17 00:00:00 2001 From: Chris Seaton Date: Thu, 31 Oct 2019 22:14:45 -0400 Subject: [PATCH] Fix NPE in coverage when a source has no path Also generally add source name support to coverage. --- .../tools/coverage/SourceCoverage.java | 8 ++++++++ .../tools/coverage/impl/CoverageCLI.java | 20 +++++++++---------- .../tools/coverage/impl/JSONPrinter.java | 1 + .../tools/coverage/impl/LCOVPrinter.java | 6 ++++++ 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage/SourceCoverage.java b/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage/SourceCoverage.java index eac5e1de404a..d301552893a9 100644 --- a/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage/SourceCoverage.java +++ b/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage/SourceCoverage.java @@ -61,4 +61,12 @@ public Source getSource() { public RootCoverage[] getRoots() { return roots; } + + public String getName() { + if (source.getPath() == null) { + return source.getName(); + } else { + return source.getPath(); + } + } } diff --git a/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage/impl/CoverageCLI.java b/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage/impl/CoverageCLI.java index b761598fb559..817f267ed03d 100644 --- a/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage/impl/CoverageCLI.java +++ b/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage/impl/CoverageCLI.java @@ -53,14 +53,12 @@ final class CoverageCLI { } private static String getHistogramLineFormat(SourceCoverage[] coverage) { - int maxPathLength = 10; + int maxNameLength = 10; for (SourceCoverage source : coverage) { - final String path = source.getSource().getPath(); - if (path != null) { - maxPathLength = Math.max(maxPathLength, path.length()); - } + final String name = source.getName(); + maxNameLength = Math.max(maxNameLength, name.length()); } - return " %-" + maxPathLength + "s | %10s | %7s | %7s "; + return " %-" + maxNameLength + "s | %10s | %7s | %7s "; } private static String percentFormat(double val) { @@ -100,11 +98,11 @@ void printLinesOutput() { printLine(); printLinesLegend(); for (SourceCoverage sourceCoverage : coverage) { - final String path = sourceCoverage.getSource().getPath(); + final String name = sourceCoverage.getName(); printLine(); printSummaryHeader(); final LineCoverage lineCoverage = new LineCoverage(sourceCoverage, strictLines); - out.println(String.format(format, path, statementCoverage(sourceCoverage), lineCoverage(lineCoverage), rootCoverage(sourceCoverage))); + out.println(String.format(format, name, statementCoverage(sourceCoverage), lineCoverage(lineCoverage), rootCoverage(sourceCoverage))); out.println(); printLinesOfSource(sourceCoverage.getSource(), lineCoverage); } @@ -136,8 +134,8 @@ void printHistogramOutput() { printSummaryHeader(); printLine(); for (SourceCoverage sourceCoverage : coverage) { - final String path = sourceCoverage.getSource().getPath(); - final String line = String.format(format, path, + final String name = sourceCoverage.getName(); + final String line = String.format(format, name, statementCoverage(sourceCoverage), lineCoverage(new LineCoverage(sourceCoverage, strictLines)), rootCoverage(sourceCoverage)); @@ -150,7 +148,7 @@ private void sortCoverage() { Arrays.sort(coverage, new Comparator() { @Override public int compare(SourceCoverage o1, SourceCoverage o2) { - return o1.getSource().getPath().compareTo(o2.getSource().getPath()); + return o1.getName().compareTo(o2.getName()); } }); } diff --git a/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage/impl/JSONPrinter.java b/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage/impl/JSONPrinter.java index 45c9c728dd45..fa7b36c57b2d 100644 --- a/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage/impl/JSONPrinter.java +++ b/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage/impl/JSONPrinter.java @@ -58,6 +58,7 @@ private static JSONObject sourceSectionJson(SourceSection section) { private static JSONObject sourceJSON(SourceCoverage coverage) { final JSONObject sourceJson = new JSONObject(); + sourceJson.put("name", coverage.getSource().getName()); sourceJson.put("path", coverage.getSource().getPath()); sourceJson.put("roots", rootsJson(coverage.getRoots())); return sourceJson; diff --git a/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage/impl/LCOVPrinter.java b/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage/impl/LCOVPrinter.java index 1e8f70af91f9..fceb8840e19e 100644 --- a/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage/impl/LCOVPrinter.java +++ b/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage/impl/LCOVPrinter.java @@ -36,6 +36,7 @@ class LCOVPrinter { private static final String END_OF_RECORD = "end_of_record"; private static final String TEST_NAME = "TN:"; + private static final String SOURCE_NAME = "SN:"; private static final String SOURCE_FILE = "SF:"; private static final String FUNCTION = "FN:"; private static final String FUNCTION_DATA = "FNDA:"; @@ -111,6 +112,7 @@ void print() { private void printSourceCoverage(SourceCoverage sourceCoverage) { printTestName(); + printSourceName(sourceCoverage); printSourceFile(sourceCoverage); printRootData(sourceCoverage); printLineData(sourceCoverage); @@ -169,6 +171,10 @@ private void printRoot(RootCoverage root) { out.println(FUNCTION + root.getSourceSection().getStartLine() + "," + root.getName()); } + private void printSourceName(SourceCoverage sourceCoverage) { + out.println(SOURCE_NAME + sourceCoverage.getSource().getName()); + } + private void printSourceFile(SourceCoverage sourceCoverage) { out.println(SOURCE_FILE + sourceCoverage.getSource().getPath()); }