class Undercover::LcovParser

Attributes

io[R]
source_files[R]

Public Class Methods

new(lcov_io) click to toggle source
# File lib/undercover/lcov_parser.rb, line 9
def initialize(lcov_io)
  @io = lcov_io
  @source_files = {}
end
parse(lcov_report_path) click to toggle source
# File lib/undercover/lcov_parser.rb, line 14
def self.parse(lcov_report_path)
  lcov_io = File.open(lcov_report_path)
  new(lcov_io).parse
end

Public Instance Methods

coverage(filepath) click to toggle source
# File lib/undercover/lcov_parser.rb, line 25
def coverage(filepath)
  _filename, coverage = source_files.find do |relative_path, _|
    relative_path == filepath
  end
  coverage || []
end
parse() click to toggle source
# File lib/undercover/lcov_parser.rb, line 19
def parse
  io.each(&method(:parse_line))
  io.close
  self
end

Private Instance Methods

parse_line(line) click to toggle source

rubocop:disable Metrics/MethodLength, Style/SpecialGlobalVars, Metrics/AbcSize

# File lib/undercover/lcov_parser.rb, line 35
def parse_line(line)
  case line
  when /^SF:(.+)/
    @current_filename = $~[1].gsub(/^\.\//, '')
    source_files[@current_filename] = []
  when /^DA:(\d+),(\d+)/
    line_no = $~[1]
    covered = $~[2]
    source_files[@current_filename] << [line_no.to_i, covered.to_i]
  when /^(BRF|BRH):(\d+)/
    # branches found/hit; no-op
  when /^BRDA:(\d+),(\d+),(\d+),(-|\d+)/
    line_no = $~[1]
    block_no = $~[2]
    branch_no = $~[3]
    covered = ($~[4] == '-' ? '0' : $~[4])
    source_files[@current_filename] << [line_no.to_i, block_no.to_i, branch_no.to_i, covered.to_i]
  when /^end_of_record$/, /^$/
    @current_filename = nil
  else
    raise LcovParseError, "could not recognise '#{line}' as valid LCOV"
  end
end