Skip to content

Commit ea988ae

Browse files
committed
Change the usage of running without normal compilation and fix it in multi-module projects with inner dependencies
1 parent 57e6d27 commit ea988ae

File tree

18 files changed

+238
-102
lines changed

18 files changed

+238
-102
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Available tasks
3030
In order to check coverage of aggregated reports one should use `gradle checkScoverage aggregateScoverage`.
3131

3232
Configuration
33-
---------------
33+
-------------
3434

3535
The plugin exposes multiple options that can be configured by setting them in an `scoverage` block within the project's
3636
build script. These options are as follows:
@@ -58,9 +58,13 @@ required for the validation to pass (otherwise `checkScoverage` will fail the bu
5858
`checkScoverage` task. For more information on the different types, please refer to the documentation of the scalac
5959
plugin (https://github.com/scoverage/scalac-scoverage-plugin).
6060

61-
* `runNormalCompilation = <boolean>` (default `true`): Determines whether both normal scalac compilation (`compileScala`)
62-
and compilation with scoverage (`compileScoverageScala`) should be executed, or if only the scoverage compilation should.
63-
It may be helpful to turn this off so that only the scoverage instrumented classes -- which are not intended for release
64-
-- will be created, thus reducing the build time.
61+
Run without normal compilation
62+
------------------------------
6563

64+
By default, running any of the plugin tasks will compile the code both using "normal" compilation (`compileScala`)
65+
and using the scoverage scalac plugin (`compileScoverageScala`).
6666

67+
In cases where you only wish to generate reports / validate coverage, but are not interested in publishing the code,
68+
it is possible to only compile the code with the scoverage scalac plugin, thus reducing build times significantly.
69+
In order to do so, simply add the arguments `-x compileScala` to the gradle execution.
70+
For example: `gradle reportScoverage -x compileScala`.

src/functionalTest/java/org.scoverage/ScalaJavaMultiModuleTest.java

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

3+
import org.gradle.testkit.runner.TaskOutcome;
34
import org.junit.Assert;
45
import org.junit.Test;
56

@@ -17,14 +18,16 @@ public void checkAndAggregateScoverage() throws Exception {
1718
AssertableBuildResult result = run("clean", ScoveragePlugin.getCHECK_NAME(),
1819
ScoveragePlugin.getAGGREGATE_NAME());
1920

21+
result.assertTaskOutcome("java_only:" + ScoveragePlugin.getCOMPILE_NAME(), TaskOutcome.NO_SOURCE);
22+
2023
result.assertTaskSkipped(ScoveragePlugin.getREPORT_NAME());
2124
result.assertTaskSucceeded("scala_only:" + ScoveragePlugin.getREPORT_NAME());
2225
result.assertTaskSucceeded("mixed_scala_java:" + ScoveragePlugin.getREPORT_NAME());
23-
result.assertTaskDoesntExist("java_only:" + ScoveragePlugin.getREPORT_NAME());
26+
result.assertTaskSkipped("java_only:" + ScoveragePlugin.getREPORT_NAME());
2427
result.assertTaskSucceeded(ScoveragePlugin.getCHECK_NAME());
2528
result.assertTaskSucceeded("scala_only:" + ScoveragePlugin.getCHECK_NAME());
2629
result.assertTaskSucceeded("mixed_scala_java:" + ScoveragePlugin.getCHECK_NAME());
27-
result.assertTaskDoesntExist("java_only:" + ScoveragePlugin.getCHECK_NAME());
30+
result.assertTaskSkipped("java_only:" + ScoveragePlugin.getCHECK_NAME());
2831
result.assertTaskSucceeded(ScoveragePlugin.getAGGREGATE_NAME());
2932

3033
assertAllReportFilesExist();

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

Lines changed: 84 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public void reportScoverage() {
1919
result.assertTaskExists(ScoveragePlugin.getREPORT_NAME());
2020
result.assertTaskExists("a:" + ScoveragePlugin.getREPORT_NAME());
2121
result.assertTaskExists("b:" + ScoveragePlugin.getREPORT_NAME());
22+
result.assertTaskExists("common:" + ScoveragePlugin.getREPORT_NAME());
2223
}
2324

2425
@Test
@@ -29,6 +30,7 @@ public void reportScoverageOnlyRoot() {
2930
result.assertTaskExists(ScoveragePlugin.getREPORT_NAME());
3031
result.assertTaskDoesntExist("a:" + ScoveragePlugin.getREPORT_NAME());
3132
result.assertTaskDoesntExist("b:" + ScoveragePlugin.getREPORT_NAME());
33+
result.assertTaskDoesntExist("common:" + ScoveragePlugin.getREPORT_NAME());
3234
}
3335

3436
@Test
@@ -39,6 +41,7 @@ public void reportScoverageOnlyA() {
3941
result.assertTaskDoesntExist(ScoveragePlugin.getREPORT_NAME());
4042
result.assertTaskExists("a:" + ScoveragePlugin.getREPORT_NAME());
4143
result.assertTaskDoesntExist("b:" + ScoveragePlugin.getREPORT_NAME());
44+
result.assertTaskDoesntExist("common:" + ScoveragePlugin.getREPORT_NAME());
4245
}
4346

4447
@Test
@@ -50,6 +53,7 @@ public void aggregateScoverage() {
5053
result.assertTaskExists("a:" + ScoveragePlugin.getREPORT_NAME());
5154
result.assertTaskExists("b:" + ScoveragePlugin.getREPORT_NAME());
5255
result.assertTaskExists(ScoveragePlugin.getAGGREGATE_NAME());
56+
result.assertTaskExists("common:" + ScoveragePlugin.getREPORT_NAME());
5357
}
5458

5559
@Test
@@ -60,9 +64,11 @@ public void checkScoverage() {
6064
result.assertTaskExists(ScoveragePlugin.getREPORT_NAME());
6165
result.assertTaskExists("a:" + ScoveragePlugin.getREPORT_NAME());
6266
result.assertTaskExists("b:" + ScoveragePlugin.getREPORT_NAME());
67+
result.assertTaskExists("common:" + ScoveragePlugin.getREPORT_NAME());
6368
result.assertTaskExists(ScoveragePlugin.getCHECK_NAME());
6469
result.assertTaskExists("a:" + ScoveragePlugin.getCHECK_NAME());
6570
result.assertTaskExists("b:" + ScoveragePlugin.getCHECK_NAME());
71+
result.assertTaskExists("common:" + ScoveragePlugin.getCHECK_NAME());
6672
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());
6773
}
6874

@@ -88,9 +94,11 @@ public void checkScoverageOnlyA() {
8894
result.assertTaskDoesntExist(ScoveragePlugin.getREPORT_NAME());
8995
result.assertTaskExists("a:" + ScoveragePlugin.getREPORT_NAME());
9096
result.assertTaskDoesntExist("b:" + ScoveragePlugin.getREPORT_NAME());
97+
result.assertTaskDoesntExist("common:" + ScoveragePlugin.getREPORT_NAME());
9198
result.assertTaskDoesntExist(ScoveragePlugin.getCHECK_NAME());
9299
result.assertTaskExists("a:" + ScoveragePlugin.getCHECK_NAME());
93100
result.assertTaskDoesntExist("b:" + ScoveragePlugin.getCHECK_NAME());
101+
result.assertTaskDoesntExist("common:" + ScoveragePlugin.getCHECK_NAME());
94102
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());
95103
}
96104

@@ -103,9 +111,11 @@ public void checkAndAggregateScoverage() throws Exception {
103111
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
104112
result.assertTaskSucceeded("a:" + ScoveragePlugin.getREPORT_NAME());
105113
result.assertTaskSucceeded("b:" + ScoveragePlugin.getREPORT_NAME());
114+
result.assertTaskSucceeded("common:" + ScoveragePlugin.getREPORT_NAME());
106115
result.assertTaskSucceeded(ScoveragePlugin.getCHECK_NAME());
107116
result.assertTaskSucceeded("a:" + ScoveragePlugin.getCHECK_NAME());
108117
result.assertTaskSucceeded("b:" + ScoveragePlugin.getCHECK_NAME());
118+
result.assertTaskSucceeded("common:" + ScoveragePlugin.getCHECK_NAME());
109119
result.assertTaskSucceeded(ScoveragePlugin.getAGGREGATE_NAME());
110120

111121
assertAllReportFilesExist();
@@ -119,7 +129,8 @@ public void checkScoverageWithoutCoverageInRoot() throws Exception {
119129
"test",
120130
"--tests", "org.hello.TestNothingSuite",
121131
"--tests", "org.hello.a.WorldASuite",
122-
"--tests", "org.hello.b.WorldBSuite");
132+
"--tests", "org.hello.b.WorldBSuite",
133+
"--tests", "org.hello.common.WorldCommonSuite");
123134

124135
result.assertTaskFailed(ScoveragePlugin.getCHECK_NAME());
125136

@@ -134,14 +145,30 @@ public void checkScoverageWithoutCoverageInA() throws Exception {
134145
"test",
135146
"--tests", "org.hello.a.TestNothingASuite",
136147
"--tests", "org.hello.WorldSuite",
137-
"--tests", "org.hello.b.WorldBSuite");
148+
"--tests", "org.hello.b.WorldBSuite",
149+
"--tests", "org.hello.common.WorldCommonSuite");
138150

