mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
f08581c7b6
COVERAGE__ and introduce coverage.so instead. How to measure coverage: (1) require "coverage.so", (2) require or load Ruby source file, and (3) Coverage.result will return the same hash as COVERAGE__. [ruby-dev:35324] * thread.c (rb_enable_coverages): start coverage measurement by using rb_add_event_hook. * thread.c (rb_get_coverages): returns current results of coverage measurement. * include/ruby/intern.h: add prototype for above two functions. * vm_core.h, vm.c: add field of coverages to rb_vm_t. * insns.def (trace): remove special handling for COVERAGE__. * iseq.c (prepare_iseq_build): switch COVERAGE__ to rb_get_coverages(). * parse.y (coverage): ditto. * thread.c (clear_coverage): ditto. * lib/coverage.rb: use coverage.so instead of COVERAGE__. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17857 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
58 lines
1.6 KiB
Ruby
58 lines
1.6 KiB
Ruby
require "coverage.so"
|
|
|
|
ext = ENV["COVERUBY_EXT"] || ".cov"
|
|
accum = ENV["COVERUBY_ACCUM"]
|
|
accum = !accum || accum == "" || !(%w(f n 0).include?(accum[0]))
|
|
pwd = Dir.pwd
|
|
|
|
at_exit do
|
|
Dir.chdir(pwd) do
|
|
Coverage.result.each do |sfile, covs|
|
|
cfile = sfile + ext
|
|
|
|
writable = proc do |f|
|
|
File.writable?(f) || File.writable?(File.dirname(f))
|
|
end
|
|
unless writable[cfile]
|
|
cfile = cfile.gsub(File.PATH_SEPARATOR, "#")
|
|
next unless writable[cfile]
|
|
end
|
|
|
|
readlines = proc do |f|
|
|
File.read(f).force_encoding("ASCII-8BIT").lines.to_a
|
|
end
|
|
|
|
sources = (readlines[sfile] rescue [])
|
|
|
|
pcovs = []
|
|
if accum
|
|
pcovs = (readlines[cfile] rescue []).map.with_index do |line, idx|
|
|
if line[/^\s*(?:(#####)|(\d+)|-):\s*\d+:(.*)$/n]
|
|
cov, line = $1 ? 0 : ($2 ? $2.to_i : nil), $3
|
|
if !sources[idx] || sources[idx].chomp != line.chomp
|
|
warn("source file changed, ignoring: `#{ cfile }'")
|
|
break []
|
|
end
|
|
cov
|
|
else
|
|
p line
|
|
warn("coverage file corrupted, ignoring: #{ cfile }")
|
|
break []
|
|
end
|
|
end
|
|
unless pcovs.empty? || pcovs.size == covs.size
|
|
warn("coverage file changed, ignoring: `#{ cfile }'")
|
|
pcovs = []
|
|
end
|
|
end
|
|
|
|
open(cfile, "w") do |out|
|
|
covs.zip(sources, pcovs).each_with_index do |(cov, line, pcov), idx|
|
|
cov += pcov || 0 if cov
|
|
cov = (cov ? (cov == 0 ? "#####" : cov.to_s) : "-").rjust(9)
|
|
out.puts("%s:% 5d:%s" % [cov, idx + 1, line])
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|