summaryrefslogtreecommitdiffstats
path: root/sample/coverage.rb
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-07-24 17:47:34 +0000
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-07-24 17:47:34 +0000
commit8355df5d968862ba992fef1454cc178f160fa3ab (patch)
tree14cd40f162860c70fb520b161b822669ba2d4ad3 /sample/coverage.rb
parent6ba1b2c00f0dd12fea24d13b6a801b0aadd10826 (diff)
downloadruby-8355df5d968862ba992fef1454cc178f160fa3ab.tar.gz
ruby-8355df5d968862ba992fef1454cc178f160fa3ab.tar.xz
ruby-8355df5d968862ba992fef1454cc178f160fa3ab.zip
* sample/coverage.rb: move from lib/coverage.rb because this remains in
an early phase of development. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@18200 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sample/coverage.rb')
-rw-r--r--sample/coverage.rb60
1 files changed, 60 insertions, 0 deletions
diff --git a/sample/coverage.rb b/sample/coverage.rb
new file mode 100644
index 000000000..3f45e9fc9
--- /dev/null
+++ b/sample/coverage.rb
@@ -0,0 +1,60 @@
+require "coverage.so"
+
+Coverage.start
+
+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