'`result_exit_status': undefined method `-' for nil:NilClass (NoMethodError)

I am trying to add the Code Climate Test Reporter to a New Rails app that we are currently building.

I have setup circleci on other projects so I modified the config to suit this project but I continue to get the following error when the rspec tests finish running.

69 file(s) with 100% coverage not shown
Traceback (most recent call last):
    3: from /home/circleci/my-app/vendor/bundle/ruby/2.7.0/gems/simplecov-0.17.1/lib/simplecov/defaults.rb:29:in `block in <main>'
    2: from /home/circleci/my-app/vendor/bundle/ruby/2.7.0/gems/simplecov-0.17.1/lib/simplecov.rb:205:in `run_exit_tasks!'
    1: from /home/circleci/my-app/vendor/bundle/ruby/2.7.0/gems/simplecov-0.17.1/lib/simplecov.rb:224:in `process_result'
/home/circleci/my-app/vendor/bundle/ruby/2.7.0/gems/simplecov-0.17.1/lib/simplecov.rb:243:in `result_exit_status': undefined method `-' for nil:NilClass (NoMethodError)

Exited with code exit status 1

CircleCI received exit code 1

Have anyone seen an error like this? If so, any suggestions for getting the Code Climate Test Reporter to work correctly with CircleCI?

config.yml

version: 2
 jobs:
  build:
    parallelism: 1  # however many CPUs you need/pay for

    #############################################
    # Container Setup
    #############################################
    docker:
      - image: circleci/ruby:2.7.2-node-browsers
        environment:
          RAILS_ENV: test
          CC_TEST_REPORTER_ID: #########################
          COVERAGE: true          
      - image: circleci/postgres:11 # database image
        environment:
          POSTGRES_USER: root
          POSTGRES_DB: circle_test
          POSTGRES_PASSWORD: circleci
      - image: docker.elastic.co/elasticsearch/elasticsearch:7.10.1
        environment:
          discovery.type: single-node
      - image: selenium/standalone-chrome-debug
        name: selenium
        ports: 
          - "4444:4444"                

    #############################################
    # Build Steps
    #############################################
    steps:
      - checkout
      - run:
          name: Configure secrets.yml
          command: mv config/secrets.ci.yml config/secrets.yml
      - run:
          name: Configure database.yml
          command: mv config/database.ci.yml config/database.yml
      - run:
          name: Configure application.yml
          command: mv config/application.ci.yml config/application.yml             

      ###########################################
      # Bundler w/ caching
      ###########################################
      - restore_cache:
          keys:
            - rails-bundle-{{ checksum "Gemfile.lock" }}
            - rails-bundle-
      - run:
          name: Configure Bundler
          command: |
            echo 'export BUNDLER_VERSION=$(cat Gemfile.lock | tail -1 | tr -d " ")' >> $BASH_ENV
            source $BASH_ENV
            gem install bundler              
      - run:
          name: Bundle Gems
          command: bundle check --path=vendor/bundle || bundle install --path=vendor/bundle
      - save_cache:
          key: rails-bundle-{{ checksum "Gemfile.lock" }}
          paths:
            - vendor/bundle

      ###########################################
      # Apt Update and install packages
      ###########################################
      - run:
          name: apt update
          command: sudo apt update
      - run:
          name: Install Packages
          command: sudo apt-get install -y graphicsmagick ghostscript ffmpeg  
      - run:
          name: Install Tesseract
          command: sudo apt-get install -y libleptonica-dev libtesseract-dev tesseract-ocr            
        
      ###########################################
      # Code Climate
      ###########################################
      - run:
          name: Install Code Climate Test Reporter
          command: |
            curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
            chmod +x ./cc-test-reporter            

      ###########################################
      # Database
      ###########################################
      - run:
          name: Wait for DB
          command: dockerize -wait tcp://localhost:5432 -timeout 1m
      - run:
          name: Load DB schema
          command: bin/rails db:schema:load --trace

      ###########################################
      # Run rspec in parallel
      ###########################################
      - run:
          name: Run rspec in parallel
          command: |
            mkdir -p test_results
            ./cc-test-reporter before-build
            bundle exec rspec --profile 10 \
                              --format RspecJunitFormatter \
                              --out test_results/rspec.xml \
                              --format progress \
                              $(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)
      - run:
          name: Code Climate Test Coverage
          command: |
            ./cc-test-reporter format-coverage -t simplecov -o "coverage/codeclimate.$CIRCLE_NODE_INDEX.json"    

      # Save test results for timing analysis
      - store_test_results:
          path: test_results
      - deploy:
          command: |
        ./cc-test-reporter sum-coverage --output - coverage/codeclimate.*.json | ./cc-test-reporter upload-coverage --debug --input -  

