Skip to content

Commit

Permalink
use jacocoAggregation plugin (#58)
Browse files Browse the repository at this point in the history
* remove dependabot as it does not support Versions.kt
  • Loading branch information
nbaztec authored Oct 4, 2022
1 parent 54b06aa commit eb7ddc6
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 14 deletions.
6 changes: 0 additions & 6 deletions .github/dependabot.yml

This file was deleted.

13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ jacocoTestReport {
```

## Multi-Project Support - Pure Kotlin/Java
It is recommended to use the [JaCoCo Aggregation plugin](https://docs.gradle.org/current/userguide/jacoco_report_aggregation_plugin.html) to consolidate
the test reports. Please refer to the [sample](https://docs.gradle.org/current/samples/sample_jvm_multi_project_with_code_coverage_standalone.html) on the website, on how to configure the plugin,
and specify the output file in the `coverallsJacoco` config.

```kotlin
coverallsJacoco {
reportPath = "code-coverage-report/build/reports/jacoco/testCodeCoverageReport/testCodeCoverageReport.xml"
}
```

## (Not Recommended) Multi-Project Support - Pure Kotlin/Java
To consolidate multiple JaCoCo coverage reports, the following code can be used to add a new task `codeCoverageReport`
```kotlin
tasks.register<JacocoReport>("codeCoverageReport") {
Expand Down Expand Up @@ -116,7 +127,7 @@ tasks.register<JacocoReport>("codeCoverageReport") {
}
```

## Multi-Project Support - Android
## (Not Recommended) Multi-Project Support - Android

To consolidate multiple JaCoCo coverage reports on Android multi-project configurations, the following code can be used to add a new task `jacocoFullReport`

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.14"
version = "1.2.15"

buildscript {
repositories {
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/CoverallsReporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,16 @@ class CoverallsReporter(val envGetter: EnvGetter) {
source_files = sourceFiles,
)

pluginExtension.coverallsRequest?.writeText(req.json(), Charsets.UTF_8)
val reqJson = req.json()
pluginExtension.coverallsRequest?.writeText(reqJson, Charsets.UTF_8)
logger.debug("request-json: $reqJson")

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

send(pluginExtension.apiEndpoint, req.json())
send(pluginExtension.apiEndpoint, reqJson)
}

fun send(endpoint: String, reqJson: String) {
Expand All @@ -102,7 +104,6 @@ class CoverallsReporter(val envGetter: EnvGetter) {
}

logger.info("sending payload to coveralls")
logger.debug(reqJson)
val res = httpClient.execute(httpPost)
if (res.statusLine.statusCode != 200) {
throw Exception(
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/SourceReportParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ object SourceReportParser {
fun parse(project: Project): List<SourceReport> {
val pluginExtension = project.extensions.getByType(CoverallsJacocoPluginExtension::class.java)

// if sources are not specified, then lookup android source sets, or if jacocoAggregate plugin is present,
// else fallback to main source set
val sourceDirs = if (pluginExtension.reportSourceSets.count() == 0) {
if (hasAndroidExtension) {
project.extensions.findByType(BaseAppModuleExtension::class.java)!!.sourceSets
.getByName("main").java.srcDirs.filterNotNull()
} else {
project.extensions.getByType(SourceSetContainer::class.java)
} else project.configurations.findByName("allCodeCoverageReportSourceDirectories")?.files
?: project.extensions.getByType(SourceSetContainer::class.java)
.getByName("main").allJava.srcDirs.filterNotNull()
}
} else {
pluginExtension.reportSourceSets.toList()
}
Expand Down
1 change: 1 addition & 0 deletions src/test/kotlin/CoverallsReporterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ Content-Transfer-Encoding: binary

val project = mockk<Project> {
every { rootDir } returns testRepo
every { configurations.findByName(any()) } returns null
every { extensions.getByType(SourceSetContainer::class.java) } returns sourceSetContainer
every { extensions.getByType(CoverallsJacocoPluginExtension::class.java) } returns pluginExtension
}
Expand Down
42 changes: 42 additions & 0 deletions src/test/kotlin/SourceReportParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal class SourceReportParserTest {
fun `SourceReportParser parses skips parsing if source directories empty`() {
val project = mockk<Project> {
every { rootDir } returns File("src/test/resources/testrepo")
every { configurations.findByName(any()) } returns null
every { extensions.getByType(SourceSetContainer::class.java) } returns mockk {
every { getByName("main").allJava.srcDirs } returns emptySet()
}
Expand All @@ -36,6 +37,7 @@ internal class SourceReportParserTest {
fun `SourceReportParser parses simple jacoco report with java styled package`() {
val project = mockk<Project> {
every { rootDir } returns File("src/test/resources/testrepo")
every { configurations.findByName(any()) } returns null
every { extensions.getByType(SourceSetContainer::class.java) } returns mockk {
every { getByName("main").allJava.srcDirs } returns setOf(testJavaStyleSourceDir)
}
Expand All @@ -60,6 +62,7 @@ internal class SourceReportParserTest {
fun `SourceReportParser parses simple android jacoco report with kotlin styled root package`() {
val project = mockk<Project> {
every { rootDir } returns File("src/test/resources/testrepo")
every { configurations.findByName(any()) } returns null
every { extensions.getByType(SourceSetContainer::class.java) } returns mockk {
every { getByName("main").allJava.srcDirs } returns setOf(testKotlinStyleSourceDir)
}
Expand Down Expand Up @@ -89,6 +92,7 @@ internal class SourceReportParserTest {
fun `SourceReportParser parses simple jacoco report without android classes`() {
val project = mockk<Project> {
every { rootDir } returns File("src/test/resources/testrepo")
every { configurations.findByName(any()) } returns null
every { extensions.getByType(SourceSetContainer::class.java) } returns mockk {
every { getByName("main").allJava.srcDirs } returns setOf(testKotlinStyleSourceDir)
}
Expand Down Expand Up @@ -118,6 +122,7 @@ internal class SourceReportParserTest {
fun `SourceReportParser parses simple jacoco report with kotlin styled root package`() {
val project = mockk<Project> {
every { rootDir } returns File("src/test/resources/testrepo")
every { configurations.findByName(any()) } returns null
every { extensions.getByType(SourceSetContainer::class.java) } returns mockk {
every { getByName("main").allJava.srcDirs } returns setOf(testKotlinStyleSourceDir)
}
Expand Down Expand Up @@ -147,6 +152,7 @@ internal class SourceReportParserTest {
fun `SourceReportParser uses report source sets and parses jacoco report`() {
val project = mockk<Project> {
every { rootDir } returns File("src/test/resources/testrepo")
every { configurations.findByName(any()) } returns null
every { extensions.getByType(CoverallsJacocoPluginExtension::class.java) } returns mockk {
every { reportPath } returns testReport.path
every { reportSourceSets } returns listOf(
Expand Down Expand Up @@ -177,10 +183,46 @@ internal class SourceReportParserTest {
assertEquals(expected, actual)
}

@Test
fun `SourceReportParser uses jacocoAggregation provided source sets and parses jacoco report`() {
val project = mockk<Project> {
every { rootDir } returns File("src/test/resources/testrepo")
every { configurations.findByName("allCodeCoverageReportSourceDirectories") } returns mockk {
every { files } returns setOf( testKotlinStyleSourceDir,
testKotlinStyleSourceDirAdditional)
}
every { extensions.getByType(CoverallsJacocoPluginExtension::class.java) } returns mockk {
every { reportPath } returns testReport.path
every { reportSourceSets } returns emptySet()
}
}

val actual = SourceReportParser.parse(project)
val expected = listOf(
SourceReport(
"src/main/kotlin/Main.kt",
"36083cd4c2ac736f9210fd3ed23504b5",
listOf(null, null, null, null, 1, 1, 1, 1, null, 1, 1, 0, 0, 1, 1, null, 1, 1, 1)
),
SourceReport(
"src/main/kotlin/internal/Util.kt",
"805ee340f4d661be591b4eb42f6164d2",
listOf(null, null, null, null, 1, 1, 1, null, null)
),
SourceReport(
"src/anotherMain/kotlin/Lib.kt",
"8b5c1c773cf81996efc19a08f0ac3648",
listOf(null, null, null, null, 1, 1, 1, null, null, null, null, null, null)
)
)
assertEquals(expected, actual)
}

@Test
fun `SourceReportParser ignores lines in report that are missing in source`() {
val project = mockk<Project> {
every { rootDir } returns File("src/test/resources/testrepo")
every { configurations.findByName(any()) } returns null
every { extensions.getByType(CoverallsJacocoPluginExtension::class.java) } returns mockk {
every { reportPath } returns testReportMissingLines.path
every { reportSourceSets } returns listOf(
Expand Down

0 comments on commit eb7ddc6

Please sign in to comment.