Skip to content

provide a CLI for writing reports from generated results #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions project/Scoverage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ object Scoverage extends Build {
.settings(libraryDependencies ++= Seq(
"org.scala-lang" % "scala-reflect" % scalaVersion.value % "provided",
"org.scala-lang" % "scala-compiler" % scalaVersion.value % "provided",
"org.rogach" %% "scallop" % "0.9.5",
"org.joda" % "joda-convert" % "1.6" % "test",
"joda-time" % "joda-time" % "2.3" % "test",
"com.typesafe.scala-logging" %% "scala-logging-slf4j" % "2.1.2" % "test"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package scoverage.report

import java.io.File

import org.rogach.scallop.ArgType.V
import org.rogach.scallop.{singleArgConverter, ValueConverter, Scallop}
import scoverage.{IOUtils, Serializer, Constants, Coverage}

import language.implicitConversions

object ReportWriter {

private def dir(base: File, name: String) = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like you put all reports in separate directories 👍

val directory = new File(base, name)
directory.mkdirs()
directory
}

def writeReports(outputDirectory: File,
baseDirectory: File,
coverage: Coverage,
coverageOutputCobertura: Boolean,
coverageOutputXML: Boolean,
coverageOutputHTML: Boolean,
coverageDebug: Boolean): Unit = {

if (coverageOutputCobertura) {
new CoberturaXmlWriter(baseDirectory, dir(outputDirectory, "cobertura")).write(coverage)
}

if (coverageOutputXML) {
val xmlDir = dir(outputDirectory, "xml")
new ScoverageXmlWriter(baseDirectory, xmlDir, false).write(coverage)
if (coverageDebug) {
new ScoverageXmlWriter(baseDirectory, xmlDir, true).write(coverage)
}
}

if (coverageOutputHTML) {
new ScoverageHtmlWriter(baseDirectory, dir(outputDirectory, "html")).write(coverage)
}

}
}

object Reports extends App {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a help method generated by Scallop? If not, it will be nice to document all parameters that this object takes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems from their documentation that --help produces a usage summary. I'll add some meaningful messages to make that more helpful.


private val defaultsToTrue = () => Some(true)
private implicit val stringToFile = singleArgConverter(new File(_))

val BASE_DIR: String = "base-dir"
val OUTPUT_DIR: String = "output-dir"
val AGGREGATE_SUB_PROJECTS: String = "aggregate"
val CLEAN_AGGREGATED_FILES: String = "clean"
val SINGLE_DATA_DIR: String = "data-dir"
val OUTPUT_COBERTURA_XML: String = "cobertura"
val OUTPUT_SCOVERAGE_DEBUG_XML: String = "debug"
val OUTPUT_SCOVERAGE_XML: String = "xml"
val OUTPUT_SCOVERAGE_HTML: String = "html"

private val opts = Scallop(args)
.opt[File](BASE_DIR, required = true)
.opt[File](OUTPUT_DIR, required = true)
.opt[Boolean](AGGREGATE_SUB_PROJECTS, required = false, default = () => None)
.opt[File](SINGLE_DATA_DIR, required = false, validate = f => Serializer.coverageFile(f).exists())
.opt[Boolean](CLEAN_AGGREGATED_FILES, default = defaultsToTrue)
.opt[Boolean](OUTPUT_SCOVERAGE_DEBUG_XML, default = defaultsToTrue)
.opt[Boolean](OUTPUT_COBERTURA_XML, default = defaultsToTrue)
.opt[Boolean](OUTPUT_SCOVERAGE_XML, default = defaultsToTrue)
.opt[Boolean](OUTPUT_SCOVERAGE_HTML, default = defaultsToTrue)
.verify

private val baseDir = opts[File](BASE_DIR)

private val coverage: Option[Coverage] = {
// opts.get[_] appears not to work as expected here
(opts.isSupplied(SINGLE_DATA_DIR), opts.isSupplied(AGGREGATE_SUB_PROJECTS)) match {
case (true, false) =>
val dataDir = opts[File](SINGLE_DATA_DIR)
val coverageFile = Serializer.coverageFile(dataDir)
if (coverageFile.exists()) {
val coverage = Serializer.deserialize(coverageFile)
val measurementFiles = IOUtils.findMeasurementFiles(dataDir)
val measurements = IOUtils.invoked(measurementFiles)
coverage.apply(measurements)
Some(coverage)
} else {
None
}
case (false, true) =>
CoverageAggregator.aggregate(baseDir, opts[Boolean](CLEAN_AGGREGATED_FILES))
case (true, true)=>
Console.err.println(s"Cannot specify $SINGLE_DATA_DIR and $AGGREGATE_SUB_PROJECTS")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to have messages prefixed with plugin name, like [scoverage] Cannot specify (...), what do you think about that?

None
case (false, false) =>
Console.err.println(s"Must specify $SINGLE_DATA_DIR or $AGGREGATE_SUB_PROJECTS")
None
}
}

coverage match {
case Some(data) =>
ReportWriter.writeReports(
opts[File](OUTPUT_DIR),
baseDir,
data,
opts[Boolean](OUTPUT_COBERTURA_XML),
opts[Boolean](OUTPUT_SCOVERAGE_XML),
opts[Boolean](OUTPUT_SCOVERAGE_HTML),
opts[Boolean](OUTPUT_SCOVERAGE_DEBUG_XML))
println("Wrote reports")
case None =>
Console.err.println(s"No coverage found")
sys.exit(1)
}

}