Skip to content

Commit 8116db0

Browse files
authored
fix risk hotspot links and allow specific partial class to be opened which is possible for hotspot links (#100)
1 parent 5b2c1a4 commit 8116db0

File tree

4 files changed

+75
-22
lines changed

4 files changed

+75
-22
lines changed

FineCodeCoverage/Core/Cobertura/CoberturaUtil.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ public void ProcessCoberturaXmlFile(string xmlFilePath)
9090
}
9191
}
9292

93-
public string[] GetSourceFiles(string assemblyName, string qualifiedClassName)
93+
public string[] GetSourceFiles(string assemblyName, string qualifiedClassName, int file)
9494
{
9595
// Note : There may be more than one file; e.g. in the case of partial classes
96+
// For riskhotspots the file parameter is available ( otherwise is -1 )
9697

9798
var package = coverageReport
9899
.Packages.Package
@@ -103,9 +104,16 @@ public string[] GetSourceFiles(string assemblyName, string qualifiedClassName)
103104
return new string[0];
104105
}
105106

106-
var classFiles = package
107+
var classes = package
107108
.Classes.Class
108-
.Where(x => x.Name.Equals(qualifiedClassName))
109+
.Where(x => x.Name.Equals(qualifiedClassName));
110+
111+
if (file != -1)
112+
{
113+
classes = new List<Class> { classes.ElementAt(file) };
114+
}
115+
116+
var classFiles = classes
109117
.Select(x => x.Filename)
110118
.ToArray();
111119

FineCodeCoverage/Core/Cobertura/ICoberturaUtil.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ interface ICoberturaUtil
88
List<CoverageLine> CoverageLines { get; }
99

1010
void ProcessCoberturaXmlFile(string xmlFilePath);
11-
string[] GetSourceFiles(string assemblyName, string qualifiedClassName);
11+
string[] GetSourceFiles(string assemblyName, string qualifiedClassName, int file);
1212
}
1313
}

