Skip to content

Commit

Permalink
Adding an option to exclude subprojects from the parent project aggre…
Browse files Browse the repository at this point in the history
…gation
  • Loading branch information
q-willboulter committed Dec 23, 2024
1 parent ffde760 commit 2a3e99b
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ plugin (https://github.com/scoverage/scalac-scoverage-plugin).

* `excludedPackages = <packages, classes and modules>` (default `not set`): Comma separated list of regexes for packages, classes and modules to exclude from coverage.

* `excludedSubProjects = <subprojects>` (default `not set`): List of Gradle subprojects to be excluded from the Scoverage parent project aggregation. Projects should be set using the standard Gradle project selector e.g. `excludedSubProjects = [ project(':parent:subproject') ]`

#### Multiple check tasks

It is possible to configure multiple checks; for instance, one check for a statement rate and another for a branch rate:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.scoverage;

import org.junit.Test;

import static org.junit.Assert.assertFalse;

public class ScalaMultiModuleWithPartialScoverageUseAndExcludedSubProjectTest extends ScoverageFunctionalTest {

public ScalaMultiModuleWithPartialScoverageUseAndExcludedSubProjectTest() {
super("scala-multi-module-with-partial-scoverage-use-and-excluded-sub-project");
}

@Test
public void reportScoverage() {

AssertableBuildResult result = dryRun("clean", ScoveragePlugin.getREPORT_NAME());

assertFalse(result.getResult().getOutput().contains("Scala sub-project 'a' doesn't have Scoverage applied and will be ignored in parent project aggregation"));
result.assertTaskExists(ScoveragePlugin.getREPORT_NAME());
result.assertTaskExists("b:" + ScoveragePlugin.getREPORT_NAME());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.hello.a

class WorldA {

def fooA(): String = {
"a"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.hello.b

class WorldB {

def fooB(): String = {
"b"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
plugins {
id 'org.scoverage' apply false
}

description = 'a multi-module Scala project that builds successfully and has modules which does not use scoverate plugin'

allprojects { p ->
repositories {
mavenCentral()
}

apply plugin: 'java'
apply plugin: 'scala'

if (p.name != 'a') {
apply plugin: 'org.scoverage'
if (project.gradle.rootProject.name == p.name) {
scoverage {
excludedSubProjects = [project(':a')]
}
}
}

dependencies {

implementation group: 'org.scala-lang', name: 'scala-library', version: "${scalaVersionMajor}.${scalaVersionMinor}.${scalaVersionBuild}"

testImplementation group: 'org.junit.platform', name: 'junit-platform-runner'

testImplementation group: 'org.scalatest', name: "scalatest_${scalaVersionMajor}.${scalaVersionMinor}"
}
}

dependencies {
implementation project(':a')
implementation project(':b')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include 'a', 'b'
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.hello

import org.hello.a.WorldA
import org.hello.a.WorldB

class World {

def foo(): String = {
WorldA.foo() + WorldB.foo()
}
}
6 changes: 6 additions & 0 deletions src/main/groovy/org/scoverage/ScoverageExtension.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class ScoverageExtension {
/** regex for each excluded file */
final ListProperty<String> excludedFiles

/** List of sub-projects excluded intentionally from aggregation */
final ListProperty<Project> excludedSubProjects

/** Options for enabling and disabling output */
final Property<Boolean> coverageOutputCobertura
final Property<Boolean> coverageOutputXML
Expand Down Expand Up @@ -74,6 +77,9 @@ class ScoverageExtension {
excludedFiles = project.objects.listProperty(String)
excludedFiles.set([])

excludedSubProjects = project.objects.listProperty(Project)
excludedSubProjects.set([])

coverageOutputCobertura = project.objects.property(Boolean)
coverageOutputCobertura.set(true)

Expand Down
6 changes: 4 additions & 2 deletions src/main/groovy/org/scoverage/ScoveragePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,14 @@ class ScoveragePlugin implements Plugin<PluginAware> {
// define aggregation task
if (!project.subprojects.empty) {
project.gradle.projectsEvaluated {
project.subprojects.each {
def excludedSubProjectPaths = project.extensions.scoverage.excludedSubProjects.get().collect { it.getPath() }
def includedSubProjects = project.subprojects.findAll { !excludedSubProjectPaths.contains(it.getPath()) }
includedSubProjects.each {
if (it.plugins.hasPlugin(ScalaPlugin) && !it.plugins.hasPlugin(ScoveragePlugin)) {
it.logger.warn("Scala sub-project '${it.name}' doesn't have Scoverage applied and will be ignored in parent project aggregation")
}
}
def childReportTasks = project.subprojects.findResults {
def childReportTasks = includedSubProjects.findResults {
it.tasks.find { task ->
task.name == REPORT_NAME && task instanceof ScoverageAggregate
}
Expand Down

0 comments on commit 2a3e99b

Please sign in to comment.