Skip to content

Commit

Permalink
Make jacoco work with @QuarkusIntegrationTest
Browse files Browse the repository at this point in the history
To make this work, essentially what we do is to overwrite
the build system output with the transformed bytecode
See quarkusio#18559 (comment)
for more details.

Fixes: quarkusio#18559
  • Loading branch information
geoand committed Sep 12, 2021
1 parent b50a4de commit b076fa6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -34,6 +37,7 @@
import io.quarkus.deployment.QuarkusClassWriter;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.ApplicationArchivesBuildItem;
import io.quarkus.deployment.builditem.ArchiveRootBuildItem;
import io.quarkus.deployment.builditem.BytecodeTransformerBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.LiveReloadBuildItem;
Expand Down Expand Up @@ -66,7 +70,8 @@ public static byte[] transform(String className, byte[] classData) {
TransformedClassesBuildItem handleClassTransformation(List<BytecodeTransformerBuildItem> bytecodeTransformerBuildItems,
ApplicationArchivesBuildItem appArchives, LiveReloadBuildItem liveReloadBuildItem,
LaunchModeBuildItem launchModeBuildItem, ClassLoadingConfig classLoadingConfig,
CurateOutcomeBuildItem curateOutcomeBuildItem, List<RemovedResourceBuildItem> removedResourceBuildItems)
CurateOutcomeBuildItem curateOutcomeBuildItem, List<RemovedResourceBuildItem> removedResourceBuildItems,
ArchiveRootBuildItem archiveRoot, LaunchModeBuildItem launchMode)
throws ExecutionException, InterruptedException {
if (bytecodeTransformerBuildItems.isEmpty() && classLoadingConfig.removedResources.isEmpty()
&& removedResourceBuildItems.isEmpty()) {
Expand Down Expand Up @@ -226,9 +231,39 @@ public TransformedClassesBuildItem.TransformedClass call() throws Exception {
}
}
}

if (launchMode.getLaunchMode() == LaunchMode.NORMAL) {
// the idea here is to write the transformed classes into the build tool's output directory to make core coverage work

for (Path path : archiveRoot.getRootDirs()) {
copyTransformedClasses(path, transformedClassesByJar.get(path));
}
}

return new TransformedClassesBuildItem(transformedClassesByJar);
}

private void copyTransformedClasses(Path originalClassesPath,
Set<TransformedClassesBuildItem.TransformedClass> transformedClasses) {
if ((transformedClasses == null) || transformedClasses.isEmpty()) {
return;
}

for (TransformedClassesBuildItem.TransformedClass transformedClass : transformedClasses) {
String classFileName = transformedClass.getFileName();
String[] fileNameParts = classFileName.split("/");
Path classFilePath = originalClassesPath;
for (String fileNamePart : fileNameParts) {
classFilePath = classFilePath.resolve(fileNamePart);
}
try {
Files.write(classFilePath, transformedClass.getData(), StandardOpenOption.WRITE);
} catch (IOException e) {
log.debug("Unable to overwrite file '" + classFilePath.toAbsolutePath() + "' with transformed class data");
}
}
}

private void handleRemovedResources(ClassLoadingConfig classLoadingConfig, CurateOutcomeBuildItem curateOutcomeBuildItem,
Map<Path, Set<TransformedClassesBuildItem.TransformedClass>> transformedClassesByJar,
List<RemovedResourceBuildItem> removedResourceBuildItems) {
Expand Down
1 change: 0 additions & 1 deletion docs/src/main/asciidoc/tests-with-coverage.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ re-build the jacoco report after the integration tests are complete, and thus pr
<configuration>
<destFile>${project.build.directory}/jacoco-quarkus.exec</destFile>
<append>true</append>
<exclClassLoaders>*QuarkusClassLoader</exclClassLoaders>
</configuration>
</execution>
<execution>
Expand Down

0 comments on commit b076fa6

Please sign in to comment.