jamesfredley commented on code in PR #15538: URL: https://github.com/apache/grails-core/pull/15538#discussion_r3012294327
########## grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/TestPhasesGradlePlugin.groovy: ########## @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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.grails.gradle.plugin.core + +import groovy.transform.CompileDynamic +import groovy.transform.CompileStatic + +import org.gradle.api.NamedDomainObjectContainer +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.artifacts.ConfigurationContainer +import org.gradle.api.artifacts.dsl.DependencyHandler +import org.gradle.api.tasks.SourceSet +import org.gradle.api.tasks.SourceSetContainer +import org.gradle.api.tasks.SourceSetOutput +import org.gradle.api.tasks.TaskContainer +import org.gradle.api.tasks.TaskProvider +import org.gradle.api.tasks.testing.Test +import org.gradle.api.tasks.testing.TestReport +import org.gradle.language.base.plugins.LifecycleBasePlugin +import org.gradle.plugins.ide.idea.model.IdeaModel +import org.gradle.plugins.ide.idea.model.IdeaModule + +import org.grails.gradle.plugin.util.SourceSets + +import static org.gradle.api.plugins.JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME +import static org.gradle.api.plugins.JavaPlugin.TEST_RUNTIME_ONLY_CONFIGURATION_NAME +import static org.gradle.api.plugins.JavaPlugin.TEST_TASK_NAME +import static org.gradle.api.tasks.SourceSet.TEST_SOURCE_SET_NAME + +/** + * Gradle plugin that provides a {@code testPhases} extension for defining + * additional test phases (e.g. integrationTest, functionalTest). + * + * <p>Each {@link TestPhase} added to the container automatically gets its own + * source set, dependency configurations, {@link Test} task, and merged test + * report contribution.</p> + * + * @since 2025.0 + */ +@CompileStatic +class TestPhasesGradlePlugin implements Plugin<Project> { + + static final String EXTENSION_NAME = 'testPhases' + static final String MERGE_TEST_REPORTS_TASK_NAME = 'mergeTestReports' + + NamedDomainObjectContainer<TestPhase> testPhases + + @Override + void apply(Project project) { + testPhases = project.container(TestPhase) + project.extensions.add(EXTENSION_NAME, testPhases) + + testPhases.all { TestPhase phase -> + configureTestPhase(project, phase) + } + } + + private void configureTestPhase(Project project, TestPhase phase) { + String phaseName = phase.name + String implConfigName = "${phaseName}Implementation" + String runtimeOnlyConfigName = "${phaseName}RuntimeOnly" + + File[] sourceDirs = findTestPhaseSources(project, phase) + List<File> acceptedSourceDirs = [] + + final SourceSetContainer sourceSets = SourceSets.findSourceSets(project) + final SourceSetOutput mainSourceSetOutput = SourceSets.findMainSourceSet(project).output + final SourceSetOutput testSourceSetOutput = SourceSets.findSourceSet(project, TEST_SOURCE_SET_NAME).output + final SourceSet phaseSourceSet = sourceSets.create(phaseName) + phaseSourceSet.compileClasspath += mainSourceSetOutput + testSourceSetOutput + phaseSourceSet.runtimeClasspath += mainSourceSetOutput + testSourceSetOutput + + if (sourceDirs != null) { + for (File srcDir in sourceDirs) { + registerSourceDir(phaseSourceSet, srcDir) + acceptedSourceDirs.add(srcDir) + } + } + + final File resources = new File(project.projectDir, 'grails-app/conf') + phaseSourceSet.resources.srcDir(resources) + + final DependencyHandler dependencies = project.dependencies + dependencies.add(implConfigName, mainSourceSetOutput) + dependencies.add(implConfigName, testSourceSetOutput) + + final ConfigurationContainer configurations = project.configurations + configurations.named(implConfigName).configure { + it.extendsFrom(configurations.named(TEST_IMPLEMENTATION_CONFIGURATION_NAME).get()) + } + configurations.named(runtimeOnlyConfigName).configure { + it.extendsFrom(configurations.named(TEST_RUNTIME_ONLY_CONFIGURATION_NAME).get()) + } + + final TaskContainer tasks = project.tasks + final TaskProvider<Test> testTask = tasks.register(phaseName, Test) + testTask.configure { + it.group = LifecycleBasePlugin.VERIFICATION_GROUP + it.testClassesDirs = phaseSourceSet.output.classesDirs + it.classpath = phaseSourceSet.runtimeClasspath + it.shouldRunAfter(TEST_TASK_NAME) + it.finalizedBy(MERGE_TEST_REPORTS_TASK_NAME) + it.reports.html.required.set(false) + it.maxParallelForks = 1 + it.testLogging { + events('passed') + } + it.systemProperty(phase.systemPropertyName, true) + } + tasks.named('check').configure { + it.dependsOn(testTask) + } + + configureMergeTestReports(project, phaseName) + + if (phase.ideaIntegration) { + final File[] files = acceptedSourceDirs.toArray(new File[acceptedSourceDirs.size()]) + integrateIdea(project, files) + } + } + + private void configureMergeTestReports(Project project, String phaseName) { + final TaskContainer tasks = project.tasks + if (tasks.names.contains(MERGE_TEST_REPORTS_TASK_NAME)) { + tasks.named(MERGE_TEST_REPORTS_TASK_NAME, TestReport).configure { + it.testResults.from( + project.files("${project.buildDir}/test-results/binary/${phaseName}"), + project.files("${project.buildDir}/test-results/${phaseName}/binary") + ) + } + } else { + tasks.register(MERGE_TEST_REPORTS_TASK_NAME, TestReport) { + it.mustRunAfter(tasks.withType(Test).toArray()) Review Comment: each test task also has `it.finalizedBy(MERGE_TEST_REPORTS_TASK_NAME)` which handles execution ordering ########## grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/TestPhasesGradlePlugin.groovy: ########## @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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.grails.gradle.plugin.core + +import groovy.transform.CompileDynamic +import groovy.transform.CompileStatic + +import org.gradle.api.NamedDomainObjectContainer +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.artifacts.ConfigurationContainer +import org.gradle.api.artifacts.dsl.DependencyHandler +import org.gradle.api.tasks.SourceSet +import org.gradle.api.tasks.SourceSetContainer +import org.gradle.api.tasks.SourceSetOutput +import org.gradle.api.tasks.TaskContainer +import org.gradle.api.tasks.TaskProvider +import org.gradle.api.tasks.testing.Test +import org.gradle.api.tasks.testing.TestReport +import org.gradle.language.base.plugins.LifecycleBasePlugin +import org.gradle.plugins.ide.idea.model.IdeaModel +import org.gradle.plugins.ide.idea.model.IdeaModule + +import org.grails.gradle.plugin.util.SourceSets + +import static org.gradle.api.plugins.JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME +import static org.gradle.api.plugins.JavaPlugin.TEST_RUNTIME_ONLY_CONFIGURATION_NAME +import static org.gradle.api.plugins.JavaPlugin.TEST_TASK_NAME +import static org.gradle.api.tasks.SourceSet.TEST_SOURCE_SET_NAME + +/** + * Gradle plugin that provides a {@code testPhases} extension for defining + * additional test phases (e.g. integrationTest, functionalTest). + * + * <p>Each {@link TestPhase} added to the container automatically gets its own + * source set, dependency configurations, {@link Test} task, and merged test + * report contribution.</p> + * + * @since 2025.0 + */ +@CompileStatic +class TestPhasesGradlePlugin implements Plugin<Project> { + + static final String EXTENSION_NAME = 'testPhases' + static final String MERGE_TEST_REPORTS_TASK_NAME = 'mergeTestReports' + + NamedDomainObjectContainer<TestPhase> testPhases + + @Override + void apply(Project project) { + testPhases = project.container(TestPhase) + project.extensions.add(EXTENSION_NAME, testPhases) + + testPhases.all { TestPhase phase -> + configureTestPhase(project, phase) + } + } + + private void configureTestPhase(Project project, TestPhase phase) { + String phaseName = phase.name + String implConfigName = "${phaseName}Implementation" + String runtimeOnlyConfigName = "${phaseName}RuntimeOnly" + + File[] sourceDirs = findTestPhaseSources(project, phase) + List<File> acceptedSourceDirs = [] + + final SourceSetContainer sourceSets = SourceSets.findSourceSets(project) + final SourceSetOutput mainSourceSetOutput = SourceSets.findMainSourceSet(project).output + final SourceSetOutput testSourceSetOutput = SourceSets.findSourceSet(project, TEST_SOURCE_SET_NAME).output + final SourceSet phaseSourceSet = sourceSets.create(phaseName) + phaseSourceSet.compileClasspath += mainSourceSetOutput + testSourceSetOutput + phaseSourceSet.runtimeClasspath += mainSourceSetOutput + testSourceSetOutput + + if (sourceDirs != null) { + for (File srcDir in sourceDirs) { + registerSourceDir(phaseSourceSet, srcDir) + acceptedSourceDirs.add(srcDir) + } + } + + final File resources = new File(project.projectDir, 'grails-app/conf') + phaseSourceSet.resources.srcDir(resources) + + final DependencyHandler dependencies = project.dependencies + dependencies.add(implConfigName, mainSourceSetOutput) + dependencies.add(implConfigName, testSourceSetOutput) + + final ConfigurationContainer configurations = project.configurations + configurations.named(implConfigName).configure { + it.extendsFrom(configurations.named(TEST_IMPLEMENTATION_CONFIGURATION_NAME).get()) + } + configurations.named(runtimeOnlyConfigName).configure { + it.extendsFrom(configurations.named(TEST_RUNTIME_ONLY_CONFIGURATION_NAME).get()) + } + + final TaskContainer tasks = project.tasks + final TaskProvider<Test> testTask = tasks.register(phaseName, Test) + testTask.configure { + it.group = LifecycleBasePlugin.VERIFICATION_GROUP + it.testClassesDirs = phaseSourceSet.output.classesDirs + it.classpath = phaseSourceSet.runtimeClasspath + it.shouldRunAfter(TEST_TASK_NAME) + it.finalizedBy(MERGE_TEST_REPORTS_TASK_NAME) + it.reports.html.required.set(false) + it.maxParallelForks = 1 + it.testLogging { + events('passed') + } + it.systemProperty(phase.systemPropertyName, true) + } + tasks.named('check').configure { + it.dependsOn(testTask) + } + + configureMergeTestReports(project, phaseName) + + if (phase.ideaIntegration) { + final File[] files = acceptedSourceDirs.toArray(new File[acceptedSourceDirs.size()]) + integrateIdea(project, files) + } + } + + private void configureMergeTestReports(Project project, String phaseName) { + final TaskContainer tasks = project.tasks + if (tasks.names.contains(MERGE_TEST_REPORTS_TASK_NAME)) { + tasks.named(MERGE_TEST_REPORTS_TASK_NAME, TestReport).configure { + it.testResults.from( + project.files("${project.buildDir}/test-results/binary/${phaseName}"), + project.files("${project.buildDir}/test-results/${phaseName}/binary") + ) + } + } else { + tasks.register(MERGE_TEST_REPORTS_TASK_NAME, TestReport) { + it.mustRunAfter(tasks.withType(Test).toArray()) + it.destinationDirectory.set(project.layout.buildDirectory.dir('reports/tests')) + it.testResults.from( + project.files("${project.buildDir}/test-results/binary/test", "${project.buildDir}/test-results/binary/${phaseName}"), Review Comment: project.layout.buildDirectory ########## grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/TestPhasesGradlePlugin.groovy: ########## @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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.grails.gradle.plugin.core + +import groovy.transform.CompileDynamic +import groovy.transform.CompileStatic + +import org.gradle.api.NamedDomainObjectContainer +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.artifacts.ConfigurationContainer +import org.gradle.api.artifacts.dsl.DependencyHandler +import org.gradle.api.tasks.SourceSet +import org.gradle.api.tasks.SourceSetContainer +import org.gradle.api.tasks.SourceSetOutput +import org.gradle.api.tasks.TaskContainer +import org.gradle.api.tasks.TaskProvider +import org.gradle.api.tasks.testing.Test +import org.gradle.api.tasks.testing.TestReport +import org.gradle.language.base.plugins.LifecycleBasePlugin +import org.gradle.plugins.ide.idea.model.IdeaModel +import org.gradle.plugins.ide.idea.model.IdeaModule + +import org.grails.gradle.plugin.util.SourceSets + +import static org.gradle.api.plugins.JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME +import static org.gradle.api.plugins.JavaPlugin.TEST_RUNTIME_ONLY_CONFIGURATION_NAME +import static org.gradle.api.plugins.JavaPlugin.TEST_TASK_NAME +import static org.gradle.api.tasks.SourceSet.TEST_SOURCE_SET_NAME + +/** + * Gradle plugin that provides a {@code testPhases} extension for defining + * additional test phases (e.g. integrationTest, functionalTest). + * + * <p>Each {@link TestPhase} added to the container automatically gets its own + * source set, dependency configurations, {@link Test} task, and merged test + * report contribution.</p> + * + * @since 2025.0 + */ +@CompileStatic +class TestPhasesGradlePlugin implements Plugin<Project> { + + static final String EXTENSION_NAME = 'testPhases' + static final String MERGE_TEST_REPORTS_TASK_NAME = 'mergeTestReports' + + NamedDomainObjectContainer<TestPhase> testPhases + + @Override + void apply(Project project) { + testPhases = project.container(TestPhase) + project.extensions.add(EXTENSION_NAME, testPhases) + + testPhases.all { TestPhase phase -> + configureTestPhase(project, phase) + } + } + + private void configureTestPhase(Project project, TestPhase phase) { + String phaseName = phase.name + String implConfigName = "${phaseName}Implementation" + String runtimeOnlyConfigName = "${phaseName}RuntimeOnly" + + File[] sourceDirs = findTestPhaseSources(project, phase) + List<File> acceptedSourceDirs = [] + + final SourceSetContainer sourceSets = SourceSets.findSourceSets(project) + final SourceSetOutput mainSourceSetOutput = SourceSets.findMainSourceSet(project).output + final SourceSetOutput testSourceSetOutput = SourceSets.findSourceSet(project, TEST_SOURCE_SET_NAME).output + final SourceSet phaseSourceSet = sourceSets.create(phaseName) + phaseSourceSet.compileClasspath += mainSourceSetOutput + testSourceSetOutput + phaseSourceSet.runtimeClasspath += mainSourceSetOutput + testSourceSetOutput + + if (sourceDirs != null) { + for (File srcDir in sourceDirs) { + registerSourceDir(phaseSourceSet, srcDir) + acceptedSourceDirs.add(srcDir) + } + } + + final File resources = new File(project.projectDir, 'grails-app/conf') + phaseSourceSet.resources.srcDir(resources) + + final DependencyHandler dependencies = project.dependencies + dependencies.add(implConfigName, mainSourceSetOutput) + dependencies.add(implConfigName, testSourceSetOutput) + + final ConfigurationContainer configurations = project.configurations + configurations.named(implConfigName).configure { + it.extendsFrom(configurations.named(TEST_IMPLEMENTATION_CONFIGURATION_NAME).get()) + } + configurations.named(runtimeOnlyConfigName).configure { + it.extendsFrom(configurations.named(TEST_RUNTIME_ONLY_CONFIGURATION_NAME).get()) + } + + final TaskContainer tasks = project.tasks + final TaskProvider<Test> testTask = tasks.register(phaseName, Test) + testTask.configure { + it.group = LifecycleBasePlugin.VERIFICATION_GROUP + it.testClassesDirs = phaseSourceSet.output.classesDirs + it.classpath = phaseSourceSet.runtimeClasspath + it.shouldRunAfter(TEST_TASK_NAME) + it.finalizedBy(MERGE_TEST_REPORTS_TASK_NAME) + it.reports.html.required.set(false) + it.maxParallelForks = 1 + it.testLogging { + events('passed') + } + it.systemProperty(phase.systemPropertyName, true) + } + tasks.named('check').configure { + it.dependsOn(testTask) + } + + configureMergeTestReports(project, phaseName) + + if (phase.ideaIntegration) { + final File[] files = acceptedSourceDirs.toArray(new File[acceptedSourceDirs.size()]) + integrateIdea(project, files) + } + } + + private void configureMergeTestReports(Project project, String phaseName) { + final TaskContainer tasks = project.tasks + if (tasks.names.contains(MERGE_TEST_REPORTS_TASK_NAME)) { + tasks.named(MERGE_TEST_REPORTS_TASK_NAME, TestReport).configure { + it.testResults.from( + project.files("${project.buildDir}/test-results/binary/${phaseName}"), + project.files("${project.buildDir}/test-results/${phaseName}/binary") + ) + } + } else { + tasks.register(MERGE_TEST_REPORTS_TASK_NAME, TestReport) { + it.mustRunAfter(tasks.withType(Test).toArray()) + it.destinationDirectory.set(project.layout.buildDirectory.dir('reports/tests')) + it.testResults.from( + project.files("${project.buildDir}/test-results/binary/test", "${project.buildDir}/test-results/binary/${phaseName}"), + project.files("${project.buildDir}/test-results/test/binary", "${project.buildDir}/test-results/${phaseName}/binary") Review Comment: project.layout.buildDirectory ########## grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/TestPhase.groovy: ########## @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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.grails.gradle.plugin.core + +import groovy.transform.CompileStatic + +import grails.util.GrailsNameUtils + +/** + * Represents a configurable test phase (e.g. integrationTest, functionalTest). + * + * <p>Default values for {@code sourceFolderName} and {@code systemPropertyName} + * are derived from the phase {@code name} so that users only need to declare the + * phase by name in most cases.</p> + * + * @since 2025.0 Review Comment: @since 7.1.0 ########## grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/IntegrationTestGradlePlugin.groovy: ########## @@ -18,135 +18,32 @@ */ package org.grails.gradle.plugin.core -import groovy.transform.CompileDynamic import groovy.transform.CompileStatic import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.api.artifacts.ConfigurationContainer -import org.gradle.api.artifacts.dsl.DependencyHandler -import org.gradle.api.tasks.SourceSet -import org.gradle.api.tasks.SourceSetContainer -import org.gradle.api.tasks.SourceSetOutput -import org.gradle.api.tasks.TaskContainer -import org.gradle.api.tasks.TaskProvider -import org.gradle.api.tasks.testing.Test -import org.gradle.api.tasks.testing.TestReport -import org.gradle.language.base.plugins.LifecycleBasePlugin -import org.gradle.plugins.ide.idea.model.IdeaModel -import org.gradle.plugins.ide.idea.model.IdeaModule - -import org.grails.gradle.plugin.util.SourceSets - -import static org.gradle.api.plugins.JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME -import static org.gradle.api.plugins.JavaPlugin.TEST_RUNTIME_ONLY_CONFIGURATION_NAME -import static org.gradle.api.plugins.JavaPlugin.TEST_TASK_NAME -import static org.gradle.api.tasks.SourceSet.TEST_SOURCE_SET_NAME /** - * Gradle plugin for adding separate src/integration-test folder to hold integration tests + * Gradle plugin for adding separate src/integration-test folder to hold integration tests. + * + * <p>This plugin applies {@link TestPhasesGradlePlugin} and registers the default + * {@code integrationTest} phase. Additional test phases can be added via the + * {@code testPhases} extension.</p> * - * Adds integrationTestImplementation and integrationTestRuntimeOnly configurations that extend from testCompileClasspath and testRuntimeClasspath + * <p>Adds integrationTestImplementation and integrationTestRuntimeOnly configurations + * that extend from testCompileClasspath and testRuntimeClasspath.</p> * */ @CompileStatic class IntegrationTestGradlePlugin implements Plugin<Project> { - static final String INTEGRATION_TEST_IMPLEMENTATION_CONFIGURATION_NAME = 'integrationTestImplementation' - static final String INTEGRATION_TEST_RUNTIME_ONLY_CONFIGURATION_NAME = 'integrationTestRuntimeOnly' static final String INTEGRATION_TEST_SOURCE_SET_NAME = 'integrationTest' - static final String INTEGRATION_TEST_TASK_NAME = 'integrationTest' - static final String MERGE_TEST_REPORTS_TASK_NAME = 'mergeTestReports' - static final String GRAILS_INTEGRATION_TEST_INDICATOR = 'is.grails.integration.test' - - boolean ideaIntegration = true - String sourceFolderName = 'src/integration-test' @Override void apply(Project project) { - File[] sourceDirs = findIntegrationTestSources(project) - - List<File> acceptedSourceDirs = [] - final SourceSetContainer sourceSets = SourceSets.findSourceSets(project) - final SourceSetOutput mainSourceSetOutput = SourceSets.findMainSourceSet(project).output - final SourceSetOutput testSourceSetOutput = SourceSets.findSourceSet(project, TEST_SOURCE_SET_NAME).output - final SourceSet integrationTest = sourceSets.create(INTEGRATION_TEST_SOURCE_SET_NAME) - integrationTest.compileClasspath += mainSourceSetOutput + testSourceSetOutput - integrationTest.runtimeClasspath += mainSourceSetOutput + testSourceSetOutput - - for (File srcDir in sourceDirs) { - registerSourceDir(integrationTest, srcDir) - acceptedSourceDirs.add(srcDir) - } - - final File resources = new File(project.projectDir, 'grails-app/conf') - integrationTest.resources.srcDir(resources) - - final DependencyHandler dependencies = project.dependencies - dependencies.add(INTEGRATION_TEST_IMPLEMENTATION_CONFIGURATION_NAME, mainSourceSetOutput) - dependencies.add(INTEGRATION_TEST_IMPLEMENTATION_CONFIGURATION_NAME, testSourceSetOutput) - - final ConfigurationContainer configurations = project.configurations - configurations.named(INTEGRATION_TEST_IMPLEMENTATION_CONFIGURATION_NAME).configure { - it.extendsFrom(configurations.named(TEST_IMPLEMENTATION_CONFIGURATION_NAME).get()) - } - configurations.named(INTEGRATION_TEST_RUNTIME_ONLY_CONFIGURATION_NAME).configure { - it.extendsFrom(configurations.named(TEST_RUNTIME_ONLY_CONFIGURATION_NAME).get()) - } - - final TaskContainer tasks = project.tasks - final TaskProvider<Test> integrationTestTask = tasks.register(INTEGRATION_TEST_TASK_NAME, Test) - integrationTestTask.configure { - it.group = LifecycleBasePlugin.VERIFICATION_GROUP - it.testClassesDirs = integrationTest.output.classesDirs - it.classpath = integrationTest.runtimeClasspath - it.shouldRunAfter(TEST_TASK_NAME) - it.finalizedBy(MERGE_TEST_REPORTS_TASK_NAME) - it.reports.html.required.set(false) - it.maxParallelForks = 1 - it.testLogging { - events('passed') - } - it.systemProperty(GRAILS_INTEGRATION_TEST_INDICATOR, true) - } - tasks.named('check').configure { - it.dependsOn(integrationTestTask) - } - - tasks.register(MERGE_TEST_REPORTS_TASK_NAME, TestReport) { - it.mustRunAfter(tasks.withType(Test).toArray()) - it.destinationDirectory.set(project.layout.buildDirectory.dir('reports/tests')) - // These must point to the binary test results directory generated by a Test task instance. - // If Test task instances are specified directly, this task would depend on them and run them. - it.testResults.from( - project.files("$project.buildDir/test-results/binary/test", "$project.buildDir/test-results/binary/integrationTest"), - // different versions of Gradle store these results in different places. ugh. - project.files("$project.buildDir/test-results/test/binary", "$project.buildDir/test-results/integrationTest/binary") - ) - } - - if (ideaIntegration) { - final File[] files = acceptedSourceDirs.toArray(new File[acceptedSourceDirs.size()]) - integrateIdea(project, files) - } - } - - @CompileDynamic - private void registerSourceDir(SourceSet integrationTest, File srcDir) { - integrationTest."${srcDir.name}".srcDir(srcDir) - } - - @CompileDynamic - private void integrateIdea(Project project, File[] acceptedSourceDirs) { - project.pluginManager.withPlugin('idea') { -> - def ideaExtension = project.getExtensions().getByType(IdeaModel) - ideaExtension.module { IdeaModule it -> - it.testSources.from(acceptedSourceDirs) - } - } - } + project.pluginManager.apply(TestPhasesGradlePlugin) - File[] findIntegrationTestSources(Project project) { - project.file(sourceFolderName).listFiles({ File file -> file.isDirectory() && !file.name.contains('.') } as FileFilter) + TestPhasesGradlePlugin phasesPlugin = project.plugins.getPlugin(TestPhasesGradlePlugin) + phasesPlugin.testPhases.create(INTEGRATION_TEST_SOURCE_SET_NAME) Review Comment: possibly `project.testPhases.create(INTEGRATION_TEST_SOURCE_SET_NAME)` or `project.extensions.getByType(NamedDomainObjectContainer).create(...)` ########## grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/TestPhasesGradlePlugin.groovy: ########## @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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.grails.gradle.plugin.core + +import groovy.transform.CompileDynamic +import groovy.transform.CompileStatic + +import org.gradle.api.NamedDomainObjectContainer +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.artifacts.ConfigurationContainer +import org.gradle.api.artifacts.dsl.DependencyHandler +import org.gradle.api.tasks.SourceSet +import org.gradle.api.tasks.SourceSetContainer +import org.gradle.api.tasks.SourceSetOutput +import org.gradle.api.tasks.TaskContainer +import org.gradle.api.tasks.TaskProvider +import org.gradle.api.tasks.testing.Test +import org.gradle.api.tasks.testing.TestReport +import org.gradle.language.base.plugins.LifecycleBasePlugin +import org.gradle.plugins.ide.idea.model.IdeaModel +import org.gradle.plugins.ide.idea.model.IdeaModule + +import org.grails.gradle.plugin.util.SourceSets + +import static org.gradle.api.plugins.JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME +import static org.gradle.api.plugins.JavaPlugin.TEST_RUNTIME_ONLY_CONFIGURATION_NAME +import static org.gradle.api.plugins.JavaPlugin.TEST_TASK_NAME +import static org.gradle.api.tasks.SourceSet.TEST_SOURCE_SET_NAME + +/** + * Gradle plugin that provides a {@code testPhases} extension for defining + * additional test phases (e.g. integrationTest, functionalTest). + * + * <p>Each {@link TestPhase} added to the container automatically gets its own + * source set, dependency configurations, {@link Test} task, and merged test + * report contribution.</p> + * + * @since 2025.0 Review Comment: @since 7.1.0 ########## grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/TestPhasesGradlePlugin.groovy: ########## @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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.grails.gradle.plugin.core + +import groovy.transform.CompileDynamic +import groovy.transform.CompileStatic + +import org.gradle.api.NamedDomainObjectContainer +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.artifacts.ConfigurationContainer +import org.gradle.api.artifacts.dsl.DependencyHandler +import org.gradle.api.tasks.SourceSet +import org.gradle.api.tasks.SourceSetContainer +import org.gradle.api.tasks.SourceSetOutput +import org.gradle.api.tasks.TaskContainer +import org.gradle.api.tasks.TaskProvider +import org.gradle.api.tasks.testing.Test +import org.gradle.api.tasks.testing.TestReport +import org.gradle.language.base.plugins.LifecycleBasePlugin +import org.gradle.plugins.ide.idea.model.IdeaModel +import org.gradle.plugins.ide.idea.model.IdeaModule + +import org.grails.gradle.plugin.util.SourceSets + +import static org.gradle.api.plugins.JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME +import static org.gradle.api.plugins.JavaPlugin.TEST_RUNTIME_ONLY_CONFIGURATION_NAME +import static org.gradle.api.plugins.JavaPlugin.TEST_TASK_NAME +import static org.gradle.api.tasks.SourceSet.TEST_SOURCE_SET_NAME + +/** + * Gradle plugin that provides a {@code testPhases} extension for defining + * additional test phases (e.g. integrationTest, functionalTest). + * + * <p>Each {@link TestPhase} added to the container automatically gets its own + * source set, dependency configurations, {@link Test} task, and merged test + * report contribution.</p> + * + * @since 2025.0 + */ +@CompileStatic +class TestPhasesGradlePlugin implements Plugin<Project> { + + static final String EXTENSION_NAME = 'testPhases' + static final String MERGE_TEST_REPORTS_TASK_NAME = 'mergeTestReports' + + NamedDomainObjectContainer<TestPhase> testPhases + + @Override + void apply(Project project) { + testPhases = project.container(TestPhase) + project.extensions.add(EXTENSION_NAME, testPhases) + + testPhases.all { TestPhase phase -> + configureTestPhase(project, phase) + } + } + + private void configureTestPhase(Project project, TestPhase phase) { + String phaseName = phase.name + String implConfigName = "${phaseName}Implementation" + String runtimeOnlyConfigName = "${phaseName}RuntimeOnly" + + File[] sourceDirs = findTestPhaseSources(project, phase) + List<File> acceptedSourceDirs = [] + + final SourceSetContainer sourceSets = SourceSets.findSourceSets(project) + final SourceSetOutput mainSourceSetOutput = SourceSets.findMainSourceSet(project).output + final SourceSetOutput testSourceSetOutput = SourceSets.findSourceSet(project, TEST_SOURCE_SET_NAME).output + final SourceSet phaseSourceSet = sourceSets.create(phaseName) + phaseSourceSet.compileClasspath += mainSourceSetOutput + testSourceSetOutput + phaseSourceSet.runtimeClasspath += mainSourceSetOutput + testSourceSetOutput + + if (sourceDirs != null) { + for (File srcDir in sourceDirs) { + registerSourceDir(phaseSourceSet, srcDir) + acceptedSourceDirs.add(srcDir) + } + } + + final File resources = new File(project.projectDir, 'grails-app/conf') + phaseSourceSet.resources.srcDir(resources) + + final DependencyHandler dependencies = project.dependencies + dependencies.add(implConfigName, mainSourceSetOutput) + dependencies.add(implConfigName, testSourceSetOutput) + + final ConfigurationContainer configurations = project.configurations + configurations.named(implConfigName).configure { + it.extendsFrom(configurations.named(TEST_IMPLEMENTATION_CONFIGURATION_NAME).get()) + } + configurations.named(runtimeOnlyConfigName).configure { + it.extendsFrom(configurations.named(TEST_RUNTIME_ONLY_CONFIGURATION_NAME).get()) + } + + final TaskContainer tasks = project.tasks + final TaskProvider<Test> testTask = tasks.register(phaseName, Test) + testTask.configure { + it.group = LifecycleBasePlugin.VERIFICATION_GROUP + it.testClassesDirs = phaseSourceSet.output.classesDirs + it.classpath = phaseSourceSet.runtimeClasspath + it.shouldRunAfter(TEST_TASK_NAME) + it.finalizedBy(MERGE_TEST_REPORTS_TASK_NAME) + it.reports.html.required.set(false) + it.maxParallelForks = 1 + it.testLogging { + events('passed') + } + it.systemProperty(phase.systemPropertyName, true) + } + tasks.named('check').configure { + it.dependsOn(testTask) + } + + configureMergeTestReports(project, phaseName) + + if (phase.ideaIntegration) { + final File[] files = acceptedSourceDirs.toArray(new File[acceptedSourceDirs.size()]) + integrateIdea(project, files) + } + } + + private void configureMergeTestReports(Project project, String phaseName) { + final TaskContainer tasks = project.tasks + if (tasks.names.contains(MERGE_TEST_REPORTS_TASK_NAME)) { + tasks.named(MERGE_TEST_REPORTS_TASK_NAME, TestReport).configure { + it.testResults.from( + project.files("${project.buildDir}/test-results/binary/${phaseName}"), + project.files("${project.buildDir}/test-results/${phaseName}/binary") + ) + } + } else { + tasks.register(MERGE_TEST_REPORTS_TASK_NAME, TestReport) { + it.mustRunAfter(tasks.withType(Test).toArray()) Review Comment: maybe `it.mustRunAfter(project.provider { tasks.withType(Test) })` instead since tasks.withType(Test).toArray() eagerly snapshots Test tasks at registration time. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
