Skip to content

Commit

Permalink
Merge pull request #118 from Ririshi/bugfix/xunit-format-missing
Browse files Browse the repository at this point in the history
Allow selecting XUnit in the web interface
  • Loading branch information
uhafner authored Apr 23, 2024
2 parents 515744d + af5e623 commit f88560a
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 41 deletions.
2 changes: 1 addition & 1 deletion plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<gitHubRepo>jenkinsci/coverage-plugin</gitHubRepo>

<!-- Library Dependencies Versions -->
<coverage-model.version>0.41.0</coverage-model.version>
<coverage-model.version>0.43.0</coverage-model.version>
<jsoup.version>1.17.2</jsoup.version>

<!-- Jenkins Plug-in Dependencies Versions -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import edu.hm.hafner.coverage.ClassNode;
import edu.hm.hafner.coverage.CoverageParser.ProcessingMode;
import edu.hm.hafner.coverage.ModuleNode;
import edu.hm.hafner.coverage.Node;
import edu.hm.hafner.coverage.PackageNode;
import edu.hm.hafner.util.FilteredLog;
import edu.hm.hafner.util.TreeStringBuilder;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand Down Expand Up @@ -48,6 +51,7 @@
import jenkins.model.Jenkins;

import io.jenkins.plugins.coverage.metrics.steps.CoverageTool.Parser;
import io.jenkins.plugins.coverage.metrics.steps.CoverageTool.ParserType;
import io.jenkins.plugins.prism.SourceCodeDirectory;
import io.jenkins.plugins.prism.SourceCodeRetention;
import io.jenkins.plugins.util.AgentFileVisitor.FileVisitorResult;
Expand Down Expand Up @@ -451,7 +455,7 @@ private void resolveAbsolutePaths(final Node rootNode, final FilePath workspace,
private String getIcon() {
var icons = tools.stream()
.map(CoverageTool::getParser)
.filter(parser -> parser != Parser.JUNIT)
.filter(parser -> parser.getParserType() != ParserType.TEST)
.map(Parser::getIcon)
.collect(Collectors.toSet());
if (icons.size() == 1) {
Expand Down Expand Up @@ -520,29 +524,50 @@ private Node aggregateResults(final FilteredLog log, final Map<Parser, List<Modu
return new ModuleNode("Empty");
}
else {
var coverageTree = Node.merge(results.values().stream()
var testCases = results.entrySet().stream().filter(entry -> entry.getKey().getParserType() == ParserType.TEST)
.map(Entry::getValue)
.flatMap(Collection::stream)
.collect(Collectors.toList()));
var tests = extractTests(results).stream()
.map(Node::getAllClassNodes)
.flatMap(Collection::stream)
.collect(Collectors.toList());
coverageTree.mapTests(tests);
var coverageNodes = results.entrySet()
.stream()
.filter(entry -> entry.getKey().getParserType() == ParserType.COVERAGE)
.map(Entry::getValue)
.flatMap(Collection::stream)
.collect(Collectors.toList());
if (coverageNodes.isEmpty()) {
log.logError("No coverage results were found, just tests! Configuration error?");

var tests = new ModuleNode("Tests");
tests.addAllChildren(testCases);
return tests;
}

var coverageTree = Node.merge(coverageNodes);
var unmappedNodes = coverageTree.mergeTests(testCases);

unmappedNodes.forEach(node -> mapTests(node, coverageTree));

return coverageTree;
}
}

private boolean isEmpty(final Map<Parser, List<ModuleNode>> results) {
return results.values().stream().mapToInt(Collection::size).sum() == 0;
private void mapTests(final ClassNode classNode, final Node coverageTree) {
var normalizedPackageName = PackageNode.normalizePackageName(classNode.getPackageName());

coverageTree.findPackage(normalizedPackageName)
.orElseGet(() -> createPackage(coverageTree, normalizedPackageName)).addChild(classNode);
}

private List<ModuleNode> extractTests(final Map<Parser, List<ModuleNode>> results) {
if (results.containsKey(Parser.JUNIT)) {
return results.remove(Parser.JUNIT);
}
else {
return List.of();
}
private PackageNode createPackage(final Node coverageTree, final String normalizedPackageName) {
var packageNode = new PackageNode(normalizedPackageName);
coverageTree.addChild(packageNode);
return packageNode;

Check warning on line 566 in plugin/src/main/java/io/jenkins/plugins/coverage/metrics/steps/CoverageRecorder.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 557-566 are not covered by tests
}

private boolean isEmpty(final Map<Parser, List<ModuleNode>> results) {
return results.values().stream().mapToInt(Collection::size).sum() == 0;
}

private ProcessingMode ignoreErrors() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected Optional<ModuleNode> processFile(final Path file, final Charset charse
CoverageParser coverageParser = parser.createParser(processingMode);
try (var inputStream = BOMInputStream.builder().setFile(file.toFile()).setCharset(charset).get();
var reader = new InputStreamReader(inputStream, charset)) {
ModuleNode node = coverageParser.parse(reader, log);
ModuleNode node = coverageParser.parse(reader, file.toString(), log);
log.logInfo("Successfully parsed file '%s'", PATH_UTIL.getAbsolutePath(file));
node.aggregateValues().forEach(v -> log.logInfo("%s", v));
return Optional.of(node);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.jenkins.plugins.coverage.metrics.steps;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;

Expand All @@ -24,6 +27,7 @@
import hudson.model.Item;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import hudson.util.ListBoxModel.Option;
import jenkins.model.Jenkins;

import io.jenkins.plugins.prism.SourceCodeRetention;
Expand Down Expand Up @@ -144,21 +148,14 @@ public static class CoverageToolDescriptor extends Descriptor<CoverageTool> {
*/
@POST
public ListBoxModel doFillParserItems() {
if (JENKINS.hasPermission(Jenkins.READ)) {
ListBoxModel options = new ListBoxModel();
add(options, Parser.JACOCO);
add(options, Parser.COBERTURA);
add(options, Parser.OPENCOVER);
add(options, Parser.PIT);
add(options, Parser.JUNIT);
add(options, Parser.NUNIT);
return options;
if (!JENKINS.hasPermission(Jenkins.READ)) {
return new ListBoxModel();
}
return new ListBoxModel();
}

private void add(final ListBoxModel options, final Parser parser) {
options.add(parser.getDisplayName(), parser.name());
List<Option> options = Stream.of(Parser.values())
.map(p -> new Option(p.getDisplayName(), p.name()))
.collect(Collectors.toList());
return new ListBoxModel(options);

Check warning on line 158 in plugin/src/main/java/io/jenkins/plugins/coverage/metrics/steps/CoverageTool.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 151-158 are not covered by tests
}

/**
Expand Down Expand Up @@ -201,32 +198,49 @@ public String getUrl() {
}
}

/**
* Parser types.
*/
public enum ParserType {
COVERAGE,
TEST
}

/**
* Supported coverage parsers.
*/
public enum Parser {
COBERTURA(Messages._Parser_Cobertura(), "**/cobertura.xml",
COBERTURA(Messages._Parser_Cobertura(), ParserType.COVERAGE,
"**/cobertura.xml",
"symbol-footsteps-outline plugin-ionicons-api"),
JACOCO(Messages._Parser_JaCoCo(), "**/jacoco.xml",
JACOCO(Messages._Parser_JaCoCo(), ParserType.COVERAGE,
"**/jacoco.xml",
"symbol-footsteps-outline plugin-ionicons-api"),
OPENCOVER(Messages._Parser_OpenCover(), "**/*opencover.xml",
OPENCOVER(Messages._Parser_OpenCover(), ParserType.COVERAGE,
"**/*opencover.xml",
"symbol-footsteps-outline plugin-ionicons-api"),
PIT(Messages._Parser_PIT(), "**/mutations.xml",
PIT(Messages._Parser_PIT(), ParserType.COVERAGE,
"**/mutations.xml",
"symbol-solid/virus-slash plugin-font-awesome-api"),
JUNIT(Messages._Parser_Junit(), "**/TEST-*.xml",
JUNIT(Messages._Parser_Junit(), ParserType.TEST,
"**/TEST-*.xml",
"symbol-solid/list-check plugin-font-awesome-api"),
NUNIT(Messages._Parser_Nunit(), "**/nunit.xml,**/TestResult.xml",
NUNIT(Messages._Parser_Nunit(), ParserType.TEST,
"**/nunit.xml,**/TestResult.xml",
"symbol-solid/list-check plugin-font-awesome-api"),
XUNIT(Messages._Parser_Xunit(), "**/xunit.xml,**/TestResult.xml",
XUNIT(Messages._Parser_Xunit(), ParserType.TEST,
"**/xunit.xml,**/TestResult.xml",
"symbol-solid/list-check plugin-font-awesome-api");

private final Localizable displayName;
private final ParserType parserType;
private final String defaultPattern;
private final String icon;

Parser(final Localizable displayName, final String defaultPattern,
final String icon) {
Parser(final Localizable displayName, final ParserType parserType,
final String defaultPattern, final String icon) {
this.displayName = displayName;
this.parserType = parserType;
this.defaultPattern = defaultPattern;
this.icon = icon;
}
Expand All @@ -235,6 +249,10 @@ public String getDisplayName() {
return displayName.toString();
}

public ParserType getParserType() {
return parserType;
}

public String getDefaultPattern() {
return defaultPattern;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected Node readJacocoResult(final String fileName) {
*/
protected Node readResult(final String fileName, final CoverageParser parser) {
try {
var node = parser.parse(Files.newBufferedReader(getResourceAsFile(fileName)), log);
var node = parser.parse(Files.newBufferedReader(getResourceAsFile(fileName)), fileName, log);
node.splitPackages();
return node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ class CoverageXmlStreamITest extends SerializableTest<Node> {

@Override
protected Node createSerializable() {
return new JacocoParser().parse(new InputStreamReader(asInputStream("jacoco-codingstyle.xml"),
StandardCharsets.UTF_8), new FilteredLog("Errors"));
var fileName = "jacoco-codingstyle.xml";
return new JacocoParser().parse(new InputStreamReader(asInputStream(fileName),
StandardCharsets.UTF_8), fileName, new FilteredLog("Errors"));
}

@Test
Expand Down

0 comments on commit f88560a

Please sign in to comment.