Skip to content

Commit

Permalink
[7.2.0] Fix swallowed I/O exception in LcovPrinter (bazelbuild#22057)
Browse files Browse the repository at this point in the history
The calling code expects the exception, not a return boolean.

Fixes bazelbuild#21982.

Closes bazelbuild#21987.

PiperOrigin-RevId: 626086576
Change-Id: I4abd7a253715c84c323e036dfbdb2fcb94a4825d

Commit
bazelbuild@f8277cf

Co-authored-by: Spencer Putt <sputt@alumni.iu.edu>
  • Loading branch information
bazel-io and sputt committed Apr 18, 2024
1 parent 8127cbe commit 2da699f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,30 @@
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Prints coverage data stored in a collection of {@link SourceFileCoverage} in a <a
* href="http://ltp.sourceforge.net/coverage/lcov/geninfo.1.php">lcov tracefile format</a>
*/
class LcovPrinter {
private static final Logger logger = Logger.getLogger(LcovPrinter.class.getName());
private final BufferedWriter bufferedWriter;

private LcovPrinter(BufferedWriter bufferedWriter) {
this.bufferedWriter = bufferedWriter;
}

static boolean print(OutputStream outputStream, Coverage coverage) {
BufferedWriter bufferedWriter;
try (Writer fileWriter = new OutputStreamWriter(outputStream, UTF_8)) {
bufferedWriter = new BufferedWriter(fileWriter);
static void print(OutputStream outputStream, Coverage coverage) throws IOException {
try (Writer fileWriter = new OutputStreamWriter(outputStream, UTF_8);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); ) {
LcovPrinter lcovPrinter = new LcovPrinter(bufferedWriter);
lcovPrinter.print(coverage);
bufferedWriter.close();
} catch (IOException exception) {
logger.log(Level.SEVERE, "Could not write to output file.");
return false;
}
return true;
}

private boolean print(Coverage coverage) {
try {
for (SourceFileCoverage sourceFile : coverage.getAllSourceFiles()) {
print(sourceFile);
}
} catch (IOException exception) {
logger.log(Level.SEVERE, "Could not write to output file.");
return false;
private void print(Coverage coverage) throws IOException {
for (SourceFileCoverage sourceFile : coverage.getAllSourceFiles()) {
print(sourceFile);
}
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void testPrintTwoFiles() throws Exception {
coverage.add(sourceFileCoverage1);
coverage.add(sourceFileCoverage2);

assertThat(LcovPrinter.print(byteOutputStream, coverage)).isTrue();
LcovPrinter.print(byteOutputStream, coverage);
byteOutputStream.close();

Iterable<String> fileLines = Splitter.on('\n').split(byteOutputStream.toString());
Expand All @@ -76,7 +76,7 @@ public void testPrintTwoFiles() throws Exception {
@Test
public void testPrintOneFile() throws Exception {
coverage.add(sourceFileCoverage1);
assertThat(LcovPrinter.print(byteOutputStream, coverage)).isTrue();
LcovPrinter.print(byteOutputStream, coverage);
byteOutputStream.close();
Iterable<String> fileLines = Splitter.on('\n').split(byteOutputStream.toString());
// Last line of the file will always be a newline.
Expand All @@ -99,7 +99,7 @@ public void testPrintBrdaLines() throws Exception {
sourceFile.addBranch(7, BranchCoverage.createWithBlockAndBranch(7, "0", "1", false, 0));
coverage.add(sourceFile);

assertThat(LcovPrinter.print(byteOutputStream, coverage)).isTrue();
LcovPrinter.print(byteOutputStream, coverage);
Iterable<String> fileLines = Splitter.on('\n').split(byteOutputStream.toString());
assertThat(fileLines)
.containsExactly(
Expand Down

0 comments on commit 2da699f

Please sign in to comment.