139151
result.assertTaskFailed("a:" + ScoveragePlugin.getCHECK_NAME());
140152

141153
assertAReportFilesExist();
142154
assertCoverage(0.0, reportDir(projectDir().toPath().resolve("a").toFile()));
143155
}
144156

157+
@Test
158+
public void checkScoverageWithoutNormalCompilationAndWithoutCoverageInCommon() throws Exception {
159+
160+
AssertableBuildResult result = runAndFail("clean",
161+
":a:test",
162+
":common:test", "--tests", "org.hello.common.TestNothingCommonSuite",
163+
"-x", "compileScala",
164+
ScoveragePlugin.getCHECK_NAME());
165+
166+
result.assertTaskFailed("common:" + ScoveragePlugin.getCHECK_NAME());
167+
168+
assertCommonReportFilesExist();
169+
assertCoverage(0.0, reportDir(projectDir().toPath().resolve("common").toFile()));
170+
}
171+
145172
@Test
146173
public void checkAndAggregateScoverageWithoutCoverageInRoot() throws Exception {
147174

@@ -151,18 +178,21 @@ public void checkAndAggregateScoverageWithoutCoverageInRoot() throws Exception {
151178
ScoveragePlugin.getAGGREGATE_NAME(), "test",
152179
"--tests", "org.hello.TestNothingSuite",
153180
"--tests", "org.hello.a.WorldASuite",
154-
"--tests", "org.hello.b.WorldBSuite");
181+
"--tests", "org.hello.b.WorldBSuite",
182+
"--tests", "org.hello.common.WorldCommonSuite");
155183

