Skip to content

Commit

Permalink
tests for append patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
rockjam committed May 13, 2018
1 parent 4a3e115 commit 2dfd893
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 88 deletions.
45 changes: 45 additions & 0 deletions main/src/mill/modules/Assembly.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package mill.modules

import java.util.jar.JarFile
import java.util.regex.Pattern

import upickle.default.{ReadWriter => RW}

object Assembly {

val defaultRules: Seq[Rule] = Seq(
Rule.Append("reference.conf"),
Rule.Exclude(JarFile.MANIFEST_NAME),
Rule.ExcludePattern(".*\\.[sS][fF]"),
Rule.ExcludePattern(".*\\.[dD][sS][aA]"),
Rule.ExcludePattern(".*\\.[rR][sS][aA]")
)

sealed trait Rule extends Product with Serializable
object Rule {
case class Append(path: String) extends Rule

object AppendPattern {
def apply(pattern: String): AppendPattern = AppendPattern(Pattern.compile(pattern))
}
case class AppendPattern(pattern: Pattern) extends Rule

case class Exclude(path: String) extends Rule

object ExcludePattern {
def apply(pattern: String): ExcludePattern = ExcludePattern(Pattern.compile(pattern))
}
case class ExcludePattern(pattern: Pattern) extends Rule

implicit lazy val appendRW: RW[Append] = upickle.default.macroRW
implicit lazy val appendPatternRW: RW[AppendPattern] = upickle.default.macroRW
implicit lazy val excludeRW: RW[Exclude] = upickle.default.macroRW
implicit lazy val excludePatternRW: RW[ExcludePattern] = upickle.default.macroRW
implicit lazy val regexRW: RW[Pattern] = upickle.default.readwriter[String].bimap[Pattern](
_.pattern(), Pattern.compile
)
implicit lazy val rulesFormat: RW[Rule] = {
RW.merge(appendRW, appendPatternRW, excludeRW, excludePatternRW)
}
}
}
29 changes: 0 additions & 29 deletions main/src/mill/modules/Jvm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import java.nio.file.{FileSystems, Files, StandardOpenOption}
import java.nio.file.attribute.PosixFilePermission
import java.util.Collections
import java.util.jar.{JarEntry, JarFile, JarOutputStream}
import java.util.regex.Pattern

import ammonite.ops._
import geny.Generator
Expand All @@ -19,34 +18,6 @@ import mill.util.Loose.Agg
import scala.collection.mutable
import scala.collection.JavaConverters._

object Assembly {

val defaultRules: Seq[Rule] = Seq(
Rule.Append("reference.conf"),
Rule.Exclude(JarFile.MANIFEST_NAME),
Rule.ExcludePattern(".*\\.[sS][fF]"),
Rule.ExcludePattern(".*\\.[dD][sS][aA]"),
Rule.ExcludePattern(".*\\.[rR][sS][aA]")
)

sealed trait Rule extends Product with Serializable
object Rule {
case class Append(path: String) extends Rule

object AppendPattern {
def apply(pattern: String): AppendPattern = AppendPattern(Pattern.compile(pattern))
}
case class AppendPattern(pattern: Pattern) extends Rule

case class Exclude(path: String) extends Rule

object ExcludePattern {
def apply(pattern: String): ExcludePattern = ExcludePattern(Pattern.compile(pattern))
}
case class ExcludePattern(pattern: Pattern) extends Rule
}
}

