Skip to content

Commit

Permalink
Properly close sources from files
Browse files Browse the repository at this point in the history
  • Loading branch information
mdedetrich authored and rolandtritsch committed Jan 24, 2025
1 parent 93fc644 commit fc7fe9b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 41 deletions.
12 changes: 9 additions & 3 deletions src/main/scala/org/scoverage/coveralls/CIService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import io.circe._
import io.circe.parser
import io.circe.generic.auto._

import scala.io.Source
import scala.io.{BufferedSource, Source}
import scala.util.control.NonFatal

trait CIService {
def name: String
Expand Down Expand Up @@ -56,11 +57,16 @@ case object GitHubActions extends CIService {
}

def getPrNumber(payloadPath: String): Option[String] = {
var source: BufferedSource = null
val lines =
try {
Some(Source.fromFile(payloadPath, "utf-8").getLines.mkString)
source = Source.fromFile(payloadPath, "utf-8")
Some(source.getLines.mkString)
} catch {
case _: Throwable => None
case NonFatal(_) => None
} finally {
if (source != null)
source.close()
}

lines match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package org.scoverage.coveralls
import sbt.Logger

import java.io.File
import scala.io.{BufferedSource, Source}
import scala.util.control.NonFatal

class CoberturaMultiSourceReader(
coberturaFile: File,
Expand Down Expand Up @@ -119,17 +121,26 @@ class CoberturaMultiSourceReader(
}

def reportForSource(source: String): SourceFileReport = {
val fileSrc = sourceEncoding match {
case Some(enc) => scala.io.Source.fromFile(source, enc)
case None => scala.io.Source.fromFile(source)
var fileSrc: BufferedSource = null
try {
fileSrc = sourceEncoding match {
case Some(enc) => Source.fromFile(source, enc)
case None => Source.fromFile(source)
}

val lineCount = fileSrc.getLines().size

val lineHitMap = lineCoverage(source)
val fullLineHit = (0 until lineCount).map(i => lineHitMap.get(i + 1))

SourceFileReport(source, fullLineHit.toList)
} catch {
case NonFatal(e) =>
throw e
} finally {
if (fileSrc != null)
fileSrc.close()
}
val lineCount = fileSrc.getLines().size
fileSrc.close()

val lineHitMap = lineCoverage(source)
val fullLineHit = (0 until lineCount).map(i => lineHitMap.get(i + 1))

SourceFileReport(source, fullLineHit.toList)
}
}

Expand Down
59 changes: 34 additions & 25 deletions src/main/scala/org/scoverage/coveralls/CoverallsClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import scalaj.http.{Http, MultiPart}
import java.io.File
import java.net.{InetAddress, Socket}
import javax.net.ssl.{SSLSocket, SSLSocketFactory}
import scala.io.{Codec, Source}
import scala.io.{BufferedSource, Codec, Source}
import scala.util.control.NonFatal

class CoverallsClient(endpoint: String, httpClient: HttpClient) {

Expand All @@ -23,30 +24,38 @@ class CoverallsClient(endpoint: String, httpClient: HttpClient) {

def postFile(file: File): CoverallsResponse = {
val codec: Codec = Codec.UTF8
val source = Source.fromFile(file)(codec)
// API want newlines encoded as \n, not sure about other escape chars
// https://coveralls.zendesk.com/hc/en-us/articles/201774865-API-Introduction
val bytes = source.getLines().mkString("\\n").getBytes(codec.charSet)
source.close()

httpClient.multipart(
url,
"json_file",
"json_file.json",
"application/json; charset=utf-8",
bytes
) match {
case CoverallHttpResponse(_, body) =>
try {
mapper.readValue(body, classOf[CoverallsResponse])
} catch {
case t: Throwable =>
CoverallsResponse(
"Failed to parse response: " + t,
error = true,
""
)
}
var source: BufferedSource = null
try {
source = Source.fromFile(file)(codec)
// API want newlines encoded as \n, not sure about other escape chars
// https://coveralls.zendesk.com/hc/en-us/articles/201774865-API-Introduction
val bytes = source.getLines().mkString("\\n").getBytes(codec.charSet)

httpClient.multipart(
url,
"json_file",
"json_file.json",
"application/json; charset=utf-8",
bytes
) match {
case CoverallHttpResponse(_, body) =>
try {
mapper.readValue(body, classOf[CoverallsResponse])
} catch {
case NonFatal(e) =>
CoverallsResponse(
"Failed to parse response: " + e,
error = true,
""
)
}
}
} catch {
case NonFatal(e) =>
throw e
} finally {
if (source != null)
source.close()
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/main/scala/org/scoverage/coveralls/CoverallsPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import sbt.internal.util.ManagedLogger
import sbt.{ScopeFilter, ThisProject, _}

import java.io.File
import scala.io.Source
import scala.io.{BufferedSource, Source}
import scala.util.control.NonFatal

object Imports {
object CoverallsKeys {
Expand Down Expand Up @@ -172,13 +173,17 @@ object CoverallsPlugin extends AutoPlugin {
def githubActionsRunIdent: Option[String] = sys.env.get("GITHUB_RUN_ID")

def repoTokenFromFile(path: String): Option[String] = {
var source: BufferedSource = null
try {
val source = Source.fromFile(path)
source = Source.fromFile(path)
val repoToken = source.mkString.trim
source.close()
Some(repoToken)
} catch {
case _: Exception => None
case NonFatal(_) => None
} finally {
if (source != null)
source.close()
}
}

Expand Down

0 comments on commit fc7fe9b

Please sign in to comment.