Skip to content

Commit

Permalink
Merge pull request #34961 from vsevel/feature/improve-jacoco-doc
Browse files Browse the repository at this point in the history
Improve Jacoco documentation with ConfigDocDefault
  • Loading branch information
geoand committed Jul 24, 2023
2 parents 62969c5 + 0b008fc commit 7597447
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
20 changes: 20 additions & 0 deletions docs/src/main/asciidoc/tests-with-coverage.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,26 @@ config is required.
WARNING: Using both the extension and the plugin requires special configuration, if you add both you will get lots of errors about classes
already being instrumented. The configuration needed is detailed below.

== Working with multi-module projects

Up until `3.2`, `data-file` and `report-location` were always relative to the module's build output directory, which prevented from
working with multi-module projects where you want to aggregate all coverages into a single parent directory. Starting in `3.3`,
specifying a `data-file` or `report-location` will assume the path as is. Here is an example on how to set up the `surefire` plugin:

[source, xml]
----
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<quarkus.jacoco.data-file>${maven.multiModuleProjectDirectory}/target/jacoco.exec</quarkus.jacoco.data-file>
<quarkus.jacoco.reuse-data-file>true</quarkus.jacoco.reuse-data-file>
<quarkus.jacoco.report-location>${maven.multiModuleProjectDirectory}/target/coverage</quarkus.jacoco.report-location>
</systemPropertyVariables>
</configuration>
</plugin
----

== Running the tests with coverage

Run `mvn verify`, the tests will be run and the results will end up in `target/jacoco-reports`. This is all that is needed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@

public class JacocoProcessor {

public static final String JACOCO_QUARKUS_EXEC = "jacoco-quarkus.exec";
public static final String JACOCO_REPORT = "jacoco-report";

@BuildStep(onlyIf = IsTest.class)
FeatureBuildItem feature() {
return new FeatureBuildItem("jacoco");
Expand All @@ -57,7 +54,8 @@ void transformerBuildItem(BuildProducer<BytecodeTransformerBuildItem> transforme
return;
}

String dataFile = getFilePath(config.dataFile, outputTargetBuildItem.getOutputDirectory(), JACOCO_QUARKUS_EXEC);
String dataFile = getFilePath(config.dataFile, outputTargetBuildItem.getOutputDirectory(),
JacocoConfig.JACOCO_QUARKUS_EXEC);
System.setProperty("jacoco-agent.destfile", dataFile);
if (!config.reuseDataFile) {
Files.deleteIfExists(Paths.get(dataFile));
Expand Down Expand Up @@ -97,7 +95,7 @@ public byte[] apply(String className, byte[] bytes) {
info.dataFile = dataFile;

File targetdir = new File(
getFilePath(config.reportLocation, outputTargetBuildItem.getOutputDirectory(), JACOCO_REPORT));
getFilePath(config.reportLocation, outputTargetBuildItem.getOutputDirectory(), JacocoConfig.JACOCO_REPORT));
info.reportDir = targetdir.getAbsolutePath();
String includes = String.join(",", config.includes);
String excludes = String.join(",", config.excludes.orElse(Collections.emptyList()));
Expand All @@ -123,7 +121,8 @@ public byte[] apply(String className, byte[] bytes) {

private void addProjectModule(ResolvedDependency module, JacocoConfig config, ReportInfo info, String includes,
String excludes, Set<String> classes, Set<String> sources) throws Exception {
String dataFile = getFilePath(config.dataFile, module.getWorkspaceModule().getBuildDir().toPath(), JACOCO_QUARKUS_EXEC);
String dataFile = getFilePath(config.dataFile, module.getWorkspaceModule().getBuildDir().toPath(),
JacocoConfig.JACOCO_QUARKUS_EXEC);
info.savedData.add(new File(dataFile).getAbsolutePath());
if (module.getSources() == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
import java.util.List;
import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigDocDefault;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;

@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public class JacocoConfig {

public static final String JACOCO_QUARKUS_EXEC = "jacoco-quarkus.exec";
public static final String JACOCO_REPORT = "jacoco-report";
public static final String TARGET_JACOCO_QUARKUS_EXEC = "target/" + JACOCO_QUARKUS_EXEC;
public static final String TARGET_JACOCO_REPORT = "target/" + JACOCO_REPORT;

/**
* The jacoco data file. By default this will be target/jacoco-quarkus.exec.
* The jacoco data file.
* The path can be relative (to the module) or absolute.
*/
@ConfigItem
@ConfigDocDefault(TARGET_JACOCO_QUARKUS_EXEC)
public Optional<String> dataFile;

/**
Expand Down Expand Up @@ -83,9 +90,10 @@ public class JacocoConfig {
public Optional<List<String>> excludes;

/**
* The location of the report files. By default this will be target/jacoco-report.
* The location of the report files.
* The path can be relative (to the module) or absolute.
*/
@ConfigItem
@ConfigDocDefault(TARGET_JACOCO_REPORT)
public Optional<String> reportLocation;
}

0 comments on commit 7597447

Please sign in to comment.