来自VSTS的SonarQube代码覆盖率

5
我正在使用VSTS托管代理VS2017运行构建、测试和SonarQube分析。我正在使用市场上的SonarQube扩展(https://marketplace.visualstudio.com/items?itemName=SonarSource.sonarqube)。使用的任务版本为4,使用的是SonarQube Scanner for MSBuild 4.0.2。
分析工作正常,但它不会将代码覆盖率结果转换为SonarQube。我可以在VSTS(构建详细信息)中看到代码覆盖率分析,但在SonarQube中却看不到。
据我所知,问题在于VSTS生成了一个扩展名为.codecoverage的二进制文件。SonarQube不知道如何处理这个文件。我很难将其转换为XML格式,因为似乎没有办法做到这一点。
有人成功地使用VSTS和SonarQube,并将代码覆盖率结果传递到SonarQube吗?你是如何设置的?
编辑1 我使用的SonarQube版本是: Version 6.7 (build 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 Studio\2017\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\1\TestResults, D:\a\1\s\TestResults
2018-03-06T11:14:57.0462747Z 11:14:57.045  Located a test results file: 
D:\a\1\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\1\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 Studio\2017\Enterprise\Team Tools\Dynamic Code 
Coverage Tools\CodeCoverage.exe
2018-03-06T11:14:57.0692731Z   Args: analyze 
/output:D:\a\1\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\1\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\1\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...

@jessehouwing,这是6.7版本(构建33306),社区版。我会将其添加到初始问题中。 - Antti Simonen
将 /d:sonar.verbose=true /d:sonar.cs.vscoveragexml.reportsPaths=**/*.coveragexml 添加到“准备 SonarQube 分析”任务的附加设置中,并将 System.debug 变量设置为 true,然后排队构建并在 OneDrive 上共享日志。 - starian chen-MSFT
.coverage文件将在SonarQube结束分析任务期间转换为coveragexml,因此您可以共享SonarQube结束分析任务的详细日志。 - starian chen-MSFT
我已将日志添加到帖子中,看起来xml文件正在正确生成。无论如何它都没有被传递给SonarQube。 - Antti Simonen
3个回答

7

VSTS扩展应自动导入覆盖率结果,但目前存在一个可能会很快被修复的回归问题:SONARMSBRU-339

在此期间,在VSTS扩展文档中 "分析.NET解决方案"中有一个解决方法:在附加属性文本区域中添加以下属性:

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

我已经阅读了关于这个问题和解决方法的内容。问题是,没有.coveragexml文件可用,只有一个二进制.coverage文件。因此,为了使这个解决方法真正起作用,我需要将二进制文件转换为xml格式。 - Antti Simonen
你能检查一下运行你的构建的代理是否有 CodeCoverage 工具吗?请参考我的日志:https://gist.github.com/bellingard/993979606a7a536e85cf2cbefbb9525a - Fabrice - SonarSource Team
我已经添加了来自VSTS分析部分的日志(并非全部)。看起来覆盖率XML文件正在正确生成,但似乎最终仍未传输到SonarQube。 - Antti Simonen
我一开始可能没有正确地添加reportsPaths属性。我重新追踪了我的步骤,现在它可以工作了。谢谢你的帮助! - Antti Simonen

3

尽管我设置了“sonar.verbose=true”和“system.debug=true”,但我无法看到“coveragexml”文件。不过,我发现另一种方法可以很好地解决问题,即使用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\15.8.0\build\netstandard1.0\CodeCoverage\CodeCoverage.exe analyze /output:$output $_.FullName
}

为了使其正常工作,请确保在您的构建流水线中安装了“内联 PowerShell”任务并将其放置在“运行代码分析”步骤之前。

1
对于使用自托管代理的用户,您还可以安装Visual Studio Test Agent 2019。其中包含一个CodeCoverage.exe文件,SonarQubeAnalyze@4 Azure DevOps任务将找到该文件。根据SonarQube文档网页,SonarQube将能够将.coverage文件转换为.coveragexml。
Visual Studio Test Agent似乎是免费的。不需要支付Visual Studio Enterprise的费用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接