Skip to content

Commit 433b79c

Browse files
committed
Fix #112 - report on specific module in a multi-module project without normal compilation fails
1 parent 924bf49 commit 433b79c

File tree

2 files changed

+67
-39
lines changed

2 files changed

+67
-39
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,38 @@ public void reportScoverageOnlyRoot() {
3636
@Test
3737
public void reportScoverageOnlyA() {
3838

39-
AssertableBuildResult result = dryRun("clean", ":a:" + ScoveragePlugin.getREPORT_NAME());
39+
AssertableBuildResult result = run("clean", ":a:" + ScoveragePlugin.getREPORT_NAME());
4040

4141
result.assertTaskDoesntExist(ScoveragePlugin.getREPORT_NAME());
42-
result.assertTaskExists("a:" + ScoveragePlugin.getREPORT_NAME());
4342
result.assertTaskDoesntExist("b:" + ScoveragePlugin.getREPORT_NAME());
4443
result.assertTaskDoesntExist("common:" + ScoveragePlugin.getREPORT_NAME());
44+
45+
result.assertTaskSucceeded("a:" + ScoveragePlugin.getCOMPILE_NAME());
46+
result.assertTaskSucceeded("a:" + ScoveragePlugin.getREPORT_NAME());
47+
48+
assertAReportFilesExist();
49+
}
50+
51+
@Test
52+
public void reportScoverageOnlyAWithoutNormalCompilation() {
53+
54+
AssertableBuildResult result = run("clean", ":a:" + ScoveragePlugin.getREPORT_NAME(),
55+
"-x", "compileScala");
56+
57+
result.assertTaskSkipped("compileScala");
58+
result.assertTaskSkipped("a:compileScala");
59+
result.assertTaskSkipped("common:compileScala");
60+
result.assertTaskSucceeded("common:" + ScoveragePlugin.getCOMPILE_NAME());
61+
result.assertTaskSucceeded("a:" + ScoveragePlugin.getCOMPILE_NAME());
62+
result.assertTaskSucceeded("a:" + ScoveragePlugin.getREPORT_NAME());
63+
64+
assertAReportFilesExist();
65+
66+
Assert.assertTrue(resolve(buildDir(resolve(projectDir(), "a")), "classes/scala/main/org/hello/a/WorldA.class").exists());
67+
Assert.assertFalse(resolve(buildDir(resolve(projectDir(), "a")), "classes/scala/scoverage/org/hello/a/WorldA.class").exists());
68+
69+
Assert.assertTrue(resolve(buildDir(resolve(projectDir(), "common")), "classes/scala/main/org/hello/common/WorldCommon.class").exists());
70+
Assert.assertFalse(resolve(buildDir(resolve(projectDir(), "common")), "classes/scala/scoverage/org/hello/common/WorldCommon.class").exists());
4571
}
4672

4773
@Test

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

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -266,51 +266,53 @@ class ScoveragePlugin implements Plugin<PluginAware> {
266266
}
267267
}
268268
}
269+
}
269270

270-
compileTask.configure {
271-
if (!graph.hasTask(originalCompileTask)) {
272-
destinationDir = originalCompileTask.destinationDir
273-
} else {
274-
doFirst {
275-
destinationDir.deleteDir()
276-
}
271+
compileTask.configure {
272+
if (!graph.hasTask(originalCompileTask)) {
273+
project.logger.info("Making scoverage compilation the primary compilation task (instead of compileScala)")
274+
destinationDir = originalCompileTask.destinationDir
275+
} else {
276+
doFirst {
277+
destinationDir.deleteDir()
278+
}
277279

278-
// delete non-instrumented classes by comparing normally compiled classes to those compiled with scoverage
279-
doLast {
280-
def originalCompileTaskName = project.sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME)
281-
.getCompileTaskName("scala")
282-
def originalDestinationDir = project.tasks[originalCompileTaskName].destinationDir
283-
284-
def findFiles = { File dir, Closure<Boolean> condition = null ->
285-
def files = []
286-
287-
if (dir.exists()) {
288-
dir.eachFileRecurse(FILES) { f ->
289-
if (condition == null || condition(f)) {
290-
def relativePath = dir.relativePath(f)
291-
files << relativePath
292-
}
280+
// delete non-instrumented classes by comparing normally compiled classes to those compiled with scoverage
281+
doLast {
282+
project.logger.info("Deleting classes compiled by scoverage but non-instrumented (identical to normal compilation)")
283+
def originalCompileTaskName = project.sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME)
284+
.getCompileTaskName("scala")
285+
def originalDestinationDir = project.tasks[originalCompileTaskName].destinationDir
286+
287+
def findFiles = { File dir, Closure<Boolean> condition = null ->
288+
def files = []
289+
290+
if (dir.exists()) {
291+
dir.eachFileRecurse(FILES) { f ->
292+
if (condition == null || condition(f)) {
293+
def relativePath = dir.relativePath(f)
294+
files << relativePath
293295
}
294296
}
295-
296-
files
297297
}
298298

299-
def isSameFile = { String relativePath ->
300-
def fileA = new File(originalDestinationDir, relativePath)
301-
def fileB = new File(destinationDir, relativePath)
302-
FileUtils.contentEquals(fileA, fileB)
303-
}
299+
files
300+
}
304301

305-
def originalClasses = findFiles(originalDestinationDir)
306-
def identicalInstrumentedClasses = findFiles(destinationDir, { f ->
307-
def relativePath = destinationDir.relativePath(f)
308-
originalClasses.contains(relativePath) && isSameFile(relativePath)
309-
})
302+
def isSameFile = { String relativePath ->
303+
def fileA = new File(originalDestinationDir, relativePath)
304+
def fileB = new File(destinationDir, relativePath)
305+
FileUtils.contentEquals(fileA, fileB)
306+
}
310307

311-
identicalInstrumentedClasses.each { f ->
312-
Files.deleteIfExists(destinationDir.toPath().resolve(f))
313-
}
308+
def originalClasses = findFiles(originalDestinationDir)
309+
def identicalInstrumentedClasses = findFiles(destinationDir, { f ->
310+
def relativePath = destinationDir.relativePath(f)
311+
originalClasses.contains(relativePath) && isSameFile(relativePath)
312+
})
313+
314+
identicalInstrumentedClasses.each { f ->
315+
Files.deleteIfExists(destinationDir.toPath().resolve(f))
314316
}
315317
}
316318
}

0 commit comments

Comments
 (0)