156184
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
157185
result.assertTaskSucceeded("a:" + ScoveragePlugin.getREPORT_NAME());
158186
result.assertTaskSucceeded("b:" + ScoveragePlugin.getREPORT_NAME());
187+
result.assertTaskSucceeded("common:" + ScoveragePlugin.getREPORT_NAME());
159188
result.assertTaskSucceeded(ScoveragePlugin.getCHECK_NAME());
160189
result.assertTaskSucceeded("a:" + ScoveragePlugin.getCHECK_NAME());
161190
result.assertTaskSucceeded("b:" + ScoveragePlugin.getCHECK_NAME());
191+
result.assertTaskSucceeded("common:" + ScoveragePlugin.getCHECK_NAME());
162192
result.assertTaskSucceeded(ScoveragePlugin.getAGGREGATE_NAME());
163193

164194
assertAllReportFilesExist();
165-
assertCoverage(66.6);
195+
assertCoverage(87.5);
166196
}
167197

168198
@Test
@@ -172,31 +202,67 @@ public void checkAndAggregateScoverageWithoutCoverageInAll() throws Exception {
172202
ScoveragePlugin.getAGGREGATE_NAME(), "test",
173203
"--tests", "org.hello.TestNothingSuite",
174204
"--tests", "org.hello.a.TestNothingASuite",
175-
"--tests", "org.hello.b.TestNothingBSuite");
205+
"--tests", "org.hello.b.TestNothingBSuite",
206+
"--tests", "org.hello.common.TestNothingCommonSuite");
176207

