Skip to content

Commit

Permalink
Merge pull request #343 from danmayer/fix_bootup_issues
Browse files Browse the repository at this point in the history
avoid running coverband on known tasks, capture errors and log warnin…
  • Loading branch information
danmayer committed Oct 19, 2019
2 parents 475660a + 4688a1f commit 7ce04ee
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Expand Up @@ -32,6 +32,8 @@ Metrics/ParameterLists:
Max: 10
Naming/AccessorMethodName:
Enabled: false
Naming/RescuedExceptionsVariableName:
Enabled: false
Naming/PredicateName:
Enabled: true
Style/TernaryParentheses:
Expand Down
19 changes: 12 additions & 7 deletions lib/coverband.rb
Expand Up @@ -99,12 +99,17 @@ def self.runtime_coverage!
Coverband::Collectors::Coverage.instance
end
unless ENV['COVERBAND_DISABLE_AUTO_START']
# Coverband should be setup as early as possible
# to capture usage of things loaded by initializers or other Rails engines
configure
start
require 'coverband/utils/railtie' if defined? ::Rails::Railtie
require 'coverband/integrations/resque' if defined? ::Resque
require 'coverband/integrations/bundler' if defined? ::Bundler
begin
# Coverband should be setup as early as possible
# to capture usage of things loaded by initializers or other Rails engines
configure
start
require 'coverband/utils/railtie' if defined? ::Rails::Railtie
require 'coverband/integrations/resque' if defined? ::Resque
require 'coverband/integrations/bundler' if defined? ::Bundler
rescue Redis::CannotConnectError => error
Coverband.configuration.logger.info "Redis is not available (#{error}), Coverband not configured"
Coverband.configuration.logger.info 'If this is a setup task like assets:precompile feel free to ignore'
end
end
end
4 changes: 3 additions & 1 deletion lib/coverband/collectors/view_tracker.rb
Expand Up @@ -29,10 +29,10 @@ def initialize(options = {})

@roots = options.fetch(:roots) { Coverband.configuration.all_root_patterns }
@roots = @roots.split(',') if @roots.is_a?(String)
@one_time_timestamp = false

@logged_views = []
@views_to_record = []
redis_store.set(tracker_time_key, Time.now.to_i) unless redis_store.exists(tracker_time_key)
end

###
Expand Down Expand Up @@ -105,6 +105,8 @@ def self.supported_version?
end

def report_views_tracked
redis_store.set(tracker_time_key, Time.now.to_i) unless @one_time_timestamp || redis_store.exists(tracker_time_key)
@one_time_timestamp = true
reported_time = Time.now.to_i
views_to_record.each do |file|
redis_store.hset(tracker_key, file, reported_time)
Expand Down
11 changes: 10 additions & 1 deletion lib/coverband/configuration.rb
Expand Up @@ -23,7 +23,16 @@ class Configuration
IGNORE_TASKS = ['coverband:clear',
'coverband:coverage',
'coverband:coverage_server',
'coverband:migrate']
'coverband:migrate',
'assets:precompile',
'db:version',
'db:create',
'db:drop',
'db:seed',
'db:setup',
'db:structure:dump',
'db:structure:load',
'db:version']

# Heroku when building assets runs code from a dynamic directory
# /tmp was added to avoid coverage from /tmp/build directories during
Expand Down
25 changes: 16 additions & 9 deletions lib/coverband/utils/railtie.rb
Expand Up @@ -11,22 +11,29 @@ def eager_load!

class Railtie < Rails::Railtie
initializer 'coverband.configure' do |app|
app.middleware.use Coverband::BackgroundMiddleware
begin
app.middleware.use Coverband::BackgroundMiddleware

if Coverband.configuration.track_views
CoverbandViewTracker = Coverband::Collectors::ViewTracker.new
Coverband.configuration.view_tracker = CoverbandViewTracker
if Coverband.configuration.track_views
CoverbandViewTracker = Coverband::Collectors::ViewTracker.new
Coverband.configuration.view_tracker = CoverbandViewTracker

ActiveSupport::Notifications.subscribe(/render_partial.action_view|render_template.action_view/) do |name, start, finish, id, payload|
CoverbandViewTracker.track_views(name, start, finish, id, payload) unless name.include?('!')
ActiveSupport::Notifications.subscribe(/render_partial.action_view|render_template.action_view/) do |name, start, finish, id, payload|
CoverbandViewTracker.track_views(name, start, finish, id, payload) unless name.include?('!')
end
end
rescue Redis::CannotConnectError => error
Coverband.configuration.logger.info "Redis is not available (#{error}), Coverband not configured"
Coverband.configuration.logger.info 'If this is a setup task like assets:precompile feel free to ignore'
end
end

config.after_initialize do
Coverband.eager_loading_coverage!
Coverband.report_coverage
Coverband.runtime_coverage!
unless Coverband.tasks_to_ignore?
Coverband.eager_loading_coverage!
Coverband.report_coverage
Coverband.runtime_coverage!
end
end

rake_tasks do
Expand Down
10 changes: 10 additions & 0 deletions test/forked/rails_rake_full_stack_test.rb
Expand Up @@ -45,4 +45,14 @@ def setup
assert_equal empty_hash, coverage_report[:eager_loading]
assert_equal empty_hash, coverage_report[:merged]
end

test "doesn't exit non-zero with error on missing redis" do
output = `COVERBAND_CONFIG=./test/rails#{Rails::VERSION::MAJOR}_dummy/config/coverband_missing_redis.rb bundle exec rake -f test/rails#{Rails::VERSION::MAJOR}_dummy/Rakefile -T`
assert_equal 0, $?.to_i
if ENV['COVERBAND_HASH_REDIS_STORE']
assert output.match(/Redis is not available/)
else
assert output.match(/coverage failed to store/)
end
end
end
3 changes: 3 additions & 0 deletions test/rails4_dummy/config/coverband_missing_redis.rb
@@ -0,0 +1,3 @@
# frozen_string_literal: true

require_relative '../../rails5_dummy/config/coverband_missing_redis'
15 changes: 15 additions & 0 deletions test/rails5_dummy/config/coverband_missing_redis.rb
@@ -0,0 +1,15 @@
# frozen_string_literal: true

Coverband.configure do |config|
config.root = Dir.pwd
config.store = Coverband::Adapters::RedisStore.new(Redis.new(db: 2, url: 'redis://127.0.0.1:123'), redis_namespace: 'coverband_test') if defined? Redis
config.ignore = %w[vendor .erb$ .slim$]
config.root_paths = []
config.logger = Rails.logger
config.verbose = true
config.background_reporting_enabled = true
config.track_gems = true
config.gem_details = true
config.use_oneshot_lines_coverage = true if ENV['ONESHOT']
config.simulate_oneshot_lines_coverage = true if ENV['SIMULATE_ONESHOT']
end

0 comments on commit 7ce04ee

Please sign in to comment.