Sha256: 96cf88401543797d7f5f09b1e3bbf865b24f0de84c90c3870f683da950c4e456

Contents?: true

Size: 1.63 KB

Versions: 47

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require 'coverage'
require 'simplecov'

# Since we load code using Modulation, and the stock coverage gem does not
# calculate coverage for code loaded using `Kernel#eval` et al, we need to use
# the TracePoint API in order to trace execution. Here we monkey-patch the two
# main Coverage class methods, start and result to use TracePoint. Otherwise we
# let SimpleCov do its business.
module Coverage
  EXCLUDE = %w{coverage eg helper run
  }.map { |n| File.expand_path("test/#{n}.rb") }

  LIB_FILES = Dir["#{File.join(FileUtils.pwd, 'lib')}/polyphony/**/*.rb"]

  class << self
    def relevant_lines_for_filename(filename)
      @classifier ||= SimpleCov::LinesClassifier.new
      @classifier.classify(IO.read(filename).lines)
    end

    def start
      @result = {}
      trace = TracePoint.new(:line) do |tp|
        next if tp.path =~ /\(/

        absolute = File.expand_path(tp.path)
        next unless LIB_FILES.include?(absolute)# =~ /^#{LIB_DIR}/

        @result[absolute] ||= relevant_lines_for_filename(absolute)
        @result[absolute][tp.lineno - 1] = 1
      end
      trace.enable
    end

    def result
      @result
    end
  end
end

class << SimpleCov::LinesClassifier
  alias_method :orig_whitespace_line?, :whitespace_line?
  def whitespace_line?(line)
    # apparently TracePoint tracing does not cover lines including only keywords
    # such as begin end etc, so here we mark those lines as whitespace, so they
    # won't count towards the coverage score.
    line.strip =~ /^(begin|end|ensure|else|\{|\})|(\s*rescue\s.+)$/ ||
                  orig_whitespace_line?(line)
  end
end

SimpleCov.start

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
polyphony-1.6 test/coverage.rb
polyphony-1.5 test/coverage.rb
polyphony-1.4 test/coverage.rb
polyphony-1.3 test/coverage.rb
polyphony-1.2.1 test/coverage.rb
polyphony-1.2 test/coverage.rb
polyphony-1.1.1 test/coverage.rb
polyphony-1.1 test/coverage.rb
polyphony-1.0.2 test/coverage.rb
polyphony-1.0.1 test/coverage.rb
polyphony-1.0 test/coverage.rb
polyphony-0.99.6 test/coverage.rb
polyphony-0.99.5 test/coverage.rb
polyphony-0.99.4 test/coverage.rb
polyphony-0.99.3 test/coverage.rb
polyphony-0.99.2 test/coverage.rb
polyphony-0.99.1 test/coverage.rb
polyphony-0.99 test/coverage.rb
polyphony-0.98 test/coverage.rb
polyphony-0.97 test/coverage.rb