Skip to content

Commit

Permalink
Merge pull request gradle#27299 Make JacocoReportAggregationPlugin
Browse files Browse the repository at this point in the history
…artifact views cc compatible

By making the given component filter specs are serializable.

Fixes gradle#26922

Co-authored-by: Rodrigo B. de Oliveira <rodrigo@gradle.com>
  • Loading branch information
bot-gradle and bamboo committed Dec 7, 2023
2 parents 19a55a7 + a850086 commit 3f58d83
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.gradle.testing.jacoco.plugins

import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import org.gradle.test.fixtures.dsl.GradleDsl
import org.gradle.test.precondition.Requires
import org.gradle.test.preconditions.IntegTestPreconditions
import spock.lang.Issue

@Requires(value = IntegTestPreconditions.NotConfigCached, reason = 'Test explicitly enables the configuration cache')
class JacocoConfigurationCacheIntegrationTest extends AbstractIntegrationSpec {

def setup() {
// run all tests with configuration caching
file('gradle.properties') << 'org.gradle.configuration-cache=true'
}

@Issue('https://github.com/gradle/gradle/issues/26922')
def 'can aggregate with `java-gradle-plugin` subproject'() {
given:
file('settings.gradle.kts') << """
include(":plugin")
dependencyResolutionManagement {
${mavenCentralRepository(GradleDsl.KOTLIN)}
}
"""
file('build.gradle.kts') << '''
plugins { id("jacoco-report-aggregation") }
reporting {
reports {
val testCodeCoverageReport by creating(JacocoCoverageReport::class) {
testType = TestSuiteType.UNIT_TEST
}
}
}
dependencies {
jacocoAggregation(project(":plugin"))
}
'''
createDir('plugin') {
file('build.gradle.kts') << '''
plugins { id("java-gradle-plugin") }
'''
}

expect:
succeeds ':testCodeCoverageReport'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.gradle.api.artifacts.ArtifactView;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.component.ComponentIdentifier;
import org.gradle.api.artifacts.component.ProjectComponentIdentifier;
import org.gradle.api.artifacts.type.ArtifactTypeDefinition;
import org.gradle.api.attributes.Category;
Expand All @@ -35,19 +36,22 @@
import org.gradle.api.plugins.jvm.JvmTestSuite;
import org.gradle.api.plugins.jvm.internal.JvmPluginServices;
import org.gradle.api.reporting.ReportingExtension;
import org.gradle.api.specs.Spec;
import org.gradle.internal.jacoco.DefaultJacocoCoverageReport;
import org.gradle.testing.base.TestSuite;
import org.gradle.testing.base.TestingExtension;
import org.gradle.testing.jacoco.tasks.JacocoReport;

import javax.inject.Inject;

import static org.gradle.api.internal.lambdas.SerializableLambdas.spec;

/**
* Adds configurations to for resolving variants containing JaCoCo code coverage results, which may span multiple subprojects. Reacts to the presence of the jvm-test-suite plugin and creates
* tasks to collect code coverage results for each named test-suite.
*
* @since 7.4
* @see <a href="https://docs.gradle.org/current/userguide/jacoco_report_aggregation_plugin.html">JaCoCo Report Aggregation Plugin reference</a>
* @since 7.4
*/
@Incubating
public abstract class JacocoReportAggregationPlugin implements Plugin<Project> {
Expand Down Expand Up @@ -83,12 +87,12 @@ public void apply(Project project) {

ArtifactView sourceDirectories = codeCoverageResultsConf.getIncoming().artifactView(view -> {
view.withVariantReselection();
view.componentFilter(id -> id instanceof ProjectComponentIdentifier);
view.componentFilter(projectComponent());
getEcosystemUtilities().configureAsSources(view);
});

ArtifactView classDirectories = codeCoverageResultsConf.getIncoming().artifactView(view -> {
view.componentFilter(id -> id instanceof ProjectComponentIdentifier);
view.componentFilter(projectComponent());
view.attributes(attributes -> {
attributes.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements.class, LibraryElements.CLASSES));
});
Expand All @@ -102,7 +106,7 @@ public void apply(Project project) {
report.getReportTask().configure(task -> {
ArtifactView executionData = codeCoverageResultsConf.getIncoming().artifactView(view -> {
view.withVariantReselection();
view.componentFilter(id -> id instanceof ProjectComponentIdentifier);
view.componentFilter(projectComponent());
view.attributes(attributes -> {
attributes.attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.class, Category.VERIFICATION));
attributes.attributeProvider(TestSuiteType.TEST_SUITE_TYPE_ATTRIBUTE, report.getTestType().map(tt -> objects.named(TestSuiteType.class, tt)));
Expand Down Expand Up @@ -131,6 +135,10 @@ public void apply(Project project) {
});
}

private static Spec<ComponentIdentifier> projectComponent() {
return spec(id -> id instanceof ProjectComponentIdentifier);
}

private void configureReportTaskInputs(JacocoReport task, ArtifactView classDirectories, ArtifactView sourceDirectories, ArtifactView executionData) {
task.getExecutionData().from(executionData.getFiles());
task.getClassDirectories().from(classDirectories.getFiles());
Expand Down

0 comments on commit 3f58d83

Please sign in to comment.