Skip to content

Commit

Permalink
Fix configuration cache compatibility issues in YamlRestCompatTestPlugin
Browse files Browse the repository at this point in the history
This fixes references to project that makes the plugin incompatible with Gradle
configuration cache

Related to elastic#57918
  • Loading branch information
breskeby committed Jun 9, 2022
1 parent 779871e commit 2dcdab6
Showing 1 changed file with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.file.Directory;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.file.RelativePath;
import org.gradle.api.internal.file.FileOperations;
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.SourceSet;
Expand All @@ -40,6 +42,8 @@
import java.util.Arrays;
import java.util.Map;

import javax.inject.Inject;

import static org.elasticsearch.gradle.internal.test.rest.RestTestUtil.setupYamlRestTestDependenciesDefaults;

/**
Expand All @@ -57,6 +61,14 @@ public class YamlRestCompatTestPlugin implements Plugin<Project> {
private static final Path RELATIVE_REST_PROJECT_RESOURCES = Path.of("src/yamlRestTest/resources");
private static final int COMPATIBLE_VERSION = Version.fromString(VersionProperties.getVersions().get("elasticsearch")).getMajor() - 1;
private static final String SOURCE_SET_NAME = "yamlRestTestV" + COMPATIBLE_VERSION + "Compat";
private ProjectLayout projectLayout;
private FileOperations fileOperations;

@Inject
public YamlRestCompatTestPlugin(ProjectLayout projectLayout, FileOperations fileOperations) {
this.projectLayout = projectLayout;
this.fileOperations = fileOperations;
}

@Override
public void apply(Project project) {
Expand Down Expand Up @@ -85,13 +97,14 @@ public void apply(Project project) {
.project(Map.of("path", ":distribution:bwc:maintenance", "configuration", "checkout"));
project.getDependencies().add(bwcMinorConfig.getName(), bwcMinor);

String projectPath = project.getPath();
Provider<CopyRestApiTask> copyCompatYamlSpecTask = project.getTasks()
.register("copyRestCompatApiTask", CopyRestApiTask.class, task -> {
task.dependsOn(bwcMinorConfig);
task.setConfig(bwcMinorConfig);
task.setAdditionalConfig(bwcMinorConfig);
task.getInclude().set(extension.getRestApi().getInclude());
task.getOutputResourceDir().set(project.getLayout().getBuildDirectory().dir(compatSpecsDir.toString()));
task.getOutputResourceDir().set(projectLayout.getBuildDirectory().dir(compatSpecsDir.toString()));
task.setSourceResourceDir(
yamlCompatTestSourceSet.getResources()
.getSrcDirs()
Expand All @@ -102,13 +115,13 @@ public void apply(Project project) {
);
task.setSkipHasRestTestCheck(true);
task.setConfigToFileTree(
config -> project.fileTree(
config -> fileOperations.fileTree(
config.getSingleFile().toPath().resolve(RELATIVE_REST_API_RESOURCES).resolve(RELATIVE_API_PATH)
)
);
task.setAdditionalConfigToFileTree(
config -> project.fileTree(
getCompatProjectPath(project, config.getSingleFile().toPath()).resolve(RELATIVE_REST_PROJECT_RESOURCES)
config -> fileOperations.fileTree(
getCompatProjectPath(projectPath, config.getSingleFile().toPath()).resolve(RELATIVE_REST_PROJECT_RESOURCES)
.resolve(RELATIVE_API_PATH)
)
);
Expand All @@ -124,20 +137,20 @@ public void apply(Project project) {
task.setAdditionalConfig(bwcMinorConfig);
task.getIncludeCore().set(extension.getRestTests().getIncludeCore());
task.getIncludeXpack().set(extension.getRestTests().getIncludeXpack());
task.getOutputResourceDir().set(project.getLayout().getBuildDirectory().dir(compatTestsDir.resolve("original").toString()));
task.getOutputResourceDir().set(projectLayout.getBuildDirectory().dir(compatTestsDir.resolve("original").toString()));
task.setCoreConfigToFileTree(
config -> project.fileTree(
config -> fileOperations.fileTree(
config.getSingleFile().toPath().resolve(RELATIVE_REST_API_RESOURCES).resolve(RELATIVE_TEST_PATH)
)
);
task.setXpackConfigToFileTree(
config -> project.fileTree(
config -> fileOperations.fileTree(
config.getSingleFile().toPath().resolve(RELATIVE_REST_XPACK_RESOURCES).resolve(RELATIVE_TEST_PATH)
)
);
task.setAdditionalConfigToFileTree(
config -> project.fileTree(
getCompatProjectPath(project, config.getSingleFile().toPath()).resolve(RELATIVE_REST_PROJECT_RESOURCES)
config -> fileOperations.fileTree(
getCompatProjectPath(projectPath, config.getSingleFile().toPath()).resolve(RELATIVE_REST_PROJECT_RESOURCES)
.resolve(RELATIVE_TEST_PATH)
)
);
Expand All @@ -147,7 +160,7 @@ public void apply(Project project) {

// copy both local source set apis and compat apis to a single location to be exported as an artifact
TaskProvider<Sync> bundleRestCompatApis = project.getTasks().register("bundleRestCompatApis", Sync.class, task -> {
task.setDestinationDir(project.getLayout().getBuildDirectory().dir("bundledCompatApis").get().getAsFile());
task.setDestinationDir(projectLayout.getBuildDirectory().dir("bundledCompatApis").get().getAsFile());
task.setIncludeEmptyDirs(false);
task.from(copyCompatYamlSpecTask.flatMap(t -> t.getOutputResourceDir().map(d -> d.dir(RELATIVE_API_PATH.toString()))));
task.from(yamlCompatTestSourceSet.getProcessResourcesTaskName(), s -> {
Expand Down Expand Up @@ -241,7 +254,7 @@ private boolean isEnabled(Project project) {
}

// TODO: implement custom extension that allows us move around of the projects between major versions and still find them
private Path getCompatProjectPath(Project project, Path checkoutDir) {
return checkoutDir.resolve(project.getPath().replaceFirst(":", "").replace(":", File.separator));
private Path getCompatProjectPath(String projectPath, Path checkoutDir) {
return checkoutDir.resolve(projectPath.replaceFirst(":", "").replace(":", File.separator));
}
}

0 comments on commit 2dcdab6

Please sign in to comment.