Skip to content

Commit

Permalink
implement output-name/add-runner-suffix on Gradle
Browse files Browse the repository at this point in the history
Update to the integration tests for checking the native binary name

Runner file name taking into account the quarkus properties
  • Loading branch information
jacobdotcosta committed Jan 4, 2023
1 parent 4fa6890 commit 2f7e5c7
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.StringJoiner;
import java.util.*;
import java.util.stream.Collectors;

import org.gradle.api.Action;
Expand Down Expand Up @@ -79,7 +75,8 @@ public void beforeTest(Test task) {
task.environment(BootstrapConstants.TEST_TO_MAIN_MAPPINGS, fileList);
project.getLogger().debug("test dir mapping - {}", fileList);

final String nativeRunner = task.getProject().getBuildDir().toPath().resolve(finalName() + "-runner")
final String nativeRunner = task.getProject().getBuildDir().toPath()
.resolve(buildNativeRunnerName(props))
.toAbsolutePath()
.toString();
props.put("native.image.path", nativeRunner);
Expand All @@ -88,6 +85,28 @@ public void beforeTest(Test task) {
}
}

public String buildNativeRunnerName(final Map<String, Object> taskSystemProps) {
// TODO: Take into account build.gradle configuration properties implemented by PR-29971
Properties properties = new Properties(taskSystemProps.size());
properties.putAll(taskSystemProps);
System.getProperties().entrySet()
.forEach(propEntry -> properties.putIfAbsent(propEntry.getKey(), propEntry.getValue()));
System.getenv().entrySet().forEach(
envEntry -> properties.putIfAbsent(envEntry.getKey(), envEntry.getValue()));
StringBuilder nativeRunnerName = new StringBuilder();
if (properties.containsKey("quarkus.package.output-name")) {
nativeRunnerName.append(properties.get("quarkus.package.output-name"));
} else {
nativeRunnerName.append(finalName());
}
if (!properties.containsKey("quarkus.package.add-runner-suffix")
|| (properties.containsKey("quarkus.package.add-runner-suffix")
&& Boolean.parseBoolean((String) properties.get("quarkus.package.add-runner-suffix")))) {
nativeRunnerName.append("-runner");
}
return nativeRunnerName.toString();
}

