SonarCloud CI 找不到 Ruby / SimpleCov 覆盖的源文件

问题描述 投票:0回答:2

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 Actions 进行扫描,根目录中包含以下 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 和 staging 都是 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
,即使我已经确认它位于应有的位置?

ruby sonarqube code-coverage
2个回答
3
投票

我对 GitHub actions、rails 和 simplecov 也有同样的问题。您需要在coverage/coverage.json 上替换simplecov 生成的路径。为此,请在 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 }}

0
投票

对于阅读此问题的任何人,由于迁移到 SonarSource/sonarqube-scan-action,因此不需要替换路径(这实际上会导致相同的错误)

© www.soinside.com 2019 - 2024. All rights reserved.