forked from scoverage/gradle-scoverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScoverageExtension.groovy
165 lines (139 loc) · 6.49 KB
/
ScoverageExtension.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
package org.scoverage
import org.gradle.api.Action
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.file.FileCollection
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.plugins.scala.ScalaPlugin
import org.gradle.api.tasks.JavaExec
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.testing.Test
import org.gradle.util.GFileUtils
import java.util.concurrent.Callable
/**
* Defines a new SourceSet for the code to be instrumented.
* Defines a new Test Task which executes normal tests with the instrumented classes.
* Defines a new Check Task which enforces an overall line coverage requirement.
*/
class ScoverageExtension {
private static boolean isEscaped(String argument) {
return (argument.startsWith('"') && argument.endsWith('"')) || (argument.startsWith('\'') && argument.endsWith('\''))
}
static String escape(String argument) {
if (isEscaped(argument)) {
return argument
} else {
return "\"$argument\""
}
}
/** a directory to write working files to */
File dataDir
/** a directory to write final output to */
File reportDir
/** sources to highlight */
File sources
/** range positioning for highlighting */
boolean highlighting = true
/** regex for each excluded package */
List<String> excludedPackages = []
/** regex for each excluded file */
List<String> excludedFiles = []
FileCollection pluginClasspath
/** Options for enabling and disabling output */
boolean coverageOutputCobertura = true
boolean coverageOutputXML = true
boolean coverageOutputHTML = true
boolean coverageDebug = false
ScoverageExtension(Project project) {
project.plugins.apply(JavaPlugin.class);
project.plugins.apply(ScalaPlugin.class);
project.afterEvaluate(configureRuntimeOptions)
project.configurations.create(ScoveragePlugin.CONFIGURATION_NAME) {
visible = false
transitive = true
description = 'Scoverage dependencies'
}
def mainSourceSet = project.sourceSets.create('scoverage') {
def original = project.sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME)
java.source(original.java)
resources.source(original.resources)
scala.source(original.scala)
compileClasspath += original.compileClasspath + project.configurations.scoverage
runtimeClasspath = it.output + project.configurations.runtime
}
def testSourceSet = project.sourceSets.create('testScoverage') {
def original = project.sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME)
java.source(original.java)
resources.source(original.resources)
scala.source(original.scala)
compileClasspath = mainSourceSet.output + project.configurations.testCompile
runtimeClasspath = it.output + mainSourceSet.output + project.configurations.scoverage + project.configurations.testRuntime
}
project.tasks.create(ScoveragePlugin.TEST_NAME, Test.class) {
conventionMapping.map("testClassesDir", new Callable<Object>() {
public Object call() throws Exception {
return testSourceSet.output.classesDir;
}
})
conventionMapping.map("classpath", new Callable<Object>() {
public Object call() throws Exception {
return testSourceSet.runtimeClasspath;
}
})
}
project.tasks.create(ScoveragePlugin.REPORT_NAME, ScoverageReport.class) {
dependsOn(project.tasks[ScoveragePlugin.TEST_NAME])
onlyIf { ScoveragePlugin.extensionIn(project).dataDir.list() }
}
project.tasks.create(ScoveragePlugin.CHECK_NAME, OverallCheckTask.class) {
dependsOn(project.tasks[ScoveragePlugin.REPORT_NAME])
}
sources = project.projectDir
dataDir = new File(project.buildDir, 'scoverage')
reportDir = new File(project.buildDir, 'reports' + File.separatorChar + 'scoverage')
def classLocation = ScoverageExtension.class.getProtectionDomain().getCodeSource().getLocation()
pluginClasspath = project.files(classLocation.file) + project.configurations.scoverage
}
private Action<Project> configureRuntimeOptions = new Action<Project>() {
@Override
void execute(Project t) {
def extension = ScoveragePlugin.extensionIn(t)
extension.dataDir.mkdirs()
Configuration configuration = t.configurations[ScoveragePlugin.CONFIGURATION_NAME]
File pluginFile
try {
pluginFile = configuration.filter { it.name.contains('plugin') }.iterator().next()
} catch(NoSuchElementException e) {
throw new GradleException("Could not find a plugin jar in configuration '${ScoveragePlugin.CONFIGURATION_NAME}'")
}
t.tasks[ScoveragePlugin.COMPILE_NAME].configure {
List<String> parameters = ['-Xplugin:' + pluginFile.absolutePath]
List<String> existingParameters = scalaCompileOptions.additionalParameters
if (existingParameters) {
parameters.addAll(existingParameters)
}
parameters.add("-P:scoverage:dataDir:${extension.dataDir.absolutePath}".toString())
if (extension.excludedPackages) {
parameters.add("-P:scoverage:excludedPackages:${extension.excludedPackages.join(';')}".toString())
}
if (extension.excludedFiles) {
parameters.add("-P:scoverage:excludedFiles:${extension.excludedFiles.join(';')}".toString())
}
if (extension.highlighting) {
parameters.add('-Yrangepos')
}
if (scalaCompileOptions.useAnt) {
scalaCompileOptions.additionalParameters = parameters.collect { escape(it) }
} else {
doFirst {
GFileUtils.deleteDirectory(destinationDir)
}
scalaCompileOptions.additionalParameters = parameters
}
// the compile task creates a store of measured statements
outputs.file(new File(extension.dataDir, 'scoverage.coverage.xml'))
}
}
}
}