Skip to content

Commit

Permalink
Create a custom Gradle plugin to configure testing (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 authored Mar 14, 2024
1 parent aaed6fb commit 3e69226
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 97 deletions.
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ plugins {
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.detekt)
alias(libs.plugins.dependency.analysis.gradle.plugin)
alias(libs.plugins.kotlinx.kover)
}

apply(plugin = "android-reporting")
Expand Down
1 change: 1 addition & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ plugins {

dependencies {
api(libs.android.gradle.api)
api(libs.kotlinx.kover.gradle)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.gradle

import com.android.build.api.dsl.LibraryExtension
import kotlinx.kover.gradle.plugin.dsl.KoverReportExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.testing.Test
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.withType

/**
* Custom Gradle plugin to configure a Pillarbox module for testing.
*/
class PillarboxTestedModulePlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
pluginManager.apply("com.android.library")
pluginManager.apply("org.jetbrains.kotlinx.kover")

extensions.configure<LibraryExtension> {
defaultConfig {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

@Suppress("UnstableApiUsage")
testOptions {
unitTests {
isIncludeAndroidResources = true
}
}
}

extensions.configure<KoverReportExtension> {
androidReports("debug") {
xml {
title.set(project.path)
}
}
}

tasks.withType<Test>().configureEach {
testLogging.exceptionFormat = TestExceptionFormat.FULL
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
implementation-class=ch.srgssr.pillarbox.gradle.PillarboxTestedModulePlugin
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ coil-base = { module = "io.coil-kt:coil-compose-base", version.ref = "coil" }
json = { module = "org.json:json", version.ref = "json" }
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
kotlinx-kover-gradle = { module = "org.jetbrains.kotlinx:kover-gradle-plugin", version.ref = "kotlinx-kover" }
kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
Expand Down Expand Up @@ -142,4 +143,3 @@ dependency-analysis-gradle-plugin = { id = "com.autonomousapps.dependency-analys
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
kotlinx-kover = { id = "org.jetbrains.kotlinx.kover", version.ref = "kotlinx-kover" }
27 changes: 2 additions & 25 deletions pillarbox-analytics/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
* License information is available from the LICENSE file.
*/

import ch.srgssr.pillarbox.gradle.PillarboxPublishingPlugin
import org.gradle.api.tasks.testing.logging.TestExceptionFormat

plugins {
alias(libs.plugins.android.library)
id("ch.srgssr.pillarbox.gradle.publishing")
id("ch.srgssr.pillarbox.gradle.tested_module")
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlinx.kover)
}

apply<PillarboxPublishingPlugin>()

android {
namespace = "ch.srgssr.pillarbox.analytics"
compileSdk = AppConfig.compileSdk
Expand All @@ -24,7 +19,6 @@ android {
buildConfigField("String", "BUILD_DATE", "\"${AppConfig.getBuildDate()}\"")
buildConfigField("String", "VERSION_NAME", "\"${version}\"")

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}

Expand All @@ -48,15 +42,6 @@ android {
buildConfig = true
resValues = false
}
testOptions {
unitTests {
isIncludeAndroidResources = true
}
}
}

tasks.withType<Test>().configureEach {
testLogging.exceptionFormat = TestExceptionFormat.FULL
}

dependencies {
Expand All @@ -77,11 +62,3 @@ dependencies {
testImplementation(libs.robolectric.annotations)
testImplementation(libs.robolectric.shadows.framework)
}

koverReport {
androidReports("debug") {
xml {
title.set(project.path)
}
}
}
27 changes: 2 additions & 25 deletions pillarbox-core-business/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,20 @@
* License information is available from the LICENSE file.
*/

import ch.srgssr.pillarbox.gradle.PillarboxPublishingPlugin
import org.gradle.api.tasks.testing.logging.TestExceptionFormat

plugins {
alias(libs.plugins.android.library)
id("ch.srgssr.pillarbox.gradle.publishing")
id("ch.srgssr.pillarbox.gradle.tested_module")
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.kotlinx.kover)
}

apply<PillarboxPublishingPlugin>()

android {
namespace = "ch.srgssr.pillarbox.core.business"
compileSdk = AppConfig.compileSdk

defaultConfig {
minSdk = AppConfig.minSdk

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}

Expand All @@ -43,15 +37,6 @@ android {
buildConfig = true
resValues = false
}
testOptions {
unitTests {
isIncludeAndroidResources = true
}
}
}

tasks.withType<Test>().configureEach {
testLogging.exceptionFormat = TestExceptionFormat.FULL
}

dependencies {
Expand Down Expand Up @@ -95,11 +80,3 @@ dependencies {
testImplementation(libs.robolectric.annotations)
testImplementation(libs.robolectric.shadows.framework)
}

koverReport {
androidReports("debug") {
xml {
title.set(project.path)
}
}
}
27 changes: 2 additions & 25 deletions pillarbox-player/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,19 @@
* License information is available from the LICENSE file.
*/

import ch.srgssr.pillarbox.gradle.PillarboxPublishingPlugin
import org.gradle.api.tasks.testing.logging.TestExceptionFormat

plugins {
alias(libs.plugins.android.library)
id("ch.srgssr.pillarbox.gradle.publishing")
id("ch.srgssr.pillarbox.gradle.tested_module")
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlinx.kover)
}

apply<PillarboxPublishingPlugin>()

android {
namespace = "ch.srgssr.pillarbox.player"
compileSdk = AppConfig.compileSdk

defaultConfig {
minSdk = AppConfig.minSdk

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFile("consumer-rules.pro")
}

Expand Down Expand Up @@ -55,15 +49,6 @@ android {
merges += "META-INF/LICENSE-notice.md"
}
}
testOptions {
unitTests {
isIncludeAndroidResources = true
}
}
}

tasks.withType<Test>().configureEach {
testLogging.exceptionFormat = TestExceptionFormat.FULL
}

dependencies {
Expand Down Expand Up @@ -105,11 +90,3 @@ dependencies {
androidTestImplementation(libs.mockk)
androidTestImplementation(libs.mockk.android)
}

koverReport {
androidReports("debug") {
xml {
title.set(project.path)
}
}
}
22 changes: 2 additions & 20 deletions pillarbox-ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,19 @@
* License information is available from the LICENSE file.
*/

import ch.srgssr.pillarbox.gradle.PillarboxPublishingPlugin
import org.gradle.api.tasks.testing.logging.TestExceptionFormat

plugins {
alias(libs.plugins.android.library)
id("ch.srgssr.pillarbox.gradle.publishing")
id("ch.srgssr.pillarbox.gradle.tested_module")
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlinx.kover)
}

apply<PillarboxPublishingPlugin>()

android {
namespace = "ch.srgssr.pillarbox.ui"
compileSdk = AppConfig.compileSdk

defaultConfig {
minSdk = AppConfig.minSdk

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFile("consumer-rules.pro")
}

Expand All @@ -47,10 +41,6 @@ android {
}
}

tasks.withType<Test>().configureEach {
testLogging.exceptionFormat = TestExceptionFormat.FULL
}

dependencies {
api(project(":pillarbox-player"))

Expand All @@ -77,11 +67,3 @@ dependencies {

debugImplementation(libs.androidx.compose.ui.tooling)
}

koverReport {
androidReports("debug") {
xml {
title.set(project.path)
}
}
}

0 comments on commit 3e69226

Please sign in to comment.