傳送門
Demo源碼:https://gitee.com/hcsaaron/cssonar-demo
《iOS-SonarQue——展示OCLint分析結(jié)果》
當(dāng)前文章:《iOS-SonarQue——使用slather生成覆蓋率報告》
系統(tǒng)環(huán)境
Mac OS 10.15.6
前言
《iOS-SonarQue——展示OCLint分析結(jié)果》中已經(jīng)成功在SonarQube
后臺展示出OCLint的分析報告了,但是覆蓋率依然沒任何體現(xiàn)。
接下來我們就要用slather
生成覆蓋率報告。
關(guān)于slather
請看源碼介紹:https://github.com/SlatherOrg/slather
安裝slather(用于生成覆蓋率報告)
sudo gem install slather
如果安裝報錯,需要更新ruby,請移步《Mac更新ruby版本》
用上面命令安裝完后喷兼,會出現(xiàn)-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插件默認(rèn)指定路徑為`sonar-reports/coverage*.xml`)
mv sonar-reports/cobertura.xml sonar-reports/coverage.xml
由于slather coverage
只能指定輸出文件夾蕴轨,默認(rèn)文件名是cobertura.xml
,而SonarQube的sonar-swift
插件的覆蓋率報告路徑指定默認(rèn)為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插件默認(rèn)指定路徑為`sonar-reports/coverage*.xml`)
mv sonar-reports/cobertura.xml sonar-reports/coverage.xml
# Sonar掃描
/bin/sh sonar-scanner -X
在工程目錄中再次執(zhí)行以下命令:
sh run-sonar.sh
執(zhí)行完成后棘脐,來看SonarQube后臺,可以看到首頁中的項目除了有15個異味龙致,還呈現(xiàn)出覆蓋率蛀缝。進入具體項目,點擊覆蓋率目代,可以查看到具體覆蓋情況屈梁。
參考資料
slather源碼:https://github.com/SlatherOrg/slather