Skip to content

Commit

Permalink
chore: jacoco integration fox
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhil-2019 committed May 1, 2020
1 parent 85f47b3 commit 74ade4b
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 120 deletions.

This file was deleted.

8 changes: 8 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ allprojects {
tasks.create<Delete>("clean") {
delete(rootProject.buildDir)
}

allprojects {
configurations.all {
resolutionStrategy {
force("org.objenesis:objenesis:2.6")
}
}
}
4 changes: 4 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ dependencies {
/* Depend on the android gradle plugin, since we want to access it in our plugin */
implementation("com.android.tools.build:gradle:4.1.0-alpha04")

implementation("org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.8.0.1969")

//implementation("org.jacoco:org.jacoco.core:0.8.5")

implementation("com.hiya:jacoco-android:0.2")

/* Depend on the default Gradle API's since we want to build a custom plugin */
Expand Down
84 changes: 84 additions & 0 deletions buildSrc/jacoco.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
apply plugin: 'jacoco'

jacoco {
toolVersion = "0.8.5"
}

tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
}

def mainSrc = "$project.projectDir/src/main/java"

def executionDataFiles = [
'jacoco/test.exec',
'jacoco/testDebugUnitTest.exec',
'outputs/code-coverage/connected/*coverage.ec'
]

subprojects { subproject ->
afterEvaluate {
subproject.apply plugin: 'jacoco'

// TODO WORKAROUND: subproject.plugins.withType is not correctly detecting Android projects
subproject.plugins.each { plugin ->
if (subproject.hasProperty('excludeFromCoverage') && subproject.excludeFromCoverage) {
logger.info "Excluding $subproject from coverage measurement"
return
}

if (isAndroidLibrary(plugin) || isAndroidApp(plugin)) {
android.jacoco.version = "0.8.5"

logger.info "Applying Android JaCoCo task for $subproject"
task jacocoTestReport(
type: JacocoReport,
dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']
) {
reports {
xml.enabled = true
html.enabled = true
}

sourceDirectories.setFrom files([mainSrc])

def javaClassDir = "$project.buildDir/intermediates/classes/debug"
def kotlinClassDir = "$project.buildDir/tmp/kotlin-classes/debug"
classDirectories.setFrom fileTree(javaClassDir) + fileTree(kotlinClassDir)

executionData.setFrom fileTree(dir: project.buildDir, includes: executionDataFiles)
}
} else if (isJvmLibrary(plugin)) {
logger.info "Applying Java JaCoCo task for $subproject"
jacocoTestReport {
dependsOn test
reports {
xml.enabled = true
html.enabled = true
}

sourceDirectories.setFrom files([mainSrc])

def javaClassDir = "$project.buildDir/classes/java/main"
def kotlinClassDir = "$project.buildDir/classes/kotlin/main"
classDirectories.setFrom fileTree(javaClassDir) + fileTree(kotlinClassDir)

executionData.setFrom fileTree(dir: project.buildDir, includes: executionDataFiles)
}
}
return
}
}
}

private static boolean isAndroidLibrary(plugin) {
plugin.toString().contains('com.android.build.gradle.LibraryPlugin')
}

private static boolean isAndroidApp(plugin) {
plugin.toString().contains('com.android.build.gradle.AppPlugin')
}

private static boolean isJvmLibrary(plugin) {
plugin instanceof JavaLibraryPlugin
}
63 changes: 46 additions & 17 deletions buildSrc/src/main/kotlin/AndroidModulePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import org.gradle.api.JavaVersion
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.testing.Test
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.withType
import org.gradle.testing.jacoco.plugins.JacocoPluginExtension
import org.gradle.testing.jacoco.plugins.JacocoTaskExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.sonarqube.gradle.SonarQubeExtension
import org.sonarqube.gradle.SonarQubeProperties

/***
* This plugin is instantiated every time when you apply the this plugin to build.gradle in feature module
Expand All @@ -21,28 +24,19 @@ class AndroidModulePlugin : Plugin<Project> {
with(project) {
plugins.apply("kotlin-android")
plugins.apply("kotlin-android-extensions")
plugins.apply("com.hiya.jacoco-android")
plugins.apply("kotlin-kapt")
//plugins.apply("com.hiya.jacoco-android")

configureSonarqube()
configureJacoco()
configureAndroidBlock()
if (name != "test_shared") {
configureCommonDependencies()
configureCoreModuleForOtherModules()
}
configureTestSharedDependencies()
configureCoreModuleForOtherModules()
configureTestSharedModuleForOtherModules()
}

project.extensions.getByType<JacocoPluginExtension>().run {
toolVersion = "0.8.4"
//configure other properties if needed
}

project.tasks.withType<Test>() {
extensions.getByType<JacocoTaskExtension>().run {
isIncludeNoLocationClasses = true
}
}
}
}
}
Expand Down Expand Up @@ -76,6 +70,12 @@ internal fun Project.configureAndroidBlock() = extensions.getByType<BaseExtensio
isReturnDefaultValues = true
}
}

buildTypes {
getByName("debug") {
isTestCoverageEnabled = true
}
}
}

internal fun Project.configureCommonDependencies() {
Expand Down Expand Up @@ -104,14 +104,10 @@ internal fun Project.configureCommonDependencies() {

internal fun Project.configureTestSharedDependencies() {
if (name != "test_shared") return
val core = findProject(":core")

extensions.getByType<BaseExtension>().run {
dependencies {

if (core != null) {
add("implementation", core)
}
add("implementation", Libs.AndroidX.Lifecycle.ext)
add("implementation", Libs.AndroidX.Lifecycle.viewModel)
add("implementation", Libs.AndroidX.Lifecycle.viewModelKtx)
Expand Down Expand Up @@ -155,4 +151,37 @@ internal fun Project.configureTestSharedModuleForOtherModules() {
}
}
}
}

internal fun Project.configureSonarqube() {
val plugin = rootProject.plugins.findPlugin("org.sonarqube")
if (plugin == null) {
println("Applying sonar qube")
rootProject.plugins.apply("org.sonarqube")
rootProject.extensions.getByType<SonarQubeExtension>().run {
properties {
property("sonar.projectKey", "nikhil-thakkar_eventbrite-clone")
property("sonar.organization", "nikhil-thakkar")
property("sonar.sources", "src/main/java")
property("sonar.sources.coveragePlugin", "jacoco")
property("sonar.host.url", "https://sonarcloud.io/")
property("sonar.exclusions", "**/*.js")
property("sonar.login", "")
}
}
}
}

internal fun Project.configureJacoco() {
apply("${rootDir}/buildSrc/jacoco.gradle")
apply("https://raw.githubusercontent.com/JakeWharton/SdkSearch/master/gradle/projectDependencyGraph.gradle")
extensions.getByType<SonarQubeExtension>().run {
properties {
property(
"sonar.coverage.jacoco.xmlReportPaths",
"${buildDir}/reports/jacoco/debug/jacoco.xml"
)
}

}
}
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
internal object Versions {
object Versions {

const val koin = "2.0.1"
const val kotlin = "1.3.41"
Expand Down

This file was deleted.

3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ kotlin.code.style=official

org.gradle.parallel = true

android.nonTransitiveRClass=true
android.nonTransitiveRClass=true
android.enableR8 = false
2 changes: 1 addition & 1 deletion home/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
plugins {
id("com.android.library")
id("dev.nikhi1.plugin.android")
}
}

This file was deleted.

1 change: 0 additions & 1 deletion onboarding/src/main/res/navigation/graph_onboarding.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/graph_onboarding"
app:moduleName="onboarding"
app:startDestination="@id/onboardingFragment">

<fragment
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions sonar-project.properties

This file was deleted.

0 comments on commit 74ade4b

Please sign in to comment.