-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
JacocoTaskExtension.java
364 lines (320 loc) · 10.6 KB
/
JacocoTaskExtension.java
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.testing.jacoco.plugins;
import com.google.common.base.Joiner;
import org.apache.commons.lang.StringUtils;
import org.gradle.api.file.FileCollection;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.LocalState;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputFile;
import org.gradle.internal.jacoco.JacocoAgentJar;
import org.gradle.process.JavaForkOptions;
import org.gradle.util.internal.RelativePathUtil;
import javax.annotation.Nullable;
import javax.inject.Inject;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
/**
* Extension for tasks that should run with a Jacoco agent to generate coverage execution data.
*/
public abstract class JacocoTaskExtension {
/**
* The types of output that the agent can use for execution data.
*/
public enum Output {
FILE,
TCP_SERVER,
TCP_CLIENT,
NONE;
/**
* Gets type in format of agent argument.
*/
public String getAsArg() {
return toString().toLowerCase(Locale.US).replaceAll("_", "");
}
}
private final JacocoAgentJar agent;
private final JavaForkOptions task;
private boolean enabled = true;
private final Property<File> destinationFile;
private List<String> includes = new ArrayList<>();
private List<String> excludes = new ArrayList<>();
private List<String> excludeClassLoaders = new ArrayList<>();
private boolean includeNoLocationClasses;
private String sessionId;
private boolean dumpOnExit = true;
private Output output = Output.FILE;
private String address;
private int port;
private File classDumpDir;
private boolean jmx;
/**
* Creates a Jacoco task extension.
*
* @param objects the object factory
* @param agent the agent JAR to use for analysis
* @param task the task we extend
*/
@Inject
public JacocoTaskExtension(ObjectFactory objects, JacocoAgentJar agent, JavaForkOptions task) {
this.agent = agent;
this.task = task;
destinationFile = objects.property(File.class);
}
/**
* Whether or not the task should generate execution data. Defaults to {@code true}.
*/
@Input
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
/**
* The path for the execution data to be written to.
*/
@Nullable
@Optional
@OutputFile
public File getDestinationFile() {
return destinationFile.getOrNull();
}
/**
* Set the provider for calculating the destination file.
*
* @param destinationFile Destination file provider
* @since 4.0
*/
public void setDestinationFile(Provider<File> destinationFile) {
this.destinationFile.set(destinationFile);
}
public void setDestinationFile(File destinationFile) {
this.destinationFile.set(destinationFile);
}
/**
* List of class names that should be included in analysis. Names can use wildcards (* and ?). If left empty, all classes will be included. Defaults to an empty list.
*/
@Nullable
@Optional
@Input
public List<String> getIncludes() {
return includes;
}
public void setIncludes(@Nullable List<String> includes) {
this.includes = includes;
}
/**
* List of class names that should be excluded from analysis. Names can use wildcard (* and ?). Defaults to an empty list.
*/
@Nullable
@Optional
@Input
public List<String> getExcludes() {
return excludes;
}
public void setExcludes(@Nullable List<String> excludes) {
this.excludes = excludes;
}
/**
* List of classloader names that should be excluded from analysis. Names can use wildcards (* and ?). Defaults to an empty list.
*/
@Nullable
@Optional
@Input
public List<String> getExcludeClassLoaders() {
return excludeClassLoaders;
}
public void setExcludeClassLoaders(@Nullable List<String> excludeClassLoaders) {
this.excludeClassLoaders = excludeClassLoaders;
}
/**
* Whether or not classes without source location should be instrumented. Defaults to {@code false}.
*
* This property is only taken into account if the used JaCoCo version supports this option (JaCoCo version >= 0.7.6)
*/
@Input
public boolean isIncludeNoLocationClasses() {
return includeNoLocationClasses;
}
public void setIncludeNoLocationClasses(boolean includeNoLocationClasses) {
this.includeNoLocationClasses = includeNoLocationClasses;
}
/**
* An identifier for the session written to the execution data. Defaults to an auto-generated identifier.
*/
@Nullable
@Optional
@Input
public String getSessionId() {
return sessionId;
}
public void setSessionId(@Nullable String sessionId) {
this.sessionId = sessionId;
}
/**
* Whether or not to dump the coverage data at VM shutdown. Defaults to {@code true}.
*/
@Input
public boolean isDumpOnExit() {
return dumpOnExit;
}
public void setDumpOnExit(boolean dumpOnExit) {
this.dumpOnExit = dumpOnExit;
}
/**
* The type of output to generate. Defaults to {@link Output#FILE}.
*/
@Input
public Output getOutput() {
return output;
}
public void setOutput(Output output) {
this.output = output;
}
/**
* IP address or hostname to use with {@link Output#TCP_SERVER} or {@link Output#TCP_CLIENT}. Defaults to localhost.
*/
@Nullable
@Optional
@Input
public String getAddress() {
return address;
}
public void setAddress(@Nullable String address) {
this.address = address;
}
/**
* Port to bind to for {@link Output#TCP_SERVER} or {@link Output#TCP_CLIENT}. Defaults to 6300.
*/
@Input
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
/**
* Path to dump all class files the agent sees are dumped to. Defaults to no dumps.
*
* @since 3.4
*/
@Nullable
@Optional
@LocalState
public File getClassDumpDir() {
return classDumpDir;
}
/**
* Sets path to dump all class files the agent sees are dumped to. Defaults to no dumps.
*
* @since 3.4
*/
public void setClassDumpDir(@Nullable File classDumpDir) {
this.classDumpDir = classDumpDir;
}
/**
* Whether or not to expose functionality via JMX under {@code org.jacoco:type=Runtime}. Defaults to {@code false}.
*
* The configuration of the jmx property is only taken into account if the used JaCoCo version supports this option (JaCoCo version >= 0.6.2)
*/
@Input
public boolean isJmx() {
return jmx;
}
public void setJmx(boolean jmx) {
this.jmx = jmx;
}
/**
* The Jacoco agent classpath.
*
* This contains only one file - the agent jar.
*
* @since 4.6
*/
@Classpath
public FileCollection getAgentClasspath() {
return agent.getAgentConf();
}
/**
* Gets all properties in the format expected of the agent JVM argument.
*
* @return state of extension in a JVM argument
*/
@Internal
public String getAsJvmArg() {
StringBuilder builder = new StringBuilder();
ArgumentAppender argument = new ArgumentAppender(builder, task.getWorkingDir());
builder.append("-javaagent:");
builder.append(agent.getJar().getAbsolutePath());
builder.append('=');
argument.append("destfile", getDestinationFile());
argument.append("append", true);
argument.append("includes", getIncludes());
argument.append("excludes", getExcludes());
argument.append("exclclassloader", getExcludeClassLoaders());
if (agent.supportsInclNoLocationClasses()) {
argument.append("inclnolocationclasses", isIncludeNoLocationClasses());
}
argument.append("sessionid", getSessionId());
argument.append("dumponexit", isDumpOnExit());
argument.append("output", getOutput().getAsArg());
argument.append("address", getAddress());
argument.append("port", getPort());
argument.append("classdumpdir", getClassDumpDir());
if (agent.supportsJmx()) {
argument.append("jmx", isJmx());
}
return builder.toString();
}
private static class ArgumentAppender {
private final StringBuilder builder;
private final File workingDirectory;
private boolean anyArgs;
public ArgumentAppender(StringBuilder builder, File workingDirectory) {
this.builder = builder;
this.workingDirectory = workingDirectory;
}
public void append(String name, @Nullable Object value) {
if (value != null
&& !((value instanceof Collection) && ((Collection) value).isEmpty())
&& !((value instanceof String) && (StringUtils.isEmpty((String) value)))
&& !((value instanceof Integer) && ((Integer) value == 0))) {
if (anyArgs) {
builder.append(',');
}
builder.append(name).append('=');
if (value instanceof Collection) {
builder.append(Joiner.on(':').join((Collection<?>) value));
} else if (value instanceof File) {
builder.append(RelativePathUtil.relativePath(workingDirectory, (File) value));
} else {
builder.append(value);
}
anyArgs = true;
}
}
}
}