How to make Sonarqube exclude a .NET (C#) project from coverage measures

前端 未结 6 1153
Happy的楠姐
Happy的楠姐 2021-02-05 14:07

Sonarqube allows for individual files to be excluded from code coverage by adding patterns in the sonar.coverage.exclusions key. This can be done on a project level by

相关标签:
6条回答
  • 2021-02-05 14:53

    This is an old question, but a more accurate answer is posted here: https://stackoverflow.com/a/49904805/987191

    Essentially, the code tab allows drilling down to settings for a project and we can use ** in administration (for the project) -> General Settings -> Analysis Scope -> Coverage exclusions

    0 讨论(0)
  • 2021-02-05 14:59

    I had the same problem, and it took me a while to figure it out:

    Set up a Directory.Build.props file in the solution directory with this content:

    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
    
      <!-- This target customises the SonarQube MSBuild runner targets to limit the projects that are analysed.
    
           Projects whose full path and file name match the specified filter will be marked as "excluded".
    
           Note that this targets file does not ever set $(SonarQubeExclude) to "false". This is to allow other
           targets to exclude projects for other reasons (e.g. a Fakes projects should always be excluded, regardless
           of whether or not they match the project filter).
    
           Also, this project will do nothing if $(SonarQubeExclude) has already been set.
    
           The regular expression uses the normal .NET regular expression syntax.
    
           Usage:
           (1) include the target in the projects being built, either by directly importing it or by
               dropping it in the one of the standard "ImportBefore" folders.
    
           (2) set $(SQExclusionFilter) to the desired regular expression
               e.g. the following example matches all projects with "CodeSense\Services" in the path:  .*\\CodeSense\\Services\\.*
    
      -->
      <PropertyGroup Condition=" $(SonarQubeExclude) == '' AND $(SQExclusionFilter) != '' ">
    
          <MatchesSQExclusionFilter Condition="$([System.Text.RegularExpressions.Regex]::IsMatch($(MSBuildProjectFullPath), $(SQExclusionFilter), System.Text.RegularExpressions.RegexOptions.IgnoreCase)) ">true</MatchesSQExclusionFilter>
          <SonarQubeExclude Condition="$(MatchesSQExclusionFilter) == 'true' " >true</SonarQubeExclude>
    
      </PropertyGroup>
    
      <!-- This target is not required: it simply writes out additional information to simplify debugging -->
      <Target Name="AnalysisProjectInfoExclude_DEBUG" BeforeTargets="CoreCompile"
              Condition="$(SQExclusionFilter) != '' ">
    
        <Message Importance="high" Text="ExclusionFilter: filter has been set.  Filter= $(SQExclusionFilter)" />
        <Message Importance="high" Text="ExclusionFilter: current project = $(MSBuildProjectFullPath)" />
        <Message Importance="high" Text="ExclusionFilter: match result = $(MatchesSQExclusionFilter)" />
    
        <Message Importance="high" Condition="$(MatchesSQExclusionFilter) == 'true' "
           Text="ExclusionFilter: project is excluded" />
    
        <Message Importance="high" Condition="$(MatchesSQExclusionFilter) != 'true' "
           Text="ExclusionFilter: project has not been excluded by the exclusion filter" />
    
      </Target>
    
    </Project>
    

    This fragment will be included in all project files as explained here https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build

    Set the regex in the environment variable SQExclusionFilter accordingly. Use double-backslashes for path separation. In windows shell escape pipe characters with a caret: e.g.

    set SQExclusionFilter=(path1\\project)^|(path2\\project)
    

    Credits to Duncan Pocklington from SQ/MS!

    • https://jira.sonarsource.com/browse/SONARMSBRU-191?focusedCommentId=155587&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-155587
    0 讨论(0)
  • 2021-02-05 15:03

    Not enough rep to comment, but if you apply both answers here, you can simply add:

    <PropertyGroup>
        <!-- Exclude the project from analysis -->
        <SonarQubeExclude>true</SonarQubeExclude>
    </PropertyGroup>
    

    in the .csproj file to exclude the entire project instead of individual files, or relying on sonarqube filters.

    0 讨论(0)
  • 2021-02-05 15:06

    Some notion :

    A .NET solution is Sonarqube's project. A .NET solutions's project is Sonarqube's project's module.

    I find two solutions to exlcure Sonarqube's module (.NET project) from code coverage.

    1) Configure exclusion from Web UI Sonarqube

    From : SonarQube MSBuild Scanner doesn't exclude files from analysis

    • Open Sonarqube's project -> Code (in top menu)
    • Open Sonarqube's module (left icon 'Open Component's Page) -> Administration (in top menu) -> General Settings
    • Add ** in Codecoverage exclusion :

    (Screenshot in French, but you get the idea)

    2) Add the property "sonar.coverage.exclusions" in csproj

    In your <.NET project>.csproj, add :

    <Project ...>
      ...
      <Target Name="BeforeBuild">
        <ItemGroup>
          <SonarQubeSetting Include="sonar.coverage.exclusions">
            <Value>**</Value>
          </SonarQubeSetting>
        </ItemGroup>
      </Target>
      ...
    </Project>
    

    This two solutions work for me, but I would prefer a global setting for Sonarqube's project (.NET Solution).

    0 讨论(0)
  • 2021-02-05 15:07

    After trying a thousand things I finally found the solution. It's necessary to add an item group in the .csproj.

    <ItemGroup>
        <Compile Update="ClassToExclude.cs">
          <!-- Exclude the file from analysis -->
          <SonarQubeExclude>true</SonarQubeExclude>
        </Compile>
      </ItemGroup>
    
    0 讨论(0)
  • 2021-02-05 15:08

    Summing up the above mentioned answers and also adding one point to it.

    1. To exclude a project from SonarQube Analysis from csproj we can achieve by adding the below code in .csproj of that project

      <PropertyGroup>
      <!-- Exclude the project from analysis -->
      <SonarQubeExclude>true</SonarQubeExclude>
      </PropertyGroup>
      
    2. To exclude a file from a project

       <ItemGroup>
       <SonarQubeSetting Include="sonar.coverage.exclusions">
       <Value>**/FileName.cs</Value>
       </SonarQubeSetting>
       </ItemGroup>
      
    3. And for multiple files

      <ItemGroup>
      <SonarQubeSetting Include="sonar.coverage.exclusions">
      <Value>**/FileName1.cs, **/FileName2.cs</Value>
      </SonarQubeSetting>
      </ItemGroup>
      

    Also can refer this for regex patterns used

    0 讨论(0)
提交回复
热议问题