-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Smarter copying of the rest specs and tests #52114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
c7d36e0
1e02fbe
02f95b8
7b81e65
f334d40
c1ee4ff
f43429c
818e761
bddf05b
0c1d164
bbcde3a
2dc1a3a
8942ac2
2f5739c
907c0dd
173b494
6d0aec5
6a74f0b
a391dd7
9583d15
1617f6f
d86fc58
852b30e
10700bc
bb84161
2e4d1d6
9e6e2aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package org.elasticsearch.gradle.test; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class RestApiSpecExtension { | ||
| private List<String> includesCoreSpec = new ArrayList<>(); | ||
| private List<String> includesCoreTests = new ArrayList<>(); | ||
| private List<String> includesXpackSpec = new ArrayList<>(); | ||
| private List<String> includesXpackTests = new ArrayList<>(); | ||
| private boolean alwaysCopySpec = false; | ||
| private boolean copyTests = false; | ||
| private boolean copyXpackTests = false; | ||
|
|
||
| public void includeCoreSpec(String include) { | ||
| includesCoreSpec.add(include); | ||
| } | ||
|
|
||
| public List<String> getIncludesCoreSpec() { | ||
| return includesCoreSpec; | ||
| } | ||
|
|
||
| public void includeCoreTests(String include) { | ||
| includesCoreTests.add(include); | ||
| } | ||
|
|
||
| public List<String> getIncludesCoreTests() { | ||
| return includesCoreTests; | ||
| } | ||
|
|
||
| public void includeXpackSpec(String include) { | ||
| includesXpackSpec.add(include); | ||
| } | ||
|
|
||
| public List<String> getIncludesXpackSpec() { | ||
| return includesXpackSpec; | ||
| } | ||
|
|
||
| public void copyCoreTests(boolean copyTests) { | ||
jakelandis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.copyTests = copyTests; | ||
| } | ||
|
|
||
| public boolean shouldCopyCoreTests() { | ||
| return copyTests; | ||
| } | ||
|
|
||
| public void includeXpackTests(String include) { | ||
| includesXpackTests.add(include); | ||
| } | ||
|
|
||
| public List<String> getIncludesXpackTests() { | ||
| return includesXpackTests; | ||
| } | ||
|
|
||
| public void copyXpackTests(boolean copyTests) { | ||
| this.copyXpackTests = copyTests; | ||
| } | ||
|
|
||
| public boolean shouldCopyXpackTests() { | ||
| return copyXpackTests; | ||
| } | ||
|
|
||
| public boolean shouldAlwaysCopySpec() { | ||
| return alwaysCopySpec; | ||
| } | ||
|
|
||
| public void alwaysCopySpec(boolean alwaysCopySpec) { | ||
| this.alwaysCopySpec = alwaysCopySpec; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| package org.elasticsearch.gradle.test; | ||
|
|
||
| import org.elasticsearch.gradle.VersionProperties; | ||
| import org.elasticsearch.gradle.info.BuildParams; | ||
| import org.elasticsearch.gradle.tool.Boilerplate; | ||
| import org.gradle.api.Plugin; | ||
| import org.gradle.api.Project; | ||
| import org.gradle.api.artifacts.Configuration; | ||
| import org.gradle.api.artifacts.Dependency; | ||
| import org.gradle.api.logging.Logger; | ||
| import org.gradle.api.logging.Logging; | ||
| import org.gradle.api.tasks.Copy; | ||
| import org.gradle.api.tasks.SourceSet; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| //TODO: change logger.info to logger.debug | ||
| public class RestApiSpecForTestingPlugin implements Plugin<Project> { | ||
|
|
||
| private static final Logger logger = Logging.getLogger(RestApiSpecForTestingPlugin.class); | ||
| private static final String EXTENSION_NAME = "restApiSpec"; | ||
|
||
| private static final String apiDir = "rest-api-spec/api"; | ||
| private static final String testDir = "rest-api-spec/test"; | ||
|
|
||
| @Override | ||
| public void apply(Project project) { | ||
|
|
||
| RestApiSpecExtension extension = project.getExtensions().create(EXTENSION_NAME, RestApiSpecExtension.class); | ||
| // need to defer to after evaluation to allow the custom extension to be populated | ||
| project.afterEvaluate(p -> { | ||
| try { | ||
| // copy tests | ||
| boolean hasCopiedTests = false; | ||
| if (extension.shouldCopyCoreTests()) { | ||
jakelandis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Configuration coreTestConfig = project.getConfigurations().create("copyRestSpecCoreTests"); | ||
| Dependency coreTestDep = project.getDependencies() | ||
| .project(Map.of("path", ":rest-api-spec", "configuration", "restSpecCoreTests")); | ||
| logger.info("Rest specs tests for project [{}] will be copied to the test resources.", project.getPath()); | ||
| createCopyTask(project, coreTestConfig, coreTestDep, extension.getIncludesCoreTests(), testDir, false); | ||
| hasCopiedTests = true; | ||
| } | ||
| if (extension.shouldCopyXpackTests()) { | ||
| Configuration xpackSpecTestsConfig = project.getConfigurations().create("copyRestSpecXpackTests"); | ||
| Dependency xpackTestsDep = project.getDependencies() | ||
| .project(Map.of("path", ":x-pack:plugin", "configuration", "restSpecXpackTests")); | ||
| logger.info("Rest specs x-pack tests for project [{}] will be copied to the test resources.", project.getPath()); | ||
| createCopyTask(project, xpackSpecTestsConfig, xpackTestsDep, extension.getIncludesXpackTests(), testDir, false); | ||
| hasCopiedTests = true; | ||
| } | ||
| // copy specs | ||
| if (projectHasRestTests(project) || hasCopiedTests || extension.shouldAlwaysCopySpec()) { | ||
| Configuration coreSpecConfig = project.getConfigurations().create("restSpec"); // name chosen for passivity | ||
| Dependency coreSpecDep = project.getDependencies() | ||
| .project(Map.of("path", ":rest-api-spec", "configuration", "restSpecCore")); | ||
|
|
||
| if (BuildParams.isInternal()) { | ||
| // source the specs from this project - this is the path for Elasticsearch builds | ||
| if (project.getPath().startsWith(":x-pack")) { | ||
| Configuration xpackSpecConfig = project.getConfigurations().create("copyRestSpecXpack"); | ||
| Dependency xpackSpecDep = project.getDependencies() | ||
| .project(Map.of("path", ":x-pack:plugin", "configuration", "restSpecXpack")); | ||
| logger.info("X-pack rest specs for project [{}] will be copied to the test resources.", project.getPath()); | ||
| createCopyTask(project, xpackSpecConfig, xpackSpecDep, extension.getIncludesXpackSpec(), apiDir, false); | ||
| } | ||
|
|
||
| logger.info("Rest specs for project [{}] will be copied to the test resources.", project.getPath()); | ||
| createCopyTask(project, coreSpecConfig, coreSpecDep, extension.getIncludesCoreSpec(), apiDir, false); | ||
|
|
||
| } else { | ||
| // source the specs from the published jar - this is the path plugin developers | ||
| logger.info( | ||
| "Rest specs for project [{}] will be copied to the test resources from the published jar (version: [{}]).", | ||
| project.getPath(), | ||
| VersionProperties.getElasticsearch() | ||
| ); | ||
| Dependency coreSpecFromJarDep = project.getDependencies() | ||
| .create("org.elasticsearch:rest-api-spec:" + VersionProperties.getElasticsearch()); | ||
|
|
||
| createCopyTask(project, coreSpecConfig, coreSpecFromJarDep, extension.getIncludesCoreSpec(), "", true); | ||
| } | ||
| } else { | ||
| logger.info("Rest specs will be ignored for project [{}] since there are no REST tests", project.getPath()); | ||
| return; | ||
| } | ||
| } catch (Exception e) { | ||
| throw new IllegalStateException("Error configuring the rest-api-spec-for-testing plugin. This is likely a bug.", e); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private void createCopyTask(Project project, Configuration config, Dependency dep, List<String> includes, String into, boolean isJar) { | ||
| project.getDependencies().add(config.getName(), dep); | ||
| Copy copyTask = project.getTasks().create(config.getName() + "Task", Copy.class, copy -> { | ||
jakelandis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| copy.from(isJar ? project.zipTree(config.getSingleFile()) : config.getSingleFile()); | ||
jakelandis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| copy.into(new File(getTestSourceSet(project).getOutput().getResourcesDir(), into)); | ||
| if (includes.isEmpty()) { | ||
| copy.include(isJar ? apiDir + "/**" : "*/**"); | ||
| } else { | ||
| includes.forEach(s -> copy.include(isJar ? apiDir + "/" + s + "*/**" : s + "*/**")); | ||
| } | ||
| copy.setIncludeEmptyDirs(false); | ||
jakelandis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| copyTask.dependsOn(config); | ||
| project.getTasks().getByName("processTestResources").dependsOn(copyTask); | ||
| } | ||
|
|
||
| private boolean projectHasRestTests(Project project) throws IOException { | ||
| File testResourceDir = getTestResourceDir(project); | ||
| if (testResourceDir == null || new File(testResourceDir, "rest-api-spec/test").exists() == false) { | ||
| return false; | ||
| } | ||
| return Files.walk(testResourceDir.toPath().resolve("rest-api-spec/test")).anyMatch(p -> p.getFileName().toString().endsWith("yml")); | ||
| } | ||
|
|
||
| private File getTestResourceDir(Project project) { | ||
| SourceSet testSources = getTestSourceSet(project); | ||
| if (testSources == null) { | ||
| return null; | ||
| } | ||
| Set<File> resourceDir = testSources.getResources() | ||
| .getSrcDirs() | ||
| .stream() | ||
| .filter(f -> f.isDirectory() && f.getParentFile().getName().equals("test") && f.getName().equals("resources")) | ||
| .collect(Collectors.toSet()); | ||
| assert resourceDir.size() <= 1; | ||
| if (resourceDir.size() == 0) { | ||
| return null; | ||
| } | ||
| return resourceDir.iterator().next(); | ||
| } | ||
|
|
||
| private SourceSet getTestSourceSet(Project project) { | ||
| return Boilerplate.getJavaSourceSets(project).findByName("test"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # | ||
| # 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. | ||
| # | ||
|
|
||
| implementation-class=org.elasticsearch.gradle.test.RestApiSpecForTestingPlugin |
Uh oh!
There was an error while loading. Please reload this page.