microsoft / codecoverage

MIT License
73 stars 12 forks source link

Cannot exclude auto-generated code from code coverage with .runsettings file #123

Open cliffAtOsaic opened 1 month ago

cliffAtOsaic commented 1 month ago

Description

Cannot exclude auto-generated code from code coverage with .runsettings file.

We are importing ElasticSearch which generates code under the path "/_/src/Elastic.Clients.Elasticsearch/_Generated" which is then deleted after the build is complete. (I cannot find this code when I search; we have no src and no _Generated directories.)

In our .runsettings file, I have tried multiple ways to exclude the ElasticSearch DLLs and the ElasticSearch generated code with negative results.

<ModulePaths>
    <Include>
      <!-- Include all loaded .dll assemblies (but not .exe assemblies): -->
      <ModulePath>.*\.dll$</ModulePath>
    </Include>
    <Exclude>
      <!-- But exclude some assemblies: -->
      <ModulePath>.*\Elastic.Clients.Elasticsearch.dll$</ModulePath>
      <ModulePath>.*\Elastic.Transport.dll$</ModulePath>
      <!-- Exclude all file paths that contain "Temp": -->
      <ModulePath>.*Temp.*</ModulePath>
      <ModulePath>.*src.*</ModulePath>
      <ModulePath>.*_Generated.*</ModulePath>
    </Exclude>
  </ModulePaths>

  <Functions>
    <Exclude>
      <Function>^Elastic.Clients.Elasticsearch.*</Function>
    </Exclude>
  </Functions>

  <Sources>
    <Exclude>
      <Source>.*src.*</Source>
      <Source>.*_Generated.*</Source>
    </Exclude>
  </Sources>

Steps to reproduce

What steps can reproduce the defect?

  1. Import ElasticSearch
  2. Create a .runsettings file with the code snippit above.
  3. Run the following command: dotnet-coverage collect -f cobertura,coverage "dotnet test --logger trx" -s .\.runsettings

Expected behavior

Auto-generated source code under the "src" and/or "_Generated" directories are excluded from code coverage.

Actual behavior

All auto-generated source code under the "src" and/or "_Generated" directories is included with code covaerage.

Diagnostic logs

log.txt

Environment

Windows 11 Version 23H2 (OS Build 22631.3447) dotnet --version = 8.0.204

nohwnd commented 1 month ago

@fhnaseer plese help here if you can :)

fhnaseer commented 1 month ago

@cliffAtOsaic <Include>/<Exclude> are regex patterns. Can you please check if your source path/module path match patterns provided in runsettings? If it still persists, then can you please provide a small repro solution?

cliffAtOsaic commented 1 month ago

@cliffAtOsaic <Include>/<Exclude> are regex patterns. Can you please check if your source path/module path match patterns provided in runsettings? If it still persists, then can you please provide a small repro solution?

Just seeing this; not sure why I didn't get an email. I'll take a look at this tomorrow and post what I learn. Thanks!