Skip to content

Commit 7547f04

Browse files
committed
(#121) Add a warning when scala subprojects don't have scoverage applied to them (and the parent does)
1 parent 11d5027 commit 7547f04

File tree

11 files changed

+149
-2
lines changed

11 files changed

+149
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.scoverage;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class MultiModulePluginNotConfiguredForScalaTest extends ScoverageFunctionalTest {
7+
8+
public MultiModulePluginNotConfiguredForScalaTest() {
9+
super("multi-module-plugin-not-configured-for-scala");
10+
}
11+
12+
@Test
13+
public void checkAndAggregateScoverage() throws Exception {
14+
15+
AssertableBuildResult result = run("clean", ScoveragePlugin.getCHECK_NAME(),
16+
ScoveragePlugin.getAGGREGATE_NAME());
17+
18+
result.assertTaskSkipped(ScoveragePlugin.getREPORT_NAME());
19+
result.assertTaskSkipped("scala_only:" + ScoveragePlugin.getREPORT_NAME());
20+
result.assertTaskSkipped("java_only:" + ScoveragePlugin.getREPORT_NAME());
21+
result.assertTaskSkipped(ScoveragePlugin.getCHECK_NAME());
22+
result.assertTaskSkipped("scala_only:" + ScoveragePlugin.getCHECK_NAME());
23+
result.assertTaskSkipped("java_only:" + ScoveragePlugin.getCHECK_NAME());
24+
result.assertTaskSkipped(ScoveragePlugin.getAGGREGATE_NAME());
25+
26+
assertReportDirsEmpty();
27+
28+
Assert.assertTrue(result.getResult().getOutput().contains("Scala sub-project 'scala_only' doesn't have Scoverage applied"));
29+
Assert.assertFalse(result.getResult().getOutput().contains("Scala sub-project 'java_only' doesn't have Scoverage applied"));
30+
}
31+
32+
private void assertReportDirsEmpty() {
33+
34+
Assert.assertFalse(reportDir().exists());
35+
Assert.assertFalse(reportDir(projectDir().toPath().resolve("scala_only").toFile()).exists());
36+
Assert.assertFalse(reportDir(projectDir().toPath().resolve("java_only").toFile()).exists());
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
plugins {
2+
id 'org.scoverage' apply false
3+
}
4+
5+
description = 'a multi-module Scala and Java project that defines scoverage only on root but not on subprojects'
6+
7+
allprojects {
8+
repositories {
9+
jcenter()
10+
}
11+
}
12+
13+
subprojects { p ->
14+
if (p.name != 'dependencies') {
15+
apply plugin: 'java'
16+
dependencies {
17+
implementation platform(project(':dependencies'))
18+
testCompile group: 'org.junit.platform', name: 'junit-platform-runner'
19+
}
20+
21+
test {
22+
useJUnitPlatform()
23+
}
24+
}
25+
}
26+
27+
apply plugin: 'org.scoverage'
28+
scoverage {
29+
minimumRate = 0.5
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
plugins {
2+
id 'java-platform'
3+
}
4+
5+
dependencies {
6+
constraints {
7+
api group: 'org.scala-lang', name: 'scala-library', version: "${scalaVersionMajor}.${scalaVersionMinor}.${scalaVersionBuild}"
8+
9+
api group: 'org.junit.vintage', name: 'junit-vintage-engine', version: junitVersion
10+
api group: 'org.junit.platform', name: 'junit-platform-runner', version: junitPlatformVersion
11+
12+
api group: 'org.scalatest', name: "scalatest_${scalaVersionMajor}.${scalaVersionMinor}", version: scalatestVersion
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies {
2+
testRuntime group: 'org.junit.vintage', name: 'junit-vintage-engine'
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.hello;
2+
3+
public class WorldJavaOnly {
4+
5+
public String foo() {
6+
String s = "java_only" + "a";
7+
return s;
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.hello;
2+
3+
import org.junit.Test;
4+
5+
public class WorldJavaOnlyTest {
6+
7+
@Test
8+
public void foo() {
9+
new WorldJavaOnly().foo();
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apply plugin: 'scala'
2+
// apply plugin: 'org.scoverage' // Oops forgot to configure scoverage
3+
4+
dependencies {
5+
compile group: 'org.scala-lang', name: 'scala-library'
6+
7+
testRuntime group: 'org.junit.vintage', name: 'junit-vintage-engine'
8+
testCompile group: 'org.scalatest', name: "scalatest_${scalaVersionMajor}.${scalaVersionMinor}"
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.hello
2+
3+
class WorldScalaOnly {
4+
5+
def foo(): String = {
6+
val s = "scala_only" + "a"
7+
s
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.hello
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 WorldScalaOnlySuite extends FunSuite {
9+
10+
test("foo") {
11+
new WorldScalaOnly().foo()
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include 'dependencies', 'java_only', 'scala_only'

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.gradle.api.Project
66
import org.gradle.api.Task
77
import org.gradle.api.invocation.Gradle
88
import org.gradle.api.plugins.PluginAware
9+
import org.gradle.api.plugins.scala.ScalaPlugin
910
import org.gradle.api.tasks.SourceSet
1011
import org.gradle.api.tasks.scala.ScalaCompile
1112
import org.gradle.api.tasks.testing.Test
@@ -281,14 +282,23 @@ class ScoveragePlugin implements Plugin<PluginAware> {
281282
}
282283

283284
// define aggregation task
284-
if (project.childProjects.size() > 0) {
285+
if (!project.subprojects.empty) {
285286
project.gradle.projectsEvaluated {
286-
def allReportTasks = project.getAllprojects().findResults {
287+
project.subprojects.each {
288+
if (it.plugins.hasPlugin(ScalaPlugin) && !it.plugins.hasPlugin(ScoveragePlugin)) {
289+
it.logger.warn("Scala sub-project '${it.name}' doesn't have Scoverage applied and will be ignored in parent project aggregation")
290+
}
291+
}
292+
def childReportTasks = project.subprojects.findResults {
287293
it.tasks.find { task ->
288294
task.name == REPORT_NAME && task instanceof ScoverageAggregate
289295
}
290296
}
297+
def allReportTasks = childReportTasks + globalReportTask
291298
def aggregationTask = project.tasks.create(AGGREGATE_NAME, ScoverageAggregate) {
299+
onlyIf {
300+
!childReportTasks.empty
301+
}
292302
dependsOn(allReportTasks)
293303
group = 'verification'
294304
runner = scoverageRunner

0 commit comments

Comments
 (0)