From 74b8e3dc4128774dfc31c73c9c3325f5448d2e13 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Tue, 3 Dec 2024 15:55:30 +0100 Subject: [PATCH] Convert Jacoco config to @ConfigMapping --- test-framework/jacoco/deployment/pom.xml | 3 - .../jacoco/deployment/JacocoProcessor.java | 17 +++--- test-framework/jacoco/runtime/pom.xml | 3 - .../quarkus/jacoco/runtime/JacocoConfig.java | 60 +++++++++---------- .../quarkus/jacoco/runtime/ReportCreator.java | 13 ++-- 5 files changed, 44 insertions(+), 52 deletions(-) diff --git a/test-framework/jacoco/deployment/pom.xml b/test-framework/jacoco/deployment/pom.xml index 7aa0f877e779a..21ac798e16142 100644 --- a/test-framework/jacoco/deployment/pom.xml +++ b/test-framework/jacoco/deployment/pom.xml @@ -52,9 +52,6 @@ ${project.version} - - -AlegacyConfigRoot=true - diff --git a/test-framework/jacoco/deployment/src/main/java/io/quarkus/jacoco/deployment/JacocoProcessor.java b/test-framework/jacoco/deployment/src/main/java/io/quarkus/jacoco/deployment/JacocoProcessor.java index e8f403b855ea7..7bc26c15ef928 100644 --- a/test-framework/jacoco/deployment/src/main/java/io/quarkus/jacoco/deployment/JacocoProcessor.java +++ b/test-framework/jacoco/deployment/src/main/java/io/quarkus/jacoco/deployment/JacocoProcessor.java @@ -56,15 +56,15 @@ void transformerBuildItem(BuildProducer 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)); } @@ -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 classes = new HashSet<>(); info.classFiles = classes; @@ -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 classes, Set 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) { diff --git a/test-framework/jacoco/runtime/pom.xml b/test-framework/jacoco/runtime/pom.xml index 449c9951353b7..8a73f6019b40f 100644 --- a/test-framework/jacoco/runtime/pom.xml +++ b/test-framework/jacoco/runtime/pom.xml @@ -82,9 +82,6 @@ ${project.version} - - -AlegacyConfigRoot=true - diff --git a/test-framework/jacoco/runtime/src/main/java/io/quarkus/jacoco/runtime/JacocoConfig.java b/test-framework/jacoco/runtime/src/main/java/io/quarkus/jacoco/runtime/JacocoConfig.java index 5891ac704f6fa..f463cbd0dc003 100644 --- a/test-framework/jacoco/runtime/src/main/java/io/quarkus/jacoco/runtime/JacocoConfig.java +++ b/test-framework/jacoco/runtime/src/main/java/io/quarkus/jacoco/runtime/JacocoConfig.java @@ -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 dataFile; + Optional 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 title; + @WithDefault("${quarkus.application.name}") + Optional title(); /** * Footer text used in HTML report pages. */ - @ConfigItem - public Optional footer; + public Optional 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 @@ -78,8 +78,8 @@ public class JacocoConfig { *
  • **/*BAR*.class targets classes that contain BAR in their name regardless of path
  • * */ - @ConfigItem(defaultValue = "**") - public List includes; + @WithDefault("**") + public List includes(); /** * A list of class files to exclude from the report. May use wildcard @@ -92,14 +92,12 @@ public class JacocoConfig { *
  • **/*BAR*.class targets classes that contain BAR in their name regardless of path
  • * */ - @ConfigItem - public Optional> excludes; + public Optional> excludes(); /** * The location of the report files. * The path can be relative (to the module) or absolute. */ - @ConfigItem @ConfigDocDefault(TARGET_JACOCO_REPORT) - public Optional reportLocation; + public Optional reportLocation(); } diff --git a/test-framework/jacoco/runtime/src/main/java/io/quarkus/jacoco/runtime/ReportCreator.java b/test-framework/jacoco/runtime/src/main/java/io/quarkus/jacoco/runtime/ReportCreator.java index 0a8351b0aab74..0e09043eae504 100644 --- a/test-framework/jacoco/runtime/src/main/java/io/quarkus/jacoco/runtime/ReportCreator.java +++ b/test-framework/jacoco/runtime/src/main/java/io/quarkus/jacoco/runtime/ReportCreator.java @@ -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; @@ -94,9 +93,9 @@ private void doRun() { } List 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 @@ -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);