Skip to content

Commit

Permalink
Merge pull request #3 from Shopify/coverage
Browse files Browse the repository at this point in the history
Fix NPE in coverage when a source has no path
  • Loading branch information
chrisseaton authored Nov 1, 2019
2 parents 8e23637 + b1456b7 commit 2067056
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
Expand All @@ -150,7 +148,7 @@ private void sortCoverage() {
Arrays.sort(coverage, new Comparator<SourceCoverage>() {
@Override
public int compare(SourceCoverage o1, SourceCoverage o2) {
return o1.getSource().getPath().compareTo(o2.getSource().getPath());
return o1.getName().compareTo(o2.getName());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:";
Expand Down Expand Up @@ -111,6 +112,7 @@ void print() {

private void printSourceCoverage(SourceCoverage sourceCoverage) {
printTestName();
printSourceName(sourceCoverage);
printSourceFile(sourceCoverage);
printRootData(sourceCoverage);
printLineData(sourceCoverage);
Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit 2067056

Please sign in to comment.