Ensure rest api specs are always copied before using test classpath (#46514)

(cherry picked from commit 45202903b4fea3a43f62594fd357ab3c98c3dd15)
This commit is contained in:
Mark Vieira 2019-09-10 08:24:05 -07:00
parent 7461259ba6
commit e5d315f6e1
No known key found for this signature in database
GPG Key ID: CA947EF7E6D4B105
2 changed files with 43 additions and 42 deletions

View File

@ -22,10 +22,12 @@ import org.elasticsearch.gradle.VersionProperties
import org.elasticsearch.gradle.testclusters.ElasticsearchCluster
import org.elasticsearch.gradle.testclusters.RestTestRunnerTask
import org.elasticsearch.gradle.testclusters.TestClustersPlugin
import org.elasticsearch.gradle.tool.Boilerplate
import org.elasticsearch.gradle.tool.ClasspathUtils
import org.gradle.api.DefaultTask
import org.gradle.api.Task
import org.gradle.api.execution.TaskExecutionAdapter
import org.gradle.api.file.FileCopyDetails
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.api.tasks.Copy
@ -39,6 +41,7 @@ import org.gradle.process.CommandLineArgumentProvider
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.util.stream.Stream
/**
* A wrapper task around setting up a cluster and running rest tests.
*/
@ -121,10 +124,10 @@ class RestIntegTestTask extends DefaultTask {
runner.systemProperty('test.cluster', System.getProperty("tests.cluster"))
}
// copy the rest spec/tests into the test resources
Task copyRestSpec = createCopyRestSpecTask()
runner.dependsOn(copyRestSpec)
// copy the rest spec/tests onto the test classpath
Copy copyRestSpec = createCopyRestSpecTask()
project.sourceSets.test.output.builtBy(copyRestSpec)
// this must run after all projects have been configured, so we know any project
// references can be accessed as a fully configured
project.gradle.projectsEvaluated {
@ -222,50 +225,37 @@ class RestIntegTestTask extends DefaultTask {
}
/**
* Creates a task (if necessary) to copy the rest spec files.
*
* @param project The project to add the copy task to
* @param includePackagedTests true if the packaged tests should be copied, false otherwise
*/
Task createCopyRestSpecTask() {
project.configurations {
restSpec
Copy createCopyRestSpecTask() {
Boilerplate.maybeCreate(project.configurations, 'restSpec') {
project.dependencies.add(
'restSpec',
ClasspathUtils.isElasticsearchProject() ? project.project(':rest-api-spec') :
"org.elasticsearch:rest-api-spec:${VersionProperties.elasticsearch}"
)
}
project.dependencies {
restSpec ClasspathUtils.isElasticsearchProject() ? project.project(':rest-api-spec') :
"org.elasticsearch:rest-api-spec:${VersionProperties.elasticsearch}"
}
Task copyRestSpec = project.tasks.findByName('copyRestSpec')
if (copyRestSpec != null) {
return copyRestSpec
}
Map copyRestSpecProps = [
name : 'copyRestSpec',
type : Copy,
dependsOn: [project.configurations.restSpec, 'processTestResources']
]
copyRestSpec = project.tasks.create(copyRestSpecProps) {
into project.sourceSets.test.output.resourcesDir
}
project.afterEvaluate {
copyRestSpec.from({ project.zipTree(project.configurations.restSpec.singleFile) }) {
include 'rest-api-spec/api/**'
if (includePackaged) {
include 'rest-api-spec/test/**'
return Boilerplate.maybeCreate(project.tasks, 'copyRestSpec', Copy) { Copy copy ->
copy.dependsOn project.configurations.restSpec
copy.into(project.sourceSets.test.output.resourcesDir)
copy.from({ project.zipTree(project.configurations.restSpec.singleFile) }) {
includeEmptyDirs = false
include 'rest-api-spec/**'
filesMatching('rest-api-spec/test/**') { FileCopyDetails details ->
if (includePackaged == false) {
details.exclude()
}
}
}
}
if (project.plugins.hasPlugin(IdeaPlugin)) {
project.idea {
module {
if (scopes.TEST != null) {
scopes.TEST.plus.add(project.configurations.restSpec)
if (project.plugins.hasPlugin(IdeaPlugin)) {
project.idea {
module {
if (scopes.TEST != null) {
scopes.TEST.plus.add(project.configurations.restSpec)
}
}
}
}
}
return copyRestSpec
}
}

View File

@ -20,6 +20,7 @@ package org.elasticsearch.gradle.tool;
import org.gradle.api.Action;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.PolymorphicDomainObjectContainer;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.UnknownTaskException;
@ -52,6 +53,16 @@ public abstract class Boilerplate {
}
public static <T> T maybeCreate(PolymorphicDomainObjectContainer<T> collection, String name, Class<T> type, Action<T> action) {
return Optional.ofNullable(collection.findByName(name))
.orElseGet(() -> {
T result = collection.create(name, type);
action.execute(result);
return result;
});
}
public static <T extends Task> TaskProvider<T> maybeRegister(TaskContainer tasks, String name, Class<T> clazz, Action<T> action) {
try {
return tasks.named(name, clazz);