Sha256: 50cb8a5b545f8d2c01b77fd989abf23214adfa9445d368d08bd5e373d91b350a

Contents?: true

Size: 1.18 KB

Versions: 14

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

require 'tempfile'

module DeepCover
  module Tools::BuiltinCoverage
    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

    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

    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-core-1.1.0 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-core-1.0.0 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-core-0.8.1 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-core-0.8.0 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-core-0.7.10 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-core-0.7.9 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-core-0.7.8 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-core-0.7.7 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-core-0.7.6 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-core-0.7.5 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-core-0.7.4 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-core-0.7.3 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-core-0.7.2 lib/deep_cover/tools/builtin_coverage.rb
deep-cover-core-0.7.1 lib/deep_cover/tools/builtin_coverage.rb