| | | 1 | | using System; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Text.RegularExpressions; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using System.Xml.Linq; |
| | | 8 | | using DirectSight.Common; |
| | | 9 | | using DirectSight.Logging; |
| | | 10 | | using DirectSight.Parser.Analysis; |
| | | 11 | | using DirectSight.Parser.Analysis.LineCoverage; |
| | | 12 | | |
| | | 13 | | namespace DirectSight.Parser; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Parser for XML reports generated by OpenCover. |
| | | 17 | | /// </summary> |
| | | 18 | | internal class OpenCoverParser : ParserBase |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Regex to analyze if a class name represents a generic class. |
| | | 22 | | /// </summary> |
| | 1 | 23 | | private static readonly Regex GenericClassRegex = |
| | 1 | 24 | | new("<.*>$", RegexOptions.Compiled); |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Regex to analyze if a class name represents an async (generic) class. |
| | | 28 | | /// Format gets generated by 'dotnet test --collect "Code Coverage;Format=Cobertura"'. |
| | | 29 | | /// </summary> |
| | 1 | 30 | | private static readonly Regex AsyncClassRegex = |
| | 1 | 31 | | new("^(?<ClassName>.+)\\.<.*>.*__(?:.+(?<GenericTypes><.+>))?", RegexOptions.Compiled); |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Regex to analyze if a method name belongs to a lamda expression. |
| | | 35 | | /// </summary> |
| | 1 | 36 | | private static readonly Regex LambdaMethodNameRegex = |
| | 1 | 37 | | new("::<.+>.+__[^\\|]+$", RegexOptions.Compiled); |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Regex to analyze if a method name is generated by compiler. |
| | | 41 | | /// </summary> |
| | 1 | 42 | | private static readonly Regex CompilerGeneratedMethodNameRegex = |
| | 1 | 43 | | new(@"<(?<CompilerGeneratedName>.+)>.+__.+::MoveNext\(\)$", RegexOptions.Compiled); |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Regex to analyze if a method name is a nested method (a method nested within a method). |
| | | 47 | | /// </summary> |
| | 1 | 48 | | private static readonly Regex LocalFunctionMethodNameRegex = |
| | 1 | 49 | | new(@"^.*(?<ParentMethodName><.+>).*__(?<NestedMethodName>[^\|]+)\|.+\((?<Arguments>.*)\).*$", RegexOptions.Comp |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Regex to extract short method name. |
| | | 53 | | /// </summary> |
| | 1 | 54 | | private static readonly Regex MethodRegex = |
| | 1 | 55 | | new(@"^.*::(?<MethodName>.+)\((?<Arguments>.*)\)$", RegexOptions.Compiled); |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Cache for method names. |
| | | 59 | | /// </summary> |
| | 1 | 60 | | private static readonly ConcurrentDictionary<string, string> MethodNameMap = new(); |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Initializes a new instance of the <see cref="OpenCoverParser" /> class. |
| | | 64 | | /// </summary> |
| | 88 | 65 | | internal OpenCoverParser() |
| | 88 | 66 | | { |
| | 88 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Parses the given XML report. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="report">The XML report.</param> |
| | | 73 | | /// <returns>The parser result.</returns> |
| | | 74 | | public ParserResult Parse(XContainer report) |
| | 88 | 75 | | { |
| | 88 | 76 | | ArgumentNullException.ThrowIfNull(report); |
| | | 77 | | |
| | 88 | 78 | | var assemblies = new List<Assembly>(); |
| | | 79 | | |
| | 88 | 80 | | var modules = report.Descendants("Module") |
| | 1102 | 81 | | .Where(m => m.Attribute("skippedDueTo") == null) |
| | 88 | 82 | | .ToArray(); |
| | 88 | 83 | | var files = report.Descendants("File").ToArray(); |
| | | 84 | | |
| | 88 | 85 | | var trackedMethods = new Dictionary<string, string>(); |
| | | 86 | | |
| | 324 | 87 | | foreach (var trackedMethodElement in report.Descendants("TrackedMethod")) |
| | 30 | 88 | | { |
| | 30 | 89 | | if (trackedMethods.ContainsKey(trackedMethodElement.Attribute("uid").Value)) |
| | 0 | 90 | | { |
| | 0 | 91 | | ConsoleLogger.Warn( |
| | 0 | 92 | | "The 'uid' of tracked method '{0}' is not unique. Ignoring tracked methods.", |
| | 0 | 93 | | trackedMethodElement.Attribute("name").Value); |
| | | 94 | | |
| | 0 | 95 | | trackedMethods.Clear(); |
| | | 96 | | |
| | 0 | 97 | | break; |
| | | 98 | | } |
| | | 99 | | else |
| | 30 | 100 | | { |
| | 30 | 101 | | trackedMethods.Add(trackedMethodElement.Attribute("uid").Value, trackedMethodElement.Attribute("name").V |
| | 30 | 102 | | } |
| | 30 | 103 | | } |
| | | 104 | | |
| | 88 | 105 | | var assemblyNames = modules |
| | 88 | 106 | | .Select(m => m.Element("ModuleName").Value) |
| | 88 | 107 | | .Distinct() |
| | 88 | 108 | | .OrderBy(a => a) |
| | 88 | 109 | | .ToArray(); |
| | | 110 | | |
| | 88 | 111 | | var assemblyModules = assemblyNames |
| | 88 | 112 | | .ToDictionary( |
| | 88 | 113 | | k => k, |
| | 264 | 114 | | v => modules.Where(t => t.Element("ModuleName").Value.Equals(v)).ToArray()); |
| | | 115 | | |
| | 440 | 116 | | foreach (var assemblyName in assemblyNames) |
| | 88 | 117 | | { |
| | 88 | 118 | | var assembly = ProcessAssembly(assemblyModules, files, trackedMethods, assemblyName); |
| | 88 | 119 | | if (assembly.Classes.Any()) |
| | 88 | 120 | | { |
| | 88 | 121 | | assemblies.Add(assembly); |
| | 88 | 122 | | } |
| | 88 | 123 | | } |
| | | 124 | | |
| | 176 | 125 | | var result = new ParserResult([.. assemblies.OrderBy(a => a.Name)], true, this.ToString()); |
| | 88 | 126 | | return result; |
| | 88 | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Processes the given assembly. |
| | | 131 | | /// </summary> |
| | | 132 | | /// <param name="assemblyModules">The modules belonging to a assembly name.</param> |
| | | 133 | | /// <param name="files">The files.</param> |
| | | 134 | | /// <param name="trackedMethods">The tracked methods.</param> |
| | | 135 | | /// <param name="assemblyName">Name of the assembly.</param> |
| | | 136 | | /// <returns>The <see cref="Assembly"/>.</returns> |
| | | 137 | | private static Assembly ProcessAssembly( |
| | | 138 | | Dictionary<string, XElement[]> assemblyModules, |
| | | 139 | | XElement[] files, |
| | | 140 | | IDictionary<string, string> trackedMethods, |
| | | 141 | | string assemblyName) |
| | 88 | 142 | | { |
| | 88 | 143 | | ConsoleLogger.Debug("Current Assembly: {0}", assemblyName); |
| | | 144 | | |
| | 88 | 145 | | var fileIdsByFilename = assemblyModules[assemblyName] |
| | 88 | 146 | | .Elements("Files") |
| | 88 | 147 | | .Elements("File") |
| | 1272 | 148 | | .GroupBy(f => f.Attribute("fullPath").Value) |
| | 2632 | 149 | | .ToDictionary(g => g.Key, g => new FileElement(g)); |
| | | 150 | | |
| | 88 | 151 | | var classNames = assemblyModules[assemblyName] |
| | 88 | 152 | | .Elements("Classes") |
| | 88 | 153 | | .Elements("Class") |
| | 2222 | 154 | | .Where(c => c.Attribute("skippedDueTo") == null) |
| | 2149 | 155 | | .Where(c => !c.Element("FullName").Value.Contains("<>f__AnonymousType")) |
| | 2149 | 156 | | .Where(c => c.Element("Methods").Elements("Method").Any()) |
| | 88 | 157 | | .Select(c => |
| | 1973 | 158 | | { |
| | 1973 | 159 | | string fullname = c.Element("FullName").Value; |
| | 1973 | 160 | | int nestedClassSeparatorIndex = fullname.IndexOf('/'); |
| | 1973 | 161 | | if (nestedClassSeparatorIndex > -1) |
| | 504 | 162 | | { |
| | 504 | 163 | | string className = fullname.Substring(0, nestedClassSeparatorIndex); |
| | 504 | 164 | | return Tuple.Create(className, className); |
| | 88 | 165 | | } |
| | 88 | 166 | | |
| | 1469 | 167 | | if (fullname.Contains('<')) |
| | 0 | 168 | | { |
| | 0 | 169 | | var match = AsyncClassRegex.Match(fullname); |
| | 88 | 170 | | |
| | 0 | 171 | | if (match.Success) |
| | 0 | 172 | | { |
| | 0 | 173 | | return Tuple.Create( |
| | 0 | 174 | | match.Groups["ClassName"].Value, |
| | 0 | 175 | | match.Groups["ClassName"].Value + match.Groups["GenericTypes"].Value); |
| | 88 | 176 | | } |
| | 0 | 177 | | } |
| | 88 | 178 | | |
| | 1469 | 179 | | return Tuple.Create(fullname, fullname); |
| | 1973 | 180 | | }) |
| | 1973 | 181 | | .Where(c => !c.Item1.Contains('<') || GenericClassRegex.IsMatch(c.Item1)) |
| | 1973 | 182 | | .Select(i => i.Item1) |
| | 88 | 183 | | .Distinct() |
| | 1469 | 184 | | .OrderBy(name => name) |
| | 88 | 185 | | .ToArray(); |
| | | 186 | | |
| | 88 | 187 | | var assembly = new Assembly(assemblyName); |
| | | 188 | | |
| | 1557 | 189 | | Parallel.ForEach(classNames, className => ProcessClass(assemblyModules, files, trackedMethods, fileIdsByFilename |
| | | 190 | | |
| | 88 | 191 | | return assembly; |
| | 88 | 192 | | } |
| | | 193 | | |
| | | 194 | | /// <summary> |
| | | 195 | | /// Processes the given class. |
| | | 196 | | /// </summary> |
| | | 197 | | /// <param name="assemblyModules">The modules belonging to a assembly name.</param> |
| | | 198 | | /// <param name="files">The files.</param> |
| | | 199 | | /// <param name="trackedMethods">The tracked methods.</param> |
| | | 200 | | /// <param name="fileIdsByFilename">Dictionary containing the file ids by filename.</param> |
| | | 201 | | /// <param name="assembly">The assembly.</param> |
| | | 202 | | /// <param name="className">Name of the class.</param> |
| | | 203 | | private static void ProcessClass( |
| | | 204 | | Dictionary<string, XElement[]> assemblyModules, |
| | | 205 | | XElement[] files, |
| | | 206 | | IDictionary<string, string> trackedMethods, |
| | | 207 | | Dictionary<string, FileElement> fileIdsByFilename, |
| | | 208 | | Assembly assembly, |
| | | 209 | | string className) |
| | 1469 | 210 | | { |
| | 1469 | 211 | | var methods = assemblyModules[assembly.Name] |
| | 1469 | 212 | | .Elements("Classes") |
| | 1469 | 213 | | .Elements("Class") |
| | 37534 | 214 | | .Where(c => c.Element("FullName").Value.Equals(className) |
| | 37534 | 215 | | || c.Element("FullName").Value.StartsWith(className + "/", StringComparison.Ordinal) |
| | 37534 | 216 | | || c.Element("FullName").Value.StartsWith(className + ".", StringComparison.Ordinal)) |
| | 1469 | 217 | | .Elements("Methods") |
| | 1469 | 218 | | .Elements("Method") |
| | 6857 | 219 | | .Where(m => m.Attribute("skippedDueTo") == null) |
| | 1469 | 220 | | .ToArray(); |
| | | 221 | | |
| | 1469 | 222 | | var fileIdsOfClassInSequencePoints = methods |
| | 1469 | 223 | | .Elements("SequencePoints") |
| | 1469 | 224 | | .Elements("SequencePoint") |
| | 17053 | 225 | | .Select(seqpnt => seqpnt.Attribute("fileid")?.Value) |
| | 17053 | 226 | | .Where(seqpnt => seqpnt != null && seqpnt != "0") |
| | 1469 | 227 | | .ToArray(); |
| | | 228 | | |
| | 1469 | 229 | | var fileIdsOfClass = fileIdsOfClassInSequencePoints |
| | 1469 | 230 | | .Distinct() |
| | 1469 | 231 | | .ToHashSet(); |
| | | 232 | | |
| | 1469 | 233 | | var filesOfClass = files |
| | 21555 | 234 | | .Where(file => fileIdsOfClass.Contains(file.Attribute("uid").Value)) |
| | 1524 | 235 | | .Select(file => file.Attribute("fullPath").Value) |
| | 1469 | 236 | | .Distinct() |
| | 1469 | 237 | | .ToArray(); |
| | | 238 | | |
| | 1469 | 239 | | var @class = new Class(className, assembly); |
| | | 240 | | |
| | 7455 | 241 | | foreach (var file in filesOfClass) |
| | 1524 | 242 | | { |
| | 1524 | 243 | | @class.AddFile(ProcessFile(trackedMethods, fileIdsByFilename[file], file, methods)); |
| | 1524 | 244 | | } |
| | | 245 | | |
| | 1469 | 246 | | assembly.AddClass(@class); |
| | 1469 | 247 | | } |
| | | 248 | | |
| | | 249 | | /// <summary> |
| | | 250 | | /// Processes the file. |
| | | 251 | | /// </summary> |
| | | 252 | | /// <param name="trackedMethods">The tracked methods.</param> |
| | | 253 | | /// <param name="fileIds">The file ids of the class.</param> |
| | | 254 | | /// <param name="filePath">The file path.</param> |
| | | 255 | | /// <param name="methods">The methods.</param> |
| | | 256 | | /// <returns>The <see cref="CodeFile"/>.</returns> |
| | | 257 | | private static CodeFile ProcessFile(IDictionary<string, string> trackedMethods, FileElement fileIds, string filePath |
| | 1524 | 258 | | { |
| | 1524 | 259 | | var seqpntsOfFile = methods |
| | 1524 | 260 | | .Elements("SequencePoints") |
| | 1524 | 261 | | .Elements("SequencePoint") |
| | 19575 | 262 | | .Where(seqpnt => (seqpnt.Attribute("fileid") != null |
| | 19575 | 263 | | && fileIds.Uids.Contains(seqpnt.Attribute("fileid").Value)) |
| | 19575 | 264 | | || (seqpnt.Attribute("fileid") == null && seqpnt.Parent.Parent.Element("FileRef") != null |
| | 19575 | 265 | | && fileIds.Uids.Contains(seqpnt.Parent.Parent.Element("FileRef").Attribute("uid").Value))) |
| | 17053 | 266 | | .Select(seqpnt => new |
| | 17053 | 267 | | { |
| | 17053 | 268 | | LineNumberStart = int.Parse(seqpnt.Attribute("sl").Value), |
| | 17053 | 269 | | LineNumberEnd = |
| | 17053 | 270 | | seqpnt.Attribute("el") != null |
| | 17053 | 271 | | ? int.Parse(seqpnt.Attribute("el").Value) |
| | 17053 | 272 | | : int.Parse(seqpnt.Attribute("sl").Value), |
| | 17053 | 273 | | Visits = seqpnt.Attribute("vc").Value.ParseLargeInteger(), |
| | 17053 | 274 | | TrackedMethodRefs = seqpnt.Elements("TrackedMethodRefs") |
| | 17053 | 275 | | .Elements("TrackedMethodRef") |
| | 7950 | 276 | | .Select(t => new |
| | 7950 | 277 | | { |
| | 7950 | 278 | | Visits = t.Attribute("vc").Value.ParseLargeInteger(), |
| | 7950 | 279 | | TrackedMethodId = t.Attribute("uid").Value |
| | 7950 | 280 | | }) |
| | 17053 | 281 | | }) |
| | 17053 | 282 | | .OrderBy(seqpnt => seqpnt.LineNumberEnd) |
| | 1524 | 283 | | .ToArray(); |
| | | 284 | | |
| | 1524 | 285 | | var branches = GetBranches(methods, fileIds); |
| | | 286 | | |
| | 1524 | 287 | | var coverageByTrackedMethod = seqpntsOfFile |
| | 17053 | 288 | | .SelectMany(s => s.TrackedMethodRefs) |
| | 3930 | 289 | | .Select(t => t.TrackedMethodId) |
| | 1524 | 290 | | .Distinct() |
| | 1524 | 291 | | .ToDictionary( |
| | 450 | 292 | | id => id, |
| | 1974 | 293 | | id => new CoverageByTrackedMethod |
| | 1974 | 294 | | { |
| | 1974 | 295 | | Coverage = LineInfoFactory.Create(0, -1), |
| | 1974 | 296 | | LineVisitStatus = LineInfoFactory.Create(0, LineVisitStatus.NotCoverable) |
| | 1974 | 297 | | }); |
| | | 298 | | |
| | 1524 | 299 | | var coverage = LineInfoFactory.Create(0, -1); |
| | 1524 | 300 | | var lineVisitStatus = LineInfoFactory.Create(0, LineVisitStatus.NotCoverable); |
| | | 301 | | |
| | 1524 | 302 | | if (seqpntsOfFile.Length > 0) |
| | 1524 | 303 | | { |
| | 1524 | 304 | | coverage = LineInfoFactory.Create(seqpntsOfFile[seqpntsOfFile.LongLength - 1].LineNumberEnd + 1, -1); |
| | 1524 | 305 | | lineVisitStatus = LineInfoFactory.Create(seqpntsOfFile[seqpntsOfFile.LongLength - 1].LineNumberEnd + 1, Line |
| | | 306 | | |
| | 5472 | 307 | | foreach (var trackedMethodCoverage in coverageByTrackedMethod) |
| | 450 | 308 | | { |
| | 450 | 309 | | trackedMethodCoverage.Value.Coverage = coverage.Clone(); |
| | 450 | 310 | | trackedMethodCoverage.Value.LineVisitStatus = lineVisitStatus.Clone(); |
| | 450 | 311 | | } |
| | | 312 | | |
| | 38678 | 313 | | foreach (var seqpnt in seqpntsOfFile) |
| | 17053 | 314 | | { |
| | 69268 | 315 | | for (int lineNumber = seqpnt.LineNumberStart; lineNumber <= seqpnt.LineNumberEnd; lineNumber++) |
| | 17581 | 316 | | { |
| | 17581 | 317 | | int visits = coverage[lineNumber] == -1 ? seqpnt.Visits : coverage[lineNumber] + seqpnt.Visits; |
| | 17581 | 318 | | coverage[lineNumber] = visits; |
| | | 319 | | |
| | 17581 | 320 | | if (lineVisitStatus[lineNumber] != LineVisitStatus.Covered) |
| | 17153 | 321 | | { |
| | 17153 | 322 | | bool partiallyCovered = false; |
| | | 323 | | |
| | | 324 | | // Use 'LineNumberStart' instead of 'lineNumber' here. Branches have line number of first line o |
| | 17153 | 325 | | if (branches.TryGetValue(seqpnt.LineNumberStart, out ICollection<Branch> branchesOfLine)) |
| | 452 | 326 | | { |
| | 1350 | 327 | | partiallyCovered = branchesOfLine.Any(b => b.BranchVisits == 0); |
| | 452 | 328 | | } |
| | | 329 | | |
| | 17153 | 330 | | LineVisitStatus statusOfLine = |
| | 17153 | 331 | | visits > 0 |
| | 17153 | 332 | | ? (partiallyCovered ? LineVisitStatus.PartiallyCovered : LineVisitStatus.Covered) |
| | 17153 | 333 | | : LineVisitStatus.NotCovered; |
| | | 334 | | |
| | 17153 | 335 | | lineVisitStatus[lineNumber] = (LineVisitStatus)Math.Max((int)lineVisitStatus[lineNumber], (int)s |
| | 17153 | 336 | | } |
| | | 337 | | |
| | 17581 | 338 | | if (visits > -1) |
| | 17581 | 339 | | { |
| | 64203 | 340 | | foreach (var trackedMethodCoverage in coverageByTrackedMethod) |
| | 5730 | 341 | | { |
| | 5730 | 342 | | if (trackedMethodCoverage.Value.Coverage[lineNumber] == -1) |
| | 5280 | 343 | | { |
| | 5280 | 344 | | trackedMethodCoverage.Value.Coverage[lineNumber] = 0; |
| | 5280 | 345 | | trackedMethodCoverage.Value.LineVisitStatus[lineNumber] = LineVisitStatus.NotCovered; |
| | 5280 | 346 | | } |
| | 5730 | 347 | | } |
| | 17581 | 348 | | } |
| | | 349 | | |
| | 60783 | 350 | | foreach (var trackedMethod in seqpnt.TrackedMethodRefs) |
| | 4020 | 351 | | { |
| | 4020 | 352 | | var trackedMethodCoverage = coverageByTrackedMethod[trackedMethod.TrackedMethodId]; |
| | | 353 | | |
| | 4020 | 354 | | int trackeMethodVisits = |
| | 4020 | 355 | | trackedMethodCoverage.Coverage[lineNumber] == -1 |
| | 4020 | 356 | | ? trackedMethod.Visits |
| | 4020 | 357 | | : trackedMethodCoverage.Coverage[lineNumber] + trackedMethod.Visits; |
| | | 358 | | |
| | 4020 | 359 | | LineVisitStatus statusOfLine = |
| | 4020 | 360 | | trackeMethodVisits > 0 |
| | 4020 | 361 | | ? (LineVisitStatus)Math.Min((int)LineVisitStatus.Covered, (int)lineVisitStatus[lineNumber]) |
| | 4020 | 362 | | : LineVisitStatus.NotCovered; |
| | | 363 | | |
| | 4020 | 364 | | trackedMethodCoverage.Coverage[lineNumber] = trackeMethodVisits; |
| | 4020 | 365 | | trackedMethodCoverage.LineVisitStatus[lineNumber] = statusOfLine; |
| | 4020 | 366 | | } |
| | 17581 | 367 | | } |
| | 17053 | 368 | | } |
| | 1524 | 369 | | } |
| | | 370 | | |
| | 1524 | 371 | | var codeFile = new CodeFile(filePath, coverage, lineVisitStatus, branches); |
| | | 372 | | |
| | 5472 | 373 | | foreach (var trackedMethodCoverage in coverageByTrackedMethod) |
| | 450 | 374 | | { |
| | | 375 | | // Sometimes no corresponding MethodRef element exists |
| | 450 | 376 | | if (trackedMethods.TryGetValue(trackedMethodCoverage.Key, out string name)) |
| | 450 | 377 | | { |
| | 450 | 378 | | string shortName = name[(name[..(name.IndexOf(':') + 1)].LastIndexOf('.') + 1)..]; |
| | 450 | 379 | | var testMethod = new TestMethod(name, shortName); |
| | 450 | 380 | | codeFile.AddCoverageByTestMethod(testMethod, trackedMethodCoverage.Value); |
| | 450 | 381 | | } |
| | 450 | 382 | | } |
| | | 383 | | |
| | 1524 | 384 | | var methodsOfFile = methods |
| | 7595 | 385 | | .Where(m => m.Element("FileRef") != null && fileIds.Uids.Contains(m.Element("FileRef").Attribute("uid").Valu |
| | 1524 | 386 | | .ToArray(); |
| | | 387 | | |
| | 1524 | 388 | | SetMethodMetrics(codeFile, methodsOfFile); |
| | 1524 | 389 | | SetCodeElements(codeFile, methodsOfFile); |
| | | 390 | | |
| | 1524 | 391 | | return codeFile; |
| | 1524 | 392 | | } |
| | | 393 | | |
| | | 394 | | /// <summary> |
| | | 395 | | /// Extracts the metrics from the given <see cref="XElement">XElements</see>. |
| | | 396 | | /// </summary> |
| | | 397 | | /// <param name="codeFile">The code file.</param> |
| | | 398 | | /// <param name="methodsOfFile">The methods of the file.</param> |
| | | 399 | | private static void SetMethodMetrics(CodeFile codeFile, IEnumerable<XElement> methodsOfFile) |
| | 1524 | 400 | | { |
| | 18369 | 401 | | foreach (var methodGroup in methodsOfFile.GroupBy(m => m.Element("Name").Value)) |
| | 4599 | 402 | | { |
| | 4599 | 403 | | var method = methodGroup.First(); |
| | | 404 | | |
| | | 405 | | // Exclude properties and lambda expressions |
| | 4599 | 406 | | if (method.Attribute("skippedDueTo") != null |
| | 4599 | 407 | | || method.HasAttributeWithValue("isGetter", "true") |
| | 4599 | 408 | | || method.HasAttributeWithValue("isSetter", "true") |
| | 4599 | 409 | | || LambdaMethodNameRegex.IsMatch(methodGroup.Key)) |
| | 1612 | 410 | | { |
| | 1612 | 411 | | continue; |
| | | 412 | | } |
| | | 413 | | |
| | 2987 | 414 | | var metrics = new List<Metric>() |
| | 2987 | 415 | | { |
| | 2987 | 416 | | Metric.CyclomaticComplexity( |
| | 2987 | 417 | | methodGroup.Max(m => int.Parse(m.Attribute("cyclomaticComplexity").Value))), |
| | 2987 | 418 | | Metric.SequenceCoverage( |
| | 2987 | 419 | | methodGroup.Max(m => decimal.Parse(m.Attribute("sequenceCoverage").Value))), |
| | 2987 | 420 | | Metric.BranchCoverage( |
| | 2987 | 421 | | methodGroup.Max(m => decimal.Parse(m.Attribute("branchCoverage").Value))) |
| | 2987 | 422 | | }; |
| | | 423 | | |
| | 8961 | 424 | | var npathComplexityAttributes = methodGroup.Select(m => m.Attribute("nPathComplexity")).Where(a => a != null |
| | | 425 | | |
| | 2987 | 426 | | if (npathComplexityAttributes.Length > 0) |
| | 2885 | 427 | | { |
| | 2885 | 428 | | metrics.Insert( |
| | 2885 | 429 | | 1, |
| | 2885 | 430 | | Metric.NPathComplexity( |
| | 8655 | 431 | | npathComplexityAttributes.Select(a => int.Parse(a.Value)).Max(a => a < 0 ? int.MaxValue : a))); |
| | 2885 | 432 | | } |
| | | 433 | | |
| | 8961 | 434 | | var crapScoreAttributes = methodGroup.Select(m => m.Attribute("crapScore")).Where(a => a != null).ToArray(); |
| | 2987 | 435 | | if (crapScoreAttributes.Length > 0) |
| | 2885 | 436 | | { |
| | 2885 | 437 | | metrics.Add(Metric.CrapScore( |
| | 5770 | 438 | | crapScoreAttributes.Max(a => decimal.Parse(a.Value)))); |
| | 2885 | 439 | | } |
| | | 440 | | |
| | 2987 | 441 | | string fullName = ExtractMethodName(methodGroup.Key); |
| | 2987 | 442 | | string shortName = MethodRegex.Replace( |
| | 2987 | 443 | | fullName, |
| | 5810 | 444 | | m => string.Format( |
| | 5810 | 445 | | "{0}({1})", |
| | 5810 | 446 | | m.Groups["MethodName"].Value, |
| | 5810 | 447 | | m.Groups["Arguments"].Value.Length > 0 ? "..." : string.Empty)); |
| | | 448 | | |
| | 2987 | 449 | | var methodMetric = new MethodMetric(fullName, shortName, metrics); |
| | | 450 | | |
| | 2987 | 451 | | var seqpnt = method |
| | 2987 | 452 | | .Elements("SequencePoints") |
| | 2987 | 453 | | .Elements("SequencePoint") |
| | 2987 | 454 | | .FirstOrDefault(); |
| | | 455 | | |
| | 2987 | 456 | | if (seqpnt != null) |
| | 2987 | 457 | | { |
| | 2987 | 458 | | methodMetric.Line = int.Parse(seqpnt.Attribute("sl").Value); |
| | 2987 | 459 | | } |
| | | 460 | | |
| | 2987 | 461 | | codeFile.AddMethodMetric(methodMetric); |
| | 2987 | 462 | | } |
| | 1524 | 463 | | } |
| | | 464 | | |
| | | 465 | | /// <summary> |
| | | 466 | | /// Gets the branches by line number. |
| | | 467 | | /// </summary> |
| | | 468 | | /// <param name="methods">The methods.</param> |
| | | 469 | | /// <param name="fileIds">The file ids of the class.</param> |
| | | 470 | | /// <returns>The branches by line number.</returns> |
| | | 471 | | private static Dictionary<int, ICollection<Branch>> GetBranches(XElement[] methods, FileElement fileIds) |
| | 1524 | 472 | | { |
| | 1524 | 473 | | var branchPoints = methods |
| | 1524 | 474 | | .Elements("BranchPoints") |
| | 1524 | 475 | | .Elements("BranchPoint") |
| | 1524 | 476 | | .ToArray(); |
| | | 477 | | |
| | | 478 | | // OpenCover supports this since version 4.5.3207 |
| | 1524 | 479 | | if (branchPoints.Length == 0 || branchPoints[0].Attribute("sl") == null) |
| | 1090 | 480 | | { |
| | 1090 | 481 | | return []; |
| | | 482 | | } |
| | | 483 | | |
| | 434 | 484 | | var result = new Dictionary<int, Dictionary<string, Branch>>(); |
| | 3110 | 485 | | foreach (var branchPoint in branchPoints) |
| | 904 | 486 | | { |
| | 904 | 487 | | if (branchPoint.Attribute("fileid") != null |
| | 904 | 488 | | && !fileIds.Uids.Contains(branchPoint.Attribute("fileid").Value)) |
| | 164 | 489 | | { |
| | | 490 | | // If fileid is available, verify that branch belongs to same file (available since version OpenCover.4. |
| | 164 | 491 | | continue; |
| | | 492 | | } |
| | | 493 | | |
| | 740 | 494 | | int lineNumber = int.Parse(branchPoint.Attribute("sl").Value); |
| | | 495 | | |
| | 740 | 496 | | string identifier = string.Format( |
| | 740 | 497 | | "{0}_{1}_{2}_{3}", |
| | 740 | 498 | | lineNumber, |
| | 740 | 499 | | branchPoint.Attribute("path").Value, |
| | 740 | 500 | | branchPoint.Attribute("offset").Value, |
| | 740 | 501 | | branchPoint.Attribute("offsetend").Value); |
| | 740 | 502 | | int vc = branchPoint.Attribute("vc").Value.ParseLargeInteger(); |
| | | 503 | | |
| | 740 | 504 | | if (result.TryGetValue(lineNumber, out var branches)) |
| | 376 | 505 | | { |
| | 376 | 506 | | if (branches.TryGetValue(identifier, out var found)) |
| | 0 | 507 | | { |
| | 0 | 508 | | found.BranchVisits += vc; |
| | 0 | 509 | | } |
| | | 510 | | else |
| | 376 | 511 | | { |
| | 376 | 512 | | branches.Add(identifier, new Branch(vc, identifier)); |
| | 376 | 513 | | } |
| | 376 | 514 | | } |
| | | 515 | | else |
| | 364 | 516 | | { |
| | 364 | 517 | | branches = new Dictionary<string, Branch> |
| | 364 | 518 | | { |
| | 364 | 519 | | { identifier, new Branch(vc, identifier) } |
| | 364 | 520 | | }; |
| | 364 | 521 | | result.Add(lineNumber, branches); |
| | 364 | 522 | | } |
| | 740 | 523 | | } |
| | | 524 | | |
| | 1162 | 525 | | return result.ToDictionary(k => k.Key, v => (ICollection<Branch>)[.. v.Value.Values]); |
| | 1524 | 526 | | } |
| | | 527 | | |
| | | 528 | | /// <summary> |
| | | 529 | | /// Extracts the methods/properties of the given <see cref="XElement">XElements</see>. |
| | | 530 | | /// </summary> |
| | | 531 | | /// <param name="codeFile">The code file.</param> |
| | | 532 | | /// <param name="methodsOfFile">The methods of the file.</param> |
| | | 533 | | private static void SetCodeElements(CodeFile codeFile, IEnumerable<XElement> methodsOfFile) |
| | 1524 | 534 | | { |
| | 13770 | 535 | | foreach (var method in methodsOfFile) |
| | 4599 | 536 | | { |
| | 4599 | 537 | | if (method.Attribute("skippedDueTo") != null |
| | 4599 | 538 | | || LambdaMethodNameRegex.IsMatch(method.Element("Name").Value)) |
| | 176 | 539 | | { |
| | 176 | 540 | | continue; |
| | | 541 | | } |
| | | 542 | | |
| | 4423 | 543 | | string fullName = ExtractMethodName(method.Element("Name").Value); |
| | 4423 | 544 | | string methodName = fullName[(fullName.LastIndexOf(':') + 1)..]; |
| | | 545 | | |
| | 4423 | 546 | | CodeElementType type = CodeElementType.Method; |
| | | 547 | | |
| | 4423 | 548 | | if (method.HasAttributeWithValue("isGetter", "true") |
| | 4423 | 549 | | || method.HasAttributeWithValue("isSetter", "true")) |
| | 1436 | 550 | | { |
| | 1436 | 551 | | type = CodeElementType.Property; |
| | 1436 | 552 | | methodName = methodName[4..]; |
| | 1436 | 553 | | } |
| | | 554 | | |
| | 4423 | 555 | | var seqpnts = method |
| | 4423 | 556 | | .Elements("SequencePoints") |
| | 4423 | 557 | | .Elements("SequencePoint") |
| | 16877 | 558 | | .Select(seqpnt => new |
| | 16877 | 559 | | { |
| | 16877 | 560 | | LineNumberStart = int.Parse(seqpnt.Attribute("sl").Value), |
| | 16877 | 561 | | LineNumberEnd = |
| | 16877 | 562 | | seqpnt.Attribute("el") != null |
| | 16877 | 563 | | ? int.Parse(seqpnt.Attribute("el").Value) |
| | 16877 | 564 | | : int.Parse(seqpnt.Attribute("sl").Value) |
| | 16877 | 565 | | }) |
| | 4423 | 566 | | .ToArray(); |
| | | 567 | | |
| | 4423 | 568 | | if (seqpnts.Length > 0) |
| | 4423 | 569 | | { |
| | 21300 | 570 | | int firstLine = seqpnts.Min(s => s.LineNumberStart); |
| | 21300 | 571 | | int lastLine = seqpnts.Max(s => s.LineNumberEnd); |
| | | 572 | | |
| | 4423 | 573 | | codeFile.AddCodeElement(new CodeElement( |
| | 4423 | 574 | | fullName, |
| | 4423 | 575 | | methodName, |
| | 4423 | 576 | | type, |
| | 4423 | 577 | | firstLine, |
| | 4423 | 578 | | lastLine, |
| | 4423 | 579 | | codeFile.CoverageQuotaInRange(firstLine, lastLine))); |
| | 4423 | 580 | | } |
| | 4423 | 581 | | } |
| | 1524 | 582 | | } |
| | | 583 | | |
| | | 584 | | /// <summary> |
| | | 585 | | /// Extracts the method name. For async methods the original name is returned. |
| | | 586 | | /// </summary> |
| | | 587 | | /// <param name="methodName">The full method name.</param> |
| | | 588 | | /// <returns>The method name.</returns> |
| | | 589 | | private static string ExtractMethodName(string methodName) |
| | 7410 | 590 | | { |
| | 7410 | 591 | | if (!MethodNameMap.TryGetValue(methodName, out var fullName)) |
| | 381 | 592 | | { |
| | 381 | 593 | | if (methodName.Contains("|")) |
| | 0 | 594 | | { |
| | 0 | 595 | | Match match = LocalFunctionMethodNameRegex.Match(methodName); |
| | | 596 | | |
| | 0 | 597 | | if (match.Success) |
| | 0 | 598 | | { |
| | 0 | 599 | | methodName = match.Groups["NestedMethodName"].Value + "(" + match.Groups["Arguments"].Value + ")"; |
| | 0 | 600 | | } |
| | 0 | 601 | | } |
| | 381 | 602 | | else if (methodName.EndsWith("::MoveNext()")) |
| | 328 | 603 | | { |
| | 328 | 604 | | Match match = CompilerGeneratedMethodNameRegex.Match(methodName); |
| | | 605 | | |
| | 328 | 606 | | if (match.Success) |
| | 328 | 607 | | { |
| | 328 | 608 | | methodName = match.Groups["CompilerGeneratedName"].Value + "()"; |
| | 328 | 609 | | } |
| | 328 | 610 | | } |
| | | 611 | | |
| | 381 | 612 | | fullName = methodName; |
| | 381 | 613 | | MethodNameMap.TryAdd(methodName, fullName); |
| | 381 | 614 | | } |
| | | 615 | | |
| | 7410 | 616 | | return fullName; |
| | 7410 | 617 | | } |
| | | 618 | | |
| | | 619 | | private class FileElement |
| | | 620 | | { |
| | | 621 | | /// <summary> |
| | | 622 | | /// Initializes a new instance of the <see cref="FileElement" /> class. |
| | | 623 | | /// </summary> |
| | | 624 | | /// <param name="elements">The File elements.</param> |
| | 1272 | 625 | | public FileElement(IEnumerable<XElement> elements) |
| | 1272 | 626 | | { |
| | 2544 | 627 | | this.Uids = [.. elements.Select(f => f.Attribute("uid").Value)]; |
| | 1272 | 628 | | } |
| | | 629 | | |
| | | 630 | | /// <summary> |
| | | 631 | | /// Gets the uids. |
| | | 632 | | /// </summary> |
| | 25932 | 633 | | public HashSet<string> Uids { get; } |
| | | 634 | | } |
| | | 635 | | } |