Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import com.fasterxml.jackson.databind.ObjectMapper
import org.elasticsearch.gradle.fixtures.AbstractGradleFuncTest
import org.gradle.testkit.runner.TaskOutcome

import java.nio.file.Path

class TestBuildInfoPluginFuncTest extends AbstractGradleFuncTest {
def "works"() {
def "basic functionality"() {
given:
file("src/main/java/com/example/Example.java") << """
package com.example;
Expand Down Expand Up @@ -60,4 +62,63 @@ class TestBuildInfoPluginFuncTest extends AbstractGradleFuncTest {
)
new ObjectMapper().readValue(output, Map.class) == expectedOutput
}

def "dependencies"() {
buildFile << """
import org.elasticsearch.gradle.plugin.GenerateTestBuildInfoTask;

plugins {
id 'java'
id 'elasticsearch.test-build-info'
}

repositories {
mavenCentral()
}

dependencies {
// We pin to specific versions here because they are known to have the properties we want to test.
// We're not actually running this code.
implementation "org.ow2.asm:asm:9.7.1" // has module-info.class
implementation "junit:junit:4.13" // has Automatic-Module-Name, and brings in hamcrest which does not
}

tasks.withType(GenerateTestBuildInfoTask.class) {
componentName = 'example-component'
outputFile = new File('build/generated-build-info/plugin-test-build-info.json')
}
"""

when:
def result = gradleRunner('generateTestBuildInfo').build()
def task = result.task(":generateTestBuildInfo")


then:
task.outcome == TaskOutcome.SUCCESS

def output = file("build/generated-build-info/plugin-test-build-info.json")
output.exists() == true

def locationFromModuleInfo = Map.of(
"module", "org.objectweb.asm",
"representative_class", Path.of('org', 'objectweb', 'asm', 'AnnotationVisitor.class').toString()
)
def locationFromManifest = Map.of(
"module", "junit",
"representative_class", Path.of('junit', 'textui', 'TestRunner.class').toString()
)
def locationFromJarFileName = Map.of(
"module", "hamcrest.core",
"representative_class", Path.of('org', 'hamcrest', 'BaseDescription.class').toString()
)
def expectedOutput = Map.of(
"component", "example-component",
"locations", List.of(locationFromModuleInfo, locationFromManifest, locationFromJarFileName)
)

def value = new ObjectMapper().readValue(output, Map.class)
expectedOutput.forEach((k,v) -> value.get(k) == v)
value == expectedOutput
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -316,20 +316,24 @@ private String extractClassNameFromDirectory(File dir) throws IOException {
* if it exists or the preset one derived from the jar task
*/
private String extractModuleNameFromDirectory(File dir) throws IOException {
List<File> files = new ArrayList<>(List.of(dir));
while (files.isEmpty() == false) {
File find = files.removeFirst();
if (find.exists()) {
if (find.getName().equals("module-info.class")) {
try (InputStream inputStream = new FileInputStream(find)) {
return extractModuleNameFromModuleInfo(inputStream);
var visitor = new SimpleFileVisitor<Path>() {
private String result = getModuleName().getOrNull();

@Override
public @NotNull FileVisitResult visitFile(@NotNull Path candidate, @NotNull BasicFileAttributes attrs) throws IOException {
String name = candidate.getFileName().toString(); // Just the part after the last dir separator
if (name.equals("module-info.class")) {
try (InputStream inputStream = new FileInputStream(candidate.toFile())) {
result = extractModuleNameFromModuleInfo(inputStream);
return TERMINATE;
}
} else if (find.isDirectory()) {
files.addAll(Arrays.asList(find.listFiles()));
} else {
return CONTINUE;
}
}
}
return getModuleName().getOrNull();
};
Files.walkFileTree(dir.toPath(), visitor);
return visitor.result;
}

/**
Expand Down