gemfile

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.2'

# Rails
# ================================================================
 gem 'pg', '>= 0.18', '< 2.0'
 gem 'puma', '~> 3.11'
 gem 'rails', '~> 6.1', '>= 6.1.3'
 gem 'webpacker', '~> 5.2', '>= 5.2.1'

 # Reduces boot times through caching; required in config/boot.rb
 gem 'bootsnap', '>= 1.1.0', require: false

 # Application Specific
 # =================================================================

 # background jobs
 gem 'sidekiq', '~> 6.1', '>= 6.1.3'

 # elasticsearch
 gem 'elasticsearch-model', '~> 6.0'
 gem 'elasticsearch-rails', '~> 6.0'

 # authentication
 gem 'devise', '~> 4.6', '>= 4.6.2'
 gem 'omniauth', '~> 1.9'
 gem 'omniauth-cas', '~> 1.1', '>= 1.1.1'

 # Log all changes to models
 gem "audited", "~> 4.9"

 # metadata / library of congress specific
 gem 'edtf', '~> 3.0', '>= 3.0.4'
 # gem 'edtf-humanize', '~> 0.0.7'

 # breadcrumbs
 gem 'loaf'

 # configruation
 gem 'figaro'

 # convert user strings to regex
 gem 'to_regexp', '~> 0.2.1'

 # files 
 gem 'mini_magick', '~> 4.11' # wrapper for imagemagick
 gem 'rtesseract'

 # used for batching jobs
 gem 'sidekiq-batch'

 # Development / Test Items (Primarily debugging)
 # =====================================================================================
 group :development, :test do
  gem 'byebug', platforms: %i[mri mingw x64_mingw] # from rails new
  gem 'factory_bot_rails'
  gem 'faker'
  gem 'pry'
  gem 'pry-rails'

  # Code critics
  gem 'rubocop'
  gem 'rubocop-performance'
  gem 'rubocop-rails'
end

group :test do
   # test suite
   gem 'capybara', '>= 2.15', '< 4.0'
   gem 'database_cleaner'
   gem 'rspec-html-matchers'
   gem 'rspec-rails'
   gem "rspec_junit_formatter"
   gem 'shoulda'
   gem 'shoulda-matchers'
   # Codeclimate is not compatible with 0.18+. See https://github.com/codeclimate/test-reporter/issues/413
   gem 'simplecov', '~> 0.17.1'
   gem 'simplecov-console'
 end

 group :development do
   gem 'annotate'
   gem 'listen', '>= 3.0.5', '< 3.2'
   gem 'spring'
   gem 'spring-watcher-listen', '~> 2.0.0'
   gem 'web-console', '>= 3.3.0'
   # performance helper
   gem 'bullet' # helps to eliminate N+1 Queries
 end


Solution 1:[1]

I had the same issue. I figured it out. Deleting my coverage folder resolved the problem for me. (It'll be created again the next time you run SimpleCov.)

Here's why that solved the problem:

I previously installed a newer version of SimpleCov in order to try out branch coverage (which CodeClimate does not yet support). So, when I switched back to version 1.17.1 to be CodeClimate compatible, the file coverage/.last_run.json contained JSON that did not match the expecations of SimpleCov 1.17.1. So, deleting the file (and the whole folder for good measure) allowed SimpleCov 1.17.1 to recreate it in a format that it understands.

Here's how I figured it out:

I ran bundle open simplecov which opened the gem source code in my editor. I then went to the line where the error occurred (simplecov.rb:243), and added a puts statement to see the value of the hash in the last_run variable. The results were missing the covered_percent and instead included branch coverage information, something version 1.17 does not support.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aridlehoover