Skip to content

Commit

Permalink
Add method level coverage reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
itxaberasategui committed Jun 2, 2023
1 parent f138489 commit 8846e19
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/CodeCoverageSummary/CodeSummary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public class CodeCoverage
public double Complexity { get; set; }
}

public class PackageCoverage : CodeCoverage
{
public List<CodeCoverage> Classes { get; set; } = new List<CodeCoverage>();
}

public class CodeSummary
{
public double LineRate { get; set; }
Expand Down
6 changes: 6 additions & 0 deletions src/CodeCoverageSummary/MethodCoverage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace CodeCoverageSummary;

public class MethodCoverage
{

}
43 changes: 35 additions & 8 deletions src/CodeCoverageSummary/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,34 @@ private static CodeSummary ParseTestResults(string filename, CodeSummary summary
throw new Exception("No package data found");

int i = 1;
foreach (var item in packages)
foreach (var package in packages)
{
CodeCoverage packageCoverage = new()
PackageCoverage packageCoverage = new()
{
Name = string.IsNullOrWhiteSpace(item.Attribute("name")?.Value) ? $"{Path.GetFileNameWithoutExtension(filename)} Package {i}" : item.Attribute("name").Value,
LineRate = double.Parse(item.Attribute("line-rate")?.Value ?? "0"),
BranchRate = double.TryParse(item.Attribute("branch-rate")?.Value ?? "0", out double bRate) ? bRate : 0,
Complexity = double.TryParse(item.Attribute("complexity")?.Value ?? "0", out double complex) ? complex : 0
Name = string.IsNullOrWhiteSpace(package.Attribute("name")?.Value) ? $"{Path.GetFileNameWithoutExtension(filename)} Package {i}" : package.Attribute("name").Value,
LineRate = double.Parse(package.Attribute("line-rate")?.Value ?? "0"),
BranchRate = double.TryParse(package.Attribute("branch-rate")?.Value ?? "0", out double bRate) ? bRate : 0,
Complexity = double.TryParse(package.Attribute("complexity")?.Value ?? "0", out double complex) ? complex : 0
};

var classes = from item in package.Descendants("class") select item;

int j = 1;
foreach (var classItem in classes)
{
CodeCoverage classCoverage = new()
{
Name = string.IsNullOrWhiteSpace(classItem.Attribute("name")?.Value) ? $"{Path.GetFileNameWithoutExtension(filename)} Class {i}" : classItem.Attribute("name").Value,
LineRate = double.Parse(classItem.Attribute("line-rate")?.Value ?? "0"),
BranchRate = double.TryParse(classItem.Attribute("branch-rate")?.Value ?? "0", out double classBRate) ? classBRate : 0,
Complexity = double.TryParse(classItem.Attribute("complexity")?.Value ?? "0", out double classComplex) ? classComplex : 0
};
packageCoverage.Classes.Add(classCoverage);
j++;

//var methods = from item in classes.Descendants("method") select item;
}

summary.Packages.Add(packageCoverage);
summary.Complexity += packageCoverage.Complexity;
i++;
Expand Down Expand Up @@ -348,12 +367,20 @@ private static string GenerateMarkdownOutput(CodeSummary summary, string badgeUr
.Append(hideComplexity ? string.Empty : " | ----------")
.AppendLine(indicators ? " | ------" : string.Empty);

foreach (CodeCoverage package in summary.Packages)
foreach (PackageCoverage package in summary.Packages)
{
markdownOutput.Append($"{package.Name} | {package.LineRate * 100:N0}%")
.Append(hideBranchRate ? string.Empty : $" | {package.BranchRate * 100:N0}%")
.Append(hideComplexity ? string.Empty : (package.Complexity % 1 == 0) ? $" | {package.Complexity}" : $" | {package.Complexity:N4}" )
.Append(hideComplexity ? string.Empty : (package.Complexity % 1 == 0) ? $" | {package.Complexity}" : $" | {package.Complexity:N4}")
.AppendLine(indicators ? $" | {GenerateHealthIndicator(package.LineRate)}" : string.Empty);

foreach (CodeCoverage classCoverage in package.Classes)
{
markdownOutput.Append($"{classCoverage.Name} | {classCoverage.LineRate * 100:N0}%")
.Append(hideBranchRate ? string.Empty : $" | {classCoverage.BranchRate * 100:N0}%")
.Append(hideComplexity ? string.Empty : (classCoverage.Complexity % 1 == 0) ? $" | {classCoverage.Complexity}" : $" | {classCoverage.Complexity:N4}")
.AppendLine(indicators ? $" | {GenerateHealthIndicator(classCoverage.LineRate)}" : string.Empty);
}
}

markdownOutput.Append($"**Summary** | **{summary.LineRate * 100:N0}%** ({summary.LinesCovered} / {summary.LinesValid})")
Expand Down

0 comments on commit 8846e19

Please sign in to comment.