Skip to content

Commit

Permalink
Merge pull request #17 from nbaztec/add-android-support
Browse files Browse the repository at this point in the history
add android support
  • Loading branch information
nbaztec authored Sep 6, 2020
2 parents 190d30c + 1a4e9e5 commit 57ebb1b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Set the value of `COVERALLS_REPO_TOKEN` from the project page on coveralls.io

coverallsJacoco {
reportPath = "" // default: "build/reports/jacoco/test/jacocoTestReport.xml"
reportSourceSets = [ sourceSets.foo, sourceSets.bar ] // optional, default: main
reportSourceSets += sourceSets.foo.allJava.srcDirs + sourceSets.bar.allJava.srcDirs // optional, default: main
apiEndpoint = "" // optional, default: https://coveralls.io/api/v1/jobs
}
```
Expand Down
9 changes: 8 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

group = "com.github.nbaztec"
version = "1.1.2"
version = "1.2.0"

tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()
}

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

repositories {
google()
mavenCentral()
jcenter()
}
Expand All @@ -19,6 +25,7 @@ dependencies {
implementation("org.eclipse.jgit", "org.eclipse.jgit", "5.8.1.202007141445-r")
implementation("org.apache.httpcomponents", "httpmime", "4.5.12")
implementation("com.google.code.gson", "gson", "2.8.5")
implementation("com.android.tools.build", "gradle", "4.0.1")
testImplementation("junit", "junit", "4.13")
testImplementation("org.junit.jupiter", "junit-jupiter-api", "5.6.2")
testRuntimeOnly("org.junit.jupiter", "junit-jupiter-engine", "5.6.2")
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/CoverallsJacocoPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package org.gradle.plugin.coveralls.jacoco
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.SourceSet
import java.io.File

open class CoverallsJacocoPluginExtension {
@Deprecated("the plugin now auto detects the root package")
var rootPackage: String? = null
var reportPath = "build/reports/jacoco/test/jacocoTestReport.xml"
var apiEndpoint = "https://coveralls.io/api/v1/jobs"
var reportSourceSets: Iterable<SourceSet> = emptySet()
var reportSourceSets: Iterable<File> = emptySet()
}

class CoverallsJacocoPlugin : Plugin<Project> {
Expand Down
15 changes: 13 additions & 2 deletions src/main/kotlin/SourceReportParser.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.gradle.plugin.coveralls.jacoco

import com.android.build.gradle.api.AndroidBasePlugin
import com.android.build.gradle.api.AndroidSourceDirectorySet
import com.android.build.gradle.api.AndroidSourceSet
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import java.io.File
import java.math.BigInteger
import java.security.MessageDigest
Expand All @@ -8,6 +12,8 @@ import org.apache.log4j.Logger
import org.dom4j.io.SAXReader
import org.gradle.api.Project
import org.gradle.api.tasks.SourceSetContainer
import org.jetbrains.kotlin.gradle.internal.AndroidExtensionsExtension


data class SourceReport(val name: String, val source_digest: String, val coverage: List<Int?>)

Expand Down Expand Up @@ -58,9 +64,14 @@ object SourceReportParser {
val pluginExtension = project.extensions.getByType(CoverallsJacocoPluginExtension::class.java)

val sourceDirs = if (pluginExtension.reportSourceSets.count() == 0) {
project.extensions.getByType(SourceSetContainer::class.java).getByName("main").allJava.srcDirs.filterNotNull()
val androidExtension = project.extensions.findByType(BaseAppModuleExtension::class.java)
androidExtension?.let {
androidExtension.sourceSets.getByName("main").java.srcDirs.filterNotNull()
} ?: project.extensions.getByType(SourceSetContainer::class.java)
.getByName("main").allJava.srcDirs.filterNotNull()

} else {
pluginExtension.reportSourceSets.flatMap { it.allJava.srcDirs }.filterNotNull()
pluginExtension.reportSourceSets.toList()
}

if (sourceDirs.isEmpty()) {
Expand Down
13 changes: 0 additions & 13 deletions src/test/kotlin/CoverallsJacocoPluginTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@ import kotlin.reflect.full.memberProperties
import kotlin.reflect.jvm.isAccessible

internal class CoverallsJacocoPluginTest {
@Test
fun `CoverallsJacocoPluginExtension deprecates rootPackage`() {
val ext = CoverallsJacocoPluginExtension()
ext.rootPackage = "test"
val actual = ext::class.memberProperties.find { it.name == "rootPackage" }!!.let {
it.isAccessible = true
it.annotations.first().annotationClass
}

assertEquals(Deprecated::class, actual)
assertEquals("test", ext.rootPackage)
}

@Test
fun `CoverallsJacocoPlugin creates extension and task with correct name`() {
val project = mockk<Project>(relaxed = true)
Expand Down
3 changes: 3 additions & 0 deletions src/test/kotlin/CoverallsReporterTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.gradle.plugin.coveralls.jacoco

import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import com.sun.net.httpserver.HttpServer
import io.mockk.every
import io.mockk.mockk
Expand All @@ -13,6 +14,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.gradle.api.Project
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.internal.impldep.com.google.common.io.Files
import org.junit.jupiter.api.AfterAll
Expand Down Expand Up @@ -196,6 +198,7 @@ Content-Transfer-Encoding: binary

val project = mockk<Project> {
every { projectDir } returns testRepo
every { extensions.findByType(BaseAppModuleExtension::class.java) } returns null
every { extensions.getByType(SourceSetContainer::class.java) } returns sourceSetContainer
every { extensions.getByType(CoverallsJacocoPluginExtension::class.java) } returns pluginExtension
}
Expand Down
36 changes: 34 additions & 2 deletions src/test/kotlin/SourceReportParserTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.gradle.plugin.coveralls.jacoco

import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import io.mockk.every
import io.mockk.mockk
import java.io.File
Expand All @@ -18,6 +19,7 @@ internal class SourceReportParserTest {
fun `SourceReportParser parses skips parsing if source directories empty`() {
val project = mockk<Project> {
every { projectDir } returns File("src/test/resources/testrepo")
every { extensions.findByType(BaseAppModuleExtension::class.java) } returns null
every { extensions.getByType(SourceSetContainer::class.java) } returns mockk {
every { getByName("main").allJava.srcDirs } returns emptySet()
}
Expand All @@ -35,6 +37,7 @@ internal class SourceReportParserTest {
fun `SourceReportParser parses simple jacoco report with java styled package`() {
val project = mockk<Project> {
every { projectDir } returns File("src/test/resources/testrepo")
every { extensions.findByType(BaseAppModuleExtension::class.java) } returns null
every { extensions.getByType(SourceSetContainer::class.java) } returns mockk {
every { getByName("main").allJava.srcDirs } returns setOf(testJavaStyleSourceDir)
}
Expand All @@ -54,13 +57,42 @@ internal class SourceReportParserTest {
assertEquals(expected, actual)
}

@Test
fun `SourceReportParser parses simple android jacoco report with kotlin styled root package`() {
val project = mockk<Project> {
every { projectDir } returns File("src/test/resources/testrepo")
every { extensions.findByType(BaseAppModuleExtension::class.java) } returns mockk {
every { sourceSets.getByName("main").java.srcDirs } returns setOf(testKotlinStyleSourceDir)
}
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)
)
)
assertEquals(expected, actual)
}

@Test
fun `SourceReportParser parses simple jacoco report with kotlin styled root package`() {
val project = mockk<Project> {
every { projectDir } returns File("src/test/resources/testrepo")
every { extensions.getByType(SourceSetContainer::class.java) } returns mockk {
every { getByName("main").allJava.srcDirs } returns setOf(testKotlinStyleSourceDir)
}
every { extensions.findByType(BaseAppModuleExtension::class.java) } returns null
every { extensions.getByType(CoverallsJacocoPluginExtension::class.java) } returns mockk {
every { reportPath } returns testReport.path
every { reportSourceSets } returns emptySet()
Expand Down Expand Up @@ -89,8 +121,8 @@ internal class SourceReportParserTest {
every { extensions.getByType(CoverallsJacocoPluginExtension::class.java) } returns mockk {
every { reportPath } returns testReport.path
every { reportSourceSets } returns listOf(
mockk { every { allJava.srcDirs } returns setOf(testKotlinStyleSourceDir) },
mockk { every { allJava.srcDirs } returns setOf(testKotlinStyleSourceDirAdditional) }
testKotlinStyleSourceDir,
testKotlinStyleSourceDirAdditional
)
}
}
Expand Down

0 comments on commit 57ebb1b

Please sign in to comment.