Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

Fix #6 problem with accessing files for modules #7

Merged
merged 1 commit into from
Dec 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,25 @@ private Element processModule(Document document, Node module) {
String moduleName = moduleElementOpenCover.getElementsByTagName("ModuleName").item(0).getTextContent();
moduleElement.setAttribute("name", moduleName);

HashMap<String, String> filesMap = new HashMap<>();

Node filesNode = moduleElementOpenCover.getElementsByTagName("Files").item(0);
NodeList listOfFiles = filesNode.getChildNodes();

HashMap<String, String> filesMap = new HashMap<>();
if (filesNode != null) {
NodeList listOfFiles = filesNode.getChildNodes();

for (int fileIndex = 0; fileIndex < listOfFiles.getLength(); fileIndex++) {
Node fileNode = listOfFiles.item(fileIndex);
if (fileNode.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
NamedNodeMap fileAttributes = fileNode.getAttributes();
for (int fileIndex = 0; fileIndex < listOfFiles.getLength(); fileIndex++) {
Node fileNode = listOfFiles.item(fileIndex);
if (fileNode.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
NamedNodeMap fileAttributes = fileNode.getAttributes();

String fileUid = fileAttributes.getNamedItem("uid").getTextContent();
String fileFullPath = fileAttributes.getNamedItem("fullPath").getTextContent();
String fileUid = fileAttributes.getNamedItem("uid").getTextContent();
String fileFullPath = fileAttributes.getNamedItem("fullPath").getTextContent();

filesMap.put(fileUid, fileFullPath);
filesMap.put(fileUid, fileFullPath);
}
}

Node classesNode = moduleElementOpenCover.getElementsByTagName("Classes").item(0);
Expand Down Expand Up @@ -152,11 +155,13 @@ private Element processMethod(Document document, Node methodNode, HashMap<String
String methodFullName = openCoverMethodElement.getElementsByTagName("Name").item(0).getTextContent();
methodElement.setAttribute("name", methodFullName);

Node fireRefNode = openCoverMethodElement.getElementsByTagName("FileRef").item(0);
Node fileRefNode = openCoverMethodElement.getElementsByTagName("FileRef").item(0);

String fileRef = fireRefNode.getAttributes().getNamedItem("uid").getTextContent();
if (filesMap.containsKey(fileRef)) {
methodElement.setAttribute("file", filesMap.get(fileRef));
if (fileRefNode != null) {
String fileRef = fileRefNode.getAttributes().getNamedItem("uid").getTextContent();
if (filesMap.containsKey(fileRef)) {
methodElement.setAttribute("file", filesMap.get(fileRef));
}
}

ArrayList<Element> lineElements = processLineElements(document, openCoverMethodElement);
Expand Down Expand Up @@ -200,17 +205,25 @@ private ArrayList<Element> processLineElements(Document document, Element openCo

NamedNodeMap branchPointAttributes = branchPoint.getAttributes();

String sourceLineNumber = branchPointAttributes.getNamedItem("sl").getTextContent();
Node sourceLineAttribute = null;

BranchInfo branchInfo = branchStatistics.get(sourceLineNumber);
if (branchInfo == null) {
branchInfo = new BranchInfo();
branchStatistics.put(sourceLineNumber, branchInfo);
if (branchPointAttributes != null) {
sourceLineAttribute = branchPointAttributes.getNamedItem("sl");
}
branchInfo.setAllBranches(branchInfo.getAllBranches() + 1);
int branchHits = Integer.parseInt(branchPointAttributes.getNamedItem("vc").getTextContent());
if (branchHits > 0) {
branchInfo.setVisitedBranches(branchInfo.getVisitedBranches() + 1);

if (sourceLineAttribute != null) {
String sourceLineNumber = sourceLineAttribute.getTextContent();

BranchInfo branchInfo = branchStatistics.get(sourceLineNumber);
if (branchInfo == null) {
branchInfo = new BranchInfo();
branchStatistics.put(sourceLineNumber, branchInfo);
}
branchInfo.setAllBranches(branchInfo.getAllBranches() + 1);
int branchHits = Integer.parseInt(branchPointAttributes.getNamedItem("vc").getTextContent());
if (branchHits > 0) {
branchInfo.setVisitedBranches(branchInfo.getVisitedBranches() + 1);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,40 @@ public void SourceFileTest() throws Exception {
CoverageResult methodCoverageResult = moduleCoverageResult.getChild("ClassLibrary.LibraryClass").getChild("System.Int32 ClassLibrary.LibraryClass::Sum(System.Int32,System.Int32)");
Assert.assertEquals(methodCoverageResult.isSourceFileAvailable(), true);
}

@Test
public void ReportWIthSkippedModules() throws Exception {
String opencoverReport = "opencoverwithskippedmodules.xml";
StringBuilder sb = new StringBuilder();
sb.append("node {")
.append("publishCoverage(");

sb.append("adapters:[");

sb.append(String.format("opencoverAdapter(path: '%s')], sourceFileResolver: sourceFiles('NEVER_STORE')", opencoverReport));
sb.append(")").append("}");

WorkflowJob project = j.createProject(WorkflowJob.class, "coverage-pipeline-test");
FilePath workspace = j.jenkins.getWorkspaceFor(project);

Objects.requireNonNull(workspace)
.child(opencoverReport)
.copyFrom(getClass().getResourceAsStream(opencoverReport));

project.setDefinition(new CpsFlowDefinition(sb.toString(), true));
WorkflowRun r = Objects.requireNonNull(project.scheduleBuild2(0)).waitForStart();
Assert.assertNotNull(r);
j.assertBuildStatusSuccess(j.waitForCompletion(r));
CoverageAction coverageAction = r.getAction(CoverageAction.class);

Ratio lineCoverage = coverageAction.getResult().getCoverage(CoverageElement.LINE);
Assert.assertEquals(lineCoverage.toString(),"1577/1657");

Ratio branchCoverage = coverageAction.getResult().getCoverage(CoverageElement.CONDITIONAL);
/*
Would be null because older reports didn't contain the "sl" attribute
which we use to process lines
*/
Assert.assertNull(branchCoverage);
}
}
Loading