Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable Redundant Unit Tests #3010

Merged
merged 2 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build-logic/src/main/kotlin/AndroidConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import org.gradle.kotlin.dsl.invoke
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions

private const val BUILD_TYPE_DEBUG = "debug"
internal const val BUILD_TYPE_DEBUG = "debug"
internal const val BUILD_TYPE_QA = "qa"
const val FLAVOR_DIMENSION_ENV = "env"
const val FLAVOR_ENV_STAGE = "stage"
private const val FLAVOR_ENV_PRODUCTION = "production"
internal const val FLAVOR_ENV_PRODUCTION = "production"

// TODO: provide Project using the new multiple context receivers functionality.
// this is prototyped in 1.6.20 and will probably reach beta in Kotlin 1.8 or 1.9
Expand Down
31 changes: 18 additions & 13 deletions build-logic/src/main/kotlin/AndroidTestConfiguration.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import com.android.build.gradle.TestedExtension
import kotlinx.kover.gradle.plugin.dsl.KoverProjectExtension
import kotlinx.kover.gradle.plugin.dsl.KoverReportExtension
import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.configure

// TODO: provide Project using the new multiple context receivers functionality.
Expand Down Expand Up @@ -32,16 +30,11 @@ internal fun TestedExtension.configureTestOptions(project: Project) {
}
}

// Kotlin Kover
project.apply(plugin = "org.jetbrains.kotlinx.kover")
project.extensions.configure<KoverReportExtension> {
project.androidComponents.onVariants {
androidReports(it.name) {
xml {
// HACK: update the report file so that codecov picks it up automatically
setReportFile(project.layout.buildDirectory.file("reports/kover/${it.name}/report.xml"))
}
}
// only enable unit tests for debug builds targeting production
project.androidComponents {
beforeVariants { builder ->
val env = builder.productFlavors.toMap()[FLAVOR_DIMENSION_ENV] ?: FLAVOR_ENV_PRODUCTION
builder.enableUnitTest = builder.buildType == BUILD_TYPE_DEBUG && env == FLAVOR_ENV_PRODUCTION
}
}

Expand All @@ -50,8 +43,20 @@ internal fun TestedExtension.configureTestOptions(project: Project) {
val totalShards = project.findProperty("testTotalShards")?.toString()?.toIntOrNull()
if (shard != null && totalShards != null) {
if (Math.floorMod(project.path.hashCode(), totalShards) != Math.floorMod(shard, totalShards)) {
project.extensions.configure<KoverProjectExtension> { disable() }
project.androidComponents.beforeVariants { it.enableUnitTest = false }
}
}

// Kotlin Kover
project.plugins.apply("org.jetbrains.kotlinx.kover")
project.extensions.configure<KoverReportExtension> {
project.androidComponents.onVariants {
androidReports(it.name) {
xml {
// HACK: update the report file so that codecov picks it up automatically
setReportFile(project.layout.buildDirectory.file("reports/kover/${it.name}/report.xml"))
}
}
}
}
}
4 changes: 4 additions & 0 deletions build-logic/src/main/kotlin/Project.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import com.android.build.api.variant.AndroidComponentsExtension
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.kotlin.dsl.getByType

internal fun Project.androidComponents(configure: Action<AndroidComponentsExtension<*, *, *>>): Unit =
extensions.configure("androidComponents", configure)

internal val Project.libs get() = project.extensions.getByType<VersionCatalogsExtension>().named("libs")
internal val Project.androidComponents
get() = project.extensions.getByName("androidComponents") as AndroidComponentsExtension<*, *, *>