class OneshotCov::Reporter

Public Class Methods

new(target_path:, logger:, emit_term: nil) click to toggle source
# File lib/oneshot_cov/reporter.rb, line 6
def initialize(target_path:, logger:, emit_term: nil)
  @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
  end
end

Public Instance Methods

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

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

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

Private Instance Methods

is_target?(filepath, value) click to toggle source
# File lib/oneshot_cov/reporter.rb, line 51
def is_target?(filepath, value)
  return false if value[:oneshot_lines].empty?
  return false if !filepath.start_with?(@target_path)
  return false if @bundler_path && filepath.start_with?(@bundler_path)
  true
end
relative_path(filepath) click to toggle source
# File lib/oneshot_cov/reporter.rb, line 58
def relative_path(filepath)
  filepath[@target_path.size..-1]
end
time_to_emit?() click to toggle source
# File lib/oneshot_cov/reporter.rb, line 40
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