class RubyCritic::Analyser::Coverage

Public Class Methods

new(analysed_modules) click to toggle source
# File lib/rubycritic/analysers/coverage.rb, line 12
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 17
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 25
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 31
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 39
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
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 109
def results
  array = []
  resultset.each do |command_name, data|
    array << ::SimpleCov::Result.from_hash(command_name => data)
  end
  array
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 58
def resultset
  @resultset ||= begin
    if (data = stored_data)
      begin
        JSON.parse(data) || {}
      rescue JSON::ParserError => err
        puts "Error: Loading .resultset.json: #{err.message}"
        {}
      end
    else
      {}
    end
  end
end
resultset_path() click to toggle source

The path to the .resultset.json cache file

# File lib/rubycritic/analysers/coverage.rb, line 48
def resultset_path
  File.join(SimpleCov.coverage_path, '.resultset.json')
end
resultset_writelock() click to toggle source
# File lib/rubycritic/analysers/coverage.rb, line 52
def resultset_writelock
  File.join(SimpleCov.coverage_path, '.resultset.json.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 74
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 88
def synchronize_resultset
  # make it reentrant
  return yield if @resultset_locked == true

  return yield unless File.exist?(resultset_writelock)

  begin
    @resultset_locked = true
    File.open(resultset_writelock, 'w+') do |file|
      file.flock(File::LOCK_EX)
      yield
    end
  ensure
    @resultset_locked = false
  end
end