我正在使用 SonarQube 6.3.1 并且正在运行 jenkins 来创建 SonarQube 仪表板。因此,我需要运行 jenkins 作业来创建 js 覆盖率报告并导入到 SonarQube 以创建 UT 覆盖率。
通过在 Jenkins 中设置 SonarQube Scanner,以下是属性:
sonar.projectKey=xxxxxxManagement
sonar.projectName=XXXXXX Management
sonar.projectVersion=1.0
sonar.language=js
sonar.sources=webapp
sonar.sourceEncoding=UTF-8
sonar.javascript.coveragePlugin=jacoco
sonar.javascript.lcov.reportPaths=target/jscoverage/jscoverage.xml
sonar.dynamicAnalysis=reuseReports
sonar.login=admin
sonar.password=admin
但是,SonarQube 仪表板显示 JS UT 覆盖率为 0
我想知道 sonar.javascript.lcov.reportPaths 设置是否错误,所以我尝试了很多方法:
sonar.javascript.lcov.reportPaths=./target/jscoverage/jscoverage.xml
我也试过:
sonar.javascript.lcov.reportPaths=/target/jscoverage/jscoverage.xml
但 UT 覆盖率始终为零
由于我可以看到正确创建的报告,我怀疑导入 SonarQube 是否失败,但 Jenkins 控制台中没有显示错误日志。
6/29 从我的调查中添加新的更新,来自 Jenkins 日志,
INFO: Analysing [/var/jenkins_home/sharedspace/target/jscoverage/jscoverage.xml]
INFO: Sensor JavaScript Squid Sensor [javascript] (done) | time=1611ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=36ms
INFO: Sensor Code Colorizer Sensor
INFO: Sensor Code Colorizer Sensor (done) | time=1ms
INFO: Sensor CPD Block Indexer
INFO: org.sonar.scanner.cpd.deprecated.DefaultCpdBlockIndexer@4644f3d4 is used for js
DEBUG: No CpdMapping for language js
INFO: Sensor CPD Block Indexer (done) | time=0ms
INFO: Calculating CPD for 16 files
DEBUG: Detection of duplications for /var/jenkins_home/sharedspace/webapp/Component.js
DEBUG: Detection of duplications for /var/jenkins_home/sharedspace/webapp/model/util.js
DEBUG: Detection of duplications for /var/jenkins_home/sharedspace/webapp/controller/IncidentDetail.controller.js
DEBUG: Detection of duplications for /var/jenkins_home/sharedspace/webapp/model/formatter.js
DEBUG: Detection of duplications for /var/jenkins_home/sharedspace/webapp/util/Validator.js
DEBUG: Detection of duplications for /var/jenkins_home/sharedspace/webapp/localService/mockserver.js
DEBUG: Detection of duplications for /var/jenkins_home/sharedspace/webapp/controller/Attachments.controller.js
DEBUG: Detection of duplications for /var/jenkins_home/sharedspace/webapp/model/models.js
DEBUG: Detection of duplications for /var/jenkins_home/sharedspace/webapp/controller/HelpRequestOverview.controller.js
DEBUG: Detection of duplications for /var/jenkins_home/sharedspace/webapp/controller/NotFound.controller.js
DEBUG: Detection of duplications for /var/jenkins_home/sharedspace/webapp/controller/ErrorHandler.js
DEBUG: Detection of duplications for /var/jenkins_home/sharedspace/webapp/controller/CreateIncident.controller.js
DEBUG: Detection of duplications for /var/jenkins_home/sharedspace/webapp/controller/BaseController.js
DEBUG: Detection of duplications for /var/jenkins_home/sharedspace/webapp/controller/HelpRequestDetail.controller.js
DEBUG: Detection of duplications for /var/jenkins_home/sharedspace/webapp/controller/HelpRequests.controller.js
DEBUG: Detection of duplications for /var/jenkins_home/sharedspace/webapp/controller/App.controller.js
INFO: CPD calculation finished
从日志中,我们可以看到没有错误消息,所以我在 Jenkins Job 的 Post Steps 中添加了 shell 脚本:
cd target/jscoverage
pwd
vi jscoverage.xml
和詹金斯日志显示:
<coverage files="11" line-rate="0.7357294" lines-covered="348" lines-valid="473"[3;2Htimestamp="1498722804050">
<modules>
<module name="/Component.js" filename="/Component.js" line-rate="0.84210527" lin[6;1Hes-covered="16" lines-valid="19">
<lines>
<line number="1" hits="1"/>
<line number="8" hits="1"/>
<line number="10" hits="1"/>
<line number="24" hits="1"/>
<line number="27" hits="1"/>
<line number="30" hits="1"/>
<line number="32" hits="1"/>
<line number="35" hits="1"/>
<line number="36" hits="1"/>
<line number="39" hits="1"/>
<line number="49" hits="1"/>
<line number="51" hits="1"/>
<line number="61" hits="1"/>
<line number="63" hits="1"/>
<line number="64" hits="1"/>
...
从 2 条日志我们可以看到报告看起来不错,SonarQube 成功读取报告,我们可以在 xml 报告中逐个文件看到 UT 覆盖率,但 UT 覆盖率仍然为零。
7/3 我添加了依赖项
<dependency>
<groupId>fr.sii.sonar</groupId>
<artifactId>sonar-coverage-lcov</artifactId>
<version>2.1.0</version>
</dependency>
并改变了
<sonar.javascript.jscoverage.reportPath>${project.build.directory}/jscoverage/jscoverage.xml</sonar.javascript.jscoverage.reportPath>
从 jscoverage.xml 到 jscoverage。lcov 但仍然无法正常工作