iOS-SonarQue——使用slather生成覆盖率报告

传送门

Demo源码:https://gitee.com/hcsaaron/cssonar-demo

《iOS-SonarQue——安装及使用》

《iOS-SonarQue——展示OCLint分析结果》

当前文章:《iOS-SonarQue——使用slather生成覆盖率报告》

系统环境

Mac OS 10.15.6

前言

《iOS-SonarQue——展示OCLint分析结果》中已经成功在SonarQube后台展示出OCLint的分析报告了,但是覆盖率依然没任何体现。

接下来我们就要用slather生成覆盖率报告。

关于slather

请看源码介绍:https://github.com/SlatherOrg/slather

安装slather(用于生成覆盖率报告)

sudo gem install slather

如果安装报错,需要更新ruby,请移步《Mac更新ruby版本》

用上面命令安装完后,会出现-bash: slather: command not found,搞了很久都不知道为什么。最后用下面方式安装完后就可以使用slather命令了:

sudo gem install -n /usr/local/bin slather

更新脚本

编辑run-sonar.sh脚本,在/bin/sh sonar-scanner -X前面增加单测编译、生成覆盖率报告的shell代码

# 运行单元测试
xcodebuild -workspace ${PROJECT_NAME} -scheme ${TEST_TARGET_NAME} -configuration Debug -enableCodeCoverage YES -destination 'platform=iOS Simulator,name=iPhone 8' test
# 生成单测覆盖率报告
slather coverage -x --output-directory sonar-reports --scheme ${TARGET_NAME} --workspace ${PROJECT_NAME} CSSonarDemo.xcodeproj

# 重命名覆盖率报告(sonar-swift插件默认指定路径为`sonar-reports/coverage*.xml`)
mv sonar-reports/cobertura.xml sonar-reports/coverage.xml

由于slather coverage只能指定输出文件夹,默认文件名是cobertura.xml,而SonarQube的sonar-swift插件的覆盖率报告路径指定默认为sonar-reports/coverage*.xml,因此将其进行重命。
如果不想进行重命名操作,也可以修改SonarQube后台指定的路径及文件命名。

最终完整脚本为:

PROJECT_NAME="CSSonarDemo.xcworkspace"
TARGET_NAME="CSSonarDemo"

TEST_TARGET_NAME="CSSonarDemoTests"
UITEST_TARGET_NAME="CSSonarDemoUITests"

rm -rf sonar-reports

mkdir sonar-reports

rm -rf ./build/derivedData

xcodebuild clean -UseModernBuildSystem=NO

xcodebuild -workspace ${PROJECT_NAME} -scheme ${TARGET_NAME} -UseModernBuildSystem=NO -derivedDataPath ./build/derivedData -configuration Debug COMPILER_INDEX_STORE_ENABLE=NO | xcpretty -r json-compilation-database -o compile_commands.json

# 生成OCLint分析报告
oclint-json-compilation-database -e Pods -- -report-type html -o oclintReport.html
oclint-json-compilation-database -e Pods -- -report-type pmd -o sonar-reports/oclint.xml \
-max-priority-1=9999 \
-max-priority-2=9999 \
-max-priority-3=9999 \

# 运行单元测试
xcodebuild -workspace ${PROJECT_NAME} -scheme ${TEST_TARGET_NAME} -configuration Debug -enableCodeCoverage YES -destination 'platform=iOS Simulator,name=iPhone 8' test
# 生成单测覆盖率报告
slather coverage -x --output-directory sonar-reports --scheme ${TARGET_NAME} --workspace ${PROJECT_NAME} CSSonarDemo.xcodeproj

# 重命名覆盖率报告(sonar-swift插件默认指定路径为`sonar-reports/coverage*.xml`)
mv sonar-reports/cobertura.xml sonar-reports/coverage.xml

# Sonar扫描
/bin/sh sonar-scanner -X

在工程目录中再次执行以下命令:

sh run-sonar.sh

执行完成后,来看SonarQube后台,可以看到首页中的项目除了有15个异味,还呈现出覆盖率。进入具体项目,点击覆盖率,可以查看到具体覆盖情况。

SonarQube首页
覆盖率情况

参考资料

slather源码:https://github.com/SlatherOrg/slather

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 夜莺2517阅读 128,214评论 1 9
  • 版本:ios 1.2.1 亮点: 1.app角标可以实时更新天气温度或选择空气质量,建议处女座就不要选了,不然老想...
    我就是沉沉阅读 7,510评论 1 6
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,889评论 28 54
  • 兔子虽然是枚小硕 但学校的硕士四人寝不够 就被分到了博士楼里 两人一间 在学校的最西边 靠山 兔子的室友身体不好 ...
    待业的兔子阅读 2,786评论 2 9

友情链接更多精彩内容