Skip to content

Commit 8779b73

Browse files
committed
Change running without normal compilation to a property so its configuration could be made only when the mode is enabled
1 parent e9a93b4 commit 8779b73

File tree

6 files changed

+103
-84
lines changed

6 files changed

+103
-84
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,22 @@ and using the scoverage scalac plugin (`compileScoverageScala`).
112112

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

118+
Note that this mode is incompatible with parallel builds in multi-module projects.
118119

119120
### Compatibility with Consistent Versions Plugin
120121

121122
In order for the plugin to work alongside [Palantir's consistent versions plugin](https://github.com/palantir/gradle-consistent-versions),
122123
the Scala version must be manually configured (via `scoverageScalaVersion`); otherwise, the plugin will attempt to
123124
resolve the compilation classpath, which is prohibited by the versions plugin.
124125

126+
Migration to 6.1.1
127+
----------------
128+
129+
* Running without normal compilation is now made with `-PscoverageCompileOnly` instead of `-x compileScala`.
130+
125131
Migration to 5.x
126132
----------------
127133

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

+14-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ public void reportScoverage() {
2222
result.assertTaskExists("common:" + ScoveragePlugin.getREPORT_NAME());
2323
}
2424

25+
@Test
26+
public void reportScoverageParallel() {
27+
28+
AssertableBuildResult result = dryRun("clean", ScoveragePlugin.getREPORT_NAME(), "--parallel");
29+
30+
result.assertTaskExists(ScoveragePlugin.getREPORT_NAME());
31+
result.assertTaskExists("a:" + ScoveragePlugin.getREPORT_NAME());
32+
result.assertTaskExists("b:" + ScoveragePlugin.getREPORT_NAME());
33+
result.assertTaskExists("common:" + ScoveragePlugin.getREPORT_NAME());
34+
}
35+
2536
@Test
2637
public void reportScoverageOnlyRoot() {
2738

@@ -52,7 +63,7 @@ public void reportScoverageOnlyA() {
5263
public void reportScoverageOnlyAWithoutNormalCompilation() {
5364

5465
AssertableBuildResult result = run("clean", ":a:" + ScoveragePlugin.getREPORT_NAME(),
55-
"-x", "compileScala");
66+
"-P" + ScoveragePlugin.getSCOVERAGE_COMPILE_ONLY_PROPERTY());
5667

5768
result.assertTaskSkipped("compileScala");
5869
result.assertTaskSkipped("a:compileScala");
@@ -186,7 +197,7 @@ public void checkScoverageWithoutNormalCompilationAndWithoutCoverageInCommon() t
186197
AssertableBuildResult result = runAndFail("clean",
187198
":a:test",
188199
":common:test", "--tests", "org.hello.common.TestNothingCommonSuite",
189-
"-x", "compileScala",
200+
"-P" + ScoveragePlugin.getSCOVERAGE_COMPILE_ONLY_PROPERTY(),
190201
ScoveragePlugin.getCHECK_NAME());
191202

192203
result.assertTaskFailed("common:" + ScoveragePlugin.getCHECK_NAME());
@@ -246,7 +257,7 @@ public void checkAndAggregateScoverageWithoutCoverageInAll() throws Exception {
246257
public void aggregateScoverageWithoutNormalCompilation() throws Exception {
247258

248259
AssertableBuildResult result = run("clean", ScoveragePlugin.getAGGREGATE_NAME(),
249-
"-x", "compileScala");
260+
"-P" + ScoveragePlugin.getSCOVERAGE_COMPILE_ONLY_PROPERTY());
250261

251262
result.assertTaskSkipped("compileScala");
252263
result.assertTaskSkipped("a:compileScala");

src/functionalTest/java/org/scoverage/ScalaMultiModuleWithMultipleTestTasksTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public void checkScoverageWithoutNormalCompilationAndWithoutCoverageInCommon() t
193193
AssertableBuildResult result = runAndFail("clean",
194194
":a:test",
195195
":common:test", "--tests", "org.hello.common.TestNothingCommonSuite",
196-
"-x", "compileScala",
196+
"-P" + ScoveragePlugin.getSCOVERAGE_COMPILE_ONLY_PROPERTY(),
197197
ScoveragePlugin.getCHECK_NAME());
198198

199199
result.assertTaskFailed("common:" + ScoveragePlugin.getCHECK_NAME());
@@ -254,7 +254,7 @@ public void checkAndAggregateScoverageWithoutCoverageInAll() throws Exception {
254254
public void aggregateScoverageWithoutNormalCompilation() throws Exception {
255255

256256
AssertableBuildResult result = run("clean", ScoveragePlugin.getAGGREGATE_NAME(),
257-
"-x", "compileScala");
257+
"-P" + ScoveragePlugin.getSCOVERAGE_COMPILE_ONLY_PROPERTY());
258258

259259
result.assertTaskSkipped("compileScala");
260260
result.assertTaskSkipped("a:compileScala");

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

+3-3
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-
"-x", "compileScala");
106+
"-P" + ScoveragePlugin.getSCOVERAGE_COMPILE_ONLY_PROPERTY());
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-
"-PexcludedFile=.*", "-x", "compileScala");
125+
"-PexcludedFile=.*", "-P" + ScoveragePlugin.getSCOVERAGE_COMPILE_ONLY_PROPERTY());
126126

127127
Assert.assertTrue(resolve(reportDir(), "index.html").exists());
128128
Assert.assertFalse(resolve(reportDir(), "src/main/scala/org/hello/World.scala.html").exists());
@@ -138,4 +138,4 @@ private void assertReportFilesExist() {
138138
Assert.assertTrue(resolve(reportDir(), "index.html").exists());
139139
Assert.assertTrue(resolve(reportDir(), "src/main/scala/org/hello/World.scala.html").exists());
140140
}
141-
}
141+
}

