来自 VSTS 的 SonarQube 代码覆盖率

SonarQube Code Coverage from VSTS

我正在使用 VSTS 托管代理 VS2017 运行 构建、测试和 SonarQube 分析。我正在使用来自市场 (https://marketplace.visualstudio.com/items?itemName=SonarSource.sonarqube) 的 SonarQube 扩展。使用的任务版本是 4,它使用 SonarQube Scanner for MSBuild 4.0.2。

分析效果很好,但它没有将代码覆盖率结果转换为 SonarQube。我可以在 VSTS(构建详细信息)中看到代码覆盖率分析,但在 SonarQube 中看不到。

据我了解,问题在于 VSTS 生成了一个扩展名为 .codecoverage 的二进制文件。 SonarQube 不知道如何处理这个文件。我无法将其转换为 XML 格式,因为我似乎没有办法做到这一点。

是否有人成功地将 VSTS 与 SonarQube 一起使用并将代码覆盖率结果也发送到 SonarQube?你是怎么设置的?

编辑 1
我使用的 SonarQube 版本是:版本 6.7(内部版本 33306),社区版

编辑 2
分析结束实际上生成了 xml 文件,如下面的评论所述。这是来自日志:

2018-03-06T11:14:56.4189055Z 11:14:56.417  Attempting to locate the 
CodeCoverage.exe tool...
2018-03-06T11:14:56.4210147Z 11:14:56.42  Attempting to locate the 
CodeCoverage.exe tool using setup configuration...
2018-03-06T11:14:56.4345085Z 11:14:56.433  Code coverage command line tool: 
C:\Program Files (x86)\Microsoft Visual Studio17\Enterprise\Team 
Tools\Dynamic Code Coverage Tools\CodeCoverage.exe
2018-03-06T11:14:56.4349101Z 11:14:56.434  Fetching code coverage report 
information from TFS...
2018-03-06T11:14:56.4363869Z 11:14:56.435  Attempting to locate a test 
results (.trx) file...
2018-03-06T11:14:57.0458245Z 11:14:57.044  Looking for TRX files in: 
D:\a\TestResults, D:\a\s\TestResults
2018-03-06T11:14:57.0462747Z 11:14:57.045  Located a test results file: 
D:\a\s\TestResults\VssAdministrator_factoryvm-az436_2018-03-
06_11_13_09.trx
2018-03-06T11:14:57.0600587Z 11:14:57.059  One code coverage attachment was 
found in the trx file: factoryvm-az436\VssAdministrator_factoryvm-az436 
2018-03-06 11_11_34.coverage
2018-03-06T11:14:57.0602504Z 11:14:57.059  Absolute path to coverage file: 
D:\a\s\TestResults\VssAdministrator_factoryvm-az436_2018-03-
06_11_13_09\In\factoryvm-az436\VssAdministrator_factoryvm-az436 2018-03-06 
11_11_34.coverage
2018-03-06T11:14:57.0691948Z 11:14:57.068  Executing file C:\Program Files 
(x86)\Microsoft Visual Studio17\Enterprise\Team Tools\Dynamic Code 
Coverage Tools\CodeCoverage.exe
2018-03-06T11:14:57.0692731Z   Args: analyze 
/output:D:\a\s\TestResults\VssAdministrator_factoryvm-az436_2018-03-
06_11_13_09\In\factoryvm-az436\VssAdministrator_factoryvm-az436 2018-03-06 
11_11_34.coveragexml D:\a\s\TestResults\VssAdministrator_factoryvm-
az436_2018-03-06_11_13_09\In\factoryvm-az436\VssAdministrator_factoryvm-
az436 2018-03-06 11_11_34.coverage 
2018-03-06T11:14:57.0694963Z   Working directory: 
D:\a\s\TestResults\VssAdministrator_factoryvm-az436_2018-03-
06_11_13_09\In\factoryvm-az436
2018-03-06T11:14:57.0695792Z   Timeout (ms):60000
2018-03-06T11:14:57.0699007Z   Process id: 3540
2018-03-06T11:14:58.7847582Z 11:14:58.783  Process returned exit code 0
2018-03-06T11:14:58.7858908Z 11:14:58.784  Updating project info files with 
code coverage information...

VSTS 扩展应该会自动导入覆盖率结果,但目前有一个回归问题将(希望)很快得到修复:SONARMSBRU-339

同时,VSTS extension documentation in "Analysing a .NET solution" 中解释了一个解决方法:在 Additional Properties 文本区域中,添加以下内容 属性:

sonar.cs.vscoveragexml.reportsPaths=**/*.coveragexml

我无法看到 'coveragexml' 文件,即使我设置了 'sonar.verbose=true'、'system.debug=true'。虽然,我发现了另一种工作正常的方法,即使用 PowerShell 脚本将“.coverage”文件转换为“.coveragexml”:

Get-ChildItem -Path $(Agent.TempDirectory) -Include "*.coverage" -File -Recurse | % {
$outfile = "$([System.IO.Path]::GetFileNameWithoutExtension($_.FullName)).coveragexml"
$output = [System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($_.FullName), $outfile)
"Analyse '$($_.Name)' with output '$outfile'..."
.$env:USERPROFILE\.nuget\packages\microsoft.codecoverage.8.0\build\netstandard1.0\CodeCoverage\CodeCoverage.exe analyze /output:$output $_.FullName
}

要使其正常运行,请确保您已为 Marketplace 安装 'Inline PowerShell' 任务并将其放置在构建管道中的 'Run Code Analysis' 步骤之前。

对于使用自托管代理的用户,您还可以安装Visual Studio Test Agent 2019

它包含 SonarQubeAnalyze@4 Azure DevOps 任务将找到的 CodeCoverage.exe 文件。根据此 SonarQube documentation webpage.

,SonarQube 将能够将 .coverage 文件转换为 .coveragexml

Visual Studio 测试代理似乎是免费的。无需为 Visual Studio 企业付费。