Skip to content

Commit

Permalink
add assembly tests for append rules
Browse files Browse the repository at this point in the history
  • Loading branch information
rockjam committed May 9, 2018
1 parent 60bca8b commit 310cf99
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
##############################
# Core Reference Config File #
##############################
bar.baz=hello
5 changes: 5 additions & 0 deletions scalalib/test/resources/hello-world-multi/core/src/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Main extends App {
val person = Person.fromString("rockjam:25")
println(s"hello ${person.name}, your age is: ${person.age}")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
###############################
# Model Reference Config File #
###############################
foo.bar=2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
object Person {
def fromString(s: String): Person = {
val Array(name, age) = s.split(":")
Person(name, age.toInt)
}
}
case class Person(name: String, age: Int)

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
########################################
# My application Reference Config File #
########################################
akka.http.client.user-agent-header="hello-world-client"
78 changes: 74 additions & 4 deletions scalalib/test/src/mill/scalalib/HelloWorldTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package mill.scalalib
import java.util.jar.JarFile

import ammonite.ops._
import ammonite.ops.ImplicitWd._
import mill._
import mill.define.{Discover, Target}
import mill.define.Target
import mill.eval.{Evaluator, Result}
import mill.scalalib.publish._
import mill.util.{TestEvaluator, TestUtil}
import mill.scalalib.publish.VersionControl
import utest._

import utest.framework.TestPath

import scala.collection.JavaConverters._
Expand Down Expand Up @@ -50,6 +48,23 @@ object HelloWorldTests extends TestSuite {
}
}

object HelloWorldAkkaHttp extends HelloBase {
object core extends HelloWorldModule{
def mainClass = Some("Main")
def ivyDeps = Agg(
ivy"com.typesafe.akka::akka-http:10.0.13"
)
}
}

object HelloWorldMulti extends HelloBase {
object core extends HelloWorldModule {
def mainClass = Some("Main")
def moduleDeps = Seq(model)
}
object model extends HelloWorldModule
}

object HelloWorldWarnUnused extends HelloBase{
object core extends HelloWorldModule {
def scalacOptions = T(Seq("-Ywarn-unused"))
Expand Down Expand Up @@ -148,6 +163,15 @@ object HelloWorldTests extends TestSuite {
attrs.get(Name.MAIN_CLASS).map(_.asInstanceOf[String])
}

def jarEntries(jar: JarFile): Set[String] = {
jar.entries().asScala.map(_.getName).toSet
}

def readFileFromJar(jar: JarFile, name: String): String = {
val is = jar.getInputStream(jar.getEntry(name))
read(is)
}

def compileClassfiles = Seq[RelPath](
"Main.class",
"Main$.class",
Expand Down Expand Up @@ -442,7 +466,7 @@ object HelloWorldTests extends TestSuite {
evalCount > 0
)
val jarFile = new JarFile(result.path.toIO)
val entries = jarFile.entries().asScala.map(_.getName).toSet
val entries = jarEntries(jarFile)

val mainPresent = entries.contains("Main.class")
assert(mainPresent)
Expand All @@ -452,6 +476,52 @@ object HelloWorldTests extends TestSuite {
assert(mainClass.contains("Main"))
}

'assemblyRules - {
'appendWithDeps - workspaceTest(HelloWorldAkkaHttp) { eval =>
val Right((result, _)) = eval.apply(HelloWorldAkkaHttp.core.assembly)

val jarFile = new JarFile(result.path.toIO)

assert(jarEntries(jarFile).contains("reference.conf"))

val referenceContent = readFileFromJar(jarFile, "reference.conf")

assert(
// akka modules configs are present
referenceContent.contains("akka-http Reference Config File"),
referenceContent.contains("Akka Actor Reference Config File"),
referenceContent.contains("Akka Stream Reference Config File"),
// our application config is present too
referenceContent.contains("My application Reference Config File"),
referenceContent.contains(
"""akka.http.client.user-agent-header="hello-world-client""""
)
)
}
'appendMultiModule - workspaceTest(
HelloWorldMulti,
resourcePath = pwd / 'scalalib / 'test / 'resources / "hello-world-multi"
) { eval =>
val Right((result, _)) = eval.apply(HelloWorldMulti.core.assembly)

val jarFile = new JarFile(result.path.toIO)

assert(jarEntries(jarFile).contains("reference.conf"))

val referenceContent = readFileFromJar(jarFile, "reference.conf")

assert(
// reference config from core module
referenceContent.contains("Core Reference Config File"),
// reference config from model module
referenceContent.contains("Model Reference Config File"),
// concatenated content
referenceContent.contains("bar.baz=hello"),
referenceContent.contains("foo.bar=2")
)
}
}

'run - workspaceTest(HelloWorldWithMain){eval =>
val Right((result, evalCount)) = eval.apply(HelloWorldWithMain.core.assembly)

Expand Down

0 comments on commit 310cf99

Please sign in to comment.