Skip to content

Commit 0930bbd

Browse files
committed
scoverage requires a separate reporting step
1 parent c1a2313 commit 0930bbd

File tree

5 files changed

+102
-12
lines changed

5 files changed

+102
-12
lines changed

Diff for: build.gradle

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ buildscript {
77
}
88
}
99

10+
repositories {
11+
mavenCentral()
12+
}
13+
1014
ext {
1115
sonatypeUser = System.env.SONATYPE_USER
1216
sonatypePass = System.env.SONATYPE_PASS
@@ -19,9 +23,15 @@ apply plugin: 'release'
1923

2024
group 'org.scoverage'
2125

26+
configurations {
27+
scoverage
28+
compile.extendsFrom scoverage
29+
}
30+
2231
dependencies {
2332
compile gradleApi()
2433
compile localGroovy()
34+
scoverage 'org.scoverage:scalac-scoverage-plugin_2.10:0.99.5'
2535
}
2636

2737
task groovydocJar(type: Jar, dependsOn: groovydoc) {

Diff for: src/main/groovy/org/scoverage/OverallCheckTask.groovy

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class OverallCheckTask extends DefaultTask {
1313

1414
@TaskAction
1515
void requireLineCoverage() {
16-
def dataDirName = project.extensions[ScoveragePlugin.CONFIGURATION_NAME].dataDirName
16+
def extension = ScoveragePlugin.extensionIn(project)
1717

18-
if (cobertura == null) cobertura = project.file("$project.buildDir/reports/$dataDirName/cobertura.xml")
18+
if (cobertura == null) cobertura = new File(extension.dataDir, 'cobertura.xml')
1919

2020
def xml = new XmlParser().parse(cobertura)
2121
def overallLineRate = xml.attribute('line-rate').toDouble()

Diff for: src/main/groovy/org/scoverage/ScoverageExtension.groovy

+49-10
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package org.scoverage
22

33
import org.gradle.api.Action
44
import org.gradle.api.Project
5+
import org.gradle.api.artifacts.ResolvedConfiguration
56
import org.gradle.api.plugins.JavaPlugin
67
import org.gradle.api.plugins.scala.ScalaPlugin
8+
import org.gradle.api.tasks.JavaExec
79
import org.gradle.api.tasks.SourceSet
810
import org.gradle.api.tasks.testing.Test
911

@@ -14,6 +16,14 @@ import org.gradle.api.tasks.testing.Test
1416
*/
1517
class ScoverageExtension {
1618

19+
/** a directory to write working files to */
20+
File dataDir
21+
/** a directory to write final output to */
22+
File reportDir
23+
/** sources to highlight */
24+
SourceSet sourceSet
25+
26+
1727
ScoverageExtension(Project project) {
1828

1929
project.plugins.apply(JavaPlugin.class);
@@ -22,11 +32,11 @@ class ScoverageExtension {
2232

2333
project.configurations.create(ScoveragePlugin.CONFIGURATION_NAME) {
2434
visible = false
25-
transitive = false
35+
transitive = true
2636
description = 'Scoverage dependencies'
2737
}
2838

29-
project.sourceSets.create(ScoveragePlugin.CONFIGURATION_NAME) {
39+
sourceSet = project.sourceSets.create(ScoveragePlugin.CONFIGURATION_NAME) {
3040
def mainSourceSet = project.sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME)
3141

3242
java.source(mainSourceSet.java)
@@ -44,34 +54,63 @@ class ScoverageExtension {
4454
dependsOn(project.tasks[ScoveragePlugin.TEST_NAME])
4555
}
4656

57+
project.tasks.create(ScoveragePlugin.REPORT_NAME, JavaExec.class) {
58+
dependsOn(project.tasks[ScoveragePlugin.TEST_NAME])
59+
}
60+
61+
dataDir = new File(project.buildDir, 'scoverage')
62+
reportDir = new File(project.buildDir, 'reports' + File.separatorChar + 'scoverage')
63+
4764
}
4865

4966
private Action<Project> configureRuntimeOptions = new Action<Project>() {
5067

5168
@Override
5269
void execute(Project t) {
70+
71+
def extension = ScoveragePlugin.extensionIn(t)
72+
extension.dataDir.mkdirs()
73+
extension.reportDir.mkdirs()
74+
75+
ResolvedConfiguration s = t.configurations[ScoveragePlugin.CONFIGURATION_NAME].resolvedConfiguration
76+
String pluginPath = s.getFirstLevelModuleDependencies().iterator().next().moduleArtifacts.iterator().next().file.absolutePath
77+
5378
t.tasks[ScoveragePlugin.COMPILE_NAME].configure {
54-
List<String> plugin = ['-Xplugin:' + t.configurations[ScoveragePlugin.CONFIGURATION_NAME].singleFile]
79+
80+
81+
List<String> plugin = ['-Xplugin:' + pluginPath]
5582
List<String> parameters = scalaCompileOptions.additionalParameters
5683
if (parameters != null) {
5784
plugin.addAll(parameters)
5885
}
86+
plugin.add("-P:scoverage:dataDir:${extension.dataDir.absolutePath}".toString())
87+
plugin.add('-P:scoverage:excludedPackages:')
5988
scalaCompileOptions.additionalParameters = plugin
6089
// exclude the scala libraries that are added to enable scala version detection
6190
classpath += t.configurations[ScoveragePlugin.CONFIGURATION_NAME]
6291
}
63-
t.tasks[ScoveragePlugin.TEST_NAME].configure {
64-
// TODO : fix this
65-
systemProperty 'scoverage.dataDir', "${t.buildDir}/reports/${t.extensions[ScoveragePlugin.CONFIGURATION_NAME].dataDirName}"
66-
systemProperty 'scoverage.basedir', "${t.rootDir.absolutePath}" // for multi-module checking
6792

93+
t.tasks[ScoveragePlugin.TEST_NAME].configure {
6894
def existingClasspath = classpath
6995
classpath = t.files(t.sourceSets[ScoveragePlugin.CONFIGURATION_NAME].output.classesDir) +
70-
project.configurations[ScoveragePlugin.CONFIGURATION_NAME] +
71-
existingClasspath
96+
project.configurations[ScoveragePlugin.CONFIGURATION_NAME] +
97+
existingClasspath
98+
}
99+
100+
t.tasks[ScoveragePlugin.REPORT_NAME].configure {
101+
classpath = project.buildscript.configurations.classpath +
102+
project.configurations[ScoveragePlugin.CONFIGURATION_NAME]
103+
main = 'org.scoverage.ScoverageReport'
104+
args = [
105+
extension.sourceSet.allSource.iterator().next().absolutePath,
106+
extension.dataDir.absolutePath,
107+
extension.reportDir.absolutePath
108+
]
109+
inputs.dir(extension.dataDir)
110+
outputs.dir(extension.reportDir)
72111
}
112+
73113
}
74114
}
75115

76-
String dataDirName = 'scoverage'
77116
}

Diff for: src/main/groovy/org/scoverage/ScoveragePlugin.groovy

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class ScoveragePlugin implements Plugin<Project> {
77
static String CONFIGURATION_NAME = 'scoverage'
88

99
static String TEST_NAME = 'testScoverage'
10+
static String REPORT_NAME = 'reportScoverage'
1011
static String CHECK_NAME = 'checkScoverage'
1112
static String COMPILE_NAME = 'compileScoverageScala'
1213

@@ -16,4 +17,8 @@ class ScoveragePlugin implements Plugin<Project> {
1617
t.extensions.create(CONFIGURATION_NAME, ScoverageExtension, t)
1718
}
1819
}
20+
21+
protected static ScoverageExtension extensionIn(Project project) {
22+
project.extensions[CONFIGURATION_NAME]
23+
}
1924
}

Diff for: src/main/groovy/org/scoverage/ScoverageReport.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.scoverage;
2+
3+
import scala.collection.Seq;
4+
import scala.collection.Set;
5+
import scoverage.Coverage;
6+
import scoverage.IOUtils;
7+
import scoverage.report.CoberturaXmlWriter;
8+
import scoverage.report.ScoverageHtmlWriter;
9+
10+
import java.io.File;
11+
import java.util.Arrays;
12+
13+
/**
14+
* late binding of scoverage core libraries (without a dependency on groovy)
15+
*/
16+
public class ScoverageReport {
17+
18+
public static void main(String... args) {
19+
File sourceDir = new File(args[0]);
20+
File dataDir = new File(args[1]);
21+
File reportDir = new File(args[2]);
22+
23+
File coverageFile = IOUtils.coverageFile(dataDir);
24+
File[] array = IOUtils.findMeasurementFiles(dataDir);
25+
// TODO: patch scoverage core to use a consistent collection type?
26+
Seq<File> measurementFiles = scala.collection.JavaConversions.asScalaBuffer(Arrays.asList(array));
27+
28+
Coverage coverage = IOUtils.deserialize(coverageFile);
29+
30+
Set<Object> measurements = IOUtils.invoked(measurementFiles);
31+
coverage.apply(measurements);
32+
33+
new ScoverageHtmlWriter(sourceDir, reportDir).write(coverage);
34+
new CoberturaXmlWriter(sourceDir, reportDir).write(coverage);
35+
}
36+
}

0 commit comments

Comments
 (0)