From 74ade4b03a8460a2ffc6433c16d3a9d9b6da5343 Mon Sep 17 00:00:00 2001 From: Nikhil Thakkar Date: Fri, 1 May 2020 20:03:51 +0200 Subject: [PATCH] chore: jacoco integration fox --- .../clone/ExampleInstrumentedTest.kt | 24 ------ build.gradle.kts | 8 ++ buildSrc/build.gradle.kts | 4 + buildSrc/jacoco.gradle | 84 +++++++++++++++++++ .../src/main/kotlin/AndroidModulePlugin.kt | 63 ++++++++++---- buildSrc/src/main/kotlin/Versions.kt | 2 +- .../core/ExampleInstrumentedTest.kt | 24 ------ gradle.properties | 3 +- home/build.gradle.kts | 2 +- .../home/ExampleInstrumentedTest.kt | 24 ------ .../main/res/navigation/graph_onboarding.xml | 1 - .../search/ExampleInstrumentedTest.kt | 24 ------ sonar-project.properties | 3 - 13 files changed, 146 insertions(+), 120 deletions(-) delete mode 100644 app/src/androidTest/java/dev/nikhi1/eventbrite/clone/ExampleInstrumentedTest.kt create mode 100644 buildSrc/jacoco.gradle delete mode 100644 core/src/androidTest/java/dev/nikhi1/eventbrite/core/ExampleInstrumentedTest.kt delete mode 100644 home/src/androidTest/java/dev/nikhi1/eventbrite/home/ExampleInstrumentedTest.kt delete mode 100644 search/src/androidTest/java/dev/nikhi1/eventbrite/search/ExampleInstrumentedTest.kt delete mode 100644 sonar-project.properties diff --git a/app/src/androidTest/java/dev/nikhi1/eventbrite/clone/ExampleInstrumentedTest.kt b/app/src/androidTest/java/dev/nikhi1/eventbrite/clone/ExampleInstrumentedTest.kt deleted file mode 100644 index 51dc374..0000000 --- a/app/src/androidTest/java/dev/nikhi1/eventbrite/clone/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,24 +0,0 @@ -package dev.nikhi1.eventbrite.clone - -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.ext.junit.runners.AndroidJUnit4 - -import org.junit.Test -import org.junit.runner.RunWith - -import org.junit.Assert.* - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("dev.nikhi1.eventbrite.clone", appContext.packageName) - } -} diff --git a/build.gradle.kts b/build.gradle.kts index dcd2b5b..df6c5eb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -17,3 +17,11 @@ allprojects { tasks.create("clean") { delete(rootProject.buildDir) } + +allprojects { + configurations.all { + resolutionStrategy { + force("org.objenesis:objenesis:2.6") + } + } +} \ No newline at end of file diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 6076a3c..e0f2b4d 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -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 */ diff --git a/buildSrc/jacoco.gradle b/buildSrc/jacoco.gradle new file mode 100644 index 0000000..ed3c277 --- /dev/null +++ b/buildSrc/jacoco.gradle @@ -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 +} diff --git a/buildSrc/src/main/kotlin/AndroidModulePlugin.kt b/buildSrc/src/main/kotlin/AndroidModulePlugin.kt index 78b3c7a..d428fd2 100644 --- a/buildSrc/src/main/kotlin/AndroidModulePlugin.kt +++ b/buildSrc/src/main/kotlin/AndroidModulePlugin.kt @@ -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 @@ -21,28 +24,19 @@ class AndroidModulePlugin : Plugin { 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().run { - toolVersion = "0.8.4" - //configure other properties if needed - } - - project.tasks.withType() { - extensions.getByType().run { - isIncludeNoLocationClasses = true - } - } } } } @@ -76,6 +70,12 @@ internal fun Project.configureAndroidBlock() = extensions.getByType().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) @@ -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().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().run { + properties { + property( + "sonar.coverage.jacoco.xmlReportPaths", + "${buildDir}/reports/jacoco/debug/jacoco.xml" + ) + } + + } } \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index 45eac09..599c58b 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -1,4 +1,4 @@ -internal object Versions { +object Versions { const val koin = "2.0.1" const val kotlin = "1.3.41" diff --git a/core/src/androidTest/java/dev/nikhi1/eventbrite/core/ExampleInstrumentedTest.kt b/core/src/androidTest/java/dev/nikhi1/eventbrite/core/ExampleInstrumentedTest.kt deleted file mode 100644 index e37b59d..0000000 --- a/core/src/androidTest/java/dev/nikhi1/eventbrite/core/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,24 +0,0 @@ -package dev.nikhi1.eventbrite.core - -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.ext.junit.runners.AndroidJUnit4 - -import org.junit.Test -import org.junit.runner.RunWith - -import org.junit.Assert.* - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("dev.nikhi1.eventbrite.core", appContext.packageName) - } -} diff --git a/gradle.properties b/gradle.properties index 1779f31..a163011 100644 --- a/gradle.properties +++ b/gradle.properties @@ -22,4 +22,5 @@ kotlin.code.style=official org.gradle.parallel = true -android.nonTransitiveRClass=true \ No newline at end of file +android.nonTransitiveRClass=true +android.enableR8 = false \ No newline at end of file diff --git a/home/build.gradle.kts b/home/build.gradle.kts index f074560..7ac076b 100644 --- a/home/build.gradle.kts +++ b/home/build.gradle.kts @@ -1,4 +1,4 @@ plugins { id("com.android.library") id("dev.nikhi1.plugin.android") -} +} \ No newline at end of file diff --git a/home/src/androidTest/java/dev/nikhi1/eventbrite/home/ExampleInstrumentedTest.kt b/home/src/androidTest/java/dev/nikhi1/eventbrite/home/ExampleInstrumentedTest.kt deleted file mode 100644 index f03c8e4..0000000 --- a/home/src/androidTest/java/dev/nikhi1/eventbrite/home/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,24 +0,0 @@ -package dev.nikhi1.eventbrite.home - -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.ext.junit.runners.AndroidJUnit4 - -import org.junit.Test -import org.junit.runner.RunWith - -import org.junit.Assert.* - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("dev.nikhi1.eventbrite.home", appContext.packageName) - } -} diff --git a/onboarding/src/main/res/navigation/graph_onboarding.xml b/onboarding/src/main/res/navigation/graph_onboarding.xml index 0e38b8a..003fd53 100644 --- a/onboarding/src/main/res/navigation/graph_onboarding.xml +++ b/onboarding/src/main/res/navigation/graph_onboarding.xml @@ -2,7 +2,6 @@