Skip to content

Commit 0e0dc13

Browse files
committed
Add variants (JU5 & 6) to instrumentation modules
1 parent 55f585d commit 0e0dc13

6 files changed

Lines changed: 88 additions & 21 deletions

File tree

build-logic/src/main/kotlin/Environment.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ import org.gradle.api.Project
44
import java.io.File
55
import java.util.Properties
66

7+
enum class SupportedJUnit(
8+
val label: String,
9+
val minSdk: Int
10+
) {
11+
JUnit5(label = "five", minSdk = 26),
12+
JUnit6(label = "six", minSdk = 35)
13+
}
14+
715
enum class SupportedAgp(
816
val version: String,
917
val gradle: String,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package extensions
2+
3+
import java.util.Locale.getDefault
4+
5+
fun String.capitalized(): String =
6+
replaceFirstChar { if (it.isLowerCase()) it.titlecase(getDefault()) else it.toString() }

build-logic/src/main/kotlin/extensions/VersionCatalogExtensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import org.gradle.api.artifacts.VersionCatalogsExtension
88
import org.gradle.api.provider.Provider
99
import org.gradle.kotlin.dsl.getByType
1010

11-
internal val Project.libs: VersionCatalog
11+
val Project.libs: VersionCatalog
1212
get() = extensions.getByType<VersionCatalogsExtension>().named("extensions.libs")
1313

1414
fun VersionCatalog.agp(version: SupportedAgp): String {

instrumentation/build.gradle.kts

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import com.android.build.api.dsl.LibraryExtension
22
import com.android.build.gradle.BaseExtension
3+
import extensions.capitalized
34
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
45
import org.gradle.api.tasks.testing.logging.TestLogEvent
56
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
@@ -32,9 +33,9 @@ apiValidation {
3233
subprojects {
3334
apply(plugin = "explicit-api-mode")
3435

35-
val jvmTarget = JvmTarget.JVM_17
36-
val javaVersion = JavaVersion.VERSION_17
37-
36+
val jvmTarget = JvmTarget.JVM_21
37+
val javaVersion = JavaVersion.toVersion(jvmTarget.target)
38+
3839
// Configure Kotlin
3940
plugins.withType<KotlinBasePlugin> {
4041
tasks.withType<KotlinCompilationTask<*>>().configureEach {
@@ -48,6 +49,13 @@ subprojects {
4849
}
4950
}
5051

52+
// Configure Java
53+
plugins.withId("java") {
54+
configure<JavaPluginExtension> {
55+
toolchain { languageVersion.set(JavaLanguageVersion.of(javaVersion.majorVersion)) }
56+
}
57+
}
58+
5159
// Configure Android
5260
plugins.withId("com.android.base") {
5361
configure<BaseExtension> {
@@ -71,6 +79,48 @@ subprojects {
7179
unitTests.isReturnDefaultValues = true
7280
}
7381

82+
// Create product flavors for each supported generation of JUnit,
83+
// then declare the corresponding BOM for each of them
84+
// to provide dependencies to each target
85+
val supportedTargets = SupportedJUnit.values()
86+
87+
flavorDimensions("target")
88+
productFlavors {
89+
supportedTargets.forEachIndexed { index, junit ->
90+
register(junit.label) {
91+
dimension = "target"
92+
isDefault = index == supportedTargets.lastIndex
93+
}
94+
}
95+
}
96+
97+
dependencies {
98+
supportedTargets.forEach { junit ->
99+
val configNames = listOf(
100+
"${junit.label}Api",
101+
"${junit.label}Implementation",
102+
"${junit.label}CompileOnly",
103+
"${junit.label}RuntimeOnly",
104+
"test${junit.label.capitalized()}Implementation",
105+
"test${junit.label.capitalized()}CompileOnly",
106+
"test${junit.label.capitalized()}RuntimeOnly",
107+
"androidTest${junit.label.capitalized()}Implementation",
108+
"androidTest${junit.label.capitalized()}CompileOnly",
109+
"androidTest${junit.label.capitalized()}RuntimeOnly",
110+
)
111+
112+
configNames.forEach { configName ->
113+
add(
114+
configurationName = configName,
115+
dependencyNotation = when (junit) {
116+
SupportedJUnit.JUnit5 -> platform(libs.junit.framework.bom5)
117+
SupportedJUnit.JUnit6 -> platform(libs.junit.framework.bom6)
118+
}
119+
)
120+
}
121+
}
122+
}
123+
74124
if (this is LibraryExtension) {
75125
lint {
76126
// JUnit 4 refers to java.lang.management APIs, which are absent on Android.

instrumentation/testutil/src/main/kotlin/de/mannodermaus/junit5/testutil/AndroidBuildUtils.kt

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,25 @@ import com.google.common.truth.Truth.assertThat
77
import de.mannodermaus.junit5.testutil.reflect.getFieldReflectively
88
import de.mannodermaus.junit5.testutil.reflect.setStaticValue
99

10-
object AndroidBuildUtils {
11-
12-
fun withApiLevel(api: Int, block: () -> Unit) = withMockedStaticField<Build.VERSION>(
13-
fieldName = "SDK_INT",
14-
value = api,
15-
block = block,
16-
)
10+
public object AndroidBuildUtils {
11+
12+
public fun withApiLevel(api: Int, block: () -> Unit) {
13+
withMockedStaticField<Build.VERSION>(
14+
fieldName = "SDK_INT",
15+
value = api,
16+
block = block,
17+
)
18+
}
1719

18-
fun withManufacturer(name: String, block: () -> Unit) = withMockedStaticField<Build>(
19-
fieldName = "MANUFACTURER",
20-
value = name,
21-
block = block,
22-
)
20+
public fun withManufacturer(name: String, block: () -> Unit) {
21+
withMockedStaticField<Build>(
22+
fieldName = "MANUFACTURER",
23+
value = name,
24+
block = block,
25+
)
26+
}
2327

24-
fun withMockedInstrumentation(arguments: Bundle = Bundle(), block: () -> Unit) {
28+
public fun withMockedInstrumentation(arguments: Bundle = Bundle(), block: () -> Unit) {
2529
val (oldInstrumentation, oldArguments) = try {
2630
InstrumentationRegistry.getInstrumentation() to InstrumentationRegistry.getArguments()
2731
} catch (ignored: Throwable) {

instrumentation/testutil/src/main/kotlin/de/mannodermaus/junit5/testutil/CollectingRunListener.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import org.junit.runner.notification.RunListener
77
/**
88
* A JUnit 4 [RunListener] that collects information about executed and failed tests.
99
*/
10-
class CollectingRunListener : RunListener() {
11-
12-
data class Results(
10+
public class CollectingRunListener : RunListener() {
11+
public data class Results(
1312
val successfulTests: List<Description>,
1413
val failedTests: List<Failure>,
1514
val ignoredTests: List<Description>
@@ -31,5 +30,5 @@ class CollectingRunListener : RunListener() {
3130
ignored += description
3231
}
3332

34-
fun getResults() = Results(success, failures, ignored)
33+
public fun getResults(): Results = Results(success, failures, ignored)
3534
}

0 commit comments

Comments
 (0)