Skip to content

Commit eb175cb

Browse files
michelouallanrenucci
authored andcommitted
Proper UTF-8 encoding and line endings on Windows (#5457)
1 parent 7e093b1 commit eb175cb

File tree

6 files changed

+41
-33
lines changed

6 files changed

+41
-33
lines changed

compiler/src/dotty/tools/dotc/decompiler/DecompilationPrinter.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package decompiler
33

44
import java.io.{OutputStream, PrintStream}
55

6+
import scala.io.Codec
7+
68
import dotty.tools.dotc.core.Contexts._
79
import dotty.tools.dotc.core.Phases.Phase
810
import dotty.tools.dotc.core.tasty.TastyPrinter
@@ -24,8 +26,8 @@ class DecompilationPrinter extends Phase {
2426
var os: OutputStream = null
2527
var ps: PrintStream = null
2628
try {
27-
os = File(outputDir.fileNamed("decompiled.scala").path).outputStream(append = true)
28-
ps = new PrintStream(os)
29+
os = File(outputDir.fileNamed("decompiled.scala").path)(Codec.UTF8).outputStream(append = true)
30+
ps = new PrintStream(os, /* autoFlush = */ false, "UTF-8")
2931
printToOutput(ps)
3032
} finally {
3133
if (os ne null) os.close()

compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dotty.tools
22
package dotc
33
package reporting
44

5+
import java.lang.System.{lineSeparator => EOL}
6+
57
import core.Contexts.Context
68
import core.Decorators._
79
import printing.Highlighting.{Blue, Red}
@@ -15,6 +17,7 @@ import scala.annotation.switch
1517
import scala.collection.mutable
1618

1719
trait MessageRendering {
20+
1821
/** Remove ANSI coloring from `str`, useful for getting real length of
1922
* strings
2023
*
@@ -101,7 +104,7 @@ trait MessageRendering {
101104

102105
msg.linesIterator
103106
.map { line => " " * (offset - 1) + "|" + padding + line}
104-
.mkString(sys.props("line.separator"))
107+
.mkString(EOL)
105108
}
106109

107110
/** The separator between errors containing the source file and error type
@@ -132,21 +135,21 @@ trait MessageRendering {
132135
|${Blue("Explanation")}
133136
|${Blue("===========")}"""
134137
)
135-
sb.append('\n').append(m.explanation)
136-
if (m.explanation.lastOption != Some('\n')) sb.append('\n')
138+
sb.append(EOL).append(m.explanation)
139+
if (m.explanation.lastOption != Some(EOL)) sb.append(EOL)
137140
sb.toString
138141
}
139142

140143
/** The whole message rendered from `msg` */
141144
def messageAndPos(msg: Message, pos: SourcePosition, diagnosticLevel: String)(implicit ctx: Context): String = {
142145
val sb = mutable.StringBuilder.newBuilder
143146
val posString = posStr(pos, diagnosticLevel, msg)
144-
if (posString.nonEmpty) sb.append(posString).append('\n')
147+
if (posString.nonEmpty) sb.append(posString).append(EOL)
145148
if (pos.exists) {
146149
val (srcBefore, srcAfter, offset) = sourceLines(pos)
147150
val marker = columnMarker(pos, offset)
148151
val err = errorMsg(pos, msg.msg, offset)
149-
sb.append((srcBefore ::: marker :: err :: outer(pos, " " * (offset - 1)) ::: srcAfter).mkString("\n"))
152+
sb.append((srcBefore ::: marker :: err :: outer(pos, " " * (offset - 1)) ::: srcAfter).mkString(EOL))
150153
} else sb.append(msg.msg)
151154
sb.toString
152155
}

compiler/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dotty.tools.repl
22

3-
import java.io.PrintStream
3+
import java.io.{File => JFile, PrintStream}
44

55
import dotty.tools.dotc.ast.Trees._
66
import dotty.tools.dotc.ast.{tpd, untpd}
@@ -324,9 +324,9 @@ class ReplDriver(settings: Array[String],
324324
state
325325

326326
case Load(path) =>
327-
val file = new java.io.File(path)
327+
val file = new JFile(path)
328328
if (file.exists) {
329-
val contents = scala.io.Source.fromFile(file).mkString
329+
val contents = scala.io.Source.fromFile(file, "UTF-8").mkString
330330
run(contents)
331331
}
332332
else {

compiler/test/dotty/tools/repl/ReplCompilerTests.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dotty.tools.repl
22

3+
import java.lang.System.{lineSeparator => EOL}
4+
35
import org.junit.Assert._
46
import org.junit.{Ignore, Test}
57

@@ -51,7 +53,7 @@ class ReplCompilerTests extends ReplTest {
5153
"val res1: Int = 20"
5254
)
5355

54-
assertEquals(expected, storedOutput().split("\n").toList)
56+
assertEquals(expected, storedOutput().split(EOL).toList)
5557
}
5658

5759
@Test def testImportMutable =
@@ -122,6 +124,6 @@ class ReplCompilerTests extends ReplTest {
122124
)
123125

124126
run(source)
125-
assertEquals(expected, storedOutput().split("\n").toList)
127+
assertEquals(expected, storedOutput().split(EOL).toList)
126128
}
127129
}

compiler/test/dotty/tools/repl/ScriptedTests.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package dotty.tools
22
package repl
33

4-
import java.io.{ File => JFile }
4+
import java.io.{File => JFile}
5+
import java.lang.System.{lineSeparator => EOL}
56

67
import org.junit.Assert._
78
import org.junit.Test
@@ -22,7 +23,7 @@ class ScriptedTests extends ReplTest with MessageRendering {
2223

2324
private def testFile(f: JFile): Unit = {
2425
val prompt = "scala>"
25-
val lines = Source.fromFile(f).getLines().buffered
26+
val lines = Source.fromFile(f, "UTF-8").getLines().buffered
2627

2728
assert(lines.head.startsWith(prompt),
2829
s"""Each file has to start with the prompt: "$prompt"""")
@@ -44,7 +45,7 @@ class ScriptedTests extends ReplTest with MessageRendering {
4445
def evaluate(state: State, input: String, prompt: String) =
4546
try {
4647
val nstate = run(input.drop(prompt.length))(state)
47-
val out = input + "\n" + storedOutput()
48+
val out = input + EOL + storedOutput()
4849
(out, nstate)
4950
}
5051
catch {
@@ -60,7 +61,7 @@ class ScriptedTests extends ReplTest with MessageRendering {
6061
}
6162

6263
val expectedOutput =
63-
Source.fromFile(f).getLines().flatMap(filterEmpties).mkString("\n")
64+
Source.fromFile(f, "UTF-8").getLines().flatMap(filterEmpties).mkString(EOL)
6465
val actualOutput = {
6566
resetToInitial()
6667
val inputRes = extractInputs(prompt)
@@ -70,7 +71,7 @@ class ScriptedTests extends ReplTest with MessageRendering {
7071
buf.append(out)
7172
nstate
7273
}
73-
buf.flatMap(filterEmpties).mkString("\n")
74+
buf.flatMap(filterEmpties).mkString(EOL)
7475
}
7576

7677
if (expectedOutput != actualOutput) {

compiler/test/dotty/tools/vulpix/ParallelTesting.scala

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@ package tools
33
package vulpix
44

55
import java.io.{File => JFile}
6-
import java.text.SimpleDateFormat
7-
import java.util.HashMap
6+
import java.lang.System.{lineSeparator => EOL}
87
import java.nio.file.StandardCopyOption.REPLACE_EXISTING
98
import java.nio.file.{Files, NoSuchFileException, Path, Paths}
9+
import java.text.SimpleDateFormat
10+
import java.util.{HashMap, Timer, TimerTask}
1011
import java.util.concurrent.{TimeUnit, TimeoutException, Executors => JExecutors}
11-
import java.util.{Timer, TimerTask}
1212

13+
import scala.collection.mutable
1314
import scala.io.Source
15+
import scala.util.{Random, Try}
1416
import scala.util.control.NonFatal
15-
import scala.util.Try
16-
import scala.collection.mutable
1717
import scala.util.matching.Regex
18-
import scala.util.Random
18+
19+
import dotc.{Compiler, Driver}
1920
import dotc.core.Contexts._
21+
import dotc.decompiler
22+
import dotc.interfaces.Diagnostic.ERROR
2023
import dotc.reporting.{Reporter, TestReporter}
2124
import dotc.reporting.diagnostic.MessageContainer
22-
import dotc.interfaces.Diagnostic.ERROR
2325
import dotc.util.DiffUtil
24-
import dotc.{Compiler, Driver}
25-
import dotc.decompiler
2626
import dotty.tools.vulpix.TestConfiguration.defaultOptions
2727

2828
/** A parallel testing suite whose goal is to integrate nicely with JUnit
@@ -535,16 +535,16 @@ trait ParallelTesting extends RunnerOrchestration { self =>
535535
val ignoredFilePathLine = "/** Decompiled from"
536536
val stripTrailingWhitespaces = "(.*\\S|)\\s+".r
537537
val output = Source.fromFile(outDir.getParent + "_decompiled" + JFile.separator + outDir.getName
538-
+ JFile.separator + "decompiled.scala").getLines().map {line =>
538+
+ JFile.separator + "decompiled.scala", "UTF-8").getLines().map {line =>
539539
stripTrailingWhitespaces.unapplySeq(line).map(_.head).getOrElse(line)
540540
}.toList
541541

542-
val check: String = Source.fromFile(checkFile).getLines().filter(!_.startsWith(ignoredFilePathLine))
543-
.mkString("\n")
542+
val check: String = Source.fromFile(checkFile, "UTF-8").getLines().filter(!_.startsWith(ignoredFilePathLine))
543+
.mkString(EOL)
544544

545-
if (output.filter(!_.startsWith(ignoredFilePathLine)).mkString("\n") != check) {
545+
if (output.filter(!_.startsWith(ignoredFilePathLine)).mkString(EOL) != check) {
546546
val outFile = dotty.tools.io.File(checkFile.toPath).addExtension(".out")
547-
outFile.writeAll(output.mkString("\n"))
547+
outFile.writeAll(output.mkString(EOL))
548548
val msg =
549549
s"""Output differed for test $name, use the following command to see the diff:
550550
| > diff $checkFile $outFile
@@ -617,7 +617,7 @@ trait ParallelTesting extends RunnerOrchestration { self =>
617617
case Success(_) if !checkFile.isDefined || !checkFile.get.exists => // success!
618618
case Success(output) => {
619619
val outputLines = output.linesIterator.toArray :+ DiffUtil.EOF
620-
val checkLines: Array[String] = Source.fromFile(checkFile.get).getLines().toArray :+ DiffUtil.EOF
620+
val checkLines: Array[String] = Source.fromFile(checkFile.get, "UTF-8").getLines().toArray :+ DiffUtil.EOF
621621
val sourceTitle = testSource.title
622622

623623
def linesMatch =
@@ -726,7 +726,7 @@ trait ParallelTesting extends RunnerOrchestration { self =>
726726
val errorMap = new HashMap[String, Integer]()
727727
var expectedErrors = 0
728728
files.filter(_.getName.endsWith(".scala")).foreach { file =>
729-
Source.fromFile(file).getLines().zipWithIndex.foreach { case (line, lineNbr) =>
729+
Source.fromFile(file, "UTF-8").getLines().zipWithIndex.foreach { case (line, lineNbr) =>
730730
val errors = line.sliding("// error".length).count(_.mkString == "// error")
731731
if (errors > 0)
732732
errorMap.put(s"${file.getAbsolutePath}:${lineNbr}", errors)

0 commit comments

Comments
 (0)