module Minitest::CoverageRunner

Constants

PWD
VERSION

Public Class Methods

coverage_baseline() click to toggle source
# File lib/minitest/coverage.rb, line 7
def self.coverage_baseline
  @@coverage_baseline
end
coverage_baseline=(o) click to toggle source
# File lib/minitest/coverage.rb, line 11
def self.coverage_baseline= o
  @@coverage_baseline = o
end
coverage_data() click to toggle source
# File lib/minitest/coverage.rb, line 19
def self.coverage_data
  @@coverage_data
end
coverage_data=(o) click to toggle source
# File lib/minitest/coverage.rb, line 23
def self.coverage_data= o
  @@coverage_data = o
end
output_coverage() click to toggle source
# File lib/minitest/coverage.rb, line 116
def output_coverage
  require "json"

  cleaned = coverage_data.reject { |path, lines|
    path.nil? or
      path.include? RbConfig::CONFIG["libdir"] or
      not path.start_with? PWD
  }

  File.open "coverage.json", "w" do |f|
    f.puts JSON.pretty_generate cleaned
  end

  warn "created coverage.json"
end

Public Instance Methods

clean_path(path) click to toggle source
# File lib/minitest/coverage.rb, line 37
def clean_path path
  path[Dir.pwd.length+1..-1]
end
cleanup(a) click to toggle source
# File lib/minitest/coverage.rb, line 112
def cleanup a
  a.join(", ").scan(/.{1,72}(?:\s+|\Z)/).map { |s| "    #{s}" }.join "\n"
end
coverage_baseline() click to toggle source
# File lib/minitest/coverage.rb, line 15
def coverage_baseline
  @@coverage_baseline
end
coverage_data() click to toggle source
# File lib/minitest/coverage.rb, line 27
def coverage_data
  @@coverage_data
end
coverage_diff(test_name, new, old) click to toggle source
# File lib/minitest/coverage.rb, line 41
def coverage_diff test_name, new, old
  puts

  path, lines = find_path_and_lines(new, test_name)

  return unless path && lines

  old_lines = old[path] || lines.map { |x| x ? 0 : nil }

  print "  #{clean_path path}"
  a, b, max = pct(old_lines), pct(lines), max(lines)

  if (a - b).abs < 0.01 then
    puts ": no change at %.1f%% of %d lines" % [a, max]
  else
    puts ": from %.1f%% to %.1f%% of %d lines" % [a, b, max]
  end
end
find_path_and_lines(coverage, test_name) click to toggle source
# File lib/minitest/coverage.rb, line 62
def find_path_and_lines coverage, test_name
  impl_name = self.impl_name test_name

  locations = self.locations

  path = (locations[impl_name] ||
          locations[locations.keys.find { |k| k =~ /::#{impl_name}$/ }])

  return [path, coverage[path]] if path && coverage[path]

  # path = (impl_name.
  #         gsub(/([a-z])([A-Z])/, '\1_\2').
  #         gsub(/::/, "/").
  #         downcase) + ".rb"
  #
  # impl_re = /\/#{path}$/
  #
  # coverage.sort.find { |p, lines| # sorting biases towards app and lib
  #   next unless p.start_with? PWD
  #   p =~ impl_re
  # }
end
impl_name(test_name) click to toggle source
# File lib/minitest/coverage.rb, line 85
def impl_name test_name
  (test_name[/^([\w:]+?)Test/, 1] || # rails style
   test_name[/^Test([\w:]+)$/, 1] || # ruby style
   test_name)
end
locations() click to toggle source
# File lib/minitest/coverage.rb, line 95
def locations
  coverage_baseline["mtc_location"] || {}
end
max(lines) click to toggle source
# File lib/minitest/coverage.rb, line 91
def max lines
  lines.compact.size
end
merge_coverage(new_coverage) click to toggle source
# File lib/minitest/coverage.rb, line 99
def merge_coverage new_coverage
  path, lines = find_path_and_lines new_coverage, name

  if path and lines then
    coverage_data[path] = lines
  else
    warn "Bad mapping for #{name}."
    warn "  Looking for #{impl_name name} amongst:"
    warn cleanup locations.keys.sort
    warn "  Skipping coverage."
  end
end
pct(lines) click to toggle source
# File lib/minitest/coverage.rb, line 134
def pct lines
  max = max lines
  n   = max - lines.count(0)
  100.0*n/max
end
run(*args) click to toggle source
Calls superclass method
# File lib/minitest/coverage.rb, line 140
def run *args
  return if self.runnable_methods.empty?

  unless impl_name self.name then
    warn "BAD NAME: #{self.name} -- can't map to implementation. Skipping"
  end

  puts
  puts "#{self.name}:"

  super

  new_coverage = Coverage.peek_result

  coverage_diff(self.name, new_coverage, coverage_data)
  merge_coverage new_coverage

  if Coverage.respond_to? :result= then
    Coverage.result = coverage_baseline
  else
    @@coverage_warning ||= false
    unless @@coverage_warning then
      warn "Unable to reset coverage baseline. Numbers will be artificially high."
      @@coverage_warning = true
    end
  end
end