jdaugherty commented on code in PR #15538: URL: https://github.com/apache/grails-core/pull/15538#discussion_r3012427939
########## 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: I went ahead and cleaned up the old paths since we're on 8.x to use Grails 7. -- 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]
