From ea8b916307b739798a1710c4443677c5e0bf314a Mon Sep 17 00:00:00 2001 From: nagachika Date: Mon, 4 Jul 2011 15:55:06 +0000 Subject: [PATCH] * ext/coverage/coverage.c: resurrect r32071 + add GC guard for rb_coverages. [ruby-core:37352] [Bug #4927] [ruby-core:36539] [Feature #4796] * test/coverage/test_coverage.rb resurrect r32071. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32404 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++++++ ext/coverage/coverage.c | 27 ++++++++++++++++++--------- test/coverage/test_coverage.rb | 25 +++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index c4f896c53f..be9edbb58d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Tue Jul 5 00:49:05 2011 CHIKANAGA Tomoyuki + + * ext/coverage/coverage.c: resurrect r32071 + add GC guard for + rb_coverages. [ruby-core:37352] [Bug #4927] + [ruby-core:36539] [Feature #4796] + + * test/coverage/test_coverage.rb resurrect r32071. + Mon Jul 4 22:24:46 2011 KOSAKI Motohiro * thread_pthread.c (get_stack): For NetBSD/FreeBSD, use diff --git a/ext/coverage/coverage.c b/ext/coverage/coverage.c index 29ac7090f3..529fac256a 100644 --- a/ext/coverage/coverage.c +++ b/ext/coverage/coverage.c @@ -11,6 +11,8 @@ #include "ruby.h" #include "vm_core.h" +static VALUE rb_coverages = Qundef; + /* * call-seq: * Coverage.start => nil @@ -21,19 +23,25 @@ static VALUE rb_coverage_start(VALUE klass) { if (!RTEST(rb_get_coverages())) { - VALUE coverages = rb_hash_new(); - RBASIC(coverages)->klass = 0; - rb_set_coverages(coverages); + if (rb_coverages == Qundef) { + rb_coverages = rb_hash_new(); + RBASIC(rb_coverages)->klass = 0; + } + rb_set_coverages(rb_coverages); } return Qnil; } static int -coverage_result_i(st_data_t key, st_data_t val, st_data_t dummy) +coverage_result_i(st_data_t key, st_data_t val, st_data_t h) { + VALUE path = (VALUE)key; VALUE coverage = (VALUE)val; - RBASIC(coverage)->klass = rb_cArray; + VALUE coverages = (VALUE)h; + coverage = rb_ary_dup(coverage); + rb_ary_clear((VALUE)val); rb_ary_freeze(coverage); + rb_hash_aset(coverages, path, coverage); return ST_CONTINUE; } @@ -48,14 +56,14 @@ static VALUE rb_coverage_result(VALUE klass) { VALUE coverages = rb_get_coverages(); + VALUE ncoverages = rb_hash_new(); if (!RTEST(coverages)) { rb_raise(rb_eRuntimeError, "coverage measurement is not enabled"); } - RBASIC(coverages)->klass = rb_cHash; - st_foreach(RHASH_TBL(coverages), coverage_result_i, 0); - rb_hash_freeze(coverages); + st_foreach(RHASH_TBL(coverages), coverage_result_i, ncoverages); + rb_hash_freeze(ncoverages); rb_reset_coverages(); - return coverages; + return ncoverages; } /* Coverage provides coverage measurement feature for Ruby. @@ -95,4 +103,5 @@ Init_coverage(void) VALUE rb_mCoverage = rb_define_module("Coverage"); rb_define_module_function(rb_mCoverage, "start", rb_coverage_start, 0); rb_define_module_function(rb_mCoverage, "result", rb_coverage_result, 0); + rb_gc_register_address(&rb_coverages); } diff --git a/test/coverage/test_coverage.rb b/test/coverage/test_coverage.rb index d5091f3d15..56966b1f0e 100644 --- a/test/coverage/test_coverage.rb +++ b/test/coverage/test_coverage.rb @@ -15,4 +15,29 @@ class TestCoverage < Test::Unit::TestCase assert_kind_of(Array, val) end end + + def test_restarting_coverage + loaded_features = $".dup + + Dir.mktmpdir {|tmp| + Dir.chdir(tmp) { + File.open("test.rb", "w") do |f| + f.puts <<-EOS + def coverage_test_method + :ok + end + EOS + end + + Coverage.start + require tmp + '/test.rb' + Coverage.result + Coverage.start + coverage_test_method + assert_equal 1, Coverage.result.size + } + } + ensure + $".replace loaded_features + end end