Skip to content

Commit

Permalink
Merge pull request #30286 from jacobdotcosta/issue-30101-1
Browse files Browse the repository at this point in the history
  • Loading branch information
glefloch authored Jan 10, 2023
2 parents 068aebe + dfabbd6 commit e5ed60f
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.StringJoiner;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -85,7 +86,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 @@ -94,6 +96,30 @@ public void beforeTest(Test task) {
}
}

public String buildNativeRunnerName(final Map<String, Object> taskSystemProps) {
Properties properties = new Properties(taskSystemProps.size());
properties.putAll(taskSystemProps);
quarkusBuildProperties.entrySet()
.forEach(buildEntry -> properties.putIfAbsent(buildEntry.getKey(), buildEntry.getValue()));
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 @@ -129,12 +129,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
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 e5ed60f

Please sign in to comment.