Skip to content

Commit

Permalink
trip git commit message, add dry run and coveralls request dump option (
Browse files Browse the repository at this point in the history
#30)

* trim git messages

* add dryRun and coverallsRequest options

* update tests

* update readme

* update version
  • Loading branch information
nbaztec authored Jan 18, 2021
1 parent 5ff4abe commit ccd618b
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 18 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,20 @@ Additionally, the following coveralls parameters may be specified via environmen

coverallsJacoco {
reportPath = "" // default: "build/reports/jacoco/test/jacocoTestReport.xml"
reportSourceSets += sourceSets.foo.allJava.srcDirs + sourceSets.bar.allJava.srcDirs // optional, default: main
apiEndpoint = "" // optional, default: https://coveralls.io/api/v1/jobs

reportSourceSets += sourceSets.foo.allJava.srcDirs + sourceSets.bar.allJava.srcDirs // default: main
apiEndpoint = "" // default: https://coveralls.io/api/v1/jobs

dryRun = false // default: false
coverallsRequest = File("build/req.json") // default: null
}
```

* `reportPath: String` - location of the jacoco xml report.
* `reportSourceSets: Iterable<File>` - a list of directories where to find the source code in.
* `apiEndpoint: String` - coveralls api endpoint for posting jobs.
* `dryRun: Boolean` - executes the task without posting to coveralls. Useful for debugging.
* `coverallsRequest: File` - writes the coveralls request payload to a file. Useful for debugging.

## Excluding Files
Please refer to the official JaCoCo documentation to exclude files from the report. An example configuration is as follows:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

group = "com.github.nbaztec"
version = "1.2.5"
version = "1.2.6"

buildscript {
repositories {
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/CoverallsJacocoPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ open class CoverallsJacocoPluginExtension {
var reportPath = "build/reports/jacoco/test/jacocoTestReport.xml"
var apiEndpoint = "https://coveralls.io/api/v1/jobs"
var reportSourceSets: Iterable<File> = emptySet()
var dryRun: Boolean = false
var coverallsRequest: File? = null
}

class CoverallsJacocoPlugin : Plugin<Project> {
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/CoverallsReporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.apache.http.util.EntityUtils
import org.apache.log4j.LogManager
import org.apache.log4j.Logger
import org.gradle.api.Project
import java.nio.charset.Charset

data class Request(
val repo_token: String,
Expand Down Expand Up @@ -69,6 +70,13 @@ class CoverallsReporter(val envGetter: EnvGetter) {
source_files = sourceFiles,
)

pluginExtension.coverallsRequest?.writeText(req.json(), Charsets.UTF_8)

if (pluginExtension.dryRun) {
logger.info("skip sending to coveralls in dry run")
return
}

send(pluginExtension.apiEndpoint, req)
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/GitInfoParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ object GitInfoParser {
commit.authorIdent.emailAddress,
commit.committerIdent.name,
commit.committerIdent.emailAddress,
commit.fullMessage
commit.fullMessage.trim(),
)
}

Expand Down
2 changes: 2 additions & 0 deletions src/test/kotlin/CoverallsJacocoPluginTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ internal class CoverallsJacocoPluginTest {
assertEquals(extension.reportPath, "build/reports/jacoco/test/jacocoTestReport.xml")
assertEquals(extension.reportSourceSets, emptySet<SourceSet>())
assertEquals(extension.apiEndpoint, "https://coveralls.io/api/v1/jobs")
assertEquals(extension.dryRun, false)
assertEquals(extension.coverallsRequest, null)
}
}
89 changes: 76 additions & 13 deletions src/test/kotlin/CoverallsReporterTest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.gradle.plugin.coveralls.jacoco

import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import com.google.gson.Gson
import com.sun.net.httpserver.HttpServer
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import io.mockk.spyk
import io.mockk.verifyAll
import kotlinx.coroutines.GlobalScope
Expand All @@ -13,8 +16,13 @@ import kotlinx.coroutines.runBlocking
import org.gradle.api.Project
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.internal.impldep.com.google.common.io.Files
import org.junit.jupiter.api.*
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotEquals
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.assertThrows
import java.io.File
import java.io.PrintWriter
import java.net.InetSocketAddress
Expand Down Expand Up @@ -66,6 +74,8 @@ internal class CoverallsReporterTest {

val project = mockProject { _, pluginExtension ->
every { pluginExtension.apiEndpoint } returns "http://${mockServer.address.hostName}:${mockServer.address.port}"
every { pluginExtension.coverallsRequest } returns null
every { pluginExtension.dryRun } returns false
}

delay(50)
Expand All @@ -82,16 +92,63 @@ internal class CoverallsReporterTest {
Unit
}

@Test
fun `CoverallsReporter parses info and writes coveralls request to a file when option set`() = runBlocking {
val envGetter = createEnvGetter(
mapOf(
"GITHUB_TOKEN" to "test-token",
"GITHUB_ACTIONS" to "true"
)
)

val coverallsRequest = File.createTempFile("test", ".json").also { it.deleteOnExit() }
val project = mockProject { _, pluginExtension ->
every { pluginExtension.coverallsRequest } returns coverallsRequest
every { pluginExtension.dryRun } returns true
}

val reporter = spyk(CoverallsReporter(envGetter), recordPrivateCalls = true)
reporter.report(project)

assertEquals(546, coverallsRequest.length())
}

@Test
fun `CoverallsReporter parses info but skips sending to coveralls when dry run is true`() = runBlocking {
val envGetter = createEnvGetter(
mapOf(
"GITHUB_TOKEN" to "test-token",
"GITHUB_ACTIONS" to "true"
)
)

val coverallsRequest = File.createTempFile("test", ".json").also { it.deleteOnExit() }
val project = mockProject { _, pluginExtension ->
every { pluginExtension.apiEndpoint } returns "http://foobar"
every { pluginExtension.coverallsRequest } returns coverallsRequest
every { pluginExtension.dryRun } returns true
}

val reporter = spyk(CoverallsReporter(envGetter), recordPrivateCalls = true)
reporter.report(project)

verifyAll(inverse = true) {
reporter["send"](any<String>(), any<Request>())
}

assertNotEquals(0, coverallsRequest.length())
}

@Test
fun `CoverallsReporter parses info and sends report to coveralls server when GITHUB_TOKEN is set`() = runBlocking {
var actual = ""
val mockServer = HttpServer.create(InetSocketAddress("localhost", 0), 0).apply {
createContext("/") { http ->
actual = http.requestBody.reader().readText()
.trim()
.split("\r\n")
.drop(1).dropLast(1)
.joinToString("\n")
.trim()
.split("\r\n")
.drop(1).dropLast(1)
.joinToString("\n")

http.sendResponseHeaders(200, 0)
http.close()
Expand All @@ -101,12 +158,16 @@ internal class CoverallsReporterTest {
mockServer.start()
}

val envGetter = createEnvGetter(mapOf(
val envGetter = createEnvGetter(
mapOf(
"GITHUB_TOKEN" to "test-token",
"GITHUB_ACTIONS" to "true"
))
)
)
val project = mockProject { _, pluginExtension ->
every { pluginExtension.apiEndpoint } returns "http://${mockServer.address.hostName}:${mockServer.address.port}"
every { pluginExtension.coverallsRequest } returns null
every { pluginExtension.dryRun } returns false
}

delay(50)
Expand All @@ -118,7 +179,7 @@ internal class CoverallsReporterTest {
Content-Type: application/json; charset=UTF-8
Content-Transfer-Encoding: binary
{"repo_token":"test-token","service_name":"github","git":{"head":{"id":"4cd72eadcc34861139b338dd859344d419244e0b","author_name":"John Doe","author_email":"test@example.com","committer_name":"John Doe","committer_email":"test@example.com","message":"test commit\n"},"branch":"master","remotes":[{"name":"origin","url":"git@github.com:test/testrepo.git"}]},"source_files":[{"name":"javaStyleSrc/main/kotlin/foo/bar/baz/Main.kt","source_digest":"36083cd4c2ac736f9210fd3ed23504b5","coverage":[null,null,null,null,1,1,1,1,null,1,1,0,0,1,1,null,1,1,1]}]}
{"repo_token":"test-token","service_name":"github","git":{"head":{"id":"4cd72eadcc34861139b338dd859344d419244e0b","author_name":"John Doe","author_email":"test@example.com","committer_name":"John Doe","committer_email":"test@example.com","message":"test commit"},"branch":"master","remotes":[{"name":"origin","url":"git@github.com:test/testrepo.git"}]},"source_files":[{"name":"javaStyleSrc/main/kotlin/foo/bar/baz/Main.kt","source_digest":"36083cd4c2ac736f9210fd3ed23504b5","coverage":[null,null,null,null,1,1,1,1,null,1,1,0,0,1,1,null,1,1,1]}]}
""".trimIndent()

assertEquals(expected, actual)
Expand All @@ -130,10 +191,10 @@ Content-Transfer-Encoding: binary
val mockServer = HttpServer.create(InetSocketAddress("localhost", 0), 0).apply {
createContext("/") { http ->
actual = http.requestBody.reader().readText()
.trim()
.split("\r\n")
.drop(1).dropLast(1)
.joinToString("\n")
.trim()
.split("\r\n")
.drop(1).dropLast(1)
.joinToString("\n")

http.sendResponseHeaders(200, 0)
http.close()
Expand All @@ -146,6 +207,8 @@ Content-Transfer-Encoding: binary
val envGetter = createEnvGetter(mapOf("COVERALLS_REPO_TOKEN" to "test-token"))
val project = mockProject { _, pluginExtension ->
every { pluginExtension.apiEndpoint } returns "http://${mockServer.address.hostName}:${mockServer.address.port}"
every { pluginExtension.coverallsRequest } returns null
every { pluginExtension.dryRun } returns false
}

delay(50)
Expand All @@ -157,7 +220,7 @@ Content-Transfer-Encoding: binary
Content-Type: application/json; charset=UTF-8
Content-Transfer-Encoding: binary
{"repo_token":"test-token","service_name":"other","git":{"head":{"id":"4cd72eadcc34861139b338dd859344d419244e0b","author_name":"John Doe","author_email":"test@example.com","committer_name":"John Doe","committer_email":"test@example.com","message":"test commit\n"},"branch":"master","remotes":[{"name":"origin","url":"git@github.com:test/testrepo.git"}]},"source_files":[{"name":"javaStyleSrc/main/kotlin/foo/bar/baz/Main.kt","source_digest":"36083cd4c2ac736f9210fd3ed23504b5","coverage":[null,null,null,null,1,1,1,1,null,1,1,0,0,1,1,null,1,1,1]}]}
{"repo_token":"test-token","service_name":"other","git":{"head":{"id":"4cd72eadcc34861139b338dd859344d419244e0b","author_name":"John Doe","author_email":"test@example.com","committer_name":"John Doe","committer_email":"test@example.com","message":"test commit"},"branch":"master","remotes":[{"name":"origin","url":"git@github.com:test/testrepo.git"}]},"source_files":[{"name":"javaStyleSrc/main/kotlin/foo/bar/baz/Main.kt","source_digest":"36083cd4c2ac736f9210fd3ed23504b5","coverage":[null,null,null,null,1,1,1,1,null,1,1,0,0,1,1,null,1,1,1]}]}
""".trimIndent()

assertEquals(expected, actual)
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/GitInfoParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class GitInfoParserTest {
"4cd72eadcc34861139b338dd859344d419244e0b",
"John Doe", "test@example.com",
"John Doe", "test@example.com",
"test commit\n"
"test commit"
),
"master",
listOf(Remote("origin", "git@github.com:test/testrepo.git"))
Expand Down

0 comments on commit ccd618b

Please sign in to comment.