Skip to content

Commit 22be0bd

Browse files
committed
Move the coverage XML validation to the functional tests and remove the "separate tests" use-case
1 parent b2fbb84 commit 22be0bd

File tree

35 files changed

+147
-359
lines changed

35 files changed

+147
-359
lines changed

build.gradle

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,25 @@ dependencies {
5555

5656
sourceSets {
5757
functionalTest {
58-
java {
59-
srcDir file('src/functionalTest/java')
60-
}
61-
resources {
62-
srcDir file('src/functionalTest/resources')
63-
}
58+
java.srcDir file('src/functionalTest/java')
59+
resources.srcDir file('src/functionalTest/resources')
6460
compileClasspath += sourceSets.main.output + configurations.testRuntime
6561
runtimeClasspath += output + compileClasspath
6662
}
6763
}
6864

6965
task functionalTest(type: Test) {
66+
description = 'Runs the functional tests.'
67+
group = 'verification'
7068
testClassesDirs = sourceSets.functionalTest.output.classesDirs
7169
classpath = sourceSets.functionalTest.runtimeClasspath
70+
mustRunAfter test
7271
}
7372

74-
functionalTest.mustRunAfter(test)
7573
check.dependsOn functionalTest
7674

7775
gradlePlugin {
78-
testSourceSets sourceSets.test, sourceSets.functionalTest
76+
testSourceSets sourceSets.functionalTest
7977
}
8078

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

src/functionalTest/java/org.scoverage/ScalaMultiModuleTest.java

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.scoverage;
22

3+
import org.junit.Assert;
34
import org.junit.Test;
45

6+
import java.io.File;
7+
58
public class ScalaMultiModuleTest extends ScoverageFunctionalTest {
69

710
public ScalaMultiModuleTest() {
@@ -92,9 +95,10 @@ public void checkScoverageOnlyA() {
9295
}
9396

9497
@Test
95-
public void checkAndAggregateScoverage() {
98+
public void checkAndAggregateScoverage() throws Exception {
9699

97-
AssertableBuildResult result = run("clean", ScoveragePlugin.getCHECK_NAME(), ScoveragePlugin.getAGGREGATE_NAME());
100+
AssertableBuildResult result = run("clean", ScoveragePlugin.getCHECK_NAME(),
101+
ScoveragePlugin.getAGGREGATE_NAME());
98102

99103
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
100104
result.assertTaskSucceeded("a:" + ScoveragePlugin.getREPORT_NAME());
@@ -103,10 +107,13 @@ public void checkAndAggregateScoverage() {
103107
result.assertTaskSucceeded("a:" + ScoveragePlugin.getCHECK_NAME());
104108
result.assertTaskSucceeded("b:" + ScoveragePlugin.getCHECK_NAME());
105109
result.assertTaskSucceeded(ScoveragePlugin.getAGGREGATE_NAME());
110+
111+
assertAllReportFilesExist();
112+
assertCoverage(100.0);
106113
}
107114

108115
@Test
109-
public void checkScoverageWithoutCoverageInRoot() {
116+
public void checkScoverageWithoutCoverageInRoot() throws Exception {
110117

111118
AssertableBuildResult result = runAndFail("clean", ScoveragePlugin.getCHECK_NAME(),
112119
ScoveragePlugin.getTEST_NAME(),
@@ -115,10 +122,13 @@ public void checkScoverageWithoutCoverageInRoot() {
115122
"--tests", "org.hello.b.WorldBSuite");
116123

117124
result.assertTaskFailed(ScoveragePlugin.getCHECK_NAME());
125+
126+
assertRootReportFilesExist();
127+
assertCoverage(0.0);
118128
}
119129

120130
@Test
121-
public void checkScoverageWithoutCoverageInA() {
131+
public void checkScoverageWithoutCoverageInA() throws Exception {
122132

123133
AssertableBuildResult result = runAndFail("clean", ScoveragePlugin.getCHECK_NAME(),
124134
ScoveragePlugin.getTEST_NAME(),
@@ -127,10 +137,13 @@ public void checkScoverageWithoutCoverageInA() {
127137
"--tests", "org.hello.b.WorldBSuite");
128138

129139
result.assertTaskFailed("a:" + ScoveragePlugin.getCHECK_NAME());
140+
141+
assertAReportFilesExist();
142+
assertCoverage(0.0, reportDir(projectDir().toPath().resolve("a").toFile()));
130143
}
131144

132145
@Test
133-
public void checkAndAggregateScoverageWithoutCoverageInRoot() {
146+
public void checkAndAggregateScoverageWithoutCoverageInRoot() throws Exception {
134147

135148
// should pass as the check on the root is for the aggregation (which covers > 50%)
136149

@@ -147,10 +160,13 @@ public void checkAndAggregateScoverageWithoutCoverageInRoot() {
147160
result.assertTaskSucceeded("a:" + ScoveragePlugin.getCHECK_NAME());
148161
result.assertTaskSucceeded("b:" + ScoveragePlugin.getCHECK_NAME());
149162
result.assertTaskSucceeded(ScoveragePlugin.getAGGREGATE_NAME());
163+
164+
assertAllReportFilesExist();
165+
assertCoverage(66.6);
150166
}
151167

152168
@Test
153-
public void checkAndAggregateScoverageWithoutCoverageInAll() {
169+
public void checkAndAggregateScoverageWithoutCoverageInAll() throws Exception {
154170

155171
AssertableBuildResult result = runAndFail("clean", ScoveragePlugin.getCHECK_NAME(),
156172
ScoveragePlugin.getAGGREGATE_NAME(), ScoveragePlugin.getTEST_NAME(),
@@ -163,5 +179,44 @@ public void checkAndAggregateScoverageWithoutCoverageInAll() {
163179
result.assertTaskSucceeded("b:" + ScoveragePlugin.getREPORT_NAME());
164180
result.assertTaskSucceeded(ScoveragePlugin.getAGGREGATE_NAME());
165181
result.assertTaskFailed(ScoveragePlugin.getCHECK_NAME());
182+
183+
assertAllReportFilesExist();
184+
assertCoverage(0.0);
185+
}
186+
187+
private void assertAllReportFilesExist() {
188+
189+
assertRootReportFilesExist();
190+
assertAReportFilesExist();
191+
assertBReportFilesExist();
192+
assertAggregationFilesExist();
193+
}
194+
195+
private void assertAggregationFilesExist() {
196+
197+
assertRootReportFilesExist();
198+
Assert.assertTrue(resolve(reportDir(), "a/src/main/scala/org/hello/a/WorldA.scala.html").exists());
199+
Assert.assertTrue(resolve(reportDir(), "b/src/main/scala/org/hello/b/WorldB.scala.html").exists());
200+
}
201+
202+
private void assertRootReportFilesExist() {
203+
204+
Assert.assertTrue(resolve(reportDir(), "index.html").exists());
205+
Assert.assertTrue(resolve(reportDir(), "src/main/scala/org/hello/World.scala.html").exists());
206+
}
207+
208+
private void assertAReportFilesExist() {
209+
210+
File aReportDir = reportDir(projectDir().toPath().resolve("a").toFile());
211+
Assert.assertTrue(resolve(aReportDir, "index.html").exists());
212+
Assert.assertTrue(resolve(aReportDir, "src/main/scala/org/hello/a/WorldA.scala.html").exists());
213+
}
214+
215+
private void assertBReportFilesExist() {
216+
217+
File bReportDir = reportDir(projectDir().toPath().resolve("b").toFile());
218+
Assert.assertTrue(resolve(bReportDir, "index.html").exists());
219+
Assert.assertTrue(resolve(bReportDir, "src/main/scala/org/hello/b/WorldB.scala.html").exists());
220+
166221
}
167-
}
222+
}

src/functionalTest/java/org.scoverage/ScalaSingleModuleTest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.scoverage;
22

3-
import org.gradle.testkit.runner.TaskOutcome;
3+
import org.junit.Assert;
44
import org.junit.Test;
55

66
public class ScalaSingleModuleTest extends ScoverageFunctionalTest {
@@ -66,7 +66,7 @@ public void aggregateScoverage() {
6666
}
6767

6868
@Test
69-
public void checkScoverage() {
69+
public void checkScoverage() throws Exception {
7070

7171
AssertableBuildResult result = run("clean", ScoveragePlugin.getCHECK_NAME());
7272

@@ -75,10 +75,13 @@ public void checkScoverage() {
7575
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
7676
result.assertTaskSucceeded(ScoveragePlugin.getCHECK_NAME());
7777
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());
78+
79+
assertReportFilesExist();
80+
assertCoverage(100.0);
7881
}
7982

8083
@Test
81-
public void checkScoverageFails() {
84+
public void checkScoverageFails() throws Exception {
8285

8386
AssertableBuildResult result = runAndFail("clean", ScoveragePlugin.getCHECK_NAME(),
8487
ScoveragePlugin.getTEST_NAME(), "--tests", "org.hello.TestNothingSuite");
@@ -88,5 +91,14 @@ public void checkScoverageFails() {
8891
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
8992
result.assertTaskFailed(ScoveragePlugin.getCHECK_NAME());
9093
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());
94+
95+
assertReportFilesExist();
96+
assertCoverage(0.0);
97+
}
98+
99+
private void assertReportFilesExist() {
100+
101+
Assert.assertTrue(resolve(reportDir(), "index.html").exists());
102+
Assert.assertTrue(resolve(reportDir(), "src/main/scala/org/hello/World.scala.html").exists());
91103
}
92104
}

src/functionalTest/java/org.scoverage/ScoverageFunctionalTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
package org.scoverage;
22

3+
import groovy.util.Node;
4+
import groovy.util.XmlParser;
35
import org.gradle.testkit.runner.BuildResult;
46
import org.gradle.testkit.runner.BuildTask;
57
import org.gradle.testkit.runner.GradleRunner;
68
import org.gradle.testkit.runner.TaskOutcome;
79
import org.junit.Assert;
10+
import org.xml.sax.SAXException;
811

912
import java.io.File;
13+
import java.io.IOException;
14+
import java.text.NumberFormat;
15+
import java.text.ParseException;
1016
import java.util.ArrayList;
1117
import java.util.Arrays;
1218
import java.util.List;
1319
import java.util.regex.Pattern;
1420

21+
import static org.hamcrest.number.IsCloseTo.closeTo;
22+
import static org.junit.Assert.assertThat;
23+
1524
public abstract class ScoverageFunctionalTest {
1625

1726
private final String projectName;
1827
private final GradleRunner runner;
28+
private final XmlParser parser;
1929

2030
protected ScoverageFunctionalTest(String projectName) {
2131

@@ -24,13 +34,31 @@ protected ScoverageFunctionalTest(String projectName) {
2434
.withProjectDir(projectDir())
2535
.withPluginClasspath()
2636
.forwardOutput();
37+
38+
try {
39+
this.parser = new XmlParser();
40+
parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false);
41+
parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
42+
} catch (Exception e) {
43+
throw new RuntimeException(e);
44+
}
2745
}
2846

2947
protected File projectDir() {
3048

3149
return new File("src/functionalTest/resources/projects/" + projectName);
3250
}
3351

52+
protected File reportDir() {
53+
54+
return reportDir(projectDir());
55+
}
56+
57+
protected File reportDir(File projectDir) {
58+
59+
return projectDir.toPath().resolve("build").resolve(ScoveragePlugin.getDEFAULT_REPORT_DIR()).toFile();
60+
}
61+
3462
protected AssertableBuildResult run(String... arguments) {
3563

3664
configureArguments(arguments);
@@ -50,6 +78,31 @@ protected AssertableBuildResult dryRun(String... arguments) {
5078
return run(withDryArgument.toArray(new String[]{}));
5179
}
5280

81+
protected void assertCoverage(Double expected) throws Exception {
82+
83+
assertCoverage(expected, reportDir());
84+
}
85+
86+
protected void assertCoverage(Double expected, File reportDir) throws Exception {
87+
88+
assertThat(coverage(reportDir, CoverageType.Statement), closeTo(expected, 1.0));
89+
assertThat(coverage(reportDir, CoverageType.Line), closeTo(expected, 1.0));
90+
}
91+
92+
protected File resolve(File file, String relativePath) {
93+
94+
return file.toPath().resolve(relativePath).toFile();
95+
}
96+
97+
private Double coverage(File reportDir, CoverageType coverageType) throws IOException, SAXException, ParseException {
98+
99+
File reportFile = reportDir.toPath().resolve(coverageType.getFileName()).toFile();
100+
Node xml = parser.parse(reportFile);
101+
Object attribute = xml.attribute(coverageType.getParamName());
102+
double rawValue = NumberFormat.getInstance().parse(attribute.toString()).doubleValue();
103+
return coverageType.normalize(rawValue) * 100.0;
104+
}
105+
53106
private void configureArguments(String... arguments) {
54107

55108
List<String> fullArguments = new ArrayList<String>();

src/main/groovy/org/scoverage/ScoverageExtension.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class ScoverageExtension {
5858
dataDir.set(new File(project.buildDir, 'scoverage'))
5959

6060
reportDir = project.objects.property(File)
61-
reportDir.set(new File(project.buildDir, 'reports' + File.separatorChar + 'scoverage'))
61+
reportDir.set(new File(project.buildDir, ScoveragePlugin.DEFAULT_REPORT_DIR))
6262

6363
highlighting = project.objects.property(Boolean)
6464
highlighting.set(true)

src/main/groovy/org/scoverage/ScoveragePlugin.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class ScoveragePlugin implements Plugin<PluginAware> {
2121
static final String COMPILE_NAME = 'compileScoverageScala'
2222
static final String AGGREGATE_NAME = 'aggregateScoverage'
2323

24+
static final String DEFAULT_REPORT_DIR = 'reports' + File.separatorChar + 'scoverage'
25+
2426
@Override
2527
void apply(PluginAware pluginAware) {
2628
if (pluginAware instanceof Project) {

src/test/groovy/org/scoverage/AcceptanceTestUtils.groovy

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)