Sha256: a82218d5db3a25af41ba917b11a33dcdf75814b85de3336978d576d1497547b8

Contents?: true

Size: 1.64 KB

Versions: 67

Compression:

Stored size: 1.64 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

67 entries across 67 versions & 1 rubygems

Version Path
polyphony-0.71 test/coverage.rb
polyphony-0.70 test/coverage.rb
polyphony-0.69 test/coverage.rb
polyphony-0.68 test/coverage.rb
polyphony-0.67 test/coverage.rb
polyphony-0.66 test/coverage.rb
polyphony-0.65 test/coverage.rb
polyphony-0.64 test/coverage.rb
polyphony-0.63 test/coverage.rb
polyphony-0.62 test/coverage.rb
polyphony-0.61 test/coverage.rb
polyphony-0.60 test/coverage.rb
polyphony-0.59.1 test/coverage.rb
polyphony-0.59 test/coverage.rb
polyphony-0.58 test/coverage.rb
polyphony-0.57.0 test/coverage.rb
polyphony-0.56.0 test/coverage.rb
polyphony-0.55.0 test/coverage.rb
polyphony-0.54.0 test/coverage.rb
polyphony-0.53.2 test/coverage.rb