Skip to content

Commit

Permalink
JENKINS-59813 pipeline-maven JacocoPublisher handle excludes properly (
Browse files Browse the repository at this point in the history
  • Loading branch information
enapps-enorman committed Mar 6, 2024
1 parent c9370bb commit 08b63fa
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public JacocoReportPublisher() {}
* <destFile>${jacoco.destFile}</destFile>
* <dumpOnExit>${jacoco.dumpOnExit}</dumpOnExit>
* <exclClassLoaders>${jacoco.exclClassLoaders}</exclClassLoaders>
* <excludes>
* <exclude>com/example/exclude/*</exclude>
* </excludes>
* <inclBootstrapClasses>${jacoco.inclBootstrapClasses}</inclBootstrapClasses>
* <inclNoLocationClasses>${jacoco.inclNoLocationClasses}</inclNoLocationClasses>
* <jmx>${jacoco.jmx}</jmx>
Expand All @@ -93,6 +96,9 @@ public JacocoReportPublisher() {}
* <destFile>${jacoco.destFile}</destFile>
* <dumpOnExit>${jacoco.dumpOnExit}</dumpOnExit>
* <exclClassLoaders>${jacoco.exclClassLoaders}</exclClassLoaders>
* <excludes>
* <exclude>com/example/exclude/*</exclude>
* </excludes>
* <inclBootstrapClasses>${jacoco.inclBootstrapClasses}</inclBootstrapClasses>
* <inclNoLocationClasses>${jacoco.inclNoLocationClasses}</inclNoLocationClasses>
* <jmx>${jacoco.jmx}</jmx>
Expand Down Expand Up @@ -219,15 +225,26 @@ public void process(@NonNull StepContext context, @NonNull Element mavenSpyLogsE
String sourceDirectoryRelativePath = XmlUtils.getPathInWorkspace(sourceDirectory, workspace);
String classesDirectoryRelativePath = XmlUtils.getPathInWorkspace(classesDirectory, workspace);

String excludes = null;
Element excludesElt = XmlUtils.getUniqueChildElementOrNull(pluginElt, "excludes");
if (excludesElt != null) {
List<Element> excludeElements = XmlUtils.getChildrenElements(excludesElt, "exclude");
excludes = excludeElements.stream()
.map(e -> e.getTextContent().trim())
.collect(Collectors.joining(","));
}

listener.getLogger()
.println("[withMaven] jacocoPublisher - Archive JaCoCo analysis results for Maven artifact "
+ mavenArtifact.toString() + " generated by " + pluginInvocation + ": execFile: " + destFile
+ ", sources: " + sourceDirectoryRelativePath + ", classes: "
+ classesDirectoryRelativePath);
+ classesDirectoryRelativePath + ", excludes: "
+ excludes);
jacocoReportDetails.add(new JacocoReportDetails(
destFile,
sourceDirectoryRelativePath,
classesDirectoryRelativePath,
excludes,
mavenArtifact.toString() + " " + pluginInvocation));
}

Expand All @@ -241,10 +258,16 @@ public void process(@NonNull StepContext context, @NonNull Element mavenSpyLogsE
String aggregatedClassesDirectory = jacocoReportDetails.stream()
.map(details -> details.classesDirectory)
.collect(Collectors.joining(","));
String aggregatedExclusionPattern = jacocoReportDetails.stream()
.map(details -> details.excludes)
.filter(e -> e != null && !e.isEmpty())
.distinct()
.collect(Collectors.joining(","));

jacocoPublisher.setExecPattern(aggregatedDestFile);
jacocoPublisher.setSourcePattern(aggregatedSourceDirectory);
jacocoPublisher.setClassPattern(aggregatedClassesDirectory);
jacocoPublisher.setExclusionPattern(aggregatedExclusionPattern);

try {
jacocoPublisher.perform(run, workspace, launcher, listener);
Expand All @@ -258,13 +281,14 @@ public void process(@NonNull StepContext context, @NonNull Element mavenSpyLogsE
}

public static class JacocoReportDetails {
final String execFile, sourceDirectory, classesDirectory, description;
final String execFile, sourceDirectory, classesDirectory, excludes, description;

public JacocoReportDetails(
String execFile, String sourceDirectory, String classesDirectory, String description) {
String execFile, String sourceDirectory, String classesDirectory, String excludes, String description) {
this.execFile = execFile;
this.sourceDirectory = sourceDirectory;
this.classesDirectory = classesDirectory;
this.excludes = excludes;
this.description = description;
}

Expand All @@ -273,7 +297,8 @@ public String toString() {
return "JacocoReportDetails{" + "execFile='"
+ execFile + '\'' + ", sourceDirectory='"
+ sourceDirectory + '\'' + ", classesDirectory='"
+ classesDirectory + '\'' + ", description='"
+ classesDirectory + '\'' + ", excludes='"
+ excludes + '\'' + ", description='"
+ description + '\'' + '}';
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package org.jenkinsci.plugins.pipeline.maven.publishers;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import hudson.model.Result;
import hudson.plugins.jacoco.JacocoBuildAction;
import hudson.plugins.jacoco.report.CoverageReport;
import hudson.plugins.jacoco.report.PackageReport;
import hudson.tasks.junit.TestResultAction;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jenkinsci.plugins.pipeline.maven.AbstractIntegrationTest;
import org.jenkinsci.plugins.pipeline.maven.TestUtils;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
Expand Down Expand Up @@ -57,5 +63,14 @@ public void maven_build_jar_with_jacoco_succeeds() throws Exception {
assertThat(jacocoBuildActions).hasSize(1);
JacocoBuildAction jacocoBuildAction = jacocoBuildActions.get(0);
assertThat(jacocoBuildAction.getProjectActions()).hasSize(1);

// verify that the excluded package is not in the report
CoverageReport result = jacocoBuildAction.getResult();
Map<String, PackageReport> children = result.getChildren();
Set<String> childrenKeys = children.keySet();
// not excluded package
assertTrue(childrenKeys.stream().anyMatch("com.example"::equals));
// excluded package
assertFalse(childrenKeys.stream().anyMatch("com.example.exclude"::equals));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
<configuration>
<excludes>
<exclude>com/example/exclude/*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>default-prepare-agent</id>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.exclude;

public class MonoModuleMavenExcludeApp {
/**
* TODO message

Check warning on line 5 in pipeline-maven/src/test/resources/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_jar_with_jacoco_project/src/main/java/com/example/exclude/MonoModuleMavenExcludeApp.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: message
* @param args
*/
public static void main(String[] args) {
System.out.println("Hello MonoModuleMavenExcludeApp");
}
}

0 comments on commit 08b63fa

Please sign in to comment.