public Property<String> getFinalName() {
return finalName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ public QuarkusBuild manifest(Action<Manifest> action) {

@OutputFile
public File getRunnerJar() {
return new File(getProject().getBuildDir(), extension().finalName() + "-runner.jar");
return new File(getProject().getBuildDir(), String.format("%s.jar", extension().buildNativeRunnerName(Map.of())));
}

@OutputFile
public File getNativeRunner() {
return new File(getProject().getBuildDir(), extension().finalName() + "-runner");
return new File(getProject().getBuildDir(), extension().buildNativeRunnerName(Map.of()));
}

@OutputDirectory
Expand Down Expand Up @@ -223,4 +223,5 @@ private String expandConfigurationKey(String shortKey) {
}
return String.format("%s.%s", NATIVE_PROPERTY_NAMESPACE, hyphenatedKey);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,76 @@ public void shouldBuildNativeImage() throws Exception {

}

@Test
public void shouldBuildNativeImageWithCustomName() throws Exception {
final File projectDir = getProjectDir("basic-java-native-module");

final BuildResult build = runGradleWrapper(projectDir, "clean", "quarkusBuild", "-Dquarkus.package.type=native",
"-Dquarkus.package.output-name=test");

assertThat(build.getTasks().get(":quarkusBuild")).isEqualTo(BuildResult.SUCCESS_OUTCOME);
final String buildOutput = build.getOutput();
// make sure the output log during the build contains some expected logs from the native-image process
CharSequence[] expectedOutput;
if (buildOutput.contains("Version info:")) { // Starting with 22.0 the native-image output changed
expectedOutput = new CharSequence[] { "Initializing...", "Performing analysis...",
"Finished generating 'test-runner' in" };
} else {
expectedOutput = new CharSequence[] { "(clinit):", "(typeflow):", "[total]:" };
}
assertThat(buildOutput)
.withFailMessage("native-image build log is missing certain expected log messages: \n\n %s", buildOutput)
.contains(expectedOutput)
.doesNotContain("Finished generating '" + NATIVE_IMAGE_NAME + "' in");
Path nativeImagePath = projectDir.toPath().resolve("build").resolve("test-runner");
assertThat(nativeImagePath).exists();
Process nativeImageProcess = runNativeImage(nativeImagePath.toAbsolutePath().toString());
try {
final String response = DevModeTestUtils.getHttpResponse("/hello");
assertThat(response)
.withFailMessage("Response %s for /hello was expected to contain the hello, but didn't", response)
.contains("hello");
} finally {
nativeImageProcess.destroy();
}

}

@Test
public void shouldBuildNativeImageWithCustomNameWithoutSuffix() throws Exception {
final File projectDir = getProjectDir("basic-java-native-module");

final BuildResult build = runGradleWrapper(projectDir, "clean", "quarkusBuild", "-Dquarkus.package.type=native",
"-Dquarkus.package.output-name=test", "-Dquarkus.package.add-runner-suffix=false");

assertThat(build.getTasks().get(":quarkusBuild")).isEqualTo(BuildResult.SUCCESS_OUTCOME);
final String buildOutput = build.getOutput();
// make sure the output log during the build contains some expected logs from the native-image process
CharSequence[] expectedOutput;
if (buildOutput.contains("Version info:")) { // Starting with 22.0 the native-image output changed
expectedOutput = new CharSequence[] { "Initializing...", "Performing analysis...",
"Finished generating 'test' in" };
} else {
expectedOutput = new CharSequence[] { "(clinit):", "(typeflow):", "[total]:" };
}
assertThat(buildOutput)
.withFailMessage("native-image build log is missing certain expected log messages: \n\n %s", buildOutput)
.contains(expectedOutput)
.doesNotContain("Finished generating '" + NATIVE_IMAGE_NAME + "' in");
Path nativeImagePath = projectDir.toPath().resolve("build").resolve("test");
assertThat(nativeImagePath).exists();
Process nativeImageProcess = runNativeImage(nativeImagePath.toAbsolutePath().toString());
try {
final String response = DevModeTestUtils.getHttpResponse("/hello");
assertThat(response)
.withFailMessage("Response %s for /hello was expected to contain the hello, but didn't", response)
.contains("hello");
} finally {
nativeImageProcess.destroy();
}

}

private Process runNativeImage(String nativeImage) throws IOException {
final ProcessBuilder processBuilder = new ProcessBuilder(nativeImage);
processBuilder.inheritIO();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,22 @@ public void nativeTestShouldRunIntegrationTest() throws Exception {
assertThat(testResult.getTasks().get(":testNative")).isEqualTo(BuildResult.SUCCESS_OUTCOME);
}

@Test
public void runNativeTestsWithOutputName() throws Exception {
final File projectDir = getProjectDir("it-test-basic-project");

final BuildResult testResult = runGradleWrapper(projectDir, "clean", "testNative",
"-Dquarkus.package.output-name=test");
assertThat(testResult.getTasks().get(":testNative")).isEqualTo(BuildResult.SUCCESS_OUTCOME);
}

@Test
public void runNativeTestsWithoutRunnerSuffix() throws Exception {
final File projectDir = getProjectDir("it-test-basic-project");

final BuildResult testResult = runGradleWrapper(projectDir, "clean", "testNative",
"-Dquarkus.package.add-runner-suffix=false");
assertThat(testResult.getTasks().get(":testNative")).isEqualTo(BuildResult.SUCCESS_OUTCOME);
}

}

0 comments on commit 2f7e5c7

Please sign in to comment.