Module: DeepCover::Tools::BuiltinCoverage

Defined in:
lib/deep_cover/tools/builtin_coverage.rb

Instance Method Summary collapse

Instance Method Details

#builtin_coverage(source, filename, lineno) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# 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) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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