Skip to content

Commit

Permalink
Merge pull request #1980 from ckipp01/variousImprovements
Browse files Browse the repository at this point in the history
Small test improvements
  • Loading branch information
ckipp01 committed Jan 9, 2023
2 parents 9d92924 + 6d89740 commit 097725c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 69 deletions.
114 changes: 59 additions & 55 deletions frontend/src/test/scala/bloop/ScalaVersionsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,70 +11,74 @@ import bloop.util.TestProject
import bloop.util.TestUtil

object ScalaVersionsSpec extends bloop.testing.BaseSuite {
test("cross-compile build to latest Scala versions") {
var loggers: List[RecordingLogger] = Nil
def compileProjectFor(scalaVersion: String): Task[Unit] = Task {
TestUtil.withinWorkspace { workspace =>
val (compilerOrg, compilerArtifact) = {
if (scalaVersion.startsWith("3.")) "org.scala-lang" -> "scala3-compiler_3.0.0-M3"
else "org.scala-lang" -> "scala-compiler"
}
var loggers: List[RecordingLogger] = Nil

def jarsForScalaVersion(version: String, logger: RecordingLogger) = {
ScalaInstance
.resolve(compilerOrg, compilerArtifact, version, logger)
.allJars
.map(AbsolutePath(_))
}
def compileProjectFor(scalaVersion: String): Task[Unit] = Task {
TestUtil.withinWorkspace { workspace =>
val (compilerOrg, compilerArtifact) = {
if (scalaVersion.startsWith("3.")) "org.scala-lang" -> "scala3-compiler_3"
else "org.scala-lang" -> "scala-compiler"
}

val source = {
if (compilerArtifact.contains("scala3-compiler")) {
"""/main/scala/Foo.scala
|class Foo { val x: String | Int = 1 }
def jarsForScalaVersion(version: String, logger: RecordingLogger) = {
ScalaInstance
.resolve(compilerOrg, compilerArtifact, version, logger)
.allJars
.map(AbsolutePath(_))
}

val source = {
if (compilerArtifact.contains("scala3-compiler")) {
"""/main/scala/Foo.scala
|class Foo { val x: String | Int = 1 }
""".stripMargin
} else {
"""/main/scala/Foo.scala
|class Foo
} else {
"""/main/scala/Foo.scala
|class Foo
""".stripMargin
}
}
}

val logger = new RecordingLogger(ansiCodesSupported = false)
loggers.synchronized { loggers = logger :: loggers }
val jars = jarsForScalaVersion(scalaVersion, logger)
val `A` = TestProject(
workspace,
"a",
List(source),
scalaOrg = Some(compilerOrg),
scalaCompiler = Some(compilerArtifact),
scalaVersion = Some(scalaVersion),
jars = jars
)
val logger = new RecordingLogger(ansiCodesSupported = false)
loggers.synchronized { loggers = logger :: loggers }
val jars = jarsForScalaVersion(scalaVersion, logger)
val `A` = TestProject(
workspace,
"a",
List(source),
scalaOrg = Some(compilerOrg),
scalaCompiler = Some(compilerArtifact),
scalaVersion = Some(scalaVersion),
jars = jars
)

val projects = List(`A`)
val state = loadState(workspace, projects, logger)
val compiledState = state.compile(`A`)
try {
Predef.assert(compiledState.status == ExitStatus.Ok)
assertValidCompilationState(compiledState, projects)
} finally loggers.synchronized { loggers = loggers.filterNot(_ == logger) }
}
val projects = List(`A`)
val state = loadState(workspace, projects, logger)
val compiledState = state.compile(`A`)
try {
Predef.assert(compiledState.status == ExitStatus.Ok)
assertValidCompilationState(compiledState, projects)
} finally loggers.synchronized { loggers = loggers.filterNot(_ == logger) }
}
}

val `2.10` = compileProjectFor("2.10.7")
val `2.11` = compileProjectFor("2.11.12")
val `2.12` = compileProjectFor("2.12.9")
val `2.13` = compileProjectFor("2.13.2")
val `2.13.3` = compileProjectFor("2.13.3")
val `2.13.7` = compileProjectFor("2.13.7")
val `2.13.8` = compileProjectFor("2.13.8")
val LatestDotty = compileProjectFor("3.0.0-M3")
val all = {
if (TestUtil.isJdk8)
List(`2.10`, `2.11`, `2.12`, `2.13`, `2.13.3`, `2.13.7`, `2.13.8`, LatestDotty)
else List(`2.12`, `2.13`, `2.13.3`, `2.13.7`)
}
val jdk8OnlyVersions = List(
"2.10.7",
"2.11.12"
)

val scalaVersions = List(
"2.12.17",
"2.13.10",
"3.1.3",
"3.2.1"
)

val allVersions = if (TestUtil.isJdk8) jdk8OnlyVersions ++ scalaVersions else scalaVersions

test("cross-compile build to latest Scala versions") {

val all = allVersions.map(compileProjectFor)

try {
TestUtil.await(FiniteDuration(120, "s"), ExecutionContext.ioScheduler) {
Expand Down
20 changes: 6 additions & 14 deletions frontend/src/test/scala/bloop/util/TestUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import _root_.monix.execution.Scheduler
import org.junit.Assert
import sbt.internal.inc.BloopComponentCompiler
import xsbti.ComponentProvider
import java.lang.management.ManagementFactory

object TestUtil {
def projectDir(base: Path, name: String): Path = base.resolve(name)
Expand Down Expand Up @@ -633,19 +634,10 @@ object TestUtil {
}

def threadDump: String = {
// Get the PID of the current JVM process
val selfName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName()
val selfPid = selfName.substring(0, selfName.indexOf('@'))

// Attach to the VM
import com.sun.tools.attach.VirtualMachine
import sun.tools.attach.HotSpotVirtualMachine;
val vm = VirtualMachine.attach(selfPid);
val hotSpotVm = vm.asInstanceOf[HotSpotVirtualMachine];

// Request a thread dump
val inputStream = hotSpotVm.remoteDataDump()
try new String(Stream.continually(inputStream.read).takeWhile(_ != -1).map(_.toByte).toArray)
finally inputStream.close()
val sb = new StringBuilder
val mxBean = ManagementFactory.getThreadMXBean()
val stacktraces = mxBean.dumpAllThreads(true, true)
stacktraces.foreach(threadInfo => sb.append(threadInfo.toString()).append("\n"))
sb.result()
}
}

0 comments on commit 097725c

Please sign in to comment.