module DeepCover::Tools::BuiltinCoverage

Public Instance Methods

builtin_coverage(source, filename, lineno) click to toggle source
# File lib/deep_cover/tools/builtin_coverage.rb, line 7
def builtin_coverage(source, filename, lineno)
  require 'coverage'
  filename = File.absolute_path(File.expand_path(filename))
  ::Coverage.start
  begin
    Tools.silence_warnings do
      execute_sample -> { filename = run_with_line_coverage(source, filename, lineno) }
    end
  ensure
    result = ::Coverage.result
  end
  unshift_coverage(result.fetch(filename), lineno)
end
run_with_line_coverage(source, filename = '<code>', lineno = 1) click to toggle source
# File lib/deep_cover/tools/builtin_coverage.rb, line 21
def run_with_line_coverage(source, filename = '<code>', lineno = 1)
  source = shift_source(source, lineno)
  f = Tempfile.new(['ruby', '.rb'])
  f.write(source)
  f.close

  begin
    require f.path
  rescue StandardError => e
    tempfile_matcher = Regexp.new("\\A#{Regexp.escape(f.path)}(?=:\\d)")
    e.backtrace.each { |l| l.sub!(tempfile_matcher, filename) }
    raise
  end
  $LOADED_FEATURES.delete(f.path)
  f.path
end

Private Instance Methods

shift_source(source, lineno) click to toggle source
# File lib/deep_cover/tools/builtin_coverage.rb, line 40
def shift_source(source, lineno)
  "\n" * (lineno - 1) + source
end
unshift_coverage(coverage, lineno) click to toggle source
# File lib/deep_cover/tools/builtin_coverage.rb, line 44
def unshift_coverage(coverage, lineno)
  coverage[(lineno - 1)..-1]
end