This repository has been archived by the owner on Nov 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
/
SpoonRunTask.groovy
281 lines (229 loc) · 8.57 KB
/
SpoonRunTask.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package com.stanfy.spoon.gradle
import com.android.ddmlib.testrunner.IRemoteAndroidTestRunner
import com.squareup.spoon.SpoonRunner
import groovy.transform.PackageScope
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.artifacts.ResolvedDependency
import org.gradle.api.tasks.*
import org.slf4j.Logger
import org.slf4j.LoggerFactory
/**
* Task for using SpoonRunner.
*/
class SpoonRunTask extends DefaultTask implements VerificationTask {
/** All sizes should be run. */
@PackageScope
static final String TEST_SIZE_ALL = "all";
/** Plugin dependency name. */
private static final String PLUGIN_DEP_NAME = "com.stanfy.spoon:spoon-gradle-plugin"
/** Spoon runner artifact name. */
private static final String SPOON_RUNNER_ARTIFACT = "spoon-runner"
/** Spoon dependency name. */
private static final String SPOON_DEP_NAME = "com.squareup.spoon:$SPOON_RUNNER_ARTIFACT"
/** Logger. */
private static final Logger LOG = LoggerFactory.getLogger(SpoonRunTask.class)
/** A title for the output website. */
@Input
String title
/** If true then test failures do not cause a build failure. */
boolean ignoreFailures
/** If true, tests will fail if no devices are connected. */
boolean failIfNoDeviceConnected
/** Debug logging switcher. */
boolean debug
/** ADB timeout in ms. */
int adbTimeout = -1
/** Name of the one test to run. */
String className
/** Name of the one test method to run. */
String methodName
/** Extra arguments to pass to instrumentation. */
List<String> instrumentationArgs
/** Whether or not animations are enabled */
boolean noAnimations
/** Size of test to be run ('small' / 'medium' / 'large'). */
@Input
String testSize
/** Instrumentation APK. */
@InputFile
File instrumentationApk
/** Application APK. */
@InputFile
File applicationApk
/** Output directory. */
@OutputDirectory
File output
/** Use all the connected devices flag. */
boolean allDevices
/** Devices to run on. */
Set<String> devices
/** Devices to skip running on. */
Set<String> skipDevices
/** The number of separate shards to create. */
int numShards
/** The shardIndex option to specify which shard to run. */
int shardIndex
/** The codeCoverage option to calculate code coverage. */
boolean codeCoverage;
/** The shard option to specify whether to shard tests or not. */
boolean shard;
/** Execute the tests device by device */
boolean sequential
/** Grants all permisions for Android M >= devices */
boolean grantAllPermissions
@TaskAction
void runSpoon() {
LOG.info("Run instrumentation tests $instrumentationApk for app $applicationApk")
LOG.debug("Title: $title")
LOG.debug("Output: $output")
LOG.debug("Ignore failures: $ignoreFailures")
LOG.debug("Fail if no device connected: $failIfNoDeviceConnected")
LOG.debug("Debug mode: $debug")
if (className) {
LOG.debug("Class name: $className")
if (methodName) {
LOG.debug("Method name: $methodName")
}
}
LOG.debug("No animations: $noAnimations")
LOG.debug("Test size: $testSize")
LOG.debug("numShards: $numShards")
LOG.debug("shardIndex: $shardIndex")
String cp = getClasspath()
LOG.debug("Classpath: $cp")
SpoonRunner.Builder runBuilder = new SpoonRunner.Builder()
.setTerminateAdb(false)
.setTitle(title)
.setApplicationApk(applicationApk)
.setInstrumentationApk(instrumentationApk)
.setOutputDirectory(output)
.setFailIfNoDeviceConnected(failIfNoDeviceConnected)
.setDebug(debug)
.setClassName(className)
.setMethodName(methodName)
.setAndroidSdk(project.android.sdkDirectory)
.setClasspath(cp)
.setNoAnimations(noAnimations)
.setCodeCoverage(codeCoverage)
.setShard(shard)
.setSequential(sequential)
.setGrantAll(grantAllPermissions)
def instrumentationArgs = this.instrumentationArgs
if (instrumentationArgs == null) {
instrumentationArgs = []
}
if (numShards > 0) {
instrumentationArgs.add "numShards=${numShards}".toString()
instrumentationArgs.add "shardIndex=${shardIndex}".toString()
}
if (instrumentationArgs) {
runBuilder.setInstrumentationArgs(instrumentationArgs)
}
if (testSize != TEST_SIZE_ALL) {
// Will throw exception with informative message if provided size is illegal
runBuilder.setTestSize(IRemoteAndroidTestRunner.TestSize.getTestSize(testSize))
}
if (adbTimeout != -1) {
LOG.info("ADB timeout $adbTimeout")
runBuilder.setAdbTimeout(adbTimeout)
}
if (skipDevices != null && !skipDevices.isEmpty()) {
skipDevices.each {
runBuilder.skipDevice(it);
}
}
if (allDevices) {
runBuilder.useAllAttachedDevices()
LOG.info("Using all the attached devices")
} else {
if (!devices) {
throw new GradleException("No devices specified to run the tests on");
}
devices.each {
runBuilder.addDevice(it)
}
LOG.info("Using devices $devices")
LOG.info("Skipping following devices: $skipDevices")
}
boolean success = runBuilder.build().run()
if (!success && !ignoreFailures) {
throw new GradleException("Tests failed! See ${output}/index.html")
}
}
private static def findCpDependency(final Project project, final String prefix) {
def configuration = project.buildscript.configurations.classpath
return configuration.resolvedConfiguration.firstLevelModuleDependencies.findResult {
return findChildDependency(it, prefix)
}
}
private static def findChildDependency(def dependency, final String prefix) {
if (dependency.name.startsWith(prefix)) {
return dependency
} else {
return dependency.children.findResult {
return findChildDependency(it, prefix)
}
}
}
private def lookupDependency(final String prefix) {
def dep = null
def p = project, pp = null
while (!dep && p) {
pp = p
dep = findCpDependency(p, prefix)
p = p.parent
}
return [dep, pp]
}
private static void collectDependencies(ResolvedDependency root, def classpath, def allDeps) {
root.children.each { dep ->
def conflict = allDeps.find { it.moduleName == dep.moduleName && it.moduleGroup == dep.moduleGroup }
if (conflict && conflict.moduleVersion != dep.moduleVersion) {
LOG.warn("There is a dependencies conflict for ${dep.moduleGroup}:${dep.modeuleName}. "
+ "Versions: ${conflict.modeuleVersion} and ${dep.modeuleVersion}")
} else {
allDeps.add dep
}
}
classpath.addAll root.allModuleArtifacts.collect { artifact -> artifact.file }
root.children.each {
collectDependencies it, classpath, allDeps
}
}
private static void checkDependencies(ResolvedDependency root, def classpath, def allDeps) {
root.children.each { dep ->
def existing = allDeps.find { it.moduleName == dep.moduleName && it.moduleGroup == dep.moduleGroup }
if (existing) {
collectDependencies(dep, classpath, allDeps)
}
}
root.children.each { dep ->
checkDependencies(dep, classpath, allDeps)
}
}
private String getClasspath() {
def (pluginDep, usedProject) = lookupDependency(PLUGIN_DEP_NAME)
if (!pluginDep) {
throw new IllegalStateException("Could not resolve spoon dependencies")
}
ResolvedDependency spoon = pluginDep.children.find { it.name.startsWith SpoonRunTask.SPOON_DEP_NAME } as ResolvedDependency
if (!spoon) { throw new IllegalStateException("Cannot find spoon-runner in dependencies") }
// Collect spoon dependencies.
def classpath = new HashSet(), allDeps = []
collectDependencies(spoon, classpath, allDeps)
// Add spoon runner.
classpath += pluginDep.allModuleArtifacts.find { it.name == SpoonRunTask.SPOON_RUNNER_ARTIFACT }.file
/*
* XXX Due to how Gradle dependencies resolution works we can get not all the required deps from the step above.
* For example spoon dep is com.android.tools.ddms:ddmlib:23.2.1.
* If you use Android plugin 1.1.3, ddmlib will be resolved to 24.1.3. And its dependencies will not traversed yet.
* Hence, we traverse the whole tree again and pick up those parts of the tree that we miss.
*/
usedProject.buildscript.configurations.classpath.resolvedConfiguration.firstLevelModuleDependencies.each {
SpoonRunTask.checkDependencies((ResolvedDependency) it, classpath, allDeps)
}
return project.files(classpath).asPath
}
}