Skip to content

fix resoruce leaks in tests and REPL, fixes #8358 #9036

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
May 26, 2020
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
3 changes: 2 additions & 1 deletion bench-run/src/main/scala/dotty/tools/benchmarks/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.openjdk.jmh.runner.options._
import java.util.concurrent.TimeUnit

import scala.io.Source
import scala.util.Using

object Bench {
def main(args: Array[String]): Unit = {
Expand Down Expand Up @@ -57,7 +58,7 @@ object Bench {
}

def paramsFromFile(file: String): Array[(String, Array[String])] = {
Source.fromFile(file).getLines.toArray.map { l =>
Using(Source.fromFile(file))(_.getLines.toArray).get.map { l =>
val Array(param, values) = l split ':'
(param, values split ',')
}
Expand Down
3 changes: 2 additions & 1 deletion bench/src/main/scala/Benchmarks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import java.util.concurrent.TimeUnit
import java.io.{File, FileOutputStream, BufferedWriter, FileWriter}
import scala.collection.JavaConverters._
import scala.io.Source
import scala.util.Using

object Bench {
val COMPILE_OPTS_FILE = "compile.txt"
Expand Down Expand Up @@ -77,7 +78,7 @@ object Bench {
}

def readCompileOptions: Seq[String] =
Source.fromFile(COMPILE_OPTS_FILE).getLines.toSeq
Using(Source.fromFile(COMPILE_OPTS_FILE))(_.getLines.toSeq).get
}

@State(Scope.Benchmark)
Expand Down
3 changes: 2 additions & 1 deletion bin/test/TestScripts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.junit.{Before, After, Test}

import scala.io.Source
import scala.sys.process.{Process, ProcessLogger}
import scala.util.Using
import java.io.{File => JFile, FileNotFoundException}

class TestScripts {
Expand All @@ -29,7 +30,7 @@ class TestScripts {

private def deletePackages: Unit = {
try {
for (jar <- Source.fromFile("./.packages").getLines())
for (jar <- Using(Source.fromFile("./.packages"))(_.getLines().toList).get)
delete(jar)

delete("./.packages")
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/repl/ReplDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.jline.reader._

import scala.annotation.tailrec
import scala.collection.JavaConverters._
import scala.util.Using

/** The state of the REPL contains necessary bindings instead of having to have
* mutation
Expand Down Expand Up @@ -351,7 +352,7 @@ class ReplDriver(settings: Array[String],
case Load(path) =>
val file = new JFile(path)
if (file.exists) {
val contents = scala.io.Source.fromFile(file, "UTF-8").mkString
val contents = Using(scala.io.Source.fromFile(file, "UTF-8"))(_.mkString).get
run(contents)
}
else {
Expand Down
3 changes: 2 additions & 1 deletion compiler/test/debug/Gen.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import scala.io.Source
import scala.collection.mutable.ListBuffer
import scala.util.Using

/** Automate testing debuggability of generated code using JDB and expect
*
Expand Down Expand Up @@ -64,7 +65,7 @@ object Gen {
}

def parse(file: String): Program = {
val lines = Source.fromFile(file).getLines().toBuffer
val lines = Using(Source.fromFile(file))(_.getLines().toBuffer).get

val breaks = new ListBuffer[Break]()
val cmds = new ListBuffer[Command]()
Expand Down
32 changes: 10 additions & 22 deletions compiler/test/dotty/tools/repl/ReplTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import vulpix.TestConfiguration
import java.lang.System.{lineSeparator => EOL}
import java.io.{ByteArrayOutputStream, File => JFile, PrintStream}
import scala.io.Source
import scala.util.Using

import scala.collection.mutable.ArrayBuffer

Expand Down Expand Up @@ -56,26 +57,8 @@ class ReplTest(withStaging: Boolean = false, out: ByteArrayOutputStream = new By

def testFile(f: JFile): Unit = {
val prompt = "scala>"
val lines = Source.fromFile(f, "UTF-8").getLines().buffered

assert(lines.head.startsWith(prompt),
s"""Each file has to start with the prompt: "$prompt"""")

def extractInputs(prompt: String): List[String] = {
val input = lines.next()

if (!input.startsWith(prompt)) extractInputs(prompt)
else if (lines.hasNext) {
// read lines and strip trailing whitespace:
while (lines.hasNext && !lines.head.startsWith(prompt))
lines.next()

input :: { if (lines.hasNext) extractInputs(prompt) else Nil }
}
else Nil
}

def evaluate(state: State, input: String, prompt: String) =
def evaluate(state: State, input: String) =
try {
val nstate = run(input.drop(prompt.length))(state)
val out = input + EOL + storedOutput()
Expand All @@ -94,13 +77,18 @@ class ReplTest(withStaging: Boolean = false, out: ByteArrayOutputStream = new By
}

val expectedOutput =
Source.fromFile(f, "UTF-8").getLines().flatMap(filterEmpties).mkString(EOL)
Using(Source.fromFile(f, "UTF-8"))(_.getLines().flatMap(filterEmpties).mkString(EOL)).get
val actualOutput = {
resetToInitial()
val inputRes = extractInputs(prompt)

val lines = Using(Source.fromFile(f, "UTF-8"))(_.getLines.toList).get
assert(lines.head.startsWith(prompt),
s"""Each file has to start with the prompt: "$prompt"""")
val inputRes = lines.filter(_.startsWith(prompt))

val buf = new ArrayBuffer[String]
inputRes.foldLeft(initialState) { (state, input) =>
val (out, nstate) = evaluate(state, input, prompt)
val (out, nstate) = evaluate(state, input)
buf.append(out)

assert(out.endsWith("\n"),
Expand Down
3 changes: 2 additions & 1 deletion compiler/test/dotty/tools/vulpix/FileDiff.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dotty.tools.vulpix

import scala.io.Source
import scala.util.Using
import java.io.File
import java.lang.System.{lineSeparator => EOL}
import java.nio.file.{Files, Paths}
Expand All @@ -17,7 +18,7 @@ object FileDiff {
def check(sourceTitle: String, outputLines: Seq[String], checkFile: String): Option[String] = {
val checkLines =
if (!(new File(checkFile)).exists) Nil
else Source.fromFile(checkFile, "UTF-8").getLines().toList
else Using(Source.fromFile(checkFile, "UTF-8"))(_.getLines().toList).get

def linesMatch =
outputLines.length == checkLines.length &&
Expand Down
52 changes: 27 additions & 25 deletions compiler/test/dotty/tools/vulpix/ParallelTesting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.util.concurrent.{TimeUnit, TimeoutException, Executors => JExecutors

import scala.collection.mutable
import scala.io.Source
import scala.util.{Random, Try, Failure => TryFailure, Success => TrySuccess}
import scala.util.{Random, Try, Failure => TryFailure, Success => TrySuccess, Using}
import scala.util.control.NonFatal
import scala.util.matching.Regex

Expand Down Expand Up @@ -654,33 +654,35 @@ trait ParallelTesting extends RunnerOrchestration { self =>
val errorMap = new HashMap[String, Integer]()
var expectedErrors = 0
files.filter(_.getName.endsWith(".scala")).foreach { file =>
Source.fromFile(file, "UTF-8").getLines().zipWithIndex.foreach { case (line, lineNbr) =>
val errors = line.toSeq.sliding("// error".length).count(_.unwrap == "// error")
if (errors > 0)
errorMap.put(s"${file.getPath}:$lineNbr", errors)

val noposErrors = line.toSeq.sliding("// nopos-error".length).count(_.unwrap == "// nopos-error")
if (noposErrors > 0) {
val nopos = errorMap.get("nopos")
val existing: Integer = if (nopos eq null) 0 else nopos
errorMap.put("nopos", noposErrors + existing)
}
Using(Source.fromFile(file, "UTF-8")) { source =>
source.getLines.zipWithIndex.foreach { case (line, lineNbr) =>
val errors = line.toSeq.sliding("// error".length).count(_.unwrap == "// error")
if (errors > 0)
errorMap.put(s"${file.getPath}:$lineNbr", errors)

val noposErrors = line.toSeq.sliding("// nopos-error".length).count(_.unwrap == "// nopos-error")
if (noposErrors > 0) {
val nopos = errorMap.get("nopos")
val existing: Integer = if (nopos eq null) 0 else nopos
errorMap.put("nopos", noposErrors + existing)
}

val anyposErrors = line.toSeq.sliding("// anypos-error".length).count(_.unwrap == "// anypos-error")
if (anyposErrors > 0) {
val anypos = errorMap.get("anypos")
val existing: Integer = if (anypos eq null) 0 else anypos
errorMap.put("anypos", anyposErrors + existing)
}
val anyposErrors = line.toSeq.sliding("// anypos-error".length).count(_.unwrap == "// anypos-error")
if (anyposErrors > 0) {
val anypos = errorMap.get("anypos")
val existing: Integer = if (anypos eq null) 0 else anypos
errorMap.put("anypos", anyposErrors + existing)
}

val possibleTypos = List("//error" -> "// error", "//nopos-error" -> "// nopos-error", "//anypos-error" -> "// anypos-error")
for ((possibleTypo, expected) <- possibleTypos) {
if (line.contains(possibleTypo))
echo(s"Warning: Possible typo in error tag in file ${file.getCanonicalPath}:$lineNbr: found `$possibleTypo` but expected `$expected`")
}
val possibleTypos = List("//error" -> "// error", "//nopos-error" -> "// nopos-error", "//anypos-error" -> "// anypos-error")
for ((possibleTypo, expected) <- possibleTypos) {
if (line.contains(possibleTypo))
echo(s"Warning: Possible typo in error tag in file ${file.getCanonicalPath}:$lineNbr: found `$possibleTypo` but expected `$expected`")
}

expectedErrors += anyposErrors + noposErrors + errors
}
expectedErrors += anyposErrors + noposErrors + errors
}
}.get
}

(errorMap, expectedErrors)
Expand Down
5 changes: 3 additions & 2 deletions doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import dotc.core.Contexts.Context
import dotc.util.SourceFile
import model.Package
import scala.io.{ Codec, Source }
import scala.util.Using
import io.{ AbstractFile, VirtualFile, File }
import scala.collection.mutable.ArrayBuffer
import util.syntax._
Expand Down Expand Up @@ -90,7 +91,7 @@ case class Site(
root
.listFiles
.find(_.getName == "sidebar.yml")
.map("---\n" + Source.fromFile(_).mkString + "\n---")
.map(f => "---\n" + Using(Source.fromFile(f))(_.mkString).get + "\n---")
.map(Yaml.apply)
.flatMap(Sidebar.apply)
.getOrElse(Sidebar.empty)
Expand Down Expand Up @@ -412,7 +413,7 @@ case class Site(
}

private def toSourceFile(f: JFile): SourceFile =
new SourceFile(AbstractFile.getFile(new File(f.toPath)), Source.fromFile(f, "UTF-8").toArray)
new SourceFile(AbstractFile.getFile(new File(f.toPath)), Using(Source.fromFile(f, "UTF-8"))(_.toArray).get)

private def collectFiles(dir: JFile, includes: String => Boolean): Array[JFile] =
dir
Expand Down
3 changes: 2 additions & 1 deletion tests/run-custom-args/tasty-interpreter/Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dotty.tools.dotc.util.DiffUtil
import dotty.tools.io.Path

import scala.io.Source
import scala.util.Using
import scala.tasty.interpreter.TastyInterpreter

object Test {
Expand Down Expand Up @@ -89,7 +90,7 @@ object Test {

val checkFile = java.nio.file.Paths.get("tests/run/" + testFileName.stripSuffix(".scala") + ".check")
if (java.nio.file.Files.exists(checkFile)) {
val expectedOutput = Source.fromFile(checkFile.toFile).getLines().mkString("", "\n", "\n")
val expectedOutput = Using(Source.fromFile(checkFile.toFile))(_.getLines().mkString("", "\n", "\n")).get

assert(expectedOutput == actualOutput,
"\n>>>>>>>>>>>>>>>>>>\n" +
Expand Down