|
| 1 | +package bloop.scalanative |
| 2 | + |
| 3 | +import java.util.concurrent.TimeUnit |
| 4 | + |
| 5 | +import scala.concurrent.duration.Duration |
| 6 | + |
| 7 | +import bloop.cli.Commands |
| 8 | +import bloop.cli.OptimizerConfig |
| 9 | +import bloop.data.Platform |
| 10 | +import bloop.data.Project |
| 11 | +import bloop.engine.Run |
| 12 | +import bloop.engine.tasks.toolchains.ScalaNativeToolchain |
| 13 | +import bloop.logging.RecordingLogger |
| 14 | +import bloop.util.TestUtil |
| 15 | + |
| 16 | +import org.junit.Assert.assertTrue |
| 17 | +import org.junit.Test |
| 18 | +import org.junit.experimental.categories.Category |
| 19 | +import bloop.engine.State |
| 20 | +import bloop.config.Config |
| 21 | + |
| 22 | +@Category(Array(classOf[bloop.FastTests])) |
| 23 | +class ScalaNativeToolchainSpec { |
| 24 | + private val state0 = { |
| 25 | + def setUpNative(p: Project): Project = { |
| 26 | + val platform = p.platform match { |
| 27 | + case nativePlatform: Platform.Native => |
| 28 | + nativePlatform.copy( |
| 29 | + toolchain = Some(ScalaNativeToolchain.apply(this.getClass.getClassLoader)) |
| 30 | + ) |
| 31 | + |
| 32 | + case _ => p.platform |
| 33 | + } |
| 34 | + p.copy(platform = platform) |
| 35 | + } |
| 36 | + |
| 37 | + val configDir = TestUtil.getBloopConfigDir("cross-test-build-scala-native-0.5") |
| 38 | + val logger = bloop.logging.BloopLogger.default(configDir.toString()) |
| 39 | + TestUtil.loadTestProject(configDir, logger, true, _.map(setUpNative)) |
| 40 | + } |
| 41 | + @Test def canLinkScalaNativeProject(): Unit = { |
| 42 | + val logger = new RecordingLogger |
| 43 | + val state = state0.copy(logger = logger) |
| 44 | + val action = Run(Commands.Link(List("test-projectNative"))) |
| 45 | + val resultingState = TestUtil.blockingExecute(action, state, maxDuration) |
| 46 | + |
| 47 | + assertTrue(s"Linking failed: ${logger.getMessages.mkString("\n")}", resultingState.status.isOk) |
| 48 | + logger.getMessages.assertContain("Generated native binary '", atLevel = "info") |
| 49 | + } |
| 50 | + |
| 51 | + @Test def canLinkScalaNativeProjectInReleaseMode(): Unit = { |
| 52 | + val logger = new RecordingLogger |
| 53 | + val mode = OptimizerConfig.Release |
| 54 | + val state = state0.copy(logger = logger) |
| 55 | + val action = Run(Commands.Link(List("test-projectNative"), optimize = Some(mode))) |
| 56 | + val resultingState = TestUtil.blockingExecute(action, state, maxDuration * 3) |
| 57 | + |
| 58 | + assertTrue( |
| 59 | + s"Linking failed: ${logger.getMessages.mkString("\n")}", |
| 60 | + resultingState.status.isOk |
| 61 | + ) |
| 62 | + logger.getMessages.assertContain("Optimizing (release-fast mode)", atLevel = "info") |
| 63 | + } |
| 64 | + |
| 65 | + @Test def canRunScalaNativeProjectDefaultMainClass(): Unit = { |
| 66 | + val logger = new RecordingLogger |
| 67 | + val state = state0.copy(logger = logger) |
| 68 | + val action = Run(Commands.Run(List("test-projectNative"))) |
| 69 | + val resultingState = TestUtil.blockingExecute(action, state, maxDuration) |
| 70 | + |
| 71 | + assertTrue( |
| 72 | + s"Linking failed: ${logger.getMessages.mkString("\n")}", |
| 73 | + resultingState.status.isOk |
| 74 | + ) |
| 75 | + logger.getMessages.assertContain("Hello, world from DefaultApp!", atLevel = "info") |
| 76 | + } |
| 77 | + |
| 78 | + @Test def canRunScalaJvmProjectDefaultMainClass(): Unit = { |
| 79 | + val logger = new RecordingLogger |
| 80 | + val state = state0.copy(logger = logger) |
| 81 | + val action = Run(Commands.Run(List("test-project"), main = None)) |
| 82 | + val resultingState = TestUtil.blockingExecute(action, state, maxDuration) |
| 83 | + |
| 84 | + assertTrue( |
| 85 | + s"Linking failed: ${logger.getMessages.mkString("\n")}", |
| 86 | + resultingState.status.isOk |
| 87 | + ) |
| 88 | + logger.getMessages.assertContain("Hello, world!", atLevel = "info") |
| 89 | + } |
| 90 | + |
| 91 | + private val maxDuration = Duration.apply(90, TimeUnit.SECONDS) |
| 92 | + private implicit class RichLogs(logs: List[(String, String)]) { |
| 93 | + def assertContain(needle: String, atLevel: String): Unit = { |
| 94 | + def failMessage = s"""Logs did not contain `$needle` at level `$atLevel`. Logs were: |
| 95 | + |${logs.mkString("\n")}""".stripMargin |
| 96 | + assertTrue( |
| 97 | + failMessage, |
| 98 | + logs.exists { |
| 99 | + case (`atLevel`, msg) => msg.contains(needle) |
| 100 | + case _ => false |
| 101 | + } |
| 102 | + ) |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments