Skip to content

Commit

Permalink
Merge 72a5d44 into 5a7da96
Browse files Browse the repository at this point in the history
  • Loading branch information
danmayer committed Apr 29, 2019
2 parents 5a7da96 + 72a5d44 commit 6c2567f
Show file tree
Hide file tree
Showing 37 changed files with 138 additions and 124 deletions.
34 changes: 25 additions & 9 deletions .rubocop.yml
Expand Up @@ -2,14 +2,21 @@ AllCops:
TargetRubyVersion: 2.4
Exclude:
- docs/**/*
- test/rails4_dummy/**/*
- test/rails5_dummy/**/*
- test/fixtures/**/*
# Get the code passing first then we will enable for tests
- test/**/*
Documentation:
Enabled: false
Metrics/MethodLength:
Enabled: false
Metrics/LineLength:
Max: 120
Max: 160
Metrics/BlockNesting:
Max: 5
Metrics/BlockLength:
Max: 30
Metrics/CyclomaticComplexity:
Enabled: false
Metrics/PerceivedComplexity:
Expand All @@ -22,20 +29,26 @@ Metrics/ModuleLength:
Enabled: false
Metrics/ParameterLists:
Max: 10
Style/AccessorMethodName:
Enabled: false
Style/PredicateName:
Naming/AccessorMethodName:
Enabled: false
Naming/PredicateName:
Enabled: true
Style/TernaryParentheses:
Enabled: false
Style/MutableConstant:
Enabled: false
Style/IdenticalConditionalBranches:
Enabled: false
Style/ClassVars:
Enabled: false
Style/MultilineBlockChain:
Enabled: false
Style/FrozenStringLiteralComment:
Enabled: true
Style/GuardClause:
Enabled: true
Style/NumericPredicate:
Enabled: false
Enabled: true
Style/NumericLiterals:
Enabled: false
Style/NumericLiteralPrefix:
Expand All @@ -44,16 +57,19 @@ Style/ClassAndModuleChildren:
Enabled: false
Performance/Casecmp:
Enabled: false
Style/MultilineMethodCallIndentation:
Performance/RegexpMatch:
Enabled: false
Layout/MultilineMethodCallIndentation:
Enabled: true
Style/MultilineOperationIndentation:
Layout/MultilineOperationIndentation:
Enabled: true
Lint/RescueException:
Enabled: true
Lint/UselessAccessModifier:
Enabled: true
Lint/ShadowingOuterLocalVariable:
Enabled: true
Style/FormatString:
Enabled: true
Lint/Eval:
Security/Eval:
Enabled: true

3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -9,6 +9,9 @@ gemfile:
- Gemfile.rails4
services:
- redis-server
script:
- bundle exec rake rubocop
- bundle exec rake
before_install:
- echo 'this is a hack to clear default bundler and force bundler 1.17.3'
- ls /home/travis/.rvm/gems/
Expand Down
6 changes: 4 additions & 2 deletions Rakefile
@@ -1,10 +1,12 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'

import 'test/benchmarks/benchmark.rake'
require 'rubocop/rake_task'

RuboCop::RakeTask.new

task default: [:test, :benchmarks]
task default: %i[test benchmarks]

task :test
require 'rake/testtask'
Expand Down
3 changes: 2 additions & 1 deletion coverband.gemspec
Expand Up @@ -33,7 +33,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rack-test'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'resque'
# temporarily needed to fun tests for classifier-reborn as part of benchmarks
spec.add_development_dependency 'rubocop'
# temporarily needed to run tests for classifier-reborn as part of benchmarks
spec.add_development_dependency 'minitest-reporters'

# TODO: used for benchmarking and tests I think we have other better benchmarking
Expand Down
2 changes: 1 addition & 1 deletion lib/coverband.rb
Expand Up @@ -64,7 +64,7 @@ def self.configuration

def self.start
Coverband::Collectors::Coverage.instance
# TODO Railtie sets up at_exit after forks, via middleware, perhaps this hsould be
# TODO: Railtie sets up at_exit after forks, via middleware, perhaps this hsould be
# added if not rails or if rails but not rackserverrunning
AtExit.register
Background.start if configuration.background_reporting_enabled && !RackServerCheck.running?
Expand Down
2 changes: 1 addition & 1 deletion lib/coverband/adapters/base.rb
Expand Up @@ -87,7 +87,7 @@ def file_hash(file)
def expand_report(report)
expanded = {}
report_time = Time.now.to_i
updated_time = self.type == Coverband::EAGER_TYPE ? nil : report_time
updated_time = type == Coverband::EAGER_TYPE ? nil : report_time
report.each_pair do |key, line_data|
extended_data = {
'first_updated_at' => report_time,
Expand Down
2 changes: 1 addition & 1 deletion lib/coverband/at_exit.rb
Expand Up @@ -28,7 +28,7 @@ def self.register
# skip reporting
else
Coverband.report_coverage(true)
#Coverband.configuration.logger&.debug('Coverband: Reported coverage before exit')
# Coverband.configuration.logger&.debug('Coverband: Reported coverage before exit')
end
end
end
Expand Down
27 changes: 10 additions & 17 deletions lib/coverband/collectors/coverage.rb
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require 'singleton'
require_relative 'delta'

Expand Down Expand Up @@ -38,22 +39,17 @@ def report_coverage(force_report = false)
return if !ready_to_report? && !force_report
raise 'no Coverband store set' unless @store

original_previous_set = Delta.previous_results
Delta.previous_results
files_with_line_usage = filtered_files(Delta.results)

###
# Hack to prevent processes and threads from reporting first Coverage hit
# when we are in runtime collection mode, which do not have a cache of previous
# coverage to remove the initial stdlib Coverage loading data
###
@store.save_report(files_with_line_usage)
rescue StandardError => err
rescue StandardError => e
if @verbose
@logger&.error 'coverage failed to store'
@logger&.error "error: #{err.inspect} #{err.message}"
@logger&.error err.backtrace
@logger&.error "error: #{e.inspect} #{e.message}"
@logger&.error e.backtrace
end
raise err if @test_env
raise e if @test_env
end

protected
Expand Down Expand Up @@ -85,19 +81,16 @@ def ready_to_report?
end

def initialize
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.3.0')
raise NotImplementedError, 'not supported until Ruby 2.3.0 and later'
end
raise NotImplementedError, 'Coverage needs Ruby > 2.3.0' if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.3.0')

require 'coverage'
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.5.0')
::Coverage.start unless ::Coverage.running?
else
::Coverage.start
end
if Coverband.configuration.safe_reload_files
Coverband.configuration.safe_reload_files.each do |safe_file|
load safe_file
end
Coverband.configuration.safe_reload_files&.each do |safe_file|
load safe_file
end
reset_instance
end
Expand Down
17 changes: 7 additions & 10 deletions lib/coverband/collectors/delta.rb
@@ -1,4 +1,5 @@
# frozen_string_literal: true

module Coverband
module Collectors
class Delta
Expand Down Expand Up @@ -45,21 +46,17 @@ def self.reset

def generate
current_coverage.each_with_object({}) do |(file, line_counts), new_results|
if @@previous_coverage[file]
new_results[file] = array_diff(line_counts, @@previous_coverage[file])
else
new_results[file] = line_counts
end
new_results[file] = if @@previous_coverage[file]
array_diff(line_counts, @@previous_coverage[file])
else
line_counts
end
end
end

def array_diff(latest, original)
latest.map.with_index do |v, i|
if (v && original[i])
[0, v - original[i]].max
else
nil
end
[0, v - original[i]].max if v && original[i]
end
end
end
Expand Down
11 changes: 5 additions & 6 deletions lib/coverband/configuration.rb
Expand Up @@ -77,16 +77,15 @@ def store
end

def store=(store)
if store.is_a?(Coverband::Adapters::Base)
@store = store
else
raise 'please pass in an subclass of Coverband::Adapters for supported stores'
end
raise 'Pass in an instance of Coverband::Adapters' unless store.is_a?(Coverband::Adapters::Base)

@store = store
end

def track_gems=(value)
@track_gems = value
return unless @track_gems

# by default we ignore vendor where many deployments put gems
# we will remove this default if track_gems is set
@ignore.delete('vendor')
Expand Down Expand Up @@ -135,7 +134,7 @@ def all_root_paths
roots
end

SKIPPED_SETTINGS = %w(@s3_secret_access_key @store)
SKIPPED_SETTINGS = %w[@s3_secret_access_key @store]
def to_h
instance_variables
.each_with_object('gem_paths': gem_paths) do |var, hash|
Expand Down
4 changes: 3 additions & 1 deletion lib/coverband/integrations/background.rb
Expand Up @@ -7,6 +7,7 @@ class Background

def self.stop
return unless @thread

@semaphore.synchronize do
if @thread
@thread.exit
Expand All @@ -16,7 +17,7 @@ def self.stop
end

def self.running?
@thread && @thread.alive?
@thread&.alive?
end

def self.start
Expand All @@ -25,6 +26,7 @@ def self.start
logger = Coverband.configuration.logger
@semaphore.synchronize do
return if running?

logger&.debug('Coverband: Starting background reporting')
sleep_seconds = Coverband.configuration.background_reporting_sleep_seconds
@thread = Thread.new do
Expand Down
2 changes: 1 addition & 1 deletion lib/coverband/integrations/resque.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true

Resque.after_fork do |job|
Resque.after_fork do |_job|
Coverband.start
Coverband.runtime_coverage!
# no reason to miss coverage on a first resque job
Expand Down
25 changes: 12 additions & 13 deletions lib/coverband/reporters/base.rb
Expand Up @@ -13,32 +13,31 @@ def report(store, _options = {})
all_roots = Coverband.configuration.all_root_paths
scov_style_report = get_current_scov_data_imp(store, all_roots)

if Coverband.configuration.verbose
msg = "report:\n #{scov_style_report.inspect}"
# Coverband.configuration.logger.debug msg
end
# These are extremelhy verbose but useful during coverband development, not generally for users
# if Coverband.configuration.verbose
# # msg = "report:\n #{scov_style_report.inspect}"
# # Coverband.configuration.logger.debug msg
# end
scov_style_report
end

protected

def fix_file_names(report_hash, roots)
if Coverband.configuration.verbose
Coverband.configuration.logger.info "fixing root: #{roots.join(', ')}"
end
Coverband.configuration.logger.info "fixing root: #{roots.join(', ')}" if Coverband.configuration.verbose

# normalize names across servers
report_hash.each_with_object({}) do |(name, report), fixed_report|
fixed_report[name] = {}
report.each_pair do |key, vals|
filename = relative_path_to_full(key, roots)
fixed_report[name][filename] = if fixed_report[name].key?(filename) && fixed_report[name][filename]['data'] && vals['data']
merged_data = merge_arrays(fixed_report[name][filename]['data'], vals['data'])
vals['data'] = merged_data
vals
else
vals
end
merged_data = merge_arrays(fixed_report[name][filename]['data'], vals['data'])
vals['data'] = merged_data
vals
else
vals
end
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/coverband/reporters/html_report.rb
Expand Up @@ -41,8 +41,6 @@ def report
end
end

private

def self.fix_reports(reports)
# list all files, even if not tracked by Coverband (0% coverage)
tracked_glob = "#{Coverband.configuration.current_root}/{app,lib,config}/**/*.{rb}"
Expand Down
1 change: 1 addition & 0 deletions lib/coverband/reporters/web.rb
Expand Up @@ -3,6 +3,7 @@
begin
require 'rack'
rescue LoadError
puts 'error loading Coverband web reporter as Rack is not available'
end

module Coverband
Expand Down

0 comments on commit 6c2567f

Please sign in to comment.