1
- @file:Suppress(" DEPRECATION" )
2
-
3
1
package de.mannodermaus.gradle.plugins.junit5.tasks
4
2
3
+ import com.android.build.api.variant.SourceDirectories
5
4
import com.android.build.api.variant.Variant
6
- import com.android.build.gradle.api.TestVariant
7
5
import de.mannodermaus.gradle.plugins.junit5.internal.config.INSTRUMENTATION_FILTER_RES_FILE_NAME
8
6
import de.mannodermaus.gradle.plugins.junit5.internal.config.JUnit5TaskConfig
9
7
import de.mannodermaus.gradle.plugins.junit5.internal.extensions.getTaskName
10
8
import de.mannodermaus.gradle.plugins.junit5.internal.extensions.junitPlatform
11
9
import org.gradle.api.DefaultTask
12
10
import org.gradle.api.Project
11
+ import org.gradle.api.file.DirectoryProperty
12
+ import org.gradle.api.provider.ListProperty
13
13
import org.gradle.api.tasks.CacheableTask
14
14
import org.gradle.api.tasks.Input
15
15
import org.gradle.api.tasks.OutputDirectory
@@ -25,20 +25,20 @@ import java.io.File
25
25
* This only allows tests to be filtered with @Tag annotations even in the instrumentation test realm.
26
26
* Other plugin DSL settings, like includeEngines/excludeEngines or includePattern/excludePattern
27
27
* are not copied out to file. This has to do with limitations of the backport implementation
28
- * of the JUnit Platform Runner, as well as some incompatibilities between Gradle and Java with regards to
29
- * how class name patterns are formatted.
28
+ * of the JUnit Platform Runner, as well as some incompatibilities between Gradle and Java
29
+ * regarding how class name patterns are formatted.
30
30
*/
31
31
@CacheableTask
32
32
public abstract class AndroidJUnit5WriteFilters : DefaultTask () {
33
33
34
34
internal companion object {
35
+ @Suppress(" UnstableApiUsage" )
35
36
fun register (
36
37
project : Project ,
37
38
variant : Variant ,
38
- instrumentationTestVariant : TestVariant
39
+ sourceDirs : SourceDirectories . Layered ,
39
40
): Boolean {
40
- val outputFolder = File (" ${project.buildDir} /generated/res/android-junit5/${instrumentationTestVariant.name} " )
41
- val configAction = ConfigAction (project, variant, outputFolder)
41
+ val configAction = ConfigAction (project, variant)
42
42
43
43
val provider = project.tasks.register(
44
44
configAction.name,
@@ -48,67 +48,66 @@ public abstract class AndroidJUnit5WriteFilters : DefaultTask() {
48
48
49
49
// Connect the output folder of the task to the instrumentation tests
50
50
// so that they are bundled into the built test application
51
- instrumentationTestVariant.registerGeneratedResFolders(
52
- project.files(outputFolder).builtBy(provider)
51
+ sourceDirs.addGeneratedSourceDirectory(
52
+ taskProvider = provider,
53
+ wiredWith = AndroidJUnit5WriteFilters ::outputFolder,
53
54
)
54
- instrumentationTestVariant.mergeResourcesProvider.configure { it.dependsOn(provider) }
55
55
56
56
return true
57
57
}
58
58
}
59
59
60
- @Input
61
- public var includeTags: List <String > = emptyList()
60
+ @get: Input
61
+ public abstract val includeTags: ListProperty <String >
62
62
63
- @Input
64
- public var excludeTags: List <String > = emptyList()
63
+ @get: Input
64
+ public abstract val excludeTags: ListProperty <String >
65
65
66
- @OutputDirectory
67
- public var outputFolder: File ? = null
66
+ @get: OutputDirectory
67
+ public abstract val outputFolder: DirectoryProperty
68
68
69
69
@TaskAction
70
70
public fun execute () {
71
- this .outputFolder?.let { folder ->
72
- // Clear out current contents of the generated folder
73
- folder.deleteRecursively()
74
-
75
- if (this .hasFilters()) {
76
- folder.mkdirs()
77
-
78
- // Re-write the new file structure into it;
79
- // the generated file will have a fixed name & is located
80
- // as a "raw" resource inside the output folder
81
- val rawFolder = File (folder, " raw" ).apply { mkdirs() }
82
- File (rawFolder, INSTRUMENTATION_FILTER_RES_FILE_NAME )
83
- .bufferedWriter()
84
- .use { writer ->
85
- // This format is a nod towards the real JUnit 5 ConsoleLauncher's arguments
86
- includeTags.forEach { tag -> writer.appendLine(" -t $tag " ) }
87
- excludeTags.forEach { tag -> writer.appendLine(" -T $tag " ) }
88
- }
89
- }
71
+ // Clear out current contents of the generated folder
72
+ val folder = outputFolder.get().asFile
73
+ folder.deleteRecursively()
74
+
75
+ val includeTags = includeTags.get()
76
+ val excludeTags = excludeTags.get()
77
+
78
+ if (includeTags.isNotEmpty() || excludeTags.isNotEmpty()) {
79
+ folder.mkdirs()
80
+
81
+ // Re-write the new file structure into it;
82
+ // the generated file will have a fixed name & is located
83
+ // as a "raw" resource inside the output folder
84
+ val rawFolder = File (folder, " raw" ).apply { mkdirs() }
85
+ File (rawFolder, INSTRUMENTATION_FILTER_RES_FILE_NAME )
86
+ .bufferedWriter()
87
+ .use { writer ->
88
+ // This format is a nod towards the real JUnit 5 ConsoleLauncher's arguments
89
+ includeTags.forEach { tag -> writer.appendLine(" -t $tag " ) }
90
+ excludeTags.forEach { tag -> writer.appendLine(" -T $tag " ) }
91
+ }
90
92
}
91
93
}
92
94
93
- private fun hasFilters () = includeTags.isNotEmpty() || excludeTags.isNotEmpty()
94
-
95
95
private class ConfigAction (
96
96
private val project : Project ,
97
97
private val variant : Variant ,
98
- private val outputFolder : File
99
98
) {
100
99
101
100
val name: String = variant.getTaskName(prefix = " writeFilters" , suffix = " androidTest" )
102
101
103
102
val type = AndroidJUnit5WriteFilters ::class .java
104
103
105
104
fun execute (task : AndroidJUnit5WriteFilters ) {
106
- task.outputFolder = outputFolder
107
-
108
- // Access filters for this particular variant & provide them to the task, too
105
+ // Access filters for this particular variant & provide them to the task
109
106
val configuration = JUnit5TaskConfig (variant, project.junitPlatform)
110
- task.includeTags = configuration.combinedIncludeTags.toList()
111
- task.excludeTags = configuration.combinedExcludeTags.toList()
107
+ task.includeTags.set(configuration.combinedIncludeTags.toList())
108
+ task.excludeTags.set(configuration.combinedExcludeTags.toList())
109
+
110
+ // Output folder is applied by Android Gradle Plugin, so there is no reason to provide a value ourselves
112
111
}
113
112
}
114
113
}
0 commit comments