Sha256: 0eb712418d0b53ee6c730e1c8b19d59b8746113a57373b8d129657c738c47196

Contents?: true

Size: 1.64 KB

Versions: 14

Compression:

Stored size: 1.64 KB

Contents

module DeepCover
  module Tools::BuiltinCoverage
    require 'coverage'

    def builtin_coverage(source, fn, lineno)
      fn = File.absolute_path(File.expand_path(fn))
      ::Coverage.start
      Tools.silence_warnings do
        execute_sample ->{ run_with_line_coverage(source, fn, lineno)}
      end
      unshift_coverage(::Coverage.result.fetch(fn), lineno)
    end

    if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
      # Executes the source as if it was in the specified file while
      # builtin coverage information is still captured
      def run_with_line_coverage(source, fn=nil, lineno=1)
        source = shift_source(source, lineno)
        Object.to_java.getRuntime.executeScript(source, fn)
      end
    else
      # In ruby 2.0 and 2.1, using 2, 3 or 4 as lineno with RubyVM::InstructionSequence.compile
      # will cause the coverage result to be truncated.
      # 1: [1,2,nil,1]
      # 2: [nil,1,2,nil]
      # 3: [nil,nil,1,2]
      # 4: [nil,nil,nil,1]
      # 5: [nil,nil,nil,nil,1,2,nil,1]
      # Using 1 and 5 or more do not seem to show this issue.
      # The workaround is to create the fake lines manually and always use the default lineno

      # Executes the source as if it was in the specified file while
      # builtin coverage information is still captured
      def run_with_line_coverage(source, fn=nil, lineno=1)
        source = shift_source(source, lineno)
        RubyVM::InstructionSequence.compile(source, fn).eval
      end
    end

    private

    def shift_source(source, lineno)
      "\n" * (lineno - 1) + source
    end

    def unshift_coverage(coverage, lineno)
      coverage[(lineno-1)..-1]
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
deep-cover-0.1.14 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.1.13 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.1.12 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.1.11 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.1.10 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.1.9 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.1.8 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.1.7 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.1.6 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.1.5 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.1.4 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.1.3 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.1.2 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.1.1 lib/deep_cover/tools/builtin_coverage.rb