object Jvm {

def interactiveSubprocess(mainClass: String,
Expand Down
15 changes: 11 additions & 4 deletions scalalib/src/mill/scalalib/JavaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import coursier.Repository
import mill.define.Task
import mill.define.TaskModule
import mill.eval.{PathRef, Result}
import mill.modules.Jvm
import mill.modules.{Assembly, Jvm}
import mill.modules.Jvm.{createAssembly, createJar}
import Lib._
import mill.scalalib.publish.{Artifact, Scope}
Expand Down Expand Up @@ -108,6 +108,8 @@ trait JavaModule extends mill.Module with TaskModule { outer =>
}
}

def assemblyRules: T[Seq[Assembly.Rule]] = T { Assembly.defaultRules }

def sources = T.sources{ millSourcePath / 'src }
def resources = T.sources{ millSourcePath / 'resources }
def generatedSources = T{ Seq.empty[PathRef] }
Expand All @@ -130,6 +132,7 @@ trait JavaModule extends mill.Module with TaskModule { outer =>
upstreamCompileOutput()
)
}

def localClasspath = T{
resources() ++ Agg(compile().classes)
}
Expand Down Expand Up @@ -158,19 +161,23 @@ trait JavaModule extends mill.Module with TaskModule { outer =>
* upstream dependencies do not change
*/
def upstreamAssembly = T{
createAssembly(upstreamAssemblyClasspath().map(_.path), mainClass())
createAssembly(
upstreamAssemblyClasspath().map(_.path),
mainClass(),
assemblyRules = assemblyRules()
)
}

def assembly = T{
createAssembly(
Agg.from(localClasspath().map(_.path)),
mainClass(),
prependShellScript(),
Some(upstreamAssembly().path)
Some(upstreamAssembly().path),
assemblyRules()
)
}


def jar = T{
createJar(
localClasspath().map(_.path).filter(exists),
Expand Down
155 changes: 100 additions & 55 deletions scalalib/test/src/mill/scalalib/HelloWorldTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ammonite.ops._
import mill._
import mill.define.Target
import mill.eval.{Evaluator, Result}
import mill.modules.Assembly
import mill.scalalib.publish._
import mill.util.{TestEvaluator, TestUtil}
import mill.scalalib.publish.VersionControl
Expand All @@ -19,10 +20,14 @@ object HelloWorldTests extends TestSuite {
trait HelloBase extends TestUtil.BaseModule{
def millSourcePath = TestUtil.getSrcPathBase() / millOuterCtx.enclosing.split('.')
}

trait HelloWorldModule extends scalalib.ScalaModule {
def scalaVersion = "2.12.4"
}

trait HelloWorldModuleWithMain extends HelloWorldModule {
def mainClass = Some("Main")
}

object HelloWorld extends HelloBase {
object core extends HelloWorldModule
Expand All @@ -43,24 +48,41 @@ object HelloWorldTests extends TestSuite {
}

object HelloWorldWithMain extends HelloBase {
object core extends HelloWorldModule{
def mainClass = Some("Main")
object core extends HelloWorldModuleWithMain
}

val akkaHttpDep = Agg(ivy"com.typesafe.akka::akka-http:10.0.13")

object HelloWorldAkkaHttpAppend extends HelloBase {
object core extends HelloWorldModuleWithMain {
def ivyDeps = akkaHttpDep

def assemblyRules = T { Seq(Assembly.Rule.Append("reference.conf")) }
}
}

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 HelloWorldAkkaHttpAppendPattern extends HelloBase {
object core extends HelloWorldModuleWithMain {
def ivyDeps = akkaHttpDep

def assemblyRules = T { Seq(Assembly.Rule.AppendPattern(".*.conf")) }
}
}

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

def assemblyRules = T { Seq(Assembly.Rule.Append("reference.conf")) }
}
object model extends HelloWorldModule
}

object HelloWorldMultiAppendPattern extends HelloBase {
object core extends HelloWorldModuleWithMain {
def moduleDeps = Seq(model)

def assemblyRules = T { Seq(Assembly.Rule.AppendPattern(".*.conf")) }
}
object model extends HelloWorldModule
}
Expand Down Expand Up @@ -192,14 +214,14 @@ object HelloWorldTests extends TestSuite {
cp(resourcePath, m.millSourcePath)
t(eval)
}




def tests: Tests = Tests {
'scalaVersion - {
'fromBuild - workspaceTest(HelloWorld){eval =>

'fromBuild - workspaceTest(HelloWorld){eval =>
val Right((result, evalCount)) = eval.apply(HelloWorld.core.scalaVersion)

assert(
Expand Down Expand Up @@ -478,49 +500,72 @@ object HelloWorldTests extends TestSuite {
}

'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""""
def checkAppend[M <: TestUtil.BaseModule](module: M,
target: Target[PathRef]) =
workspaceTest(module) { eval =>
val Right((result, _)) = eval.apply(target)

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")
}

def checkAppendMulti[M <: TestUtil.BaseModule](
module: M,
target: Target[PathRef]) =
workspaceTest(
module,
resourcePath = pwd / 'scalalib / 'test / 'resources / "hello-world-multi"
) { eval =>
val Right((result, _)) = eval.apply(target)

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")
)
}

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")
)
}
'appendWithDeps - checkAppend(
HelloWorldAkkaHttpAppend,
HelloWorldAkkaHttpAppend.core.assembly
)
'appendMultiModule - checkAppendMulti(
HelloWorldMultiAppend,
HelloWorldMultiAppend.core.assembly
)
'appendPatternWithDeps - checkAppend(
HelloWorldAkkaHttpAppendPattern,
HelloWorldAkkaHttpAppendPattern.core.assembly
)
'appendMultiModule - checkAppendMulti(
HelloWorldMultiAppendPattern,
HelloWorldMultiAppendPattern.core.assembly
)
}

'run - workspaceTest(HelloWorldWithMain){eval =>
Expand Down

0 comments on commit 2dfd893

Please sign in to comment.