simplecov-ruby / simplecov

Code coverage for Ruby with a powerful configuration library and automatic merging of coverage across test suites
MIT License
4.76k stars 551 forks source link

The simplecov/coverage is not monitoring project files after upgrade rails #989

Open ricsdeol opened 3 years ago

ricsdeol commented 3 years ago

Hi after upgrade rails from: 5.0 to 5.2 the simplecov stopped monitoring the project ruby files at app/. I tried many things, like upgrade ruby, rspec, simplecov, reinstall looking doc configuration.

Troubleshooting

I cloned simplecov at vendor/ and put a breakpont at adapt_coverage_result mehod (/lib/simplecov.rb @ line 408) and discovery Coverage.result is not monitoring any files on myprojct/app :(

/myproject $ rspec spec/models/
Warning: method `sort` already defined in AdminIndeva::LostReasonsController

Randomized with seed 46679
.........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

Finished in 2 minutes 10.3 seconds (files took 10.05 seconds to load)
1145 examples, 0 failures

Randomized with seed 46679

Frame number: 0/12

From: /myproject/vendor/simplecov/lib/simplecov.rb @ line 408 SimpleCov.adapt_coverage_result:

    401: def adapt_coverage_result
    402:   covarage = Coverage.result
    403:   @result = SimpleCov::ResultAdapter.call(covarage)
    404: 
    405: 
    406:   binding.pry
    407: 
 => 408: end

[1] pry(SimpleCov)> covarage.filter {|k,_| k.start_with? '/myproject/' }.keys                                                                    
=> ["/myproject/spec/models/accounts/account_spec.rb",
 "/myproject/spec/rails_helper.rb",
 "/myproject/config/environment.rb",
 "/myproject/config/application.rb",
 "/myproject/config/boot.rb",
 "/myproject/lib/extensions/elastic_search.rb",
 "/myproject/config/environments/test.rb"]
[2] pry(SimpleCov)> @result.filter {|k,_| k.start_with? '/myproject/' }.keys                                                                     
=> ["/myproject/spec/models/accounts/account_spec.rb",
 "/myproject/spec/rails_helper.rb",
 "/myproject/config/environment.rb",
 "/myproject/config/application.rb",
 "/myproject/config/boot.rb",
 "/myproject/lib/extensions/elastic_search.rb",
 "/myproject/config/environments/test.rb"]
[3] pry(SimpleCov)>     

Versions

Ruby Version: ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux-musl] same error happend on 2.6.7 Simplecov: 0.21.2 Rails: 5.2.5 Rspec:

Config Files

spec/rails_helper.rb

require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'

