class OneshotCoverage::Reporter

Public Class Methods

new( coverage_module:, target_path:, logger:, emit_term: nil, cover_bundle_path: false ) click to toggle source
# File lib/oneshot_coverage.rb, line 25
def initialize(
  coverage_module:,
  target_path:,
  logger:,
  emit_term: nil,
  cover_bundle_path: false
)
  @coverage_module = coverage_module
  @target_path = target_path
  @logger = logger
  @emit_term = emit_term
  if @emit_term
    @next_emit_time = Time.now.to_i + rand(@emit_term)
  end

  if defined?(Bundler)
    @bundler_path = Bundler.bundle_path.to_s
    @cover_bundle_path = cover_bundle_path
  end
end

Public Instance Methods

emit(force_emit) click to toggle source
# File lib/oneshot_coverage.rb, line 46
def emit(force_emit)
  if !force_emit
    if !time_to_emit?
      return
    end
  end

  logs =
    @coverage_module.result(clear: true, stop: false).
    select { |k, v| is_target?(k, v) }.
    map do |filepath, v|
      formatted_path = format_filepath(filepath)
      OneshotLog.new(format_filepath(filepath), md5_hash_for(filepath), v[:oneshot_lines])
    end

  if logs.size > 0
    @logger.post(logs)
  end
end

Private Instance Methods

format_filepath(filepath) click to toggle source
# File lib/oneshot_coverage.rb, line 90
def format_filepath(filepath)
  if from_bundler?(filepath)
    filepath
  else
    relative_path(filepath)
  end
end
from_bundler?(filepath) click to toggle source
# File lib/oneshot_coverage.rb, line 86
def from_bundler?(filepath)
  @bundler_path && filepath.start_with?(@bundler_path)
end
is_target?(filepath, value) click to toggle source
# File lib/oneshot_coverage.rb, line 79
def is_target?(filepath, value)
  return false if value[:oneshot_lines].empty?
  return @cover_bundle_path if from_bundler?(filepath)
  return false if !filepath.start_with?(@target_path)
  true
end
md5_hash_cache() click to toggle source
# File lib/oneshot_coverage.rb, line 106
def md5_hash_cache
  @md5_hash_cache ||= {}
end
md5_hash_for(filepath) click to toggle source
# File lib/oneshot_coverage.rb, line 110
def md5_hash_for(filepath)
  if md5_hash_cache.key? filepath
    md5_hash_cache[filepath]
  else
    md5_hash_cache[filepath] = Digest::MD5.file(filepath).hexdigest
  end
end
relative_path(filepath) click to toggle source
# File lib/oneshot_coverage.rb, line 98
def relative_path(filepath)
  if filepath.include?(@target_path)
    filepath[@target_path.size..-1]
  else
    filepath
  end
end
time_to_emit?() click to toggle source
# File lib/oneshot_coverage.rb, line 68
def time_to_emit?
  if @emit_term
    if @next_emit_time > Time.now.to_i
      return false # Do not emit until next_emit_time
    else
      @next_emit_time += @emit_term
    end
  end
  true
end