Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
Don't configure jacoco to exclude generated files in gradle 5+ (#107)
Browse files Browse the repository at this point in the history
## Before this PR

Jacoco logic to exclude generated code from code coverage fails on Gradle 5.0+

## After this PR

We don't attempt to configure jacoco if Gradle is 5.0 or higher
  • Loading branch information
dansanduleac authored Mar 7, 2019
1 parent 6511c6a commit fd8ed30
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/main/groovy/org/inferred/gradle/ProcessorsPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.gradle.plugins.ide.idea.IdeaPlugin
import org.gradle.plugins.ide.idea.model.IdeaModel
import org.gradle.plugins.ide.idea.model.IdeaModule
import org.gradle.util.GUtil
import org.gradle.util.GradleVersion

class ProcessorsPlugin implements Plugin<Project> {

Expand Down Expand Up @@ -222,14 +223,17 @@ class ProcessorsPlugin implements Plugin<Project> {
}

private static configureJacoco(Project project) {
// JacocoReport.classDirectories was deprecated in gradle 5 and breaks with the current code
if (GradleVersion.current() >= GradleVersion.version("5.0")) {
return
}

project.tasks.withType(jacocoReportClass).all({ jacocoReportTask ->
// Use same trick as FindBugs above - assume that a class with a matching .java file is generated, and exclude
jacocoReportTask.doFirst {
def generatedSources = jacocoReportTask.classDirectories.asFileTree.filter {
it.path.endsWith '.java'
}

//
jacocoReportTask.classDirectories = jacocoReportTask.classDirectories.asFileTree.filter {
if (generatedSources.contains(it)) return false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,71 @@ class ProcessorsPluginFunctionalTest extends AbstractPluginTest {
!report.contains('covered="0"')
}

void testJacocoIntegrationDoesNotBreakInGradle5() throws IOException {
this.gradleVersion = gradleVersion
buildFile << """
repositories {
maven {
url "https://dl.bintray.com/palantir/releases"
}
}
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'org.inferred.processors'
jacocoTestReport {
reports {
xml.enabled true
html.enabled false
}
}
dependencies {
processor 'org.immutables:value:2.0.21'
testCompile "junit:junit:4.12"
}
"""

file("src/main/java/MyClass.java") << """
import org.immutables.value.Value;
@Value.Immutable
public interface MyClass {
@Value.Parameter String getValue();
class Builder extends ImmutableMyClass.Builder {}
}
"""

file('src/test/java/MyClassTest.java') << """
import org.junit.Test;
public class MyClassTest {
@Test
public void testBuilder() {
new MyClass.Builder();
}
}
"""

expect:
def result = runTasksSuccessfully("test", "jacocoTestReport", "--info", "-s")
result.task(':jacocoTestReport').outcome == TaskOutcome.SUCCESS

// Ensure generated classes not included in JaCoCo report
def report = file('build/reports/jacoco/test/jacocoTestReport.xml').text
!report.empty

and:
// TODO(dsanduleac): re-enable these if we decide to implement this logic for gradle 5+
// !report.contains('name="Immutable')
// !report.contains('covered="0"')

where:
gradleVersion << ['5.0']
}


/** See <a href="https://github.com/palantir/gradle-processors/issues/3">issue #3</a> */
void testProcessorJarsNotExported() throws IOException {
buildFile << """
Expand Down

0 comments on commit fd8ed30

Please sign in to comment.