Skip to content

Commit

Permalink
Merge pull request #44900 from gsmet/configmapping-jacoco
Browse files Browse the repository at this point in the history
Convert Jacoco config to @ConfigMapping
  • Loading branch information
gsmet authored Dec 3, 2024
2 parents 4a444d7 + 74b8e3d commit cc63103
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 52 deletions.
3 changes: 0 additions & 3 deletions test-framework/jacoco/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ void transformerBuildItem(BuildProducer<BytecodeTransformerBuildItem> transforme
//no code coverage for continuous testing, it does not really make sense
return;
}
if (!config.enabled) {
if (!config.enabled()) {
log.debug("quarkus-jacoco is disabled via config");
return;
}

String dataFile = getFilePath(config.dataFile, outputTargetBuildItem.getOutputDirectory(),
String dataFile = getFilePath(config.dataFile(), outputTargetBuildItem.getOutputDirectory(),
JacocoConfig.JACOCO_QUARKUS_EXEC);
System.setProperty("jacoco-agent.destfile", dataFile);
if (!config.reuseDataFile) {
if (!config.reuseDataFile()) {
Files.deleteIfExists(Paths.get(dataFile));
}

Expand Down Expand Up @@ -97,15 +97,16 @@ public byte[] apply(String className, byte[] bytes) {
}).build());
}
}
if (config.report) {
if (config.report()) {
ReportInfo info = new ReportInfo();
info.dataFile = dataFile;

File targetdir = new File(
getFilePath(config.reportLocation, outputTargetBuildItem.getOutputDirectory(), JacocoConfig.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()));
String includes = String.join(",", config.includes());
String excludes = String.join(",", config.excludes().orElse(Collections.emptyList()));
Set<String> classes = new HashSet<>();
info.classFiles = classes;

Expand All @@ -128,7 +129,7 @@ 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(),
String dataFile = getFilePath(config.dataFile(), module.getWorkspaceModule().getBuildDir().toPath(),
JacocoConfig.JACOCO_QUARKUS_EXEC);
info.savedData.add(new File(dataFile).getAbsolutePath());
if (module.getSources() == null) {
Expand Down
3 changes: 0 additions & 3 deletions test-framework/jacoco/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,68 @@
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;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public class JacocoConfig {
@ConfigMapping(prefix = "quarkus.jacoco")
public interface 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;
static final String JACOCO_QUARKUS_EXEC = "jacoco-quarkus.exec";
static final String JACOCO_REPORT = "jacoco-report";
static final String TARGET_JACOCO_QUARKUS_EXEC = "target/" + JACOCO_QUARKUS_EXEC;
static final String TARGET_JACOCO_REPORT = "target/" + JACOCO_REPORT;

/**
* Whether or not the jacoco extension is enabled.
* Whether or not the Jacoco extension is enabled.
*/
@ConfigItem(defaultValue = "true")
public boolean enabled;
@WithDefault("true")
boolean enabled();

/**
* The jacoco data file.
* The Jacoco data file.
* The path can be relative (to the module) or absolute.
*/
@ConfigItem
@ConfigDocDefault(TARGET_JACOCO_QUARKUS_EXEC)
public Optional<String> dataFile;
Optional<String> dataFile();

/**
* Whether to reuse ({@code true}) or delete ({@code false}) the jacoco
* Whether to reuse ({@code true}) or delete ({@code false}) the Jacoco
* data file on each run.
*/
@ConfigItem(defaultValue = "false")
public boolean reuseDataFile;
@WithDefault("false")
boolean reuseDataFile();

/**
* If Quarkus should generate the Jacoco report
*/
@ConfigItem(defaultValue = "true")
public boolean report;
@WithDefault("true")
boolean report();

/**
* Encoding of the generated reports.
*/
@ConfigItem(defaultValue = "UTF-8")
public String outputEncoding;
@WithDefault("UTF-8")
String outputEncoding();

/**
* Name of the root node HTML report pages.
*/
@ConfigItem(defaultValue = "${quarkus.application.name}")
public Optional<String> title;
@WithDefault("${quarkus.application.name}")
Optional<String> title();

/**
* Footer text used in HTML report pages.
*/
@ConfigItem
public Optional<String> footer;
public Optional<String> footer();

/**
* Encoding of the source files.
*/
@ConfigItem(defaultValue = "UTF-8")
public String sourceEncoding;
@WithDefault("UTF-8")
public String sourceEncoding();

/**
* A list of class files to include in the report. May use wildcard
Expand All @@ -78,8 +78,8 @@ public class JacocoConfig {
* <li><code>&#42;&#42;/&#42;BAR&#42;.class</code> targets classes that contain BAR in their name regardless of path</li>
* </ul>
*/
@ConfigItem(defaultValue = "**")
public List<String> includes;
@WithDefault("**")
public List<String> includes();

/**
* A list of class files to exclude from the report. May use wildcard
Expand All @@ -92,14 +92,12 @@ public class JacocoConfig {
* <li><code>&#42;&#42;/&#42;BAR&#42;.class</code> targets classes that contain BAR in their name regardless of path</li>
* </ul>
*/
@ConfigItem
public Optional<List<String>> excludes;
public Optional<List<String>> excludes();

/**
* 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;
public Optional<String> reportLocation();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
import org.jacoco.report.csv.CSVFormatter;
import org.jacoco.report.html.HTMLFormatter;
import org.jacoco.report.xml.XMLFormatter;
import org.jboss.logging.Logger;

public class ReportCreator implements Runnable {
private static final Logger log = Logger.getLogger(ReportCreator.class);

private final ReportInfo reportInfo;
private final JacocoConfig config;

Expand Down Expand Up @@ -94,9 +93,9 @@ private void doRun() {
}

List<IReportVisitor> formatters = new ArrayList<>();
addXmlFormatter(new File(targetdir, "jacoco.xml"), config.outputEncoding, formatters);
addCsvFormatter(new File(targetdir, "jacoco.csv"), config.outputEncoding, formatters);
addHtmlFormatter(targetdir, config.outputEncoding, config.footer.orElse(""), Locale.getDefault(),
addXmlFormatter(new File(targetdir, "jacoco.xml"), config.outputEncoding(), formatters);
addCsvFormatter(new File(targetdir, "jacoco.csv"), config.outputEncoding(), formatters);
addHtmlFormatter(targetdir, config.outputEncoding(), config.footer().orElse(""), Locale.getDefault(),
formatters);

//now for the hacky bit
Expand All @@ -106,9 +105,9 @@ private void doRun() {
loader.getExecutionDataStore().getContents());
MultiSourceFileLocator sourceFileLocator = new MultiSourceFileLocator(4);
for (String i : reportInfo.sourceDirectories) {
sourceFileLocator.add(new DirectorySourceFileLocator(new File(i), config.sourceEncoding, 4));
sourceFileLocator.add(new DirectorySourceFileLocator(new File(i), config.sourceEncoding(), 4));
}
final IBundleCoverage bundle = builder.getBundle(config.title.orElse(reportInfo.artifactId));
final IBundleCoverage bundle = builder.getBundle(config.title().orElse(reportInfo.artifactId));
visitor.visitBundle(bundle, sourceFileLocator);
visitor.visitEnd();
System.out.println("Generated Jacoco reports in " + targetdir);
Expand Down

0 comments on commit cc63103

Please sign in to comment.