SteveGilham / altcover

Cross-platform coverage gathering and processing tool set for dotnet/.Net Framework and Mono
MIT License
494 stars 17 forks source link

Exclude compiler-generated members and classes #189

Closed macco3k closed 11 months ago

macco3k commented 1 year ago

Hi all,

I was trying to configure altcover to provide coverage for my app. While I managed to exclude assemblies without source files, I did not find any way to get rid of weird automatically-generated types such as <>f__AnonymousType0<T1, T2>. Is that something actually possible with altcover?

image

SteveGilham commented 1 year ago

Reports in OpenCover format are exactly that : they follow OpenCover's precedent (drawn originally from NCover 1.x) of reporting everything from an assembly, and mostly leaving it to any downstream reporting tool to decide what to display. As an example, the well known reportgenerator tool elides such types automatically.

Using a type filter like --typeFilter=f__AnonymousType will reduce the class to a stub, again as per OpenCover. As a worked example, this takes an anonymous type from

        <Class>
          <Summary numSequencePoints="0" visitedSequencePoints="0" numBranchPoints="0" visitedBranchPoints="0" sequenceCoverage="0" branchCoverage="0" maxCyclomaticComplexity="0" minCyclomaticComplexity="0" visitedClasses="0" numClasses="0" visitedMethods="0" numMethods="0" minCrapScore="0" maxCrapScore="0" />
          <FullName>&lt;&gt;f__AnonymousType0`1</FullName>
          <Methods>
... eliding constructor, accessor, equality, to-string and hash code methods...
          </Methods>
        </Class>

to

        <Class skippedDueTo="Filter">
          <Summary numSequencePoints="0" visitedSequencePoints="0" numBranchPoints="0" visitedBranchPoints="0" sequenceCoverage="0" branchCoverage="0" maxCyclomaticComplexity="0" minCyclomaticComplexity="0" visitedClasses="0" numClasses="0" visitedMethods="0" numMethods="0" minCrapScore="0" maxCrapScore="0" />
          <FullName>&lt;&gt;f__AnonymousType0`1</FullName>
          <Methods />
        </Class>

which may be sufficient further hint for other reporting tools to ignore the type, or would at least simplify an intermediate XML processing stage to remove all <Class skippedDueTo="Filter" /> nodes.

The problem can be sidestepped entirely by use of coverlet format JSON reporting - that option takes the opposite extreme approach and collapses all coverable code into the methods in the user level source where it appears.

SteveGilham commented 1 year ago

Having observed that anonymous types (C#) and records (F#) are compiled without any debug information for their internals, release 8.6.68 now omits them in the same way as automatic properties, and for the same reasons - that there is no "there" there for coverage purposes, and so no point in cluttering up the default report with them.

In the broader context, for other constructs that are [CompilerGenerated], using --attributeFilter is still the recommended first approach for removing them from the report (with the same skippedDueTo="Filter" proviso as above).

macco3k commented 1 year ago

Ah that's great news. I'll be testing it soon enough. In the meantime, thanks a lot for the quick reply and help!