繁体   English   中英

如何使 Sonarqube 从覆盖率测量中排除 .NET (C#) 项目

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

Sonarqube 允许通过在sonar.coverage.exclusions键中添加模式来将单个文件排除在代码覆盖范围之外。 这可以通过在 UI 中甚至在 .csproj 文件中通过指定SonarQubeSetting元素来在项目级别上完成。 IE

<SonarQubeSetting Include="sonar.coverage.exclusions"> <Value>**/*.cs</Value> </SonarQubeSetting>

但是,这两种方法似乎都不起作用。 使用 SonarQube文档中指定的模式无法提供所需的结果。 我也知道SonarQubeExclude MSBuild 属性的存在,但我不想走这条路,因为它会将我的项目排除在所有其他分析之外。

还有另一种可能是我失踪了吗? 或者根本不可能从代码覆盖率中排除项目中的所有类?

总结上面提到的答案,并补充一点。

  1. 要从 csproj 的 SonarQube 分析中排除项目,我们可以通过在该项目的 .csproj 中添加以下代码来实现

    <PropertyGroup> <!-- Exclude the project from analysis --> <SonarQubeExclude>true</SonarQubeExclude> </PropertyGroup>
  2. 从项目中排除文件

     <ItemGroup> <SonarQubeSetting Include="sonar.coverage.exclusions"> <Value>**/FileName.cs</Value> </SonarQubeSetting> </ItemGroup>
  3. 对于多个文件

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

也可以参考这里使用的正则表达式模式

没有足够的代表发表评论,但如果你在这里应用这两个答案,你可以简单地添加:

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

在 .csproj 文件中排除整个项目而不是单个文件,或依赖 sonarqube 过滤器。

一些概念:

.NET 解决方案是 SonarQube 的项目。 .NET 解决方案的项目是 SonarQube 的项目模块。

特定的 SonarQube MsBuild 分析器将找到所有 csproj/vbproj。 对于每个 .NET 项目,分析器应用 SonarQube 的项目设置。

如果您的存储库是:

MyApp.sln
MyApp.Shell\
    MyApp.Shell.csproj
    Windows\MainWindows.cs
MyApp.Core\
    MyApp.Core.csproj
    FooService.cs

要忽略 MyApp.Shell 项目上的代码覆盖率,您将直观地添加设置sonar.coverage.exclusions=MyApp.Shell\\* 但是MsBuild分析器单独分析每个.NET项目,根目录直接在csproj/vbproj目录下。 当分析 MyApp.Shell.csproj 时,忽略模式MyApp.Shell\\*适用于Windows\\MainWindows.cs

要从代码覆盖率中排除特定项目,我们需要在 SonarQube 的模块(.NET 项目)级别应用此设置。 我找到了两种解决方案来做到这一点。

1)在csproj中添加属性“sonar.coverage.exclusions”

在您的 <.NET 项目>.csproj 中,添加:

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

2) 从 Web UI Sonarqube 配置排除

从更新到 SonarQube 8.*,我无法在 IHM 中检索该选项。

来自: SonarQube MSBuild Scanner 不会从分析中排除文件

  • 打开 Sonarqube 的项目 -> 代码(在顶部菜单中)
  • 打开 Sonarqube 的模块(左侧图标“打开组件页面”)-> 管理(在顶部菜单中)-> 常规设置
  • 在代码覆盖排除中添加**:

(法语截图,但你懂的) 声纳网络用户界面

这两个解决方案对我有用,但我更喜欢 Sonarqube 项目(.NET 解决方案)的全局设置。

在尝试了一千件事之后,我终于找到了解决方案。 有必要在.csproj 中添加一个项目组。

<ItemGroup>
    <Compile Update="ClassToExclude.cs">
      <!-- Exclude the file from analysis -->
      <SonarQubeExclude>true</SonarQubeExclude>
    </Compile>
  </ItemGroup>

我遇到了同样的问题,我花了一段时间才弄清楚:

使用以下内容在解决方案目录中设置Directory.Build.props文件:

<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>

此片段将包含在所有项目文件中,如此处所述https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build

相应地在环境变量SQExclusionFilter 中设置正则表达式。 使用双反斜杠进行路径分隔。 在 windows shell 中,带有插入符号的转义管道字符:例如

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

感谢来自 SQ/MS 的 Duncan Pocklington!

这是一个老问题,但这里发布了更准确的答案: https : //stackoverflow.com/a/49904805/987191

本质上,代码选项卡允许深入到项目的设置,我们可以在管理中使用 **(对于项目)-> 常规设置 -> 分析范围 -> 覆盖排除

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM