SonarCloud CI无法找到Ruby / SimpleCov覆盖范围的源文件

zzoitvuj  于 2022-11-04  发布在  Ruby
关注(0)|答案(1)|浏览(141)

tl;dr -GitHub操作上的SonarCloud CI警告说,尽管确认了文件在Docker文件系统中报告的路径下,但它找不到任何报告了覆盖率的源文件。
我有一个带有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操作进行扫描,根目录中包含以下sonar-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 }}

(main和分段都是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

...然后对应用程序中的每个文件重复此操作。
为什么不行?为什么SonarCloud扫描程序无法在覆盖率文件中报告的路径上找到some_file.rb,即使我已经确认它应该在那里?

ljsrvy3e

ljsrvy3e1#

我在GitHub操作、rails和simpecov上也遇到了同样的问题。你需要替换coverage/coverage. json上simpecov生成的路径。要做到这一点,在你的sonarcloud扫描之前运行这一步。另外,你可以检查这篇文章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 }}

相关问题