Skip to content

Commit 6563d53

Browse files
committed
Replace the need to copy Kotlin classes manually (see #29)
1 parent f5b8819 commit 6563d53

File tree

9 files changed

+82
-159
lines changed

9 files changed

+82
-159
lines changed

android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/Plugin.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package de.mannodermaus.gradle.plugins.junit5
22

3-
import de.mannodermaus.gradle.plugins.junit5.tasks.AndroidJUnit5CopyKotlin
3+
import de.mannodermaus.gradle.plugins.junit5.providers.KotlinTestRootDirectoryProvider
4+
import de.mannodermaus.gradle.plugins.junit5.providers.TestRootDirectoryProvider
45
import de.mannodermaus.gradle.plugins.junit5.tasks.AndroidJUnit5JacocoReport
56
import de.mannodermaus.gradle.plugins.junit5.tasks.AndroidJUnit5UnitTest
67
import org.gradle.api.Plugin
@@ -97,15 +98,19 @@ class AndroidJUnitPlatformPlugin : Plugin<Project> {
9798
val isKotlinApplied = projectConfig.kotlinPluginApplied
9899

99100
testVariants.forEach { variant ->
100-
val testTask = AndroidJUnit5UnitTest.create(this, variant)
101+
// Aggregate non-standard test root directories
102+
val rootProviders = mutableSetOf<TestRootDirectoryProvider>()
103+
if (isKotlinApplied) {
104+
rootProviders += KotlinTestRootDirectoryProvider(this, variant)
105+
}
106+
107+
// Create JUnit 5 test task
108+
val testTask = AndroidJUnit5UnitTest.create(this, variant, rootProviders)
101109

102110
if (isJacocoApplied) {
111+
// Create a Jacoco friend task
103112
AndroidJUnit5JacocoReport.create(this, testTask)
104113
}
105-
106-
if (isKotlinApplied) {
107-
AndroidJUnit5CopyKotlin.create(this, testTask)
108-
}
109114
}
110115
}
111116
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package de.mannodermaus.gradle.plugins.junit5.providers
2+
3+
import com.android.build.gradle.api.BaseVariant
4+
import de.mannodermaus.gradle.plugins.junit5.unitTestVariant
5+
import org.gradle.api.Project
6+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
7+
import java.io.File
8+
9+
/**
10+
* Provides test root directories for Kotlin sources,
11+
* with which a JUnit 5 Task can be enhanced.
12+
*/
13+
class KotlinTestRootDirectoryProvider(
14+
private val project: Project,
15+
private val variant: BaseVariant) : TestRootDirectoryProvider {
16+
17+
override fun testRootDirectories(): Set<File> {
18+
// Hook in the Kotlin destination directories to the JUnit 5 Task.
19+
// Note: The resulting Set might be empty for modules powered by AGP2:
20+
// The legacy Kotlin integration automatically copies over Kotlin classes
21+
// into the Java directories, which renders dedicated Gradle tasks useless.
22+
val kotlinTaskNames = listOf(
23+
kotlinTaskName(variant),
24+
kotlinTaskName(variant.unitTestVariant))
25+
26+
return kotlinTaskNames
27+
.map { project.tasks.findByName(it) }
28+
.filter { it != null }
29+
.map { it as KotlinCompile }
30+
.map { it.destinationDir }
31+
.toSet()
32+
}
33+
34+
private fun kotlinTaskName(variant: BaseVariant) =
35+
"compile${variant.name.capitalize()}Kotlin"
36+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package de.mannodermaus.gradle.plugins.junit5.providers
2+
3+
import java.io.File
4+
5+
interface TestRootDirectoryProvider {
6+
fun testRootDirectories(): Set<File>
7+
}

android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/tasks/Jacoco.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import org.gradle.testing.jacoco.tasks.JacocoReport
88

99
private const val TASK_NAME_DEFAULT = "jacocoTestReport"
1010
private const val GROUP_REPORTING = "reporting"
11-
private const val JACOCO_PLUGIN_EXT = "jacoco"
12-
private const val JACOCO_TASK_EXT = "jacoco"
1311

1412
/**
1513
* Jacoco Test Reporting Task connected to a variant-aware JUnit 5 task.

android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/tasks/Kotlin.kt

Lines changed: 0 additions & 40 deletions
This file was deleted.

android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/tasks/UnitTest.kt

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.android.build.gradle.internal.scope.VariantScope
66
import com.android.build.gradle.tasks.factory.AndroidUnitTest
77
import com.android.builder.core.VariantType
88
import de.mannodermaus.gradle.plugins.junit5.AndroidJUnitPlatformExtension
9+
import de.mannodermaus.gradle.plugins.junit5.providers.TestRootDirectoryProvider
910
import de.mannodermaus.gradle.plugins.junit5.engines
1011
import de.mannodermaus.gradle.plugins.junit5.filters
1112
import de.mannodermaus.gradle.plugins.junit5.getExcludeClassNamePatterns
@@ -39,8 +40,11 @@ private const val VERIFICATION_GROUP = JavaBasePlugin.VERIFICATION_GROUP
3940
open class AndroidJUnit5UnitTest : JavaExec() {
4041

4142
companion object {
42-
fun create(project: Project, variant: BaseVariant): AndroidJUnit5UnitTest {
43-
val configAction = ConfigAction(project, variant)
43+
fun create(
44+
project: Project,
45+
variant: BaseVariant,
46+
additionalRoots: Set<TestRootDirectoryProvider>): AndroidJUnit5UnitTest {
47+
val configAction = ConfigAction(project, variant, additionalRoots)
4448
return project.tasks.create(configAction.name, configAction.type, configAction)
4549
}
4650
}
@@ -57,12 +61,28 @@ open class AndroidJUnit5UnitTest : JavaExec() {
5761
@Optional
5862
var assetsCollection: FileCollection? = null
5963

64+
/**
65+
* Default Provider implementation for test root directories.
66+
* This will look up the main & test root directories
67+
* of the variant connected to a given JUnit 5 task.
68+
*/
69+
private class DefaultTestRootDirectoryProvider(
70+
private val variant: BaseVariant) : TestRootDirectoryProvider {
71+
override fun testRootDirectories() = setOf(
72+
// e.g. "build/intermediates/classes/debug/..."
73+
variant.variantData.scope.javaOutputDir,
74+
// e.g. "build/intermediates/classes/test/debug/..."
75+
variant.unitTestVariant.variantData.scope.javaOutputDir
76+
)
77+
}
78+
6079
/**
6180
* Configuration closure for an Android JUnit5 test task.
6281
*/
6382
private class ConfigAction(
6483
val project: Project,
65-
val variant: BaseVariant
84+
val variant: BaseVariant,
85+
val additionalRoots: Set<TestRootDirectoryProvider>
6686
) : TaskConfigAction<AndroidJUnit5UnitTest> {
6787

6888
private val scope: VariantScope = variant.variantData.scope
@@ -94,15 +114,11 @@ open class AndroidJUnit5UnitTest : JavaExec() {
94114
task.classpath = getDefaultUnitTestTask().classpath +
95115
project.configurations.getByName("junitPlatform")
96116

97-
// Aggregate the source folders for test cases
98-
// (usually, the unit test variant's folders should be enough,
117+
// Aggregate test root directories, starting with the default set of folders.
118+
// (Usually, the unit test variant's folders should be enough,
99119
// however we aggregate the main scope's output as well)
100-
val testRootDirs = listOf(
101-
// e.g. "build/intermediates/classes/debug/..."
102-
scope.javaOutputDir,
103-
// e.g. "build/intermediates/classes/test/debug/..."
104-
variant.unitTestVariant.variantData.scope.javaOutputDir
105-
)
120+
val defaultProvider = DefaultTestRootDirectoryProvider(variant)
121+
val testRootDirs = defaultProvider.testRootDirectories() + additionalRoots.flatMap { it.testRootDirectories() }
106122

107123
project.logInfo("Assembled JUnit 5 Task '$task.name':")
108124
testRootDirs.forEach { project.logInfo("|__ $it") }
@@ -187,7 +203,7 @@ open class AndroidJUnit5UnitTest : JavaExec() {
187203
private fun buildArgs(
188204
junit5: AndroidJUnitPlatformExtension,
189205
reportsDir: File,
190-
testRootDirs: List<File>): List<String> {
206+
testRootDirs: Set<File>): List<String> {
191207
val args = mutableListOf<String>()
192208

193209
// Log Details

sample/build.gradle

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import com.android.build.gradle.BaseExtension
2-
31
buildscript {
42
repositories {
53
jcenter()
@@ -15,20 +13,6 @@ apply plugin: "kotlin-android"
1513
apply plugin: "de.mannodermaus.android-junit5"
1614
apply plugin: "jacoco"
1715

18-
project.afterEvaluate {
19-
20-
def mi = BaseExtension.class.getDeclaredField("extraModelInfo")
21-
mi.accessible = true
22-
23-
def val = mi.get(android)
24-
println "Extra Model Info: $val"
25-
26-
val.class.getDeclaredFields().each {
27-
it.accessible = true
28-
println " |__ $it --> ${it.get(val)}"
29-
}
30-
}
31-
3216
android {
3317
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3418
// A Note to my forgetful self:
@@ -68,8 +52,3 @@ dependencies {
6852
testImplementation junit5Params()
6953
testCompileOnly junit5EmbeddedRuntime()
7054
}
71-
72-
project.afterEvaluate {
73-
// tasks.getByName("copyKotlinUnitTestClassesDebug").enabled = false
74-
// tasks.getByName("junitPlatformTestDebug").enabled = false
75-
}

tests/testAgp3x/src/test/groovy/de/mannodermaus/gradle/plugins/junit5/AGP3PluginSpec.groovy

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -135,30 +135,4 @@ class AGP3PluginSpec extends BasePluginSpec {
135135
project.tasks.findByName("jacocoTestReportDebug") == null
136136
project.tasks.findByName("jacocoTestReportRelease") == null
137137
}
138-
139-
def "Feature: Kotlin Integration"() {
140-
when:
141-
def project = factory.newProject(rootProject())
142-
.asAndroidFeature()
143-
.applyJunit5Plugin()
144-
.applyKotlinPlugin()
145-
.buildAndEvaluate()
146-
147-
then:
148-
project.tasks.getByName("copyKotlinUnitTestClassesDebug")
149-
project.tasks.getByName("copyKotlinUnitTestClassesRelease")
150-
}
151-
152-
def "Feature: Kotlin Tasks not added if plugin absent"() {
153-
when:
154-
def project = factory.newProject(rootProject())
155-
.asAndroidFeature()
156-
.applyJunit5Plugin()
157-
.applyKotlinPlugin(false)
158-
.buildAndEvaluate()
159-
160-
then:
161-
project.tasks.findByName("copyKotlinUnitTestClassesDebug") == null
162-
project.tasks.findByName("copyKotlinUnitTestClassesRelease") == null
163-
}
164138
}

tests/testCommon/src/test/groovy/de/mannodermaus/gradle/plugins/junit5/BasePluginSpec.groovy

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -281,56 +281,4 @@ abstract class BasePluginSpec extends Specification {
281281
project.tasks.findByName("jacocoTestReportDebug") == null
282282
project.tasks.findByName("jacocoTestReportRelease") == null
283283
}
284-
285-
def "Application: Kotlin Integration"() {
286-
when:
287-
def project = factory.newProject(rootProject())
288-
.asAndroidApplication()
289-
.applyJunit5Plugin()
290-
.applyKotlinPlugin()
291-
.buildAndEvaluate()
292-
293-
then:
294-
project.tasks.getByName("copyKotlinUnitTestClassesDebug")
295-
project.tasks.getByName("copyKotlinUnitTestClassesRelease")
296-
}
297-
298-
def "Library: Kotlin Integration"() {
299-
when:
300-
def project = factory.newProject(rootProject())
301-
.asAndroidLibrary()
302-
.applyJunit5Plugin()
303-
.applyKotlinPlugin()
304-
.buildAndEvaluate()
305-
306-
then:
307-
project.tasks.getByName("copyKotlinUnitTestClassesDebug")
308-
project.tasks.getByName("copyKotlinUnitTestClassesRelease")
309-
}
310-
311-
def "Application: Kotlin Tasks not added if plugin absent"() {
312-
when:
313-
def project = factory.newProject(rootProject())
314-
.asAndroidApplication()
315-
.applyJunit5Plugin()
316-
.applyKotlinPlugin(false)
317-
.buildAndEvaluate()
318-
319-
then:
320-
project.tasks.findByName("copyKotlinUnitTestClassesDebug") == null
321-
project.tasks.findByName("copyKotlinUnitTestClassesRelease") == null
322-
}
323-
324-
def "Library: Kotlin Tasks not added if plugin absent"() {
325-
when:
326-
def project = factory.newProject(rootProject())
327-
.asAndroidLibrary()
328-
.applyJunit5Plugin()
329-
.applyKotlinPlugin(false)
330-
.buildAndEvaluate()
331-
332-
then:
333-
project.tasks.findByName("copyKotlinUnitTestClassesDebug") == null
334-
project.tasks.findByName("copyKotlinUnitTestClassesRelease") == null
335-
}
336284
}

0 commit comments

Comments
 (0)