Przeglądaj źródła

Build: Simplify rest spec hack configuration (#29149)

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.
Ryan Ernst 7 lat temu
rodzic
commit
f7a1267e4f

+ 57 - 5
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<Boolean> 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<Boolean> 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
+    }
 }

+ 0 - 78
buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestSpecHack.groovy

@@ -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
-    }
-}