class RubyCritic::Analyser::Coverage

Constants

RESULTSET_FILENAME

Public Class Methods

new(analysed_modules) click to toggle source
# File lib/rubycritic/analysers/coverage.rb, line 14
def initialize(analysed_modules)
  @analysed_modules = analysed_modules
  @result = results.first
end

Public Instance Methods

run() click to toggle source
# File lib/rubycritic/analysers/coverage.rb, line 19
def run
  @analysed_modules.each do |analysed_module|
    analysed_module.coverage = find_coverage_percentage(analysed_module)
    print green '.'
  end
  puts ''
end
to_s() click to toggle source
# File lib/rubycritic/analysers/coverage.rb, line 27
def to_s
  'simple_cov'
end

Private Instance Methods

find_coverage_percentage(analysed_module) click to toggle source
# File lib/rubycritic/analysers/coverage.rb, line 33
def find_coverage_percentage(analysed_module)
  source_file = find_source_file(analysed_module)

  return 0 unless source_file

  source_file.covered_percent
end
find_source_file(analysed_module) click to toggle source
# File lib/rubycritic/analysers/coverage.rb, line 41
def find_source_file(analysed_module)
  return unless @result

  needle = File.join(SimpleCov.root, analysed_module.path)

  @result.source_files.detect { |file| file.filename == needle }
end
parse_resultset(data) click to toggle source
# File lib/rubycritic/analysers/coverage.rb, line 67
def parse_resultset(data)
  return {} unless data

  JSON.parse(data) || {}
rescue JSON::ParserError => err
  puts "Error: Loading #{RESULTSET_FILENAME}: #{err.message}"
  {}
end
results() click to toggle source

Gets the resultset hash and re-creates all included instances of SimpleCov::Result from that. All results that are above the SimpleCov.merge_timeout will be dropped. Returns an array of SimpleCov::Result items.

# File lib/rubycritic/analysers/coverage.rb, line 114
def results
  if Gem.loaded_specs['simplecov'].version >= Gem::Version.new('0.19')
    ::SimpleCov::Result.from_hash(resultset)
  else
    resultset.map { |command_name, data| ::SimpleCov::Result.from_hash(command_name => data) }
  end
end
resultset() click to toggle source

Loads the cached resultset from JSON and returns it as a Hash, caching it for subsequent accesses.

# File lib/rubycritic/analysers/coverage.rb, line 63
def resultset
  @resultset ||= parse_resultset(stored_data)
end
resultset_path() click to toggle source

The path to the cache file

# File lib/rubycritic/analysers/coverage.rb, line 50
def resultset_path
  if (cp = Config.coverage_path)
    SimpleCov.coverage_dir(cp)
  end
  File.join(SimpleCov.coverage_path, RESULTSET_FILENAME)
end
resultset_writelock() click to toggle source
# File lib/rubycritic/analysers/coverage.rb, line 57
def resultset_writelock
  "#{resultset_path}.lock"
end
stored_data() click to toggle source

Returns the contents of the resultset cache as a string or if the file is missing or empty nil

# File lib/rubycritic/analysers/coverage.rb, line 77
def stored_data
  synchronize_resultset do
    return unless File.exist?(resultset_path)

    return unless (data = File.read(resultset_path))

    return if data.length < 2

    data
  end
end
synchronize_resultset() { || ... } click to toggle source

Ensure only one process is reading or writing the resultset at any given time

# File lib/rubycritic/analysers/coverage.rb, line 91
def synchronize_resultset(&proc)
  # make it reentrant
  return yield if defined?(@resultset_locked) && @resultset_locked == true

  return yield unless File.exist?(resultset_writelock)

  with_lock(&proc)
end
with_lock() { || ... } click to toggle source
# File lib/rubycritic/analysers/coverage.rb, line 100
def with_lock
  @resultset_locked = true
  File.open(resultset_writelock, 'w+') do |file|
    file.flock(File::LOCK_EX)
    yield
  end
ensure
  @resultset_locked = false
end