Class: Undercover::LcovParser

Inherits:
Object
  • Object
show all
Includes:
RootToRelativePaths
Defined in:
lib/undercover/lcov_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RootToRelativePaths

#fix_relative_filepath

Constructor Details

#initialize(lcov_io, opts) ⇒ LcovParser

Returns a new instance of LcovParser.



14
15
16
17
18
# File 'lib/undercover/lcov_parser.rb', line 14

def initialize(lcov_io, opts)
  @io = lcov_io
  @source_files = {}
  @code_dir = opts&.path
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



12
13
14
# File 'lib/undercover/lcov_parser.rb', line 12

def io
  @io
end

#source_filesObject (readonly)

Returns the value of attribute source_files.



12
13
14
# File 'lib/undercover/lcov_parser.rb', line 12

def source_files
  @source_files
end

Class Method Details

.parse(lcov_report_path, opts = nil) ⇒ Object



20
21
22
23
# File 'lib/undercover/lcov_parser.rb', line 20

def self.parse(lcov_report_path, opts = nil)
  lcov_io = File.open(lcov_report_path)
  new(lcov_io, opts).parse
end

Instance Method Details

#branch_label(_filepath, _branch_no) ⇒ Object



61
62
63
# File 'lib/undercover/lcov_parser.rb', line 61

def branch_label(_filepath, _branch_no)
  nil
end

#coverage(filepath) ⇒ Object



31
32
33
34
35
36
# File 'lib/undercover/lcov_parser.rb', line 31

def coverage(filepath)
  _filename, coverage = source_files.find do |relative_path, _|
    relative_path == fix_relative_filepath(filepath)
  end
  coverage || []
end

#ignored_filesObject



65
66
67
68
# File 'lib/undercover/lcov_parser.rb', line 65

def ignored_files
  # supported by SimplecovResultAdapter only
  []
end

#parseObject



25
26
27
28
29
# File 'lib/undercover/lcov_parser.rb', line 25

def parse
  io.each(&method(:parse_line))
  io.close
  self
end

#skipped?(_filepath, _line_no) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/undercover/lcov_parser.rb', line 56

def skipped?(_filepath, _line_no)
  # this is why lcov parser will be deprecated
  false
end

#total_branch_coverageObject



47
48
49
50
51
52
53
54
# File 'lib/undercover/lcov_parser.rb', line 47

def total_branch_coverage
  all_lines = source_files.values.flatten(1)
  return 0 if all_lines.empty?

  all_branches = all_lines.select { _1.size == 4 }
  total_f = all_branches.select { |_l_no, _block_no, _br_no, hits| hits.positive? }.size.to_f / all_branches.size
  total_f.round(3)
end

#total_coverageObject



38
39
40
41
42
43
44
45
# File 'lib/undercover/lcov_parser.rb', line 38

def total_coverage
  all_lines = source_files.values.flatten(1)
  return 0 if all_lines.empty?

  all_lines = all_lines.select { _1.size == 2 }
  total_f = all_lines.select { |_line_no, hits| hits.positive? }.size.to_f / all_lines.size
  total_f.round(3)
end