Skip to content

Commit

Permalink
Support COVERALLS_PARALLEL for parallel builds
Browse files Browse the repository at this point in the history
This sets the optional parallel field in the coverage API, allowing
people to upload from multiple jobs in parallel. There's an API
endpoint that needs to be posted once all jobs are finished.

Fixes: scoverage#125
  • Loading branch information
huonw committed Mar 19, 2019
1 parent 2d4a59b commit 747403d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ To specify encoding, add the following to your `build.sbt`
scalacOptions += Seq("-encoding", "UTF-8")
```

## Uploading coverage from multiple jobs in parallel

Coveralls supports multiple CI jobs recording and uploading coverage, if a special flag is set and [a webhook](https://docs.coveralls.io/parallel-build-webhook) is posted once all jobs are complete.

### Put the flag directly in your `build.sbt`

``` scala
import org.scoverage.coveralls.Imports.CoverallsKeys._

coverallsParallel := true
```

### Add an environment variable

Add an environment variable `COVERALLS_PARALLEL`, for example:

export COVERALLS_PARALLEL=true

## Using Travis-Pro

It is important to set the correct `service_name` when using Travis-Pro. The default is to use `travis-ci`. To override this value, add the following to your `build.sbt`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class CoverallPayloadWriter(
repoToken: Option[String],
travisJobId: Option[String],
serviceName: Option[String],
parallel: Boolean,
gitClient: GitClient) {

val repoRootDirStr = repoRootDir.getCanonicalPath.replace(File.separator, "/") + "/"
Expand All @@ -35,6 +36,7 @@ class CoverallPayloadWriter(
writeOpt("service_name", serviceName)
writeOpt("service_job_id", travisJobId)
writeOpt("service_pull_request", sys.env.get("CI_PULL_REQUEST"))
gen.writeBooleanField("parallel", parallel)

addGitInfo

Expand Down
5 changes: 4 additions & 1 deletion src/main/scala/org/scoverage/coveralls/CoverallsPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ object Imports {
val coverallsEncoding = SettingKey[String]("encoding")
val coverallsEndpoint = SettingKey[Option[String]]("coverallsEndpoint")
val coverallsGitRepoLocation = SettingKey[Option[String]]("coveralls-git-repo")
val coverallsParallel = SettingKey[Boolean]("coverallsParallel")
}
}

Expand All @@ -46,7 +47,8 @@ object CoverallsPlugin extends AutoPlugin {
coverallsServiceName := travisJobIdent map { _ => "travis-ci" },
coverallsFile := crossTarget.value / "coveralls.json",
coberturaFile := crossTarget.value / "coverage-report" / "cobertura.xml",
coverallsGitRepoLocation := Some(".")
coverallsGitRepoLocation := Some("."),
coverallsParallel := sys.env.get("COVERALLS_PARALLEL") == Some("true")
)

val aggregateFilter = ScopeFilter(inAggregates(ThisProject), inConfigurations(Compile)) // must be outside of the 'coverageAggregate' task (see: https://github.com/sbt/sbt/issues/1095 or https://github.com/sbt/sbt/issues/780)
Expand Down Expand Up @@ -83,6 +85,7 @@ object CoverallsPlugin extends AutoPlugin {
repoToken,
travisJobIdent,
coverallsServiceName.value,
coverallsParallel.value,
new GitClient(repoRootDirectory)(log)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class CoverallPayloadWriterTest extends WordSpec with BeforeAndAfterAll with Mat
}
}

def coverallsWriter(writer: Writer, tokenIn: Option[String], travisJobIdIn: Option[String], serviceName: Option[String]) =
new CoverallPayloadWriter(new File("").getAbsoluteFile, new File(""), tokenIn, travisJobIdIn, serviceName, testGitClient) {
def coverallsWriter(writer: Writer, tokenIn: Option[String], travisJobIdIn: Option[String], serviceName: Option[String], parallel: Boolean) =
new CoverallPayloadWriter(new File("").getAbsoluteFile, new File(""), tokenIn, travisJobIdIn, serviceName, parallel, testGitClient) {
override def generator(file: File) = {
val factory = new JsonFactory()
factory.createGenerator(writer)
Expand All @@ -36,35 +36,35 @@ class CoverallPayloadWriterTest extends WordSpec with BeforeAndAfterAll with Mat

"generate a correct starting payload with travis job id" in {
val w = new StringWriter()
val coverallsW = coverallsWriter(w, Some("testRepoToken"), Some("testTravisJob"), Some("travis-ci"))
val coverallsW = coverallsWriter(w, Some("testRepoToken"), Some("testTravisJob"), Some("travis-ci"), false)

coverallsW.start
coverallsW.flush()

w.toString should equal(
"""{"repo_token":"testRepoToken","service_name":"travis-ci","service_job_id":"testTravisJob",""" +
"""{"repo_token":"testRepoToken","service_name":"travis-ci","service_job_id":"testTravisJob","parallel":false,""" +
expectedGit +
""","source_files":["""
)
}

"generate a correct starting payload without travis job id" in {
val w = new StringWriter()
val coverallsW = coverallsWriter(w, Some("testRepoToken"), None, None)
val coverallsW = coverallsWriter(w, Some("testRepoToken"), None, None, false)

coverallsW.start
coverallsW.flush()

w.toString should equal(
"""{"repo_token":"testRepoToken",""" +
"""{"repo_token":"testRepoToken","parallel":false,""" +
expectedGit +
""","source_files":["""
)
}

"add source files correctly" in {
val w = new StringWriter()
val coverallsW = coverallsWriter(w, Some("testRepoToken"), None, Some("travis-ci"))
val coverallsW = coverallsWriter(w, Some("testRepoToken"), None, Some("travis-ci"), false)

val projectRoot = new File("").getAbsolutePath.replace(File.separator, "/") + "/"

Expand All @@ -80,13 +80,27 @@ class CoverallPayloadWriterTest extends WordSpec with BeforeAndAfterAll with Mat

"end the file correctly" in {
val w = new StringWriter()
val coverallsW = coverallsWriter(w, Some("testRepoToken"), None, Some("travis-ci"))
val coverallsW = coverallsWriter(w, Some("testRepoToken"), None, Some("travis-ci"), false)

coverallsW.start
coverallsW.end()

w.toString should endWith("]}")
}

"include parallel correctly" in {
val w = new StringWriter()
val coverallsW = coverallsWriter(w, Some("testRepoToken"), None, None, true)

coverallsW.start
coverallsW.flush()

w.toString should equal(
"""{"repo_token":"testRepoToken","parallel":true,""" +
expectedGit +
""","source_files":["""
)
}
}
}
}

0 comments on commit 747403d

Please sign in to comment.