tl;dr - SonarCloud CI on GitHub操作警告说,它无法找到任何已报告覆盖率的源文件,尽管确认这些文件位于所报告路径的停靠文件系统中。
我有一个带有rspec规范的Ruby / Rails应用程序,它使用SimpleCov及其JSON格式化程序生成覆盖率统计数据(因此,我的rails_helper.rb启动:
require 'simplecov'
require "simplecov_json_formatter"
SimpleCov.formatter = SimpleCov::Formatter::JSONFormatter
SimpleCov.start('rails') do
add_filter ['/channels/', '/jobs/', '/mailers/']
end我已经将SonarCloud CI设置为使用GitHub操作进行扫描,根目录中有以下声呐-project.properties:
sonar.projectKey=asilano_my-app
sonar.organization=asilano
sonar.ruby.coverage.reportPaths=coverage/coverage.json
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
sonar.sources=app,lib
sonar.tests=spec以及以下GitHub工作流:
name: Test and Deploy
on:
pull_request:
types: [opened, synchronize, reopened]
branches:
- 'main'
- 'staging'
push:
branches:
- 'main'
- 'staging'
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
bundler-cache: true
- name: Install PostgreSQL client
run: |
sudo apt-get -yqq install libpq-dev
- name: Build App
env:
PGHOST: localhost
PGUSER: postgres
PGPASSWORD: postgres
RAILS_ENV: test
RAILS_MASTER_KEY: ${{ secrets.TEST_MASTER_KEY }}
run: |
bin/rails db:setup
yarn install
- name: Run Tests
env:
PGHOST: localhost
PGUSER: postgres
PGPASSWORD: postgres
RAILS_ENV: test
RAILS_MASTER_KEY: ${{ secrets.TEST_MASTER_KEY }}
run: |
bundle exec rspec
- name: Where Am I?
run: |
head coverage/coverage.json
ls -l /home/runner/work/my-app/my-app/app/lib/some_file.rb
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.SONAR_GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}(在SonarCloud中,主分支和分期都是持久的分支)
Where Am I?步骤是尝试调试我遇到的问题。它显示coverage.json的顶部是:
{
"meta": {
"simplecov_version": "0.21.2"
},
"coverage": {
"/home/runner/work/my-app/my-app/app/lib/some_file.rb": {
"lines": [
1,
1,
1,并通过ls确认上述路径存在:
-rw-r--r-- 1 runner docker 1729 Oct 24 08:15 /home/runner/work/my-app/my-app/app/lib/some_file.rb但是,SonarCloud扫描步骤警告说,覆盖率文件提到了some_file.rb,但是在文件中找不到它:
INFO: Sensor SimpleCov Sensor for Ruby coverage [ruby]
WARN: File '/home/runner/work/my-app/my-app/app/lib/some_file.rb' is present in coverage report but cannot be found in filesystem然后,...and对应用程序中的每个文件进行重复。
为什么不行?为什么SonarCloud扫描仪不能在覆盖文件中报告的路径上找到some_file.rb,即使我已经确认了它应该在哪里呢?
发布于 2022-11-01 17:47:47
对于GitHub操作、rails和simplecov,我也有同样的问题。您需要替换simplecov在coverage/overage.json上生成的路径。要执行此操作,请在声纳云扫描之前执行此步骤。另外,您也可以查看这个帖子https://community.sonarsource.com/t/code-coverage-doesnt-work-with-github-action/16747
- name: Running rails tests
run: bundle exec rspec
- name: fix code coverage paths
working-directory: ./coverage
run: |
sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' coverage.json
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}https://stackoverflow.com/questions/74199483
复制相似问题