From d478c5b6b60ccea7a7e08eb3b3d31954cec79784 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 19 Mar 2018 13:59:54 -0700 Subject: [PATCH] Build: Simplify rest spec hack configuration This commit creates the copyRestSpec task for rest integ tests immediately on creation of the RestIntegTestTask instead of lazily in afterEvaluate. This allows other projects to add additional rest specs to be copied, instead of needing to create another parallel copy task. --- .../gradle/test/RestIntegTestTask.groovy | 62 +++++++++++++-- .../gradle/test/RestSpecHack.groovy | 78 ------------------- 2 files changed, 57 insertions(+), 83 deletions(-) delete mode 100644 buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestSpecHack.groovy diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestIntegTestTask.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestIntegTestTask.groovy index bb86c1e0995a8..3c7554453b5e2 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestIntegTestTask.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestIntegTestTask.groovy @@ -20,10 +20,15 @@ package org.elasticsearch.gradle.test import com.carrotsearch.gradle.junit4.RandomizedTestingTask import org.elasticsearch.gradle.BuildPlugin +import org.elasticsearch.gradle.VersionProperties import org.gradle.api.DefaultTask +import org.gradle.api.Project import org.gradle.api.Task import org.gradle.api.execution.TaskExecutionAdapter import org.gradle.api.internal.tasks.options.Option +import org.gradle.api.provider.Property +import org.gradle.api.provider.Provider +import org.gradle.api.tasks.Copy import org.gradle.api.tasks.Input import org.gradle.api.tasks.TaskState @@ -47,7 +52,7 @@ public class RestIntegTestTask extends DefaultTask { /** Flag indicating whether the rest tests in the rest spec should be run. */ @Input - boolean includePackaged = false + Property includePackaged = project.objects.property(Boolean) public RestIntegTestTask() { runner = project.tasks.create("${name}Runner", RandomizedTestingTask.class) @@ -92,10 +97,9 @@ public class RestIntegTestTask extends DefaultTask { } // copy the rest spec/tests into the test resources - RestSpecHack.configureDependencies(project) - project.afterEvaluate { - runner.dependsOn(RestSpecHack.configureTask(project, includePackaged)) - } + Task copyRestSpec = createCopyRestSpecTask(project, includePackaged) + runner.dependsOn(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 { @@ -109,6 +113,11 @@ public class RestIntegTestTask extends DefaultTask { } } + /** Sets the includePackaged property */ + public void includePackaged(boolean include) { + includePackaged.set(include) + } + @Option( option = "debug-jvm", description = "Enable debugging configuration, to allow attaching a debugger to elasticsearch." @@ -184,4 +193,47 @@ public class RestIntegTestTask extends DefaultTask { println('=========================================') } + + /** + * 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 + */ + private static Task createCopyRestSpecTask(Project project, Provider includePackagedTests) { + project.configurations { + restSpec + } + project.dependencies { + restSpec "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 (includePackagedTests.get()) { + include 'rest-api-spec/test/**' + } + } + } + project.idea { + module { + if (scopes.TEST != null) { + scopes.TEST.plus.add(project.configurations.restSpec) + } + } + } + return copyRestSpec + } } diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestSpecHack.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestSpecHack.groovy deleted file mode 100644 index 296ae7115789f..0000000000000 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestSpecHack.groovy +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch 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 - * - * http://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.elasticsearch.gradle.test - -import org.elasticsearch.gradle.VersionProperties -import org.gradle.api.Project -import org.gradle.api.Task -import org.gradle.api.tasks.Copy - -/** - * The rest-api-spec tests are loaded from the classpath. However, they - * currently must be available on the local filesystem. This class encapsulates - * setting up tasks to copy the rest spec api to test resources. - */ -public class RestSpecHack { - /** - * Sets dependencies needed to copy the rest spec. - * @param project The project to add rest spec dependency to - */ - public static void configureDependencies(Project project) { - project.configurations { - restSpec - } - project.dependencies { - restSpec "org.elasticsearch:rest-api-spec:${VersionProperties.elasticsearch}" - } - } - - /** - * 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 - */ - public static Task configureTask(Project project, boolean includePackagedTests) { - 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) { - from { project.zipTree(project.configurations.restSpec.singleFile) } - include 'rest-api-spec/api/**' - if (includePackagedTests) { - include 'rest-api-spec/test/**' - } - into project.sourceSets.test.output.resourcesDir - } - project.idea { - module { - if (scopes.TEST != null) { - scopes.TEST.plus.add(project.configurations.restSpec) - } - } - } - return copyRestSpec - } -}