177208
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
178209
result.assertTaskSucceeded("a:" + ScoveragePlugin.getREPORT_NAME());
179210
result.assertTaskSucceeded("b:" + ScoveragePlugin.getREPORT_NAME());
211+
result.assertTaskSucceeded("common:" + ScoveragePlugin.getREPORT_NAME());
180212
result.assertTaskSucceeded(ScoveragePlugin.getAGGREGATE_NAME());
181213
result.assertTaskFailed(ScoveragePlugin.getCHECK_NAME());
182214

183215
assertAllReportFilesExist();
184216
assertCoverage(0.0);
185217
}
186218

219+
@Test
220+
public void aggregateScoverageWithoutNormalCompilation() throws Exception {
221+
222+
AssertableBuildResult result = run("clean", ScoveragePlugin.getAGGREGATE_NAME(),
223+
"-x", "compileScala");
224+
225+
result.assertTaskSkipped("compileScala");
226+
result.assertTaskSkipped("a:compileScala");
227+
result.assertTaskSkipped("b:compileScala");
228+
result.assertTaskSkipped("common:compileScala");
229+
result.assertTaskSucceeded(ScoveragePlugin.getCOMPILE_NAME());
230+
result.assertTaskSucceeded("a:" + ScoveragePlugin.getCOMPILE_NAME());
231+
result.assertTaskSucceeded("b:" + ScoveragePlugin.getCOMPILE_NAME());
232+
result.assertTaskSucceeded("common:" + ScoveragePlugin.getCOMPILE_NAME());
233+
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
234+
result.assertTaskSucceeded("a:" + ScoveragePlugin.getREPORT_NAME());
235+
result.assertTaskSucceeded("b:" + ScoveragePlugin.getREPORT_NAME());
236+
result.assertTaskSucceeded("common:" + ScoveragePlugin.getREPORT_NAME());
237+
result.assertTaskSucceeded(ScoveragePlugin.getAGGREGATE_NAME());
238+
239+
assertAllReportFilesExist();
240+
assertCoverage(100.0);
241+
242+
Assert.assertTrue(resolve(buildDir(resolve(projectDir(), "a")), "classes/scala/main/org/hello/a/WorldA.class").exists());
243+
Assert.assertFalse(resolve(buildDir(resolve(projectDir(), "a")), "classes/scala/scoverage/org/hello/a/WorldA.class").exists());
244+
245+
Assert.assertTrue(resolve(buildDir(resolve(projectDir(), "b")), "classes/scala/main/org/hello/b/WorldB.class").exists());
246+
Assert.assertFalse(resolve(buildDir(resolve(projectDir(), "b")), "classes/scala/scoverage/org/hello/b/WorldB.class").exists());
247+
248+
Assert.assertTrue(resolve(buildDir(resolve(projectDir(), "common")), "classes/scala/main/org/hello/common/WorldCommon.class").exists());
249+
Assert.assertFalse(resolve(buildDir(resolve(projectDir(), "common")), "classes/scala/scoverage/org/hello/common/WorldCommon.class").exists());
250+
}
251+
187252
private void assertAllReportFilesExist() {
188253

189254
assertRootReportFilesExist();
190255
assertAReportFilesExist();
191256
assertBReportFilesExist();
257+
assertCommonReportFilesExist();
192258
assertAggregationFilesExist();
193259
}
194260

195261
private void assertAggregationFilesExist() {
196262

197-
assertRootReportFilesExist();
198263
Assert.assertTrue(resolve(reportDir(), "a/src/main/scala/org/hello/a/WorldA.scala.html").exists());
199264
Assert.assertTrue(resolve(reportDir(), "b/src/main/scala/org/hello/b/WorldB.scala.html").exists());
265+
Assert.assertTrue(resolve(reportDir(), "common/src/main/scala/org/hello/common/WorldCommon.scala.html").exists());
200266
}
201267