FineCodeCoverage/Core/ReportGenerator/ReportGeneratorUtil.cs

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,17 @@ public void ProcessUnifiedHtmlFile(string htmlFile, bool darkMode, out string co
300300
301301
// TEXT changes
302302
var assemblyClassDelimiter = "!";
303+
303304
var outerHtml = doc.DocumentNode.OuterHtml;
304305
var htmlSb = new StringBuilder(outerHtml);
306+
305307
var assembliesSearch = "var assemblies = [";
306308
var startIndex = outerHtml.IndexOf(assembliesSearch) + assembliesSearch.Length - 1;
307309
var endIndex = outerHtml.IndexOf("var historicCoverageExecutionTimes");
308310
var assembliesToReplace = outerHtml.Substring(startIndex, endIndex - startIndex);
309311
endIndex = assembliesToReplace.LastIndexOf(']');
310312
assembliesToReplace = assembliesToReplace.Substring(0, endIndex + 1);
313+
311314
var assemblies = JArray.Parse(assembliesToReplace);
312315
foreach (JObject assembly in assemblies)
313316
{
@@ -344,7 +347,29 @@ public void ProcessUnifiedHtmlFile(string htmlFile, bool darkMode, out string co
344347
var assembliesReplaced = assemblies.ToString();
345348
htmlSb.Replace(assembliesToReplace, assembliesReplaced);
346349
347-
htmlSb.Replace(".table-fixed", ".table-fixed-ignore-me");
350+
//is this even present if there are no riskhotspots
351+
var riskHotspotsSearch = "var riskHotspots = [";
352+
var rhStartIndex = outerHtml.IndexOf(riskHotspotsSearch) + riskHotspotsSearch.Length - 1;
353+
var rhEndIndex = outerHtml.IndexOf("var branchCoverageAvailable");
354+
var rhToReplace = outerHtml.Substring(rhStartIndex, rhEndIndex - rhStartIndex);
355+
rhEndIndex = rhToReplace.LastIndexOf(']');
356+
rhToReplace = rhToReplace.Substring(0, rhEndIndex + 1);
357+
358+
var riskHotspots = JArray.Parse(rhToReplace);
359+
foreach (JObject riskHotspot in riskHotspots)
360+
{
361+
var assembly = riskHotspot["assembly"].ToString();
362+
var qualifiedClassName = riskHotspot["class"].ToString();
363+
// simplify name
364+
var lastIndexOfDotInName = qualifiedClassName.LastIndexOf('.');
365+
if (lastIndexOfDotInName != -1) riskHotspot["class"] = qualifiedClassName.Substring(lastIndexOfDotInName).Trim('.');
366+
var newReportPath = $"#{assembly}{assemblyClassDelimiter}{qualifiedClassName}.html";
367+
riskHotspot["reportPath"] = newReportPath;
368+
}
369+
var riskHotspotsReplaced = riskHotspots.ToString();
370+
htmlSb.Replace(rhToReplace, riskHotspotsReplaced);
371+
372+
htmlSb.Replace(".table-fixed", ".table-fixed-ignore-me");
348373
349374
htmlSb.Replace("</head>", $@"
350375
<style type=""text/css"">
@@ -399,19 +424,6 @@ public void ProcessUnifiedHtmlFile(string htmlFile, bool darkMode, out string co
399424
400425
eventListener(window,'focus',function(){{window.external.DocumentFocused()}});
401426
402-
var classes = {{}};
403-
404-
Array.prototype.forEach.call(assemblies, function (assembly) {{
405-
setTimeout(function () {{
406-
Array.prototype.forEach.call(assembly.classes, function (classs) {{
407-
setTimeout(function () {{
408-
classs.assembly = assembly;
409-
classes[classs.rp] = classs;
410-
}});
411-
}});
412-
}});
413-
}});
414-
415427
eventListener(document, 'click', function (event) {{
416428
417429
var target = event.target;
@@ -435,7 +447,7 @@ public void ProcessUnifiedHtmlFile(string htmlFile, bool darkMode, out string co
435447
if (fileLine.indexOf('#') !== -1)
436448
fileLine = fileLine.substring(fileLine.indexOf('#') + 1).replace('file', '').replace('line', '').split('_');
437449
else
438-
fileLine = ['0', '0'];
450+
fileLine = ['-1', '0'];
439451
440452
window.external.OpenFile(assembly, qualifiedClassName, parseInt(fileLine[0]), parseInt(fileLine[1]));
441453
@@ -513,8 +525,41 @@ public void ProcessUnifiedHtmlFile(string htmlFile, bool darkMode, out string co
513525
{ button: 'btnSummary', content: 'table-fixed' },
514526
{ button: 'btnRiskHotspots', content: 'risk-hotspots' },
515527
];
516-
517-
var openTab = function (tabIndex) {
528+
529+
var addedFileIndexToRiskHotspots = false;
530+
var addFileIndexToRiskHotspotsClassLink = function(){
531+
if(!addedFileIndexToRiskHotspots){
532+
addedFileIndexToRiskHotspots = true;
533+
var riskHotspotsElements = document.getElementsByTagName('risk-hotspots');
534+
if(riskHotspotsElements.length == 1){{
535+
var riskHotspotsElement = riskHotspotsElements[0];
536+
var riskHotspotsTable = riskHotspotsElement.querySelector('table');
537+
var rhBody = riskHotspotsTable.querySelector('tbody');
538+
var rows = rhBody.rows;
539+
for(var i=0;i<rows.length;i++){
540+
var row = rows[i];
541+
var cells = row.cells;
542+
var classCell = cells[1];
543+
var classLink = classCell.children[0];
544+
var methodCell = cells[2];
545+
var classLink = classCell.children[0];
546+
var methodLink = methodCell.children[0];
547+
var methodHash = methodLink.hash;
548+
var methodHtmlIndex = methodHash.indexOf('.html');
549+
var fileLine = methodHash.substring(methodHtmlIndex + 6);
550+
var fileAndLine = fileLine.replace('file', '').replace('line', '').split('_');
551+
var file = fileAndLine[0];
552+
var line = fileAndLine[1];
553+
classLink.href = classLink.hash + '#file' + file + '_line0';
554+
}
555+
}}
556+
}
557+
}
558+
559+
var openTab = function (tabIndex) {
560+
if(tabIndex==2){{
561+
addFileIndexToRiskHotspotsClassLink();
562+
}}
518563
for (var i = 0; i < tabs.length; i++) {
519564
520565
var tab = tabs[i];

FineCodeCoverage/Core/SourceFileOpener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public async System.Threading.Tasks.Task OpenFileAsync(string assemblyName, stri
3232
{
3333
// Note : There may be more than one file; e.g. in the case of partial classes
3434
//remove CoverageReport
35-
var sourceFiles = coberturaUtil.GetSourceFiles(assemblyName, qualifiedClassName);
35+
var sourceFiles = coberturaUtil.GetSourceFiles(assemblyName, qualifiedClassName,file);
3636

3737
if (!sourceFiles.Any())
3838
{

0 commit comments

Comments
 (0)