class Covhura

Public Instance Methods

merge(old, new) click to toggle source
# File lib/covhura.rb, line 36
def merge(old, new)
  new.reduce(old) do |acc, (file_path, file_coverage)|
    acc[file_path] ||= {}
    acc[file_path][:lines] ||= {}

    old_coverage = acc.dig(file_path, :lines)

    file_coverage[:lines].each do |line_number, line|
      acc[file_path][:lines][line_number] = old_coverage.has_key?(line_number) ?
        merge_line(old_coverage[line_number], line) :
        line
    end

    acc
  end
end
translate(doc) click to toggle source
# File lib/covhura.rb, line 11
def translate(doc)
  lines = doc.lines
  first_line = lines
    .detect { |line| line.strip() != "" }
    .strip()

  if xml?(doc)
    doc = Nokogiri::XML(doc)
    if clover?(doc)
      translator = Report::Clover
    elsif cobertura?(doc)
      translator = Report::Cobertura
    end
  elsif json?(first_line)
    doc = JSON.parse(doc)
    translator = Report::SimpleCov
  elsif lcov?(first_line)
    translator = Report::LCov
  end

  raise "Coverage Format Not Supported" if !defined?(translator) || translator.nil?

  translator.new.translate(doc)
end

Private Instance Methods

clover?(doc) click to toggle source
# File lib/covhura.rb, line 55
def clover?(doc)
  doc.xpath("/coverage")&.attr("generated") != nil
end
cobertura?(doc) click to toggle source
# File lib/covhura.rb, line 59
def cobertura?(doc)
  doc.internal_subset.to_s.match(/cobertura/i) != nil
end
json?(first_line) click to toggle source
# File lib/covhura.rb, line 67
def json?(first_line)
  first_line[0] == "{" || first_line[0] == "["
end
lcov?(first_line) click to toggle source
# File lib/covhura.rb, line 71
def lcov?(first_line)
  first_line[0...3] == "TN:"
end
merge_line(old, new) click to toggle source
# File lib/covhura.rb, line 75
def merge_line(old, new)
  {
    number: old[:number],
    hits: (old[:hits] || 0) + (new[:hits] || 0),
    type: old[:type] || new[:type]
  }
end
xml?(first_line) click to toggle source
# File lib/covhura.rb, line 63
def xml?(first_line)
  first_line[0...5] == "<?xml"
end