src/functionalTest/java/org/scoverage/ScalaSingleModuleWithMultipleTestTasksTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void reportScoverageWithExcludedClasses() throws Exception {
121121
public void reportScoverageWithoutNormalCompilation() throws Exception {
122122

123123
AssertableBuildResult result = run("clean", ScoveragePlugin.getREPORT_NAME(),
124-
"-x", "compileScala");
124+
"-P" + ScoveragePlugin.getSCOVERAGE_COMPILE_ONLY_PROPERTY());
125125

126126
result.assertTaskSkipped("compileScala");
127127
result.assertTaskSucceeded(ScoveragePlugin.getCOMPILE_NAME());
@@ -140,7 +140,7 @@ public void reportScoverageWithoutNormalCompilation() throws Exception {
140140
public void reportScoverageWithoutNormalCompilationAndWithExcludedClasses() throws Exception {
141141

142142
AssertableBuildResult result = run("clean", ScoveragePlugin.getREPORT_NAME(),
143-
"-PexcludedFile=.*", "-x", "compileScala");
143+
"-PexcludedFile=.*", "-P" + ScoveragePlugin.getSCOVERAGE_COMPILE_ONLY_PROPERTY());
144144

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

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

+74-72
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ScoveragePlugin implements Plugin<PluginAware> {
2525
static final String COMPILE_NAME = 'compileScoverageScala'
2626
static final String AGGREGATE_NAME = 'aggregateScoverage'
2727
static final String DEFAULT_SCALA_VERSION = '2.13.6'
28+
static final String SCOVERAGE_COMPILE_ONLY_PROPERTY = 'scoverageCompileOnly';
2829

2930
static final String DEFAULT_REPORT_DIR = 'reports' + File.separatorChar + 'scoverage'
3031

@@ -97,7 +98,6 @@ class ScoveragePlugin implements Plugin<PluginAware> {
9798

9899
def compileTask = project.tasks[instrumentedSourceSet.getCompileTaskName("scala")]
99100
compileTask.mustRunAfter(originalCompileTask)
100-
originalJarTask.mustRunAfter(compileTask)
101101

102102
def globalReportTask = project.tasks.register(REPORT_NAME, ScoverageAggregate)
103103
def globalCheckTask = project.tasks.register(CHECK_NAME)
@@ -154,24 +154,6 @@ class ScoveragePlugin implements Plugin<PluginAware> {
154154

155155
configureCheckTask(project, extension, globalCheckTask, globalReportTask)
156156

157-
// make this project's scoverage compilation depend on scoverage compilation of any other project
158-
// which this project depends on its normal compilation
159-
// (essential when running without normal compilation on multi-module projects with inner dependencies)
160-
def originalCompilationDependencies = recursiveDependenciesOf(compileTask).findAll {
161-
it instanceof ScalaCompile
162-
}
163-
originalCompilationDependencies.each {
164-
def dependencyProjectCompileTask = it.project.tasks.findByName(COMPILE_NAME)
165-
def dependencyProjectReportTask = it.project.tasks.findByName(REPORT_NAME)
166-
if (dependencyProjectCompileTask != null) {
167-
compileTask.dependsOn(dependencyProjectCompileTask)
168-
// we don't want this project's tests to affect the other project's report
169-
testTasks.each {
170-
it.mustRunAfter(dependencyProjectReportTask)
171-
}
172-
}
173-
}
174-
175157
compileTask.configure {
176158
List<String> parameters = []
177159
List<String> existingParameters = scalaCompileOptions.additionalParameters
@@ -207,6 +189,79 @@ class ScoveragePlugin implements Plugin<PluginAware> {
207189
}
208190
}
209191

192+
if (project.hasProperty(SCOVERAGE_COMPILE_ONLY_PROPERTY)) {
193+
project.logger.info("Making scoverage compilation the primary compilation task (instead of compileScala)")
194+
195+
originalCompileTask.enabled = false;
196+
compileTask.destinationDirectory = originalCompileTask.destinationDirectory
197+
originalJarTask.mustRunAfter(compileTask)
198+
199+
// make this project's scoverage compilation depend on scoverage compilation of any other project
200+
// which this project depends on its normal compilation
201+
def originalCompilationDependencies = recursiveDependenciesOf(compileTask).findAll {
202+
it instanceof ScalaCompile
203+
}
204+
originalCompilationDependencies.each {
205+
def dependencyProjectCompileTask = it.project.tasks.findByName(COMPILE_NAME)
206+
def dependencyProjectReportTask = it.project.tasks.findByName(REPORT_NAME)
207+
if (dependencyProjectCompileTask != null) {
208+
compileTask.dependsOn(dependencyProjectCompileTask)
209+
// we don't want this project's tests to affect the other project's report
210+
testTasks.each {
211+
it.mustRunAfter(dependencyProjectReportTask)
212+
}
213+
}
214+
}
215+
} else {
216+
compileTask.configure {
217+
doFirst {
218+
destinationDir.deleteDir()
219+
}
220+
221+
// delete non-instrumented classes by comparing normally compiled classes to those compiled with scoverage
222+
doLast {
223+
project.logger.info("Deleting classes compiled by scoverage but non-instrumented (identical to normal compilation)")
224+
def originalCompileTaskName = project.sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME)
225+
.getCompileTaskName("scala")
226+
def originalDestinationDirectory = project.tasks[originalCompileTaskName].destinationDirectory
227+
def originalDestinationDir = originalDestinationDirectory.get().asFile
228+
def destinationDir = destinationDirectory.get().asFile
229+
230+
231+
def findFiles = { File dir, Closure<Boolean> condition = null ->
232+
def files = []
233+
234+
if (dir.exists()) {
235+
dir.eachFileRecurse(FILES) { f ->
236+
if (condition == null || condition(f)) {
237+
def relativePath = dir.relativePath(f)
238+
files << relativePath
239+
}
240+
}
241+
}
242+
243+
files
244+
}
245+
246+
def isSameFile = { String relativePath ->
247+
def fileA = new File(originalDestinationDir, relativePath)
248+
def fileB = new File(destinationDir, relativePath)
249+
FileUtils.contentEquals(fileA, fileB)
250+
}
251+
252+
def originalClasses = findFiles(originalDestinationDir)
253+
def identicalInstrumentedClasses = findFiles(destinationDir, { f ->
254+
def relativePath = destinationDir.relativePath(f)
255+
originalClasses.contains(relativePath) && isSameFile(relativePath)
256+
})
257+
258+
identicalInstrumentedClasses.each { f ->
259+
Files.deleteIfExists(destinationDir.toPath().resolve(f))
260+
}
261+
}
262+
}
263+
}
264+
210265
project.gradle.taskGraph.whenReady { graph ->
211266
def hasAnyReportTask = reportTasks.any { graph.hasTask(it) }
212267

@@ -228,59 +283,6 @@ class ScoveragePlugin implements Plugin<PluginAware> {
228283
}
229284
}
230285
}
231-
232-
compileTask.configure {
233-
if (!graph.hasTask(originalCompileTask)) {
234-
project.logger.info("Making scoverage compilation the primary compilation task (instead of compileScala)")
235-
destinationDirectory = originalCompileTask.destinationDirectory
236-
} else {
237-
doFirst {
238-
def destinationDir = destinationDirectory.get().asFile
239-
destinationDir.deleteDir()
240-
}
241-
242-
// delete non-instrumented classes by comparing normally compiled classes to those compiled with scoverage
243-
doLast {
244-
project.logger.info("Deleting classes compiled by scoverage but non-instrumented (identical to normal compilation)")
245-
def originalCompileTaskName = project.sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME)
246-
.getCompileTaskName("scala")
247-
def originalDestinationDirectory = project.tasks[originalCompileTaskName].destinationDirectory
248-
def originalDestinationDir = originalDestinationDirectory.get().asFile
249-
def destinationDir = destinationDirectory.get().asFile
250-
251-
def findFiles = { File dir, Closure<Boolean> condition = null ->
252-
def files = []
253-
254-
if (dir.exists()) {
255-
dir.eachFileRecurse(FILES) { f ->
256-
if (condition == null || condition(f)) {
257-
def relativePath = dir.relativePath(f)
258-
files << relativePath
259-
}
260-
}
261-
}
262-
263-
files
264-
}
265-
266-
def isSameFile = { String relativePath ->
267-
def fileA = new File(originalDestinationDir, relativePath)
268-
def fileB = new File(destinationDir, relativePath)
269-
FileUtils.contentEquals(fileA, fileB)
270-
}
271-
272-
def originalClasses = findFiles(originalDestinationDir)
273-
def identicalInstrumentedClasses = findFiles(destinationDir, { f ->
274-
def relativePath = destinationDir.relativePath(f)
275-
originalClasses.contains(relativePath) && isSameFile(relativePath)
276-
})
277-
278-
identicalInstrumentedClasses.each { f ->
279-
Files.deleteIfExists(destinationDir.toPath().resolve(f))
280-
}
281-
}
282-
}
283-
}
284286
}
285287

286288
// define aggregation task

0 commit comments

Comments
 (0)