Search in sources :

Example 1 with TestSuite

use of org.gradle.testing.base.TestSuite in project gradle by gradle.

the class JacocoReportAggregationPlugin method apply.

@Override
public void apply(Project project) {
    project.getPluginManager().apply("org.gradle.reporting-base");
    project.getPluginManager().apply("jvm-ecosystem");
    project.getPluginManager().apply("jacoco");
    Configuration jacocoAggregation = project.getConfigurations().create(JACOCO_AGGREGATION_CONFIGURATION_NAME);
    jacocoAggregation.setDescription("Collects project dependencies for purposes of JaCoCo coverage report aggregation");
    jacocoAggregation.setVisible(false);
    jacocoAggregation.setCanBeConsumed(false);
    jacocoAggregation.setCanBeResolved(false);
    ObjectFactory objects = project.getObjects();
    Configuration sourceDirectoriesConf = project.getConfigurations().create("allCodeCoverageReportSourceDirectories");
    sourceDirectoriesConf.setDescription("Supplies the source directories used to produce all aggregated JaCoCo coverage data reports");
    sourceDirectoriesConf.extendsFrom(jacocoAggregation);
    sourceDirectoriesConf.setVisible(false);
    sourceDirectoriesConf.setCanBeConsumed(false);
    sourceDirectoriesConf.setCanBeResolved(true);
    sourceDirectoriesConf.attributes(attributes -> {
        attributes.attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.class, Bundling.EXTERNAL));
        attributes.attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.class, Category.VERIFICATION));
        attributes.attribute(VerificationType.VERIFICATION_TYPE_ATTRIBUTE, objects.named(VerificationType.class, VerificationType.MAIN_SOURCES));
    });
    ArtifactView sourceDirectories = sourceDirectoriesConf.getIncoming().artifactView(view -> {
        view.componentFilter(id -> id instanceof ProjectComponentIdentifier);
        view.lenient(true);
    });
    Configuration classDirectoriesConf = project.getConfigurations().create("allCodeCoverageReportClassDirectories");
    classDirectoriesConf.extendsFrom(jacocoAggregation);
    classDirectoriesConf.setDescription("Supplies the class directories used to produce all aggregated JaCoCo coverage data reports");
    classDirectoriesConf.setVisible(false);
    classDirectoriesConf.setCanBeConsumed(false);
    classDirectoriesConf.setCanBeResolved(true);
    ArtifactView classDirectories = classDirectoriesConf.getIncoming().artifactView(view -> {
        view.componentFilter(id -> id instanceof ProjectComponentIdentifier);
    });
    ReportingExtension reporting = project.getExtensions().getByType(ReportingExtension.class);
    reporting.getReports().registerBinding(JacocoCoverageReport.class, DefaultJacocoCoverageReport.class);
    // iterate and configure each user-specified report, creating a <reportName>ExecutionData configuration for each
    reporting.getReports().withType(JacocoCoverageReport.class).all(report -> {
        // A resolvable configuration to collect JaCoCo coverage data; typically named "testCodeCoverageReportExecutionData"
        Configuration executionDataConf = project.getConfigurations().create(report.getName() + "ExecutionData");
        executionDataConf.extendsFrom(jacocoAggregation);
        executionDataConf.setDescription(String.format("Supplies JaCoCo coverage data to the %s.  External library dependencies may appear as resolution failures, but this is expected behavior.", report.getName()));
        executionDataConf.setVisible(false);
        executionDataConf.setCanBeConsumed(false);
        executionDataConf.setCanBeResolved(true);
        executionDataConf.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)));
            attributes.attribute(VerificationType.VERIFICATION_TYPE_ATTRIBUTE, objects.named(VerificationType.class, VerificationType.JACOCO_RESULTS));
        });
        report.getReportTask().configure(task -> {
            Callable<FileCollection> executionData = () -> executionDataConf.getIncoming().artifactView(view -> {
                view.componentFilter(id -> id instanceof ProjectComponentIdentifier);
                view.lenient(true);
            }).getFiles();
            configureReportTaskInputs(task, classDirectories, sourceDirectories, executionData);
        });
    });
    // convention for synthesizing reports based on existing test suites in "this" project
    project.getPlugins().withId("jvm-test-suite", plugin -> {
        // Depend on this project for aggregation
        project.getDependencies().add(JACOCO_AGGREGATION_CONFIGURATION_NAME, project);
        TestingExtension testing = project.getExtensions().getByType(TestingExtension.class);
        ExtensiblePolymorphicDomainObjectContainer<TestSuite> testSuites = testing.getSuites();
        testSuites.withType(JvmTestSuite.class).all(testSuite -> {
            reporting.getReports().create(testSuite.getName() + "CodeCoverageReport", JacocoCoverageReport.class, report -> {
                report.getTestType().convention(testSuite.getTestType());
            });
        });
    });
}
Also used : TestSuiteType(org.gradle.api.attributes.TestSuiteType) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) ReportingExtension(org.gradle.api.reporting.ReportingExtension) Project(org.gradle.api.Project) Category(org.gradle.api.attributes.Category) TestingExtension(org.gradle.testing.base.TestingExtension) Callable(java.util.concurrent.Callable) FileCollection(org.gradle.api.file.FileCollection) JvmTestSuite(org.gradle.api.plugins.jvm.JvmTestSuite) Bundling(org.gradle.api.attributes.Bundling) ArtifactView(org.gradle.api.artifacts.ArtifactView) Configuration(org.gradle.api.artifacts.Configuration) TestSuite(org.gradle.testing.base.TestSuite) DefaultJacocoCoverageReport(org.gradle.internal.jacoco.DefaultJacocoCoverageReport) VerificationType(org.gradle.api.attributes.VerificationType) ObjectFactory(org.gradle.api.model.ObjectFactory) ExtensiblePolymorphicDomainObjectContainer(org.gradle.api.ExtensiblePolymorphicDomainObjectContainer) Incubating(org.gradle.api.Incubating) JacocoReport(org.gradle.testing.jacoco.tasks.JacocoReport) Plugin(org.gradle.api.Plugin) ReportingExtension(org.gradle.api.reporting.ReportingExtension) Category(org.gradle.api.attributes.Category) Configuration(org.gradle.api.artifacts.Configuration) DefaultJacocoCoverageReport(org.gradle.internal.jacoco.DefaultJacocoCoverageReport) TestingExtension(org.gradle.testing.base.TestingExtension) VerificationType(org.gradle.api.attributes.VerificationType) FileCollection(org.gradle.api.file.FileCollection) ArtifactView(org.gradle.api.artifacts.ArtifactView) Bundling(org.gradle.api.attributes.Bundling) ObjectFactory(org.gradle.api.model.ObjectFactory) JvmTestSuite(org.gradle.api.plugins.jvm.JvmTestSuite) TestSuite(org.gradle.testing.base.TestSuite) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) JvmTestSuite(org.gradle.api.plugins.jvm.JvmTestSuite)

