Skip to content

Commit 6852417

Browse files
prdoyleelasticsearchmachineldematte
authored
[8.19] Improve TestBuildInfoPluginFuncTest (#128322) (#128406)
* Improve TestBuildInfoPluginFuncTest (#128322) * Use walkFileTree for extractModuleNameFromDirectory * More tests in TestBuildInfoPluginFuncTest * Remove stray line from debugging * [CI] Auto commit changes from spotless * Eliminate List.reversed() call --------- Co-authored-by: elasticsearchmachine <[email protected]> * Fix the Text class package change in example plugins (#128316) --------- Co-authored-by: elasticsearchmachine <[email protected]> Co-authored-by: Lorenzo Dematté <[email protected]>
1 parent c32b447 commit 6852417

File tree

4 files changed

+83
-19
lines changed

4 files changed

+83
-19
lines changed

build-tools/src/integTest/groovy/org/elasticsearch/gradle/test/TestBuildInfoPluginFuncTest.groovy

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import org.elasticsearch.gradle.fixtures.AbstractGradleFuncTest
66
import org.gradle.testkit.runner.TaskOutcome
77

88
class TestBuildInfoPluginFuncTest extends AbstractGradleFuncTest {
9-
def "works"() {
9+
def "basic functionality"() {
1010
given:
1111
file("src/main/java/com/example/Example.java") << """
1212
package com.example;
@@ -60,4 +60,62 @@ class TestBuildInfoPluginFuncTest extends AbstractGradleFuncTest {
6060
)
6161
new ObjectMapper().readValue(output, Map.class) == expectedOutput
6262
}
63+
64+
def "dependencies"() {
65+
buildFile << """
66+
import org.elasticsearch.gradle.plugin.GenerateTestBuildInfoTask;
67+
68+
plugins {
69+
id 'java'
70+
id 'elasticsearch.test-build-info'
71+
}
72+
73+
repositories {
74+
mavenCentral()
75+
}
76+
77+
dependencies {
78+
// We pin to specific versions here because they are known to have the properties we want to test.
79+
// We're not actually running this code.
80+
implementation "org.ow2.asm:asm:9.7.1" // has module-info.class
81+
implementation "junit:junit:4.13" // has Automatic-Module-Name, and brings in hamcrest which does not
82+
}
83+
84+
tasks.withType(GenerateTestBuildInfoTask.class) {
85+
componentName = 'example-component'
86+
outputFile = new File('build/generated-build-info/plugin-test-build-info.json')
87+
}
88+
"""
89+
90+
when:
91+
def result = gradleRunner('generateTestBuildInfo').build()
92+
def task = result.task(":generateTestBuildInfo")
93+
94+
95+
then:
96+
task.outcome == TaskOutcome.SUCCESS
97+
98+
def output = file("build/generated-build-info/plugin-test-build-info.json")
99+
output.exists() == true
100+
101+
def locationFromModuleInfo = Map.of(
102+
"module", "org.objectweb.asm",
103+
"representative_class", 'org/objectweb/asm/AnnotationVisitor.class'
104+
)
105+
def locationFromManifest = Map.of(
106+
"module", "junit",
107+
"representative_class", 'junit/textui/TestRunner.class'
108+
)
109+
def locationFromJarFileName = Map.of(
110+
"module", "hamcrest.core",
111+
"representative_class", 'org/hamcrest/BaseDescription.class'
112+
)
113+
def expectedOutput = Map.of(
114+
"component", "example-component",
115+
"locations", List.of(locationFromModuleInfo, locationFromManifest, locationFromJarFileName)
116+
)
117+
118+
def value = new ObjectMapper().readValue(output, Map.class)
119+
value == expectedOutput
120+
}
63121
}

build-tools/src/main/java/org/elasticsearch/gradle/plugin/GenerateTestBuildInfoTask.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import java.nio.file.attribute.BasicFileAttributes;
4242
import java.security.CodeSource;
4343
import java.util.ArrayList;
44-
import java.util.Arrays;
44+
import java.util.Comparator;
4545
import java.util.List;
4646
import java.util.jar.JarEntry;
4747
import java.util.jar.JarFile;
@@ -209,17 +209,16 @@ private String extractModuleNameFromJar(File file, JarFile jarFile) throws IOExc
209209
* @return a {@link StringBuilder} with the {@code META-INF/versions/<version number>} if it exists; otherwise null
210210
*/
211211
private static StringBuilder versionDirectoryIfExists(JarFile jarFile) {
212+
Comparator<Integer> numericOrder = Integer::compareTo;
212213
List<Integer> versions = jarFile.stream()
213214
.filter(je -> je.getName().startsWith(META_INF_VERSIONS_PREFIX) && je.getName().endsWith("/module-info.class"))
214215
.map(
215216
je -> Integer.parseInt(
216217
je.getName().substring(META_INF_VERSIONS_PREFIX.length(), je.getName().length() - META_INF_VERSIONS_PREFIX.length())
217218
)
218219
)
220+
.sorted(numericOrder.reversed())
219221
.toList();
220-
versions = new ArrayList<>(versions);
221-
versions.sort(Integer::compareTo);
222-
versions = versions.reversed();
223222
int major = Runtime.version().feature();
224223
StringBuilder path = new StringBuilder(META_INF_VERSIONS_PREFIX);
225224
for (int version : versions) {
@@ -300,7 +299,10 @@ private String extractClassNameFromDirectory(File dir) throws IOException {
300299
public @NotNull FileVisitResult visitFile(@NotNull Path candidate, @NotNull BasicFileAttributes attrs) {
301300
String name = candidate.getFileName().toString(); // Just the part after the last dir separator
302301
if (name.endsWith(".class") && (name.equals("module-info.class") || name.contains("$")) == false) {
303-
result = candidate.toAbsolutePath().toString().substring(dir.getAbsolutePath().length() + 1);
302+
result = candidate.toAbsolutePath()
303+
.toString()
304+
.substring(dir.getAbsolutePath().length() + 1)
305+
.replace(File.separatorChar, '/');
304306
return TERMINATE;
305307
} else {
306308
return CONTINUE;
@@ -316,20 +318,24 @@ private String extractClassNameFromDirectory(File dir) throws IOException {
316318
* if it exists or the preset one derived from the jar task
317319
*/
318320
private String extractModuleNameFromDirectory(File dir) throws IOException {
319-
List<File> files = new ArrayList<>(List.of(dir));
320-
while (files.isEmpty() == false) {
321-
File find = files.removeFirst();
322-
if (find.exists()) {
323-
if (find.getName().equals("module-info.class")) {
324-
try (InputStream inputStream = new FileInputStream(find)) {
325-
return extractModuleNameFromModuleInfo(inputStream);
321+
var visitor = new SimpleFileVisitor<Path>() {
322+
private String result = getModuleName().getOrNull();
323+
324+
@Override
325+
public @NotNull FileVisitResult visitFile(@NotNull Path candidate, @NotNull BasicFileAttributes attrs) throws IOException {
326+
String name = candidate.getFileName().toString(); // Just the part after the last dir separator
327+
if (name.equals("module-info.class")) {
328+
try (InputStream inputStream = new FileInputStream(candidate.toFile())) {
329+
result = extractModuleNameFromModuleInfo(inputStream);
330+
return TERMINATE;
326331
}
327-
} else if (find.isDirectory()) {
328-
files.addAll(Arrays.asList(find.listFiles()));
332+
} else {
333+
return CONTINUE;
329334
}
330335
}
331-
}
332-
return getModuleName().getOrNull();
336+
};
337+
Files.walkFileTree(dir.toPath(), visitor);
338+
return visitor.result;
333339
}
334340

335341
/**

plugins/examples/custom-suggester/src/main/java/org/elasticsearch/example/customsuggester/CustomSuggester.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import org.apache.lucene.search.IndexSearcher;
1313
import org.apache.lucene.util.CharsRefBuilder;
14-
import org.elasticsearch.common.text.Text;
14+
import org.elasticsearch.xcontent.Text;
1515
import org.elasticsearch.search.suggest.Suggest;
1616
import org.elasticsearch.search.suggest.Suggester;
1717

plugins/examples/custom-suggester/src/main/java/org/elasticsearch/example/customsuggester/CustomSuggestion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import org.elasticsearch.common.io.stream.StreamInput;
1313
import org.elasticsearch.common.io.stream.StreamOutput;
14-
import org.elasticsearch.common.text.Text;
14+
import org.elasticsearch.xcontent.Text;
1515
import org.elasticsearch.search.suggest.Suggest;
1616
import org.elasticsearch.xcontent.ParseField;
1717
import org.elasticsearch.xcontent.XContentBuilder;

0 commit comments

Comments
 (0)