-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
753d965
commit a752950
Showing
11 changed files
with
235 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,23 @@ | ||
name := "sbt-jacoco" | ||
organization := "com.github.sbt" | ||
|
||
version in ThisBuild := "3.0.3" | ||
version in ThisBuild := "3.1.0-M1" | ||
|
||
sbtPlugin := true | ||
crossSbtVersions := Seq("0.13.16", "1.0.2") | ||
|
||
val jacocoVersion = "0.7.9" | ||
val circeVersion = "0.8.0" | ||
|
||
libraryDependencies ++= Seq( | ||
"org.jacoco" % "org.jacoco.core" % jacocoVersion, | ||
"org.jacoco" % "org.jacoco.report" % jacocoVersion, | ||
"com.jsuereth" %% "scala-arm" % "2.0", | ||
"org.scalatest" %% "scalatest" % "3.0.4" % Test, | ||
"org.mockito" % "mockito-all" % "1.10.19" % Test | ||
"org.jacoco" % "org.jacoco.core" % jacocoVersion, | ||
"org.jacoco" % "org.jacoco.report" % jacocoVersion, | ||
"com.jsuereth" %% "scala-arm" % "2.0", | ||
"com.fasterxml.jackson.core" % "jackson-core" % "2.9.2", | ||
"org.scalaj" %% "scalaj-http" % "2.3.0", | ||
"commons-codec" % "commons-codec" % "1.11", | ||
"org.scalatest" %% "scalatest" % "3.0.4" % Test, | ||
"org.mockito" % "mockito-all" % "1.10.19" % Test | ||
) | ||
|
||
scalacOptions ++= Seq( | ||
|
@@ -51,3 +55,7 @@ headerLicense := Some(HeaderLicense.Custom( | |
enablePlugins(ParadoxSitePlugin, GhpagesPlugin) | ||
paradoxNavigationDepth in Paradox := 3 | ||
git.remoteRepo := "[email protected]:sbt/sbt-jacoco.git" | ||
|
||
addCompilerPlugin( | ||
"org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/scala/com/github/sbt/jacoco/coveralls/CoverallsClient.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.github.sbt.jacoco.coveralls | ||
|
||
import java.io.{File, FileInputStream} | ||
|
||
import sbt.Keys.TaskStreams | ||
|
||
import scalaj.http.{Http, MultiPart} | ||
|
||
object CoverallsClient { | ||
private val jobsUrl = "https://coveralls.io/api/v1/jobs" | ||
|
||
def sendReport(reportFile: File, streams: TaskStreams): Unit = { | ||
val response = Http(jobsUrl) | ||
.postMulti( | ||
MultiPart( | ||
"json_file", | ||
"json_file.json", | ||
"application/json", | ||
new FileInputStream(reportFile), | ||
reportFile.length(), | ||
_ => ()) | ||
).asString | ||
|
||
if (response.isSuccess) { | ||
streams.log.info("Upload complete") | ||
} else { | ||
streams.log.error(s"Unexpected response from coveralls: ${response.code}") | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/scala/com/github/sbt/jacoco/coveralls/CoverallsReportFormat.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.github.sbt.jacoco.coveralls | ||
|
||
import java.io.File | ||
|
||
import com.github.sbt.jacoco.report.formats.JacocoReportFormat | ||
import org.jacoco.report.IReportVisitor | ||
import sbt._ | ||
|
||
class CoverallsReportFormat(sourceDirs: Seq[File], projectRootDir: File, jobId: String, repoToken: Option[String]) | ||
extends JacocoReportFormat { | ||
|
||
override def createVisitor(directory: File, encoding: String): IReportVisitor = { | ||
IO.createDirectory(directory) | ||
|
||
new CoverallsReportVisitor(directory / "coveralls.json", sourceDirs, projectRootDir, jobId, repoToken) | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/main/scala/com/github/sbt/jacoco/coveralls/CoverallsReportVisitor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.github.sbt.jacoco.coveralls | ||
|
||
import java.io.File | ||
import java.{util => ju} | ||
|
||
import com.fasterxml.jackson.core.{JsonEncoding, JsonFactory} | ||
import org.apache.commons.codec.digest.DigestUtils | ||
import org.jacoco.core.analysis.{IBundleCoverage, ILine, IPackageCoverage, ISourceFileCoverage} | ||
import org.jacoco.core.data.{ExecutionData, SessionInfo} | ||
import org.jacoco.report.{IReportGroupVisitor, IReportVisitor, ISourceFileLocator} | ||
import sbt._ | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
class CoverallsReportVisitor( | ||
output: File, | ||
sourceDirs: Seq[File], | ||
projectRootDir: File, | ||
jobId: String, | ||
repoToken: Option[String]) | ||
extends IReportVisitor | ||
with IReportGroupVisitor { | ||
|
||
private val digest = new DigestUtils("MD5") | ||
|
||
private val jsonFactory = new JsonFactory() | ||
private val json = jsonFactory.createGenerator(output, JsonEncoding.UTF8) | ||
|
||
json.writeStartObject() | ||
|
||
repoToken foreach { token => | ||
json.writeStringField("repo_token", token) | ||
} | ||
|
||
json.writeStringField("service_job_id", jobId) | ||
json.writeStringField("service_name", "travis-ci") | ||
|
||
json.writeArrayFieldStart("source_files") | ||
|
||
override def visitInfo(sessionInfos: ju.List[SessionInfo], executionData: ju.Collection[ExecutionData]): Unit = {} | ||
|
||
override def visitGroup(name: String): IReportGroupVisitor = this | ||
|
||
override def visitBundle(bundle: IBundleCoverage, locator: ISourceFileLocator): Unit = { | ||
bundle.getPackages.asScala foreach { pkg: IPackageCoverage => | ||
pkg.getSourceFiles.asScala foreach { source: ISourceFileCoverage => | ||
json.writeStartObject() | ||
|
||
//noinspection ScalaStyle | ||
val (filename, md5) = findFile(pkg.getName, source.getName) match { | ||
case Some(file) => | ||
(IO.relativize(projectRootDir, file).getOrElse(file.getName), digest.digestAsHex(file)) | ||
|
||
case None => | ||
(source.getName, "") | ||
} | ||
|
||
json.writeStringField("name", filename) | ||
json.writeStringField("source_digest", md5) | ||
|
||
json.writeArrayFieldStart("coverage") | ||
|
||
(0 to source.getLastLine) foreach { l => | ||
val line: ILine = source.getLine(l) | ||
|
||
if (line.getInstructionCounter.getTotalCount == 0) { | ||
// non-code line | ||
json.writeNull() | ||
} else { | ||
json.writeNumber(line.getInstructionCounter.getCoveredCount) | ||
} | ||
} | ||
|
||
json.writeEndArray() | ||
|
||
json.writeEndObject() | ||
} | ||
} | ||
} | ||
|
||
override def visitEnd(): Unit = { | ||
json.writeEndArray() | ||
json.writeEndObject() | ||
json.close() | ||
} | ||
|
||
private def findFile(packageName: String, fileName: String): Option[File] = { | ||
// TODO make common with source file locator | ||
sourceDirs.map(d => d / packageName / fileName).find(_.exists()) | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/scala/com/github/sbt/jacoco/coveralls/JacocoCoverallsPlugin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.github.sbt.jacoco.coveralls | ||
|
||
import java.io.File | ||
|
||
import com.github.sbt.jacoco.{JacocoPlugin, _} | ||
import com.github.sbt.jacoco.report.ReportUtils | ||
import sbt.Keys._ | ||
import sbt._ | ||
|
||
object JacocoCoverallsPlugin extends BaseJacocoPlugin { | ||
override def requires: Plugins = JacocoPlugin | ||
override def trigger: PluginTrigger = noTrigger | ||
|
||
override protected def srcConfig = Test | ||
|
||
object autoImport { | ||
val jacocoCoveralls: TaskKey[Unit] = taskKey("Upload JaCoCo reports to Coveralls") | ||
|
||
val jacocoCoverallsJobId: SettingKey[String] = settingKey("todo") | ||
val jacocoCoverallsGenerateReport: TaskKey[Unit] = taskKey("TODO") | ||
val jacocoCoverallsOutput: SettingKey[File] = settingKey("File to store Coveralls coverage") | ||
|
||
val jacocoCoverallsRepoToken: SettingKey[Option[String]] = settingKey("todo") | ||
} | ||
|
||
import autoImport._ // scalastyle:ignore import.grouping | ||
|
||
override def projectSettings: Seq[Setting[_]] = Seq( | ||
jacocoCoverallsOutput := jacocoReportDirectory.value, | ||
jacocoCoveralls := Def.task { | ||
CoverallsClient.sendReport(jacocoCoverallsOutput.value / "coveralls.json", streams.value) | ||
}.value, | ||
jacocoCoverallsGenerateReport := Def.task { | ||
val coverallsFormat = | ||
new CoverallsReportFormat( | ||
coveredSources.value, | ||
baseDirectory.value, | ||
jacocoCoverallsJobId.value, | ||
jacocoCoverallsRepoToken.value) | ||
|
||
ReportUtils.generateReport( | ||
jacocoCoverallsOutput.value, | ||
jacocoDataFile.value, | ||
jacocoReportSettings.value.withFormats(coverallsFormat), | ||
coveredSources.value, | ||
classesToCover.value, | ||
jacocoSourceSettings.value, | ||
streams.value, | ||
checkCoverage = false | ||
) | ||
}.value, | ||
jacocoCoveralls := (jacocoCoveralls dependsOn jacocoCoverallsGenerateReport).value, | ||
// TODO fail if no job id | ||
// TODO manual job id | ||
jacocoCoverallsJobId := sys.env.getOrElse("TRAVIS_JOB_ID", "unknown"), | ||
jacocoCoverallsRepoToken := None | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.github.sbt | ||
|
||
import com.github.sbt.jacoco.data.ProjectData | ||
import sbt.ResolvedProject | ||
|
||
package object jacoco { | ||
private[jacoco] def projectData(project: ResolvedProject): ProjectData = { | ||
ProjectData(project.id) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters