[7.x] Fix incremental build support copying REST api and tests (#52896)

A recent PR #52114 introduced two new tasks to copy the REST api and tests.
A couple bugs were found in that initial PR that prevents the incremental
build from working as expected.

The pattern match of empty string is equivalent to match all and it was coded
as match none. Fixed with explicit checks against empty patterns.

The fileCollection.plus return value was ignored. Fixed by changing how the
input's fileTree is constructed.

If a project has an src/test/resources directory, and tests are being copied
without a rest-api-spec/test directory could result no-op. Masked by the other
bugs and fixed by minor changes to logic to determine if a project has tests.
This commit is contained in:
Jake Landis 2020-02-27 12:44:00 -06:00 committed by GitHub
parent eac38e9847
commit 1c29140f24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 23 deletions

View File

@ -84,16 +84,25 @@ public class CopyRestApiTask extends DefaultTask {
@SkipWhenEmpty @SkipWhenEmpty
@InputFiles @InputFiles
public FileTree getInputDir() { public FileTree getInputDir() {
xpackPatternSet.setIncludes(includeXpack.get().stream().map(prefix -> prefix + "*/**").collect(Collectors.toList())); FileTree coreFileTree = null;
ConfigurableFileCollection fileCollection = getProject().files(xpackConfig.getAsFileTree().matching(xpackPatternSet)); FileTree xpackFileTree = null;
if (BuildParams.isInternal()) { if (includeXpack.get().isEmpty() == false) {
corePatternSet.setIncludes(includeCore.get().stream().map(prefix -> prefix + "*/**").collect(Collectors.toList())); xpackPatternSet.setIncludes(includeXpack.get().stream().map(prefix -> prefix + "*/**").collect(Collectors.toList()));
fileCollection.plus(coreConfig.getAsFileTree().matching(corePatternSet)); xpackFileTree = xpackConfig.getAsFileTree().matching(xpackPatternSet);
} else {
fileCollection.plus(coreConfig);
} }
boolean projectHasYamlRestTests = projectHasYamlRestTests();
if (includeCore.get().isEmpty() == false || projectHasYamlRestTests) {
if (BuildParams.isInternal()) {
corePatternSet.setIncludes(includeCore.get().stream().map(prefix -> prefix + "*/**").collect(Collectors.toList()));
coreFileTree = coreConfig.getAsFileTree().matching(corePatternSet); // directory on disk
} else {
coreFileTree = coreConfig.getAsFileTree(); // jar file
}
}
ConfigurableFileCollection fileCollection = getProject().files(coreFileTree, xpackFileTree);
// if project has rest tests or the includes are explicitly configured execute the task, else NO-SOURCE due to the null input // if project has rest tests or the includes are explicitly configured execute the task, else NO-SOURCE due to the null input
return projectHasYamlRestTests() || includeCore.get().isEmpty() == false || includeXpack.get().isEmpty() == false return projectHasYamlRestTests || includeCore.get().isEmpty() == false || includeXpack.get().isEmpty() == false
? fileCollection.getAsFileTree() ? fileCollection.getAsFileTree()
: null; : null;
} }
@ -148,15 +157,13 @@ public class CopyRestApiTask extends DefaultTask {
return false; return false;
} }
try { try {
if (testSourceResourceDir != null) { if (testSourceResourceDir != null && new File(testSourceResourceDir, "rest-api-spec/test").exists()) {
return new File(testSourceResourceDir, "rest-api-spec/test").exists() == false return Files.walk(testSourceResourceDir.toPath().resolve("rest-api-spec/test"))
|| Files.walk(testSourceResourceDir.toPath().resolve("rest-api-spec/test")) .anyMatch(p -> p.getFileName().toString().endsWith("yml"));
.anyMatch(p -> p.getFileName().toString().endsWith("yml"));
} }
if (testOutputResourceDir != null) { if (testOutputResourceDir != null && new File(testOutputResourceDir, "rest-api-spec/test").exists()) {
return new File(testOutputResourceDir, "rest-api-spec/test").exists() == false return Files.walk(testOutputResourceDir.toPath().resolve("rest-api-spec/test"))
|| Files.walk(testOutputResourceDir.toPath().resolve("rest-api-spec/test")) .anyMatch(p -> p.getFileName().toString().endsWith("yml"));
.anyMatch(p -> p.getFileName().toString().endsWith("yml"));
} }
} catch (IOException e) { } catch (IOException e) {
throw new IllegalStateException(String.format("Error determining if this project [%s] has rest tests.", getProject()), e); throw new IllegalStateException(String.format("Error determining if this project [%s] has rest tests.", getProject()), e);

View File

@ -81,14 +81,22 @@ public class CopyRestTestsTask extends DefaultTask {
@SkipWhenEmpty @SkipWhenEmpty
@InputFiles @InputFiles
public FileTree getInputDir() { public FileTree getInputDir() {
xpackPatternSet.setIncludes(includeXpack.get().stream().map(prefix -> prefix + "*/**").collect(Collectors.toList())); FileTree coreFileTree = null;
ConfigurableFileCollection fileCollection = getProject().files(xpackConfig.getAsFileTree().matching(xpackPatternSet)); FileTree xpackFileTree = null;
if (BuildParams.isInternal()) { if (includeXpack.get().isEmpty() == false) {
corePatternSet.setIncludes(includeCore.get().stream().map(prefix -> prefix + "*/**").collect(Collectors.toList())); xpackPatternSet.setIncludes(includeXpack.get().stream().map(prefix -> prefix + "*/**").collect(Collectors.toList()));
fileCollection.plus(coreConfig.getAsFileTree().matching(corePatternSet)); xpackFileTree = xpackConfig.getAsFileTree().matching(xpackPatternSet);
} else {
fileCollection.plus(coreConfig);
} }
if (includeCore.get().isEmpty() == false) {
if (BuildParams.isInternal()) {
corePatternSet.setIncludes(includeCore.get().stream().map(prefix -> prefix + "*/**").collect(Collectors.toList()));
coreFileTree = coreConfig.getAsFileTree().matching(corePatternSet); // directory on disk
} else {
coreFileTree = coreConfig.getAsFileTree(); // jar file
}
}
ConfigurableFileCollection fileCollection = getProject().files(coreFileTree, xpackFileTree);
// copy tests only if explicitly requested // copy tests only if explicitly requested
return includeCore.get().isEmpty() == false || includeXpack.get().isEmpty() == false ? fileCollection.getAsFileTree() : null; return includeCore.get().isEmpty() == false || includeXpack.get().isEmpty() == false ? fileCollection.getAsFileTree() : null;
} }