|
|
@@ -20,22 +20,18 @@ package org.elasticsearch.gradle.test.rest;
|
|
|
|
|
|
import org.elasticsearch.gradle.VersionProperties;
|
|
|
import org.elasticsearch.gradle.info.BuildParams;
|
|
|
-import org.elasticsearch.gradle.util.GradleUtils;
|
|
|
import org.gradle.api.DefaultTask;
|
|
|
-import org.gradle.api.Project;
|
|
|
import org.gradle.api.file.ArchiveOperations;
|
|
|
import org.gradle.api.file.FileCollection;
|
|
|
import org.gradle.api.file.FileSystemOperations;
|
|
|
import org.gradle.api.file.FileTree;
|
|
|
import org.gradle.api.file.ProjectLayout;
|
|
|
-import org.gradle.api.plugins.JavaPluginConvention;
|
|
|
import org.gradle.api.provider.ListProperty;
|
|
|
import org.gradle.api.tasks.Input;
|
|
|
import org.gradle.api.tasks.InputFiles;
|
|
|
import org.gradle.api.tasks.Internal;
|
|
|
import org.gradle.api.tasks.OutputDirectory;
|
|
|
import org.gradle.api.tasks.SkipWhenEmpty;
|
|
|
-import org.gradle.api.tasks.SourceSet;
|
|
|
import org.gradle.api.tasks.TaskAction;
|
|
|
import org.gradle.api.tasks.util.PatternFilterable;
|
|
|
import org.gradle.api.tasks.util.PatternSet;
|
|
|
@@ -46,8 +42,6 @@ import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.nio.file.Files;
|
|
|
import java.util.Objects;
|
|
|
-import java.util.Optional;
|
|
|
-import java.util.Set;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@@ -57,13 +51,17 @@ import static org.elasticsearch.gradle.util.GradleUtils.getProjectPathFromTask;
|
|
|
* Copies the files needed for the Rest YAML specs to the current projects test resources output directory.
|
|
|
* This is intended to be be used from {@link RestResourcesPlugin} since the plugin wires up the needed
|
|
|
* configurations and custom extensions.
|
|
|
+ *
|
|
|
* @see RestResourcesPlugin
|
|
|
*/
|
|
|
public class CopyRestApiTask extends DefaultTask {
|
|
|
private static final String REST_API_PREFIX = "rest-api-spec/api";
|
|
|
+ private static final String REST_TEST_PREFIX = "rest-api-spec/test";
|
|
|
private final ListProperty<String> includeCore = getProject().getObjects().listProperty(String.class);
|
|
|
private final ListProperty<String> includeXpack = getProject().getObjects().listProperty(String.class);
|
|
|
- private String sourceSetName;
|
|
|
+
|
|
|
+ private File outputResourceDir;
|
|
|
+ private File sourceResourceDir;
|
|
|
private boolean skipHasRestTestCheck;
|
|
|
private FileCollection coreConfig;
|
|
|
private FileCollection xpackConfig;
|
|
|
@@ -75,27 +73,21 @@ public class CopyRestApiTask extends DefaultTask {
|
|
|
private final PatternFilterable corePatternSet;
|
|
|
private final PatternFilterable xpackPatternSet;
|
|
|
private final ProjectLayout projectLayout;
|
|
|
+ private final FileSystemOperations fileSystemOperations;
|
|
|
+ private final ArchiveOperations archiveOperations;
|
|
|
|
|
|
@Inject
|
|
|
- public CopyRestApiTask(ProjectLayout projectLayout) {
|
|
|
- corePatternSet = getPatternSetFactory().create();
|
|
|
- xpackPatternSet = getPatternSetFactory().create();
|
|
|
+ public CopyRestApiTask(
|
|
|
+ ProjectLayout projectLayout,
|
|
|
+ Factory<PatternSet> patternSetFactory,
|
|
|
+ FileSystemOperations fileSystemOperations,
|
|
|
+ ArchiveOperations archiveOperations
|
|
|
+ ) {
|
|
|
+ corePatternSet = patternSetFactory.create();
|
|
|
+ xpackPatternSet = patternSetFactory.create();
|
|
|
this.projectLayout = projectLayout;
|
|
|
- }
|
|
|
-
|
|
|
- @Inject
|
|
|
- protected Factory<PatternSet> getPatternSetFactory() {
|
|
|
- throw new UnsupportedOperationException();
|
|
|
- }
|
|
|
-
|
|
|
- @Inject
|
|
|
- protected FileSystemOperations getFileSystemOperations() {
|
|
|
- throw new UnsupportedOperationException();
|
|
|
- }
|
|
|
-
|
|
|
- @Inject
|
|
|
- protected ArchiveOperations getArchiveOperations() {
|
|
|
- throw new UnsupportedOperationException();
|
|
|
+ this.fileSystemOperations = fileSystemOperations;
|
|
|
+ this.archiveOperations = archiveOperations;
|
|
|
}
|
|
|
|
|
|
@Input
|
|
|
@@ -108,11 +100,6 @@ public class CopyRestApiTask extends DefaultTask {
|
|
|
return includeXpack;
|
|
|
}
|
|
|
|
|
|
- @Input
|
|
|
- String getSourceSetName() {
|
|
|
- return sourceSetName;
|
|
|
- }
|
|
|
-
|
|
|
@Input
|
|
|
public boolean isSkipHasRestTestCheck() {
|
|
|
return skipHasRestTestCheck;
|
|
|
@@ -149,12 +136,7 @@ public class CopyRestApiTask extends DefaultTask {
|
|
|
|
|
|
@OutputDirectory
|
|
|
public File getOutputDir() {
|
|
|
- return new File(
|
|
|
- getSourceSet().orElseThrow(() -> new IllegalArgumentException("could not find source set [" + sourceSetName + "]"))
|
|
|
- .getOutput()
|
|
|
- .getResourcesDir(),
|
|
|
- REST_API_PREFIX
|
|
|
- );
|
|
|
+ return new File(outputResourceDir, REST_API_PREFIX);
|
|
|
}
|
|
|
|
|
|
@TaskAction
|
|
|
@@ -163,7 +145,7 @@ public class CopyRestApiTask extends DefaultTask {
|
|
|
String projectPath = getProjectPathFromTask(getPath());
|
|
|
if (BuildParams.isInternal()) {
|
|
|
getLogger().debug("Rest specs for project [{}] will be copied to the test resources.", projectPath);
|
|
|
- getFileSystemOperations().copy(c -> {
|
|
|
+ fileSystemOperations.copy(c -> {
|
|
|
c.from(coreConfigToFileTree.apply(coreConfig));
|
|
|
c.into(getOutputDir());
|
|
|
c.include(corePatternSet.getIncludes());
|
|
|
@@ -174,10 +156,9 @@ public class CopyRestApiTask extends DefaultTask {
|
|
|
projectPath,
|
|
|
VersionProperties.getElasticsearch()
|
|
|
);
|
|
|
- getFileSystemOperations().copy(c -> {
|
|
|
- c.from(getArchiveOperations().zipTree(coreConfig.getSingleFile())); // jar file
|
|
|
- // this ends up as the same dir as outputDir
|
|
|
- c.into(Objects.requireNonNull(getSourceSet().orElseThrow().getOutput().getResourcesDir()));
|
|
|
+ fileSystemOperations.copy(c -> {
|
|
|
+ c.from(archiveOperations.zipTree(coreConfig.getSingleFile())); // jar file
|
|
|
+ c.into(Objects.requireNonNull(outputResourceDir));
|
|
|
if (includeCore.get().isEmpty()) {
|
|
|
c.include(REST_API_PREFIX + "/**");
|
|
|
} else {
|
|
|
@@ -190,16 +171,16 @@ public class CopyRestApiTask extends DefaultTask {
|
|
|
// only copy x-pack specs if explicitly instructed
|
|
|
if (includeXpack.get().isEmpty() == false) {
|
|
|
getLogger().debug("X-pack rest specs for project [{}] will be copied to the test resources.", projectPath);
|
|
|
- getFileSystemOperations().copy(c -> {
|
|
|
+ fileSystemOperations.copy(c -> {
|
|
|
c.from(xpackConfigToFileTree.apply(xpackConfig));
|
|
|
c.into(getOutputDir());
|
|
|
c.include(xpackPatternSet.getIncludes());
|
|
|
});
|
|
|
}
|
|
|
- // TODO: once https://github.com/elastic/elasticsearch/pull/62968 lands ensure that this uses `getFileSystemOperations()`
|
|
|
+
|
|
|
// copy any additional config
|
|
|
if (additionalConfig != null) {
|
|
|
- getFileSystemOperations().copy(c -> {
|
|
|
+ fileSystemOperations.copy(c -> {
|
|
|
c.from(additionalConfigToFileTree.apply(additionalConfig));
|
|
|
c.into(getOutputDir());
|
|
|
});
|
|
|
@@ -210,19 +191,18 @@ public class CopyRestApiTask extends DefaultTask {
|
|
|
* Returns true if any files with a .yml extension exist the test resources rest-api-spec/test directory (from source or output dir)
|
|
|
*/
|
|
|
private boolean projectHasYamlRestTests() {
|
|
|
- File testSourceResourceDir = getTestSourceResourceDir();
|
|
|
- File testOutputResourceDir = getTestOutputResourceDir(); // check output for cases where tests are copied programmatically
|
|
|
-
|
|
|
- if (testSourceResourceDir == null && testOutputResourceDir == null) {
|
|
|
+ if (sourceResourceDir == null && outputResourceDir == null) {
|
|
|
return false;
|
|
|
}
|
|
|
try {
|
|
|
- if (testSourceResourceDir != null && new File(testSourceResourceDir, "rest-api-spec/test").exists()) {
|
|
|
- return Files.walk(testSourceResourceDir.toPath().resolve("rest-api-spec/test"))
|
|
|
+ // check source folder for tests
|
|
|
+ if (sourceResourceDir != null && new File(sourceResourceDir, REST_TEST_PREFIX).exists()) {
|
|
|
+ return Files.walk(sourceResourceDir.toPath().resolve(REST_TEST_PREFIX))
|
|
|
.anyMatch(p -> p.getFileName().toString().endsWith("yml"));
|
|
|
}
|
|
|
- if (testOutputResourceDir != null && new File(testOutputResourceDir, "rest-api-spec/test").exists()) {
|
|
|
- return Files.walk(testOutputResourceDir.toPath().resolve("rest-api-spec/test"))
|
|
|
+ // check output for cases where tests are copied programmatically
|
|
|
+ if (outputResourceDir != null && new File(outputResourceDir, REST_TEST_PREFIX).exists()) {
|
|
|
+ return Files.walk(new File(outputResourceDir, REST_TEST_PREFIX).toPath())
|
|
|
.anyMatch(p -> p.getFileName().toString().endsWith("yml"));
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
@@ -231,39 +211,12 @@ public class CopyRestApiTask extends DefaultTask {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- private File getTestSourceResourceDir() {
|
|
|
- Optional<SourceSet> testSourceSet = getSourceSet();
|
|
|
- if (testSourceSet.isPresent()) {
|
|
|
- SourceSet testSources = testSourceSet.get();
|
|
|
- Set<File> resourceDir = testSources.getResources()
|
|
|
- .getSrcDirs()
|
|
|
- .stream()
|
|
|
- .filter(f -> f.isDirectory() && f.getParentFile().getName().equals(getSourceSetName()) && f.getName().equals("resources"))
|
|
|
- .collect(Collectors.toSet());
|
|
|
- assert resourceDir.size() <= 1;
|
|
|
- if (resourceDir.size() == 0) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- return resourceDir.iterator().next();
|
|
|
- } else {
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private File getTestOutputResourceDir() {
|
|
|
- Optional<SourceSet> testSourceSet = getSourceSet();
|
|
|
- return testSourceSet.map(sourceSet -> sourceSet.getOutput().getResourcesDir()).orElse(null);
|
|
|
+ public void setSourceResourceDir(File sourceResourceDir) {
|
|
|
+ this.sourceResourceDir = sourceResourceDir;
|
|
|
}
|
|
|
|
|
|
- private Optional<SourceSet> getSourceSet() {
|
|
|
- Project project = getProject();
|
|
|
- return project.getConvention().findPlugin(JavaPluginConvention.class) == null
|
|
|
- ? Optional.empty()
|
|
|
- : Optional.ofNullable(GradleUtils.getJavaSourceSets(project).findByName(getSourceSetName()));
|
|
|
- }
|
|
|
-
|
|
|
- public void setSourceSetName(String sourceSetName) {
|
|
|
- this.sourceSetName = sourceSetName;
|
|
|
+ public void setOutputResourceDir(File outputResourceDir) {
|
|
|
+ this.outputResourceDir = outputResourceDir;
|
|
|
}
|
|
|
|
|
|
public void setSkipHasRestTestCheck(boolean skipHasRestTestCheck) {
|
|
|
@@ -303,4 +256,5 @@ public class CopyRestApiTask extends DefaultTask {
|
|
|
public FileCollection getXpackConfig() {
|
|
|
return xpackConfig;
|
|
|
}
|
|
|
+
|
|
|
}
|