Skip to content

Ensure utf8 encoding is defaulted everywhere. #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions scalac-scoverage-plugin/src/main/scala/scoverage/IOUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ object IOUtils {

private val UnixSeperator: Char = '/'
private val WindowsSeperator: Char = '\\'
private val UTF8Encoding: String = "UTF-8"

def getName(path: String): Any = {
val index = {
Expand All @@ -37,9 +36,12 @@ object IOUtils {
findMeasurementFiles(dataDir).foreach(_.delete)
def clean(dataDir: String): Unit = clean(new File(dataDir))

def writeToFile(file: File, str: String) = {
def writeToFile(file: File, str: String)(implicit encoding: String) = {
val writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(file), UTF8Encoding)
new OutputStreamWriter(
new FileOutputStream(file),
encoding
)
)
try {
writer.write(str)
Expand Down Expand Up @@ -86,10 +88,13 @@ object IOUtils {
file.getName == Constants.XMLReportFilenameWithDebug

// loads all the invoked statement ids from the given files
def invoked(files: Seq[File]): Set[(Int, String)] = {
def invoked(
files: Seq[File]
)(implicit encoding: String): Set[(Int, String)] = {
val acc = mutable.Set[(Int, String)]()
files.foreach { file =>
val reader = Source.fromFile(file)
val reader =
Source.fromFile(file, encoding)
for (line <- reader.getLines()) {
if (!line.isEmpty) {
acc += (line.split(" ").toList match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ package scoverage.report

import java.io.File

class BaseReportWriter(sourceDirectories: Seq[File], outputDir: File) {
import scala.io.Codec

class BaseReportWriter(
sourceDirectories: Seq[File],
outputDir: File,
sourceEncoding: Option[String]
) {

implicit val encoding = sourceEncoding.fold(Codec.UTF8.name)(identity)

// Source paths in canonical form WITH trailing file separator
private val formattedSourcePaths: Seq[String] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import scoverage.DoubleFormat.twoFractionDigits
import scoverage._

/** @author Stephen Samuel */
class CoberturaXmlWriter(sourceDirectories: Seq[File], outputDir: File)
extends BaseReportWriter(sourceDirectories, outputDir) {
class CoberturaXmlWriter(
sourceDirectories: Seq[File],
outputDir: File,
sourceEncoding: Option[String]
) extends BaseReportWriter(sourceDirectories, outputDir, sourceEncoding) {

def this(baseDir: File, outputDir: File) = {
this(Seq(baseDir), outputDir)
def this(baseDir: File, outputDir: File, sourceEncoding: Option[String]) = {
this(Seq(baseDir), outputDir, sourceEncoding)
}

def write(coverage: Coverage): Unit = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package scoverage.report

import scala.io.Codec
import scala.io.Source

import _root_.scoverage.MeasuredFile
Expand Down Expand Up @@ -77,7 +78,7 @@ class CodeGrid(mFile: MeasuredFile, sourceEncoding: Option[String]) {
private def source(mfile: MeasuredFile): String = {
val src = sourceEncoding match {
case Some(enc) => Source.fromFile(mfile.source, enc)
case None => Source.fromFile(mfile.source)
case None => Source.fromFile(mfile.source, Codec.UTF8.name)
}
try src.mkString
finally src.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package scoverage.report

import java.io.File

import scala.io.Codec

import scoverage.Coverage
import scoverage.IOUtils
import scoverage.Serializer

object CoverageAggregator {

implicit val encoding: String = Codec.UTF8.name

// to be used by gradle-scoverage plugin
def aggregate(dataDirs: Array[File], sourceRoot: File): Option[Coverage] =
aggregate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ScoverageHtmlWriter(
sourceDirectories: Seq[File],
outputDir: File,
sourceEncoding: Option[String]
) extends BaseReportWriter(sourceDirectories, outputDir) {
) extends BaseReportWriter(sourceDirectories, outputDir, sourceEncoding) {

// to be used by gradle-scoverage plugin
def this(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ import scoverage._
class ScoverageXmlWriter(
sourceDirectories: Seq[File],
outputDir: File,
debug: Boolean
) extends BaseReportWriter(sourceDirectories, outputDir) {
debug: Boolean,
sourceEncoding: Option[String]
) extends BaseReportWriter(sourceDirectories, outputDir, sourceEncoding) {

def this(sourceDir: File, outputDir: File, debug: Boolean) = {
this(Seq(sourceDir), outputDir, debug)
def this(
sourceDir: File,
outputDir: File,
debug: Boolean,
sourceEncoding: Option[String]
) = {
this(Seq(sourceDir), outputDir, debug, sourceEncoding)
}

def write(coverage: Coverage): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class CoberturaXmlWriterTest extends FunSuite {
)
)

val writer = new CoberturaXmlWriter(sourceRoot, dir)
val writer = new CoberturaXmlWriter(sourceRoot, dir, None)
writer.write(coverage)

val domFactory = DocumentBuilderFactory.newInstance()
Expand Down Expand Up @@ -309,7 +309,7 @@ class CoberturaXmlWriterTest extends FunSuite {
)
)

val writer = new CoberturaXmlWriter(sourceRoot, dir)
val writer = new CoberturaXmlWriter(sourceRoot, dir, None)
writer.write(coverage)

// Needed to acount for https://github.com/scala/scala-xml/pull/177
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import java.io.File
import java.io.FileWriter
import java.util.UUID

import scala.io.Codec

import munit.FunSuite

/** @author Stephen Samuel */
class IOUtilsTest extends FunSuite {

implicit val encoding: String = Codec.UTF8.name

test("should parse measurement files") {
val file = File.createTempFile("scoveragemeasurementtest", "txt")
val writer = new FileWriter(file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scoverage

import java.io.File

import scala.io.Codec
import scala.tools.nsc.Global
import scala.tools.nsc.plugins.PluginComponent
import scala.tools.nsc.transform.Transform
Expand All @@ -24,7 +25,7 @@ class LocationCompiler(

def writeCodeSnippetToTempFile(code: String): File = {
val file = File.createTempFile("code_snippet", ".scala")
IOUtils.writeToFile(file, code)
IOUtils.writeToFile(file, code)(Codec.UTF8.name)
file.deleteOnExit()
file
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.io.FileNotFoundException
import java.net.URL

import scala.collection.mutable.ListBuffer
import scala.io.Codec
import scala.tools.nsc.Global
import scala.tools.nsc.Settings
import scala.tools.nsc.plugins.PluginComponent
Expand Down Expand Up @@ -135,7 +136,7 @@ class ScoverageCompiler(

def writeCodeSnippetToTempFile(code: String): File = {
val file = File.createTempFile("scoverage_snippet", ".scala")
IOUtils.writeToFile(file, code)
IOUtils.writeToFile(file, code)(Codec.UTF8.name)
file.deleteOnExit()
file
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class InvokerConcurrencyTest extends FunSuite {
}
}

futures.foreach(Await.result(_, 1.second))
futures.foreach(Await.result(_, 3.second))

// Now verify that the measurement file is not corrupted by loading it
val measurementFiles = Invoker.findMeasurementFiles(measurementDir)
Expand Down