Skip to content

Commit

Permalink
BuildTarget: Resolve exception when no parent modules available
Browse files Browse the repository at this point in the history
This fixes a regression introduced in 62d654e.
  • Loading branch information
tindzk committed Apr 25, 2020
1 parent dffd692 commit 586c328
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 52 deletions.
19 changes: 10 additions & 9 deletions src/main/scala/seed/cli/BuildTarget.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,16 @@ object BuildTarget {
val modulePath = build(m).path
val target = build(m).module.target(t)

val moduleSourcePaths = parentModules((m, t)).flatMap { module =>
val targets = BuildConfig.allTargets(build, module)
targets
.flatMap {
case (m, t) => BuildConfig.platformModule(build(m).module, t)
}
.flatMap(_.sources)
.map(_.toAbsolutePath.toString)
}
val moduleSourcePaths =
parentModules.getOrElse((m, t), List()).flatMap { module =>
val targets = BuildConfig.allTargets(build, module)
targets
.flatMap {
case (m, t) => BuildConfig.platformModule(build(m).module, t)
}
.flatMap(_.sources)
.map(_.toAbsolutePath.toString)
}

target.`class` match {
case Some(c) =>
Expand Down
60 changes: 17 additions & 43 deletions src/test/scala/seed/generation/BloopIntegrationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ import bloop.config.Config.JsConfig
import bloop.config.ConfigEncoderDecoders
import minitest.TestSuite
import org.apache.commons.io.FileUtils
import seed.{Log, LogLevel, cli}
import seed.{Log, cli}
import seed.Cli.{Command, PackageConfig}
import seed.cli.util.RTS
import seed.config.BuildConfig
import seed.generation.util.TestProcessHelper
import seed.generation.util.{CustomTargetUtil, TestProcessHelper}
import seed.generation.util.TestProcessHelper.ec
import seed.model.Config

import scala.concurrent.Future
import seed.generation.util.BuildUtil.tempPath

import scala.collection.mutable.ListBuffer

object BloopIntegrationSpec extends TestSuite[Unit] {
override def setupSuite(): Unit = TestProcessHelper.semaphore.acquire()
override def tearDownSuite(): Unit = TestProcessHelper.semaphore.release()
Expand Down Expand Up @@ -278,72 +276,47 @@ object BloopIntegrationSpec extends TestSuite[Unit] {
}
}

def buildCustomTarget(
def buildCustomTargetAndRun(
name: String,
expectFailure: Boolean = false
): Future[Either[List[String], List[String]]] = {
val path = Paths.get("test", name)

val config = BuildConfig.load(path, Log.urgent).get
val testId = "integ"
val (config, lines, uio) =
CustomTargetUtil.buildCustomTarget(name, "demo", testId)
import config._
val buildPath = tempPath.resolve(name)
Files.createDirectory(buildPath)
cli.Generate.ui(
Config(),
projectPath,
buildPath,
resolvers,
build,
Command.Bloop(packageConfig),
Log.urgent
)

val lines = ListBuffer[String]()

val result = seed.cli.Build.build(
path,
Some(buildPath),
List("demo"),
watch = false,
tmpfs = false,
progress = false,
new Log(lines += _, identity, LogLevel.Warn, false),
stdOut => lines ++= stdOut.split('\n'),
_ => ()
)

val uio = result.right.get

if (expectFailure)
RTS.unsafeRunToFuture(uio).failed.map { _ =>
assert(lines.nonEmpty)
Left(lines.toList)
val l = lines()
assert(l.nonEmpty)
Left(l)
} else {
RTS.unsafeRunSync(uio)

val generatedFile = projectPath.resolve("demo").resolve("Generated.scala")
assert(Files.exists(generatedFile))

val buildPath = tempPath.resolve(testId).resolve(name)
TestProcessHelper
.runBloop(buildPath)("run", "demo")
.map { x =>
Files.delete(generatedFile)
assertEquals(lines.toList, List())
assertEquals(lines(), List())
Right(x.split("\n").toList)
}
}
}

testAsync("Build project with custom class target") { _ =>
buildCustomTarget("custom-class-target").map(
buildCustomTargetAndRun("custom-class-target").map(
lines => assertEquals(lines.right.get.count(_ == "42"), 1)
)
}

testAsync(
"Build project with custom class target (shared by multiple modules)"
) { _ =>
buildCustomTarget("custom-class-target-shared").map(
buildCustomTargetAndRun("custom-class-target-shared").map(
lines =>
assertEquals(
lines.right.get.map(_.split("test/").last),
Expand All @@ -356,10 +329,11 @@ object BloopIntegrationSpec extends TestSuite[Unit] {
}

testAsync("Build project with custom command target") { _ =>
buildCustomTarget("custom-command-target").map { lines =>
buildCustomTargetAndRun("custom-command-target").map { lines =>
assertEquals(lines.right.get.count(_ == "42"), 1)

val path = tempPath
.resolve("integ")
.resolve("custom-command-target")
.resolve(".bloop")
.resolve("demo.json")
Expand All @@ -377,12 +351,12 @@ object BloopIntegrationSpec extends TestSuite[Unit] {
}

testAsync("Build project with failing custom command target") { _ =>
buildCustomTarget("custom-command-target-fail", expectFailure = true)
buildCustomTargetAndRun("custom-command-target-fail", expectFailure = true)
.map(_ => ())
}

testAsync("Build project with failing compilation") { _ =>
buildCustomTarget("compilation-failure", expectFailure = true).map(
buildCustomTargetAndRun("compilation-failure", expectFailure = true).map(
log =>
// Must indicate correct position
assert(
Expand Down
31 changes: 31 additions & 0 deletions src/test/scala/seed/generation/BuildTargetSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package seed.generation

import java.nio.file.Files

import minitest.TestSuite
import seed.cli.util.RTS
import seed.generation.util.{CustomTargetUtil, TestProcessHelper}
import seed.generation.util.TestProcessHelper.ec

object BuildTargetSpec extends TestSuite[Unit] {
override def setupSuite(): Unit = TestProcessHelper.semaphore.acquire()
override def tearDownSuite(): Unit = TestProcessHelper.semaphore.release()

override def setup(): Unit = ()
override def tearDown(env: Unit): Unit = ()

testAsync("Build custom target") { _ =>
val (config, _, uio) =
CustomTargetUtil.buildCustomTarget(
"custom-command-target",
"utils",
"build-target"
)
import config._
RTS.unsafeRunToFuture(uio).map { _ =>
val generatedFile = projectPath.resolve("demo").resolve("Generated.scala")
assert(Files.exists(generatedFile))
Files.delete(generatedFile)
}
}
}
53 changes: 53 additions & 0 deletions src/test/scala/seed/generation/util/CustomTargetUtil.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package seed.generation.util

import java.nio.file.{Files, Paths}

import seed.Cli.Command
import seed.{Log, LogLevel, cli}
import seed.config.BuildConfig
import seed.generation.BloopIntegrationSpec.packageConfig
import seed.generation.util.BuildUtil.tempPath
import seed.model.Config

import scala.collection.mutable.ListBuffer

object CustomTargetUtil {
def buildCustomTarget(
name: String,
module: String,
testId: String
): (BuildConfig.Result, () => List[String], zio.UIO[Unit]) = {
val path = Paths.get("test", name)

val config = BuildConfig.load(path, Log.urgent).get
import config._
val buildPath = tempPath.resolve(testId).resolve(name)
Files.createDirectories(buildPath)
cli.Generate.ui(
Config(),
projectPath,
buildPath,
resolvers,
build,
Command.Bloop(packageConfig),
Log.urgent
)

val lines = ListBuffer[String]()

val result = seed.cli.Build.build(
path,
Some(buildPath),
List(module),
watch = false,
tmpfs = false,
progress = false,
new Log(lines += _, identity, LogLevel.Warn, false),
stdOut => lines ++= stdOut.split('\n'),
_ => ()
)

val uio = result.right.get
(config, () => lines.toList, uio)
}
}

0 comments on commit 586c328

Please sign in to comment.