summaryrefslogtreecommitdiff
path: root/tool/run-lcov.rb
blob: 47f4591463cdb84cb4cb4ed0552e54197ec26a79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!ruby
require "pathname"
require "open3"

def backup_gcda_files(gcda_files)
  gcda_files = gcda_files.map do |gcda|
    [gcda, gcda.sub_ext(".bak")]
  end
  begin
    gcda_files.each do |before, after|
      before.rename(after)
    end
    yield
  ensure
    gcda_files.each do |before, after|
      after.rename(before)
    end
  end
end

$info_files = []
def run_lcov(dir, info)
  $info_files << info
  system("lcov", "-c", "-d", dir, "--rc", "lcov_branch_coverage=1", "-o", info)
end

def gen_rb_lcov(file)
  res = Marshal.load(File.binread(file))

  open("lcov-rb-all.info", "w") do |f|
    f.puts "TN:" # no test name
    base_dir = File.dirname(__dir__)
    res.each do |path, cov|
      next unless path.start_with?(base_dir)
      next if path.start_with?(File.join(base_dir, "test"))
      f.puts "SF:#{ path }"

      total = covered = 0
      cov.each_with_index do |count, lineno|
        next unless count
        f.puts "DA:#{ lineno + 1 },#{ count }"
        total += 1
        covered += 1 if count > 0
      end
      f.puts "LF:#{ total }"
      f.puts "LH:#{ covered }"

      f.puts "end_of_record"
    end
  end
end

def gen_rb_lcov(file)
  res = Marshal.load(File.binread(file))

  open("lcov-rb-all.info", "w") do |f|
    f.puts "TN:" # no test name
    base_dir = File.dirname(File.dirname(__dir__))
    res.each do |path, cov|
      next unless path.start_with?(base_dir)
      next if path.start_with?(File.join(base_dir, "test"))
      f.puts "SF:#{ path }"

      # function coverage
      total = covered = 0
      cov[:methods].each do |(name, lineno), count|
        f.puts "FN:#{ lineno },#{ name }"
        total += 1
        covered += 1 if count > 0
      end
      f.puts "FNF:#{ total }"
      f.puts "FNF:#{ covered }"
      cov[:methods].each do |(name, _), count|
        f.puts "FNDA:#{ count },#{ name }"
      end

      # line coverage
      total = covered = 0
      cov[:lines].each_with_index do |count, lineno|
        next unless count
        f.puts "DA:#{ lineno + 1 },#{ count }"
        total += 1
        covered += 1 if count > 0
      end
      f.puts "LF:#{ total }"
      f.puts "LH:#{ covered }"

      # branch coverage
      total = covered = 0
      id = 0
      cov[:branches].each do |(base_type, base_lineno), targets|
        i = 0
        targets.each do |(target_type, target_lineno), count|
          f.puts "BRDA:#{ base_lineno },#{ id },#{ i },#{ count }"
          total += 1
          covered += 1 if count > 0
          i += 1
        end
        id += 1
      end
      f.puts "BRF:#{ total }"
      f.puts "BRH:#{ covered }"
      f.puts "end_of_record"
    end
  end
end

gcda_files = Pathname.glob("**/*.gcda")
ext_gcda_files = gcda_files.select {|f| f.fnmatch("ext/*") }
rubyspec_temp_gcda_files = gcda_files.select {|f| f.fnmatch("rubyspec_temp/*") }

backup_gcda_files(rubyspec_temp_gcda_files) do
  if ext_gcda_files != []
    backup_gcda_files(ext_gcda_files) do
      info = "lcov-root.info"
      run_lcov(".", info)
    end
  end
  ext_gcda_files.group_by {|f| f.descend.to_a[1] }.each do |key, files|
    info = "lcov-#{ key.to_s.gsub(File::Separator, "-") }.info"
    run_lcov(key.to_s, info)
  end
end
if $info_files != []
  system("lcov", *$info_files.flat_map {|f| ["-a", f] }, "--rc", "lcov_branch_coverage=1", "-o", "lcov-c-all.info")
  system("genhtml", "--branch-coverage", "--ignore-errors", "source", "lcov-c-all.info", "-o", "lcov-c-out")
end

if File.readable?("test-coverage.dat")
  gen_rb_lcov("test-coverage.dat")
  system("genhtml", "--branch-coverage", "--ignore-errors", "source", "lcov-rb-all.info", "-o", "lcov-rb-out")
end

if File.readable?("lcov-c-all.info") && File.readable?("lcov-rb-all.info")
  system("lcov", "-a", "lcov-c-all.info", "-a", "lcov-rb-all.info", "--rc", "lcov_branch_coverage=1", "-o", "lcov-all.info") || raise
  system("genhtml", "--branch-coverage", "--ignore-errors", "source", "lcov-all.info", "-o", "lcov-out")
end