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

View File

@ -20,6 +20,7 @@ package org.elasticsearch.gradle.tool;
import org.gradle.api.Action; import org.gradle.api.Action;
import org.gradle.api.NamedDomainObjectContainer; import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.PolymorphicDomainObjectContainer;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.Task; import org.gradle.api.Task;
import org.gradle.api.UnknownTaskException; 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) { public static <T extends Task> TaskProvider<T> maybeRegister(TaskContainer tasks, String name, Class<T> clazz, Action<T> action) {
try { try {
return tasks.named(name, clazz); return tasks.named(name, clazz);