Sha256: 7441d06357cf5f1205716d5eb05ec2d3896ac5dbe60e5213ed96f0adfe898f9a

Contents?: true

Size: 1.74 KB

Versions: 6

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module DeepCover
  module Tools::BuiltinCoverage
    def builtin_coverage(source, filename, lineno)
      require 'coverage'
      filename = File.absolute_path(File.expand_path(filename))
      ::Coverage.start
      Tools.silence_warnings do
        execute_sample -> { run_with_line_coverage(source, filename, lineno) }
      end
      unshift_coverage(::Coverage.result.fetch(filename), 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, filename = nil, lineno = 1)
        source = shift_source(source, lineno)
        Object.to_java.getRuntime.executeScript(source, filename)
      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, filename = nil, lineno = 1)
        source = shift_source(source, lineno)
        RubyVM::InstructionSequence.compile(source, filename).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

6 entries across 6 versions & 1 rubygems

Version Path
deep-cover-0.6.0 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.5.7 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.5.6 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.5.5 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.5.4 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-0.5.3 lib/deep_cover/tools/builtin_coverage.rb