require File.expand_path('../config/environment', __dir__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?

require 'rspec/rails'
require 'factory_bot_rails'
require 'faker'
require 'sidekiq/testing'
require 'pundit/matchers'
require 'aasm/rspec'
require 'money-rails/test_helpers'

Dir[Rails.root.join('spec/support/**/*.rb')].sort.each { |f| require f }

# It is causing some false negatives, so I left it disabled by default.
# This lib helps to know which diff point of an array or hash is different.
# require 'super_diff/rspec-rails'

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  ...
end
Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

ActiveSupport::Deprecation.silenced = true

spec/spec_helper.rb

# frozen_string_literal: true
ENV['RAILS_ENV'] ||= 'test'

require 'simplecov'

SimpleCov.start 'rails' do
  add_filter 'app/admin/'
  # Ignore vendored stuff
  add_filter 'vendor'

  add_group 'Controllers', 'app/controllers'
  add_group 'Graphql', 'app/graphql'
  add_group 'Decorators', 'app/decorators'
  add_group 'Forms', 'app/forms'
  add_group 'Policies', 'app/policies'
  add_group 'Services', 'app/services'
  add_group 'Uploaders', 'app/uploaders'
  add_group 'Workers', 'app/workers'
  add_group 'Queries', 'app/queries'
  add_group 'Validators', 'app/validators'
end

RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end
end

config/environments/test.rb

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # The test environment is used exclusively to run your application's
  # test suite. You never need to work with it otherwise. Remember that
  # your test database is "scratch space" for the test suite and is wiped
  # and recreated between test runs. Don't rely on the data there!
  config.cache_classes = false

  # Do not eager load code on boot. This avoids loading your whole application
  # just for the purpose of running a single test. If you are using a tool that
  # preloads Rails for running tests, you may have to set it to true.
  config.eager_load = false

  # Configure public file server for tests with Cache-Control for performance.
  config.public_file_server.enabled = true
  config.public_file_server.headers = {
    'Cache-Control' => "public, max-age=#{1.hour.to_i}"
  }

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Raise exceptions instead of rendering exception templates.
  config.action_dispatch.show_exceptions = false

  # Disable request forgery protection in test environment.
  config.action_controller.allow_forgery_protection = false

  # Store uploaded files on the local file system in a temporary directory
  config.active_storage.service = :test

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.delivery_method = :test

  config.action_mailer.perform_caching = false

  # Tell Action Mailer not to deliver emails to the real world.
  # The :test delivery method accumulates sent emails in the
  # ActionMailer::Base.deliveries array.
  config.action_mailer.asset_host = 'http://myproject.dev'
  config.action_mailer.default_url_options = { host: 'myproject.dev' }
  config.action_mailer.delivery_method = :test

  # Print deprecation notices to the stderr.
  config.active_support.deprecation = :stderr

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true
  config.cache_store = :null_store
end

Does Anyone could help me please?

tycooon commented 3 years ago

Are you sure you are running tests with spring disabled?

ricsdeol commented 3 years ago

Are you sure you are running tests with spring disabled?

Yes, I removed from Gemfile, bin/spring and other references. I tried disable bootsnap at config/boot.rb too.

ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)

require 'bundler/setup' # Set up gems listed in the Gemfile.
# require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
tycooon commented 3 years ago

This looks really strange, because AFAIK the way Ruby Coverage lib works, is it includes all the files that were required after Coverage.start call which happens in SimpleCov.start in this case.

I see you are calling rspec without bundle exec btw, maybe it's some custom script?

ricsdeol commented 3 years ago

This looks really strange, because AFAIK the way Ruby Coverage lib works, is it includes all the files that were required after Coverage.start call which happens in SimpleCov.start in this case.

I see you are calling rspec without bundle exec btw, maybe it's some custom script?

Hi, so I dont use custom script, ou bin/rspec. I tried with bundle exec and the problem continue :/

/myproject $ bundle exec rspec spec/models/accounts/

Randomized with seed 21737
........................................

Finished in 6.11 seconds (files took 11.87 seconds to load)
40 examples, 0 failures

Randomized with seed 21737
# result from
#  def adapt_coverage_result
#    covarage = Coverage.result
#    @result = SimpleCov::ResultAdapter.call(covarage)
#    puts covarage.filter {|k,_| k.start_with? '/myproject/' }.keys
#  end

/myproject/spec/models/accounts/account_spec.rb
/myproject/spec/rails_helper.rb
/myproject/config/environment.rb
/myproject/config/application.rb
/myproject/config/boot.rb
/myproject/lib/extensions/elastic_search.rb
/myproject/config/environments/test.rb
Coverage report generated for RSpec to /myproject/coverage. 14 / 21331 LOC (0.07%) covered.
721p commented 3 years ago

@ricsdeol You could try setting eager loading see if that makes a difference. It might also be worth seeing what other gems were updated during your original update, maybe one of them is causing your app dir to be loaded during the bundler initialization process when it loads gems

Ajmal commented 3 years ago

@ptrippett thank you

config.eager_load = true

Now my coverage result back to normal

tycooon commented 3 years ago

Wow, for some reason I checked that and decided that false is the value we need here. Maybe you can close the issue btw :)

calebl commented 2 years ago

With minitest, this combination worked for me (ruby 3.0.2p107, Rails 6.1.4.1):

In config/environments/test.rb: config.eager_load = true

Disable spring when running with: DISABLE_SPRING=1 rails test

zenspider commented 2 months ago

Please close