diff --git a/contrib/scoverage/src/mill/contrib/scoverage/ScoverageModule.scala b/contrib/scoverage/src/mill/contrib/scoverage/ScoverageModule.scala index fd603983b5b..eee13c22d86 100644 --- a/contrib/scoverage/src/mill/contrib/scoverage/ScoverageModule.scala +++ b/contrib/scoverage/src/mill/contrib/scoverage/ScoverageModule.scala @@ -3,8 +3,8 @@ package mill.contrib.scoverage import coursier.Repository import mill._ import mill.api.{Loose, PathRef, Result} -import mill.main.BuildInfo import mill.contrib.scoverage.api.ScoverageReportWorkerApi.ReportType +import mill.main.BuildInfo import mill.scalalib.api.ZincWorkerUtil import mill.scalalib.{Dep, DepSyntax, JavaModule, ScalaModule} import mill.util.Util.millProjectModule @@ -188,6 +188,7 @@ trait ScoverageModule extends ScalaModule { outer: ScalaModule => PathRef(T.dest) } + override def compileResources: T[Seq[PathRef]] = outer.compileResources override def generatedSources: Target[Seq[PathRef]] = T { outer.generatedSources() } override def allSources: Target[Seq[PathRef]] = T { outer.allSources() } override def moduleDeps: Seq[JavaModule] = outer.moduleDeps @@ -225,30 +226,25 @@ trait ScoverageModule extends ScalaModule { outer: ScalaModule => def xmlCoberturaReport(): Command[Unit] = T.command { doReport(ReportType.XmlCobertura) } def consoleReport(): Command[Unit] = T.command { doReport(ReportType.Console) } - override def skipIdea = outer.skipIdea + override def skipIdea = true } trait ScoverageTests extends ScalaTests { - override def upstreamAssemblyClasspath = T { - super.upstreamAssemblyClasspath() ++ - resolveDeps(T.task { - outer.scoverageRuntimeDeps().map(bindDependency()) - })() - } - override def compileClasspath = T { - super.compileClasspath() ++ - resolveDeps(T.task { - outer.scoverageRuntimeDeps().map(bindDependency()) - })() - } - override def runClasspath = T { - super.runClasspath() ++ + + /** + * Alter classfiles and resources from upstream modules and dependencies + * by removing the ones from outer.localRunClasspath() and replacing them + * with outer.scoverage.localRunClasspath() + */ + override def runClasspath: T[Seq[PathRef]] = T { + val outerLocalRunClasspath = outer.localRunClasspath().toSet + super.runClasspath().filterNot( + outerLocalRunClasspath + ) ++ + outer.scoverage.localRunClasspath() ++ resolveDeps(T.task { outer.scoverageRuntimeDeps().map(bindDependency()) })() } - - // Need the sources compiled with scoverage instrumentation to run. - override def moduleDeps: Seq[JavaModule] = Seq(outer.scoverage) } } diff --git a/example/misc/7-contrib-scoverage/build.sc b/example/misc/7-contrib-scoverage/build.sc new file mode 100644 index 00000000000..24b8a777610 --- /dev/null +++ b/example/misc/7-contrib-scoverage/build.sc @@ -0,0 +1,60 @@ +import mill._, scalalib._ +import $ivy.`com.lihaoyi::mill-contrib-scoverage:` + +import mill.contrib.scoverage._ + +object foo extends RootModule with ScoverageModule { + def scoverageVersion = "2.1.0" + def scalaVersion = "2.13.11" + def ivyDeps = Agg( + ivy"com.lihaoyi::scalatags:0.12.0", + ivy"com.lihaoyi::mainargs:0.6.2" + ) + + object test extends ScoverageTests /*with TestModule.Utest */{ + def ivyDeps = Agg(ivy"com.lihaoyi::utest:0.7.11") + def testFramework = "utest.runner.Framework" + } +} + +// This is a basic Mill build for a single `ScalaModule`, enhanced with +// Scoverage plugin. The root module extends the `ScoverageModule` and +// specifies the version of scoverage version to use here: `2.1.0`. This +// version can be changed if there is a newer one. Now you can call the +// scoverage targets to produce coverage reports. +// The sub test module extends `ScoverageTests` to transform the +// execution of the various testXXX targets to use scoverage and produce +// coverage data. +// This lets us perform the coverage operations but before that you +// must first run the test. +// `./mill test` then `./mill scoverage.consoleReport` and get your +// coverage into your console output. +// +// You can download this example project using the *download* link above +// if you want to try out the commands below yourself. The only requirement is +// that you have some version of the JVM installed; the `./mill` script takes +// care of any further dependencies that need to be downloaded. + +/** Usage + +> ./mill test # Run the tests and produce the coverage data +... ++ foo.FooTests.simple ...

hello

++ foo.FooTests.escaping ...

<hello>

+ +> ./mill resolve scoverage._ # List what tasks are available to run from scoverage +... +scoverage.consoleReport +... +scoverage.htmlReport +... +scoverage.xmlCoberturaReport +... +scoverage.xmlReport +... + +> ./mill scoverage.consoleReport +... +Statement coverage.: 16.67% +Branch coverage....: 100.00% +*/ diff --git a/example/misc/7-contrib-scoverage/src/Foo.scala b/example/misc/7-contrib-scoverage/src/Foo.scala new file mode 100644 index 00000000000..2de577a0280 --- /dev/null +++ b/example/misc/7-contrib-scoverage/src/Foo.scala @@ -0,0 +1,16 @@ +package foo +import scalatags.Text.all._ +import mainargs.{main, ParserForMethods} + +object Foo { + def generateHtml(text: String) = { + h1(text).toString + } + + @main + def main(text: String) = { + println(generateHtml(text)) + } + + def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args) +} diff --git a/example/misc/7-contrib-scoverage/test/src/FooTests.scala b/example/misc/7-contrib-scoverage/test/src/FooTests.scala new file mode 100644 index 00000000000..c893d7d3f66 --- /dev/null +++ b/example/misc/7-contrib-scoverage/test/src/FooTests.scala @@ -0,0 +1,16 @@ +package foo +import utest._ +object FooTests extends TestSuite { + def tests = Tests { + test("simple") { + val result = Foo.generateHtml("hello") + assert(result == "

hello

") + result + } + test("escaping") { + val result = Foo.generateHtml("") + assert(result == "

<hello>

") + result + } + } +}