202268
private void assertRootReportFilesExist() {
@@ -207,16 +273,22 @@ private void assertRootReportFilesExist() {
207273

208274
private void assertAReportFilesExist() {
209275

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());
276+
File reportDir = reportDir(projectDir().toPath().resolve("a").toFile());
277+
Assert.assertTrue(resolve(reportDir, "index.html").exists());
278+
Assert.assertTrue(resolve(reportDir, "src/main/scala/org/hello/a/WorldA.scala.html").exists());
213279
}
214280

215281
private void assertBReportFilesExist() {
216282

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());
283+
File reportDir = reportDir(projectDir().toPath().resolve("b").toFile());
284+
Assert.assertTrue(resolve(reportDir, "index.html").exists());
285+
Assert.assertTrue(resolve(reportDir, "src/main/scala/org/hello/b/WorldB.scala.html").exists());
286+
}
287+
288+
private void assertCommonReportFilesExist() {
220289

290+
File reportDir = reportDir(projectDir().toPath().resolve("common").toFile());
291+
Assert.assertTrue(resolve(reportDir, "index.html").exists());
292+
Assert.assertTrue(resolve(reportDir, "src/main/scala/org/hello/common/WorldCommon.scala.html").exists());
221293
}
222294
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void reportScoverageWithExcludedClasses() throws Exception {
103103
public void reportScoverageWithoutNormalCompilation() throws Exception {
104104

105105
AssertableBuildResult result = run("clean", ScoveragePlugin.getREPORT_NAME(),
106-
"-PrunNormalCompilation=false");
106+
"-x", "compileScala");
107107

108108
result.assertTaskSkipped("compileScala");
109109
result.assertTaskSucceeded(ScoveragePlugin.getCOMPILE_NAME());
@@ -122,7 +122,7 @@ public void reportScoverageWithoutNormalCompilation() throws Exception {
122122
public void reportScoverageWithoutNormalCompilationAndWithExcludedClasses() throws Exception {
123123

124124
AssertableBuildResult result = run("clean", ScoveragePlugin.getREPORT_NAME(),
125-
"-PrunNormalCompilation=false", "-PexcludedFile=.*");
125+
"-PexcludedFile=.*", "-x", "compileScala");
126126

127127
Assert.assertTrue(resolve(reportDir(), "index.html").exists());
128128
Assert.assertFalse(resolve(reportDir(), "src/main/scala/org/hello/World.scala.html").exists());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private String fullTaskName(String taskName) {
193193

194194
private boolean taskExists(String taskName) {
195195

196-
Pattern regex = Pattern.compile("^" + fullTaskName(taskName), Pattern.MULTILINE);
196+
Pattern regex = Pattern.compile("^(> Task )?" + fullTaskName(taskName), Pattern.MULTILINE);
197197
return regex.matcher(result.getOutput()).find();
198198
}
199199
}

src/functionalTest/resources/projects/scala-java-multi-module/mixed_scala_java/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ dependencies {
99

1010
testCompile group: 'org.scalatest', name: "scalatest_${scalaVersionMajor}.${scalaVersionMinor}", version: scalatestVersion
1111
}
12+
13+
// A common practice in mixed java/scala modules to make Java code able to import Scala code
14+
ext.configureSources = { set, name ->
15+
set.scala.srcDir("src/$name/java")
16+
set.java.srcDirs = []
17+
}
18+
configureSources(sourceSets.main, 'main')
19+
configureSources(sourceSets.test, 'test')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies {
2+
compile project(":common")
3+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.hello.a
22

3+
import org.hello.common.WorldCommon
4+
35
class WorldA {
46

57
def fooA(): String = {
6-
val s = "a" + "a"
8+
val s = "a" + new WorldCommon().fooCommon()
79
s
810
}
911
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies {
2+
compile project(":common")
3+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.hello.b
22

3+
import org.hello.common.WorldCommon
4+
35
class WorldB {
46

57
def fooB(): String = {
6-
val s = "b" + "b"
8+
val s = "b" + new WorldCommon().fooCommon()
79
s
810
}
911
}

src/functionalTest/resources/projects/scala-multi-module/build.gradle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,4 @@ allprojects {
3131
scoverage {
3232
minimumRate = 0.5
3333
}
34-
}
35-
36-
scoverage {
37-
minimumRate = 0.5
3834
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.hello.common
2+
3+
class WorldCommon {
4+
5+
def fooCommon(): String = {
6+
val s = "common" + "a"
7+
s
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.hello.common
2+
3+
import org.junit.runner.RunWith
4+
import org.scalatest.FunSuite
5+
import org.scalatest.junit.JUnitRunner
6+
7+
@RunWith(classOf[JUnitRunner])
8+
class TestNothingCommonSuite extends FunSuite {
9+
10+
test("nothing") {
11+
}
12+
}

0 commit comments

Comments
 (0)