Search in sources :

Example 6 with TestingExtension

use of org.gradle.testing.base.TestingExtension 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)

Example 7 with TestingExtension

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

the class IdeaPlugin method configureIdeaModuleForTestSuites.

private void configureIdeaModuleForTestSuites(final Project project) {
    final TestingExtension testing = project.getExtensions().getByType(TestingExtension.class);
    final IdeaModule ideaModule = ideaModelFor(project).getModule();
    testing.getSuites().withType(JvmTestSuite.class).configureEach(suite -> {
        ideaModule.getTestSources().from(suite.getSources().getAllJava().getSourceDirectories());
        ideaModule.getTestResources().from(suite.getSources().getResources().getSourceDirectories());
    });
}
Also used : IdeaModule(org.gradle.plugins.ide.idea.model.IdeaModule) TestingExtension(org.gradle.testing.base.TestingExtension) JvmTestSuite(org.gradle.api.plugins.jvm.JvmTestSuite)

Example 8 with TestingExtension

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

the class EclipsePlugin method configureJavaClasspath.

private static void configureJavaClasspath(final Project project, final TaskProvider<GenerateEclipseClasspath> task, final EclipseModel model, Collection<SourceSet> testSourceSetsConvention, Collection<Configuration> testConfigurationsConvention) {
    project.getPlugins().withType(JavaPlugin.class, new Action<JavaPlugin>() {

        @Override
        public void execute(JavaPlugin javaPlugin) {
            ((IConventionAware) model.getClasspath()).getConventionMapping().map("plusConfigurations", new Callable<Collection<Configuration>>() {

                @Override
                public Collection<Configuration> call() {
                    SourceSetContainer sourceSets = project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets();
                    List<Configuration> sourceSetsConfigurations = Lists.newArrayListWithCapacity(sourceSets.size() * 2);
                    ConfigurationContainer configurations = project.getConfigurations();
                    for (SourceSet sourceSet : sourceSets) {
                        sourceSetsConfigurations.add(configurations.getByName(sourceSet.getCompileClasspathConfigurationName()));
                        sourceSetsConfigurations.add(configurations.getByName(sourceSet.getRuntimeClasspathConfigurationName()));
                    }
                    return sourceSetsConfigurations;
                }
            }).cache();
            ((IConventionAware) model.getClasspath()).getConventionMapping().map("classFolders", new Callable<List<File>>() {

                @Override
                public List<File> call() {
                    List<File> result = Lists.newArrayList();
                    for (SourceSet sourceSet : project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets()) {
                        result.addAll(sourceSet.getOutput().getDirs().getFiles());
                    }
                    return result;
                }
            });
            task.configure(new Action<GenerateEclipseClasspath>() {

                @Override
                public void execute(GenerateEclipseClasspath task) {
                    for (SourceSet sourceSet : project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets()) {
                        task.dependsOn(sourceSet.getOutput().getDirs());
                    }
                }
            });
            SourceSetContainer sourceSets = project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets();
            sourceSets.configureEach(new Action<SourceSet>() {

                @Override
                public void execute(SourceSet sourceSet) {
                    if (sourceSet.getName().toLowerCase(Locale.ROOT).contains("test")) {
                        // source sets with 'test' in their name are marked as test on the Eclipse classpath
                        testSourceSetsConvention.add(sourceSet);
                        // resolved dependencies from the source sets with 'test' in their name are marked as test on the Eclipse classpath
                        testConfigurationsConvention.add(project.getConfigurations().findByName(sourceSet.getCompileClasspathConfigurationName()));
                        testConfigurationsConvention.add(project.getConfigurations().findByName(sourceSet.getRuntimeClasspathConfigurationName()));
                    }
                }
            });
            project.getConfigurations().all(new Action<Configuration>() {

                @Override
                public void execute(Configuration configuration) {
                    if (configuration.isCanBeResolved() && configuration.getName().toLowerCase(Locale.ROOT).contains("test")) {
                        // resolved dependencies from custom configurations with 'test' in their name are marked as test on the Eclipse classpath
                        testConfigurationsConvention.add(configuration);
                    }
                }
            });
        }
    });
    project.getPlugins().withType(JavaTestFixturesPlugin.class, new Action<JavaTestFixturesPlugin>() {

        @Override
        public void execute(JavaTestFixturesPlugin javaTestFixturesPlugin) {
            model.getClasspath().getContainsTestFixtures().convention(true);
            project.getPluginManager().withPlugin("java", new Action<AppliedPlugin>() {

                @Override
                public void execute(AppliedPlugin appliedPlugin) {
                    SourceSetContainer sourceSets = project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets();
                    SourceSet sourceSet = sourceSets.getByName(TestFixturesSupport.TEST_FIXTURE_SOURCESET_NAME);
                    // the testFixtures source set is marked as test on the Eclipse classpath
                    testSourceSetsConvention.add(sourceSet);
                    // resolved dependencies from the testFixtures source set are marked as test on the Eclipse classpath
                    testConfigurationsConvention.add(project.getConfigurations().findByName(sourceSet.getCompileClasspathConfigurationName()));
                    testConfigurationsConvention.add(project.getConfigurations().findByName(sourceSet.getRuntimeClasspathConfigurationName()));
                }
            });
        }
    });
    project.getPlugins().withType(TestSuiteBasePlugin.class, testSuiteBasePlugin -> {
        TestingExtension testing = project.getExtensions().getByType(TestingExtension.class);
        ExtensiblePolymorphicDomainObjectContainer<TestSuite> suites = testing.getSuites();
        suites.withType(JvmTestSuite.class).configureEach(jvmTestSuite -> {
            // jvm test suite source sets are marked as test on the Eclipse classpath
            testSourceSetsConvention.add(jvmTestSuite.getSources());
            // resolved dependencies from jvm test suites are marked as test on the Eclipse classpath
            testConfigurationsConvention.add(project.getConfigurations().findByName(jvmTestSuite.getSources().getCompileClasspathConfigurationName()));
            testConfigurationsConvention.add(project.getConfigurations().findByName(jvmTestSuite.getSources().getRuntimeClasspathConfigurationName()));
        });
    });
}
Also used : Action(org.gradle.api.Action) Configuration(org.gradle.api.artifacts.Configuration) TestingExtension(org.gradle.testing.base.TestingExtension) JavaPlugin(org.gradle.api.plugins.JavaPlugin) JavaPluginExtension(org.gradle.api.plugins.JavaPluginExtension) JavaTestFixturesPlugin(org.gradle.api.plugins.JavaTestFixturesPlugin) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) Callable(java.util.concurrent.Callable) SourceSet(org.gradle.api.tasks.SourceSet) JvmTestSuite(org.gradle.api.plugins.jvm.JvmTestSuite) TestSuite(org.gradle.testing.base.TestSuite) ConfigurationContainer(org.gradle.api.artifacts.ConfigurationContainer) Collection(java.util.Collection) List(java.util.List) AppliedPlugin(org.gradle.api.plugins.AppliedPlugin) IConventionAware(org.gradle.api.internal.IConventionAware) File(java.io.File) JvmTestSuite(org.gradle.api.plugins.jvm.JvmTestSuite)

Aggregations

JvmTestSuite (org.gradle.api.plugins.jvm.JvmTestSuite)8 TestingExtension (org.gradle.testing.base.TestingExtension)8 TestSuite (org.gradle.testing.base.TestSuite)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 Test (org.gradle.api.tasks.testing.Test)2 File (java.io.File)1 Collection (java.util.Collection)1