Example 2 with TestSuite

use of org.gradle.testing.base.TestSuite in project gradle by gradle.

the class JacocoPlugin method configureCoverageDataElementsVariants.

private void configureCoverageDataElementsVariants(Project project) {
    project.getPlugins().withType(JvmTestSuitePlugin.class, p -> {
        final TestingExtension testing = project.getExtensions().getByType(TestingExtension.class);
        final ExtensiblePolymorphicDomainObjectContainer<TestSuite> testSuites = testing.getSuites();
        testSuites.withType(JvmTestSuite.class).configureEach(suite -> {
            suite.getTargets().configureEach(target -> {
                createCoverageDataVariant(project, suite, target);
            });
        });
    });
}
Also used : TestingExtension(org.gradle.testing.base.TestingExtension) JvmTestSuite(org.gradle.api.plugins.jvm.JvmTestSuite) TestSuite(org.gradle.testing.base.TestSuite) JvmTestSuite(org.gradle.api.plugins.jvm.JvmTestSuite)

Example 3 with TestSuite

use of org.gradle.testing.base.TestSuite in project gradle by gradle.

the class JvmTestSuitePlugin method configureTestDataElementsVariants.

private void configureTestDataElementsVariants(Project project) {
    final TestingExtension testing = project.getExtensions().getByType(TestingExtension.class);
    final ExtensiblePolymorphicDomainObjectContainer<TestSuite> testSuites = testing.getSuites();
    testSuites.withType(JvmTestSuite.class).configureEach(suite -> {
        suite.getTargets().configureEach(target -> {
            addTestResultsVariant(project, suite, target);
        });
    });
}
Also used : TestingExtension(org.gradle.testing.base.TestingExtension) JvmTestSuite(org.gradle.api.plugins.jvm.JvmTestSuite) TestSuite(org.gradle.testing.base.TestSuite) DefaultJvmTestSuite(org.gradle.api.plugins.jvm.internal.DefaultJvmTestSuite) JvmTestSuite(org.gradle.api.plugins.jvm.JvmTestSuite) DefaultJvmTestSuite(org.gradle.api.plugins.jvm.internal.DefaultJvmTestSuite)

Example 4 with TestSuite

use of org.gradle.testing.base.TestSuite in project gradle by gradle.

the class TestReportAggregationPlugin method apply.

@Override
public void apply(Project project) {
    project.getPluginManager().apply("org.gradle.reporting-base");
    final Configuration testAggregation = project.getConfigurations().create(TEST_REPORT_AGGREGATION_CONFIGURATION_NAME);
    testAggregation.setDescription("A configuration to collect test execution results");
    testAggregation.setVisible(false);
    testAggregation.setCanBeConsumed(false);
    testAggregation.setCanBeResolved(false);
    ReportingExtension reporting = project.getExtensions().getByType(ReportingExtension.class);
    reporting.getReports().registerBinding(AggregateTestReport.class, DefaultAggregateTestReport.class);
    ObjectFactory objects = project.getObjects();
    final DirectoryProperty testReportDirectory = objects.directoryProperty().convention(reporting.getBaseDirectory().dir(TestingBasePlugin.TESTS_DIR_NAME));
    // prepare testReportDirectory with a reasonable default, but override with JavaPluginExtension#testReportDirectory if available
    project.getPlugins().withId("java-base", plugin -> {
        JavaPluginExtension javaPluginExtension = project.getExtensions().getByType(JavaPluginExtension.class);
        testReportDirectory.convention(javaPluginExtension.getTestReportDir());
    });
    // iterate and configure each user-specified report, creating a <reportName>Results configuration for each
    reporting.getReports().withType(AggregateTestReport.class).all(report -> {
        // A resolvable configuration to collect test results; typically named "testResults"
        Configuration testResultsConf = project.getConfigurations().create(report.getName() + "Results");
        testResultsConf.extendsFrom(testAggregation);
        testResultsConf.setDescription(String.format("Supplies test result data to the %s.  External library dependencies may appear as resolution failures, but this is expected behavior.", report.getName()));
        testResultsConf.setVisible(false);
        testResultsConf.setCanBeConsumed(false);
        testResultsConf.setCanBeResolved(true);
        testResultsConf.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)));
            attributes.attribute(VerificationType.VERIFICATION_TYPE_ATTRIBUTE, objects.named(VerificationType.class, VerificationType.TEST_RESULTS));
        });
        report.getReportTask().configure(task -> {
            Callable<FileCollection> testResults = () -> testResultsConf.getIncoming().artifactView(view -> {
                view.componentFilter(id -> id instanceof ProjectComponentIdentifier);
                view.lenient(true);
            }).getFiles();
            task.getTestResults().from(testResults);
            task.getDestinationDirectory().convention(testReportDirectory.dir(report.getTestType().map(tt -> tt + "/aggregated-results")));
        });
    });
    // convention for synthesizing reports based on existing test suites in "this" project
    project.getPlugins().withId("jvm-test-suite", plugin -> {
        // Depend on this project for aggregation
        project.getDependencies().add(TEST_REPORT_AGGREGATION_CONFIGURATION_NAME, project);
        TestingExtension testing = project.getExtensions().getByType(TestingExtension.class);
        ExtensiblePolymorphicDomainObjectContainer<TestSuite> testSuites = testing.getSuites();
        testSuites.withType(JvmTestSuite.class).all(testSuite -> {
            reporting.getReports().create(testSuite.getName() + "AggregateTestReport", AggregateTestReport.class, report -> {
                report.getTestType().convention(testSuite.getTestType());
            });
        });
    });
}
Also used : TestSuiteType(org.gradle.api.attributes.TestSuiteType) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) TestingBasePlugin(org.gradle.testing.base.plugins.TestingBasePlugin) ReportingExtension(org.gradle.api.reporting.ReportingExtension) Project(org.gradle.api.Project) Category(org.gradle.api.attributes.Category) DefaultAggregateTestReport(org.gradle.api.internal.tasks.testing.DefaultAggregateTestReport) TestingExtension(org.gradle.testing.base.TestingExtension) Callable(java.util.concurrent.Callable) AggregateTestReport(org.gradle.api.tasks.testing.AggregateTestReport) FileCollection(org.gradle.api.file.FileCollection) JvmTestSuite(org.gradle.api.plugins.jvm.JvmTestSuite) Configuration(org.gradle.api.artifacts.Configuration) TestSuite(org.gradle.testing.base.TestSuite) VerificationType(org.gradle.api.attributes.VerificationType) ObjectFactory(org.gradle.api.model.ObjectFactory) DirectoryProperty(org.gradle.api.file.DirectoryProperty) ExtensiblePolymorphicDomainObjectContainer(org.gradle.api.ExtensiblePolymorphicDomainObjectContainer) Incubating(org.gradle.api.Incubating) Plugin(org.gradle.api.Plugin) ReportingExtension(org.gradle.api.reporting.ReportingExtension) Category(org.gradle.api.attributes.Category) Configuration(org.gradle.api.artifacts.Configuration) TestingExtension(org.gradle.testing.base.TestingExtension) VerificationType(org.gradle.api.attributes.VerificationType) FileCollection(org.gradle.api.file.FileCollection) DirectoryProperty(org.gradle.api.file.DirectoryProperty) ObjectFactory(org.gradle.api.model.ObjectFactory) JvmTestSuite(org.gradle.api.plugins.jvm.JvmTestSuite) TestSuite(org.gradle.testing.base.TestSuite) DefaultAggregateTestReport(org.gradle.api.internal.tasks.testing.DefaultAggregateTestReport) AggregateTestReport(org.gradle.api.tasks.testing.AggregateTestReport) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) JvmTestSuite(org.gradle.api.plugins.jvm.JvmTestSuite)

Example 5 with TestSuite

use of org.gradle.testing.base.TestSuite in project gradle by gradle.

the class JvmTestSuitePlugin method apply.

@Override
public void apply(Project project) {
    project.getPluginManager().apply("org.gradle.test-suite-base");
    project.getPluginManager().apply("org.gradle.java-base");
    JavaPluginExtension java = project.getExtensions().getByType(JavaPluginExtension.class);
    TestingExtension testing = project.getExtensions().getByType(TestingExtension.class);
    ExtensiblePolymorphicDomainObjectContainer<TestSuite> testSuites = testing.getSuites();
    testSuites.registerBinding(JvmTestSuite.class, DefaultJvmTestSuite.class);
    // TODO: Deprecate this behavior?
    // Why would any Test task created need to use the test source set's classes?
    project.getTasks().withType(Test.class).configureEach(test -> {
        // The test task may have already been created but the test sourceSet may not exist yet.
        // So defer looking up the java extension and sourceSet until the convention mapping is resolved.
        // See https://github.com/gradle/gradle/issues/18622
        test.getConventionMapping().map("testClassesDirs", () -> project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets().findByName(SourceSet.TEST_SOURCE_SET_NAME).getOutput().getClassesDirs());
        test.getConventionMapping().map("classpath", () -> project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets().findByName(SourceSet.TEST_SOURCE_SET_NAME).getRuntimeClasspath());
        test.getModularity().getInferModulePath().convention(java.getModularity().getInferModulePath());
    });
    testSuites.withType(JvmTestSuite.class).all(testSuite -> {
        testSuite.getTestType().convention(getDefaultTestType(testSuite));
        testSuite.getTargets().all(target -> {
            target.getTestTask().configure(test -> {
                test.getConventionMapping().map("testClassesDirs", () -> testSuite.getSources().getOutput().getClassesDirs());
                test.getConventionMapping().map("classpath", () -> testSuite.getSources().getRuntimeClasspath());
            });
        });
    });
    configureTestDataElementsVariants(project);
}
Also used : TestingExtension(org.gradle.testing.base.TestingExtension) JvmTestSuite(org.gradle.api.plugins.jvm.JvmTestSuite) TestSuite(org.gradle.testing.base.TestSuite) DefaultJvmTestSuite(org.gradle.api.plugins.jvm.internal.DefaultJvmTestSuite) Test(org.gradle.api.tasks.testing.Test) JvmTestSuite(org.gradle.api.plugins.jvm.JvmTestSuite) DefaultJvmTestSuite(org.gradle.api.plugins.jvm.internal.DefaultJvmTestSuite)

Aggregations

JvmTestSuite (org.gradle.api.plugins.jvm.JvmTestSuite)6 TestSuite (org.gradle.testing.base.TestSuite)6 TestingExtension (org.gradle.testing.base.TestingExtension)6 Callable (java.util.concurrent.Callable)3 Configuration (org.gradle.api.artifacts.Configuration)3 ExtensiblePolymorphicDomainObjectContainer (org.gradle.api.ExtensiblePolymorphicDomainObjectContainer)2 Incubating (org.gradle.api.Incubating)2 Plugin (org.gradle.api.Plugin)2 Project (org.gradle.api.Project)2 ProjectComponentIdentifier (org.gradle.api.artifacts.component.ProjectComponentIdentifier)2 Category (org.gradle.api.attributes.Category)2 TestSuiteType (org.gradle.api.attributes.TestSuiteType)2 VerificationType (org.gradle.api.attributes.VerificationType)2 FileCollection (org.gradle.api.file.FileCollection)2 ObjectFactory (org.gradle.api.model.ObjectFactory)2 DefaultJvmTestSuite (org.gradle.api.plugins.jvm.internal.DefaultJvmTestSuite)2 ReportingExtension (org.gradle.api.reporting.ReportingExtension)2 File (java.io.File)1 Collection (java.util.Collection)1 List (java.util.List)1