6 def backup_gcda_files(gcda_files)
7 gcda_files = gcda_files.map do |gcda|
8 [gcda, gcda.sub_ext(".bak")]
11 gcda_files.each do |before, after|
16 gcda_files.each do |before, after|
23 system("lcov", "--rc", "lcov_branch_coverage=1", *args)
27 def run_lcov_capture(dir, info)
29 run_lcov("--capture", "-d", dir, "-o", info)
32 def run_lcov_merge(files, info)
33 run_lcov(*files.flat_map {|f| ["--add-tracefile", f] }, "-o", info)
36 def run_lcov_remove(info_src, info_out)
38 dirs << File.join(Dir.tmpdir, "*")
42 ext/nkf/nkf-utf8/nkf.c
43 ).each {|f| dirs << File.join(File.dirname(__dir__), f) }
44 run_lcov("--remove", info_src, *dirs, "-o", info_out)
47 def run_genhtml(info, out)
48 system("genhtml", "--branch-coverage", "--ignore-errors", "source", info, "-o", out)
52 res = Marshal.load(File.binread(file))
54 open("lcov-rb-all.info", "w") do |f|
55 f.puts "TN:" # no test name
56 base_dir = File.dirname(__dir__)
57 res.each do |path, cov|
58 next unless path.start_with?(base_dir)
59 next if path.start_with?(File.join(base_dir, "test"))
63 cov.each_with_index do |count, lineno|
65 f.puts "DA:#{ lineno + 1 },#{ count }"
67 covered += 1 if count > 0
69 f.puts "LF:#{ total }"
70 f.puts "LH:#{ covered }"
72 f.puts "end_of_record"
78 res = Marshal.load(File.binread(file))
80 open("lcov-rb-all.info", "w") do |f|
81 f.puts "TN:" # no test name
82 base_dir = File.dirname(File.dirname(__dir__))
83 res.each do |path, cov|
84 next unless path.start_with?(base_dir)
85 next if path.start_with?(File.join(base_dir, "test"))
90 cov[:methods].each do |(klass, name, lineno), count|
91 f.puts "FN:#{ lineno },#{ klass }##{ name }"
93 covered += 1 if count > 0
95 f.puts "FNF:#{ total }"
96 f.puts "FNF:#{ covered }"
97 cov[:methods].each do |(klass, name, _), count|
98 f.puts "FNDA:#{ count },#{ klass }##{ name }"
103 cov[:lines].each_with_index do |count, lineno|
105 f.puts "DA:#{ lineno + 1 },#{ count }"
107 covered += 1 if count > 0
109 f.puts "LF:#{ total }"
110 f.puts "LH:#{ covered }"
115 cov[:branches].each do |(_base_type, _, base_lineno), targets|
117 targets.each do |(_target_type, _target_lineno), count|
118 f.puts "BRDA:#{ base_lineno },#{ id },#{ i },#{ count }"
120 covered += 1 if count > 0
125 f.puts "BRF:#{ total }"
126 f.puts "BRH:#{ covered }"
127 f.puts "end_of_record"
132 gcda_files = Pathname.glob("**/*.gcda")
133 ext_gcda_files = gcda_files.select {|f| f.fnmatch("ext/*") }
134 rubyspec_temp_gcda_files = gcda_files.select {|f| f.fnmatch("rubyspec_temp/*") }
136 backup_gcda_files(rubyspec_temp_gcda_files) do
137 if ext_gcda_files != []
138 backup_gcda_files(ext_gcda_files) do
139 info = "lcov-root.info"
140 run_lcov_capture(".", info)
143 ext_gcda_files.group_by {|f| f.descend.to_a[1] }.each do |key, files|
144 info = "lcov-#{ key.to_s.gsub(File::Separator, "-") }.info"
145 run_lcov_capture(key.to_s, info)
149 run_lcov_merge($info_files, "lcov-c-all.info")
150 run_lcov_remove("lcov-c-all.info", "lcov-c-all-filtered.info")
151 run_genhtml("lcov-c-all-filtered.info", "lcov-c-out")
154 if File.readable?("test-coverage.dat")
155 gen_rb_lcov("test-coverage.dat")
156 run_lcov_remove("lcov-rb-all.info", "lcov-rb-all-filtered.info")
157 run_genhtml("lcov-rb-all-filtered.info", "lcov-rb-out")
160 if File.readable?("lcov-c-all.info") && File.readable?("lcov-rb-all.info")
161 run_lcov_merge(%w(lcov-c-all.info lcov-rb-all.info), "lcov-all.info")
162 run_lcov_remove("lcov-all.info", "lcov-all-filtered.info")
163 run_genhtml("lcov-all-filtered.info", "lcov-out")