Project

General

Profile

Bug #12220 ยป 0001-ext-coverage-coverage.c-Fully-reset-coverage-to-not-.patch

Eregon (Benoit Daloze), 03/31/2016 04:46 PM

View differences:

ChangeLog
Fri Apr 1 01:26:00 2016 Benoit Daloze <eregontp@gmail.com>
* ext/coverage/coverage.c: Fully reset coverage to not persist global state.
It was returning old file coverages as empty arrays to the user.
[ruby-core:74596] [Bug #12220]
* ext/coverage/coverage.c (rb_coverages): remove unused static state.
* thread.c: Moved and renamed coverage_clear_result_i to reset_coverage_i.
* test/coverage/test_coverage.rb: improve precision of tests.
Fri Apr 1 01:13:55 2016 Benoit Daloze <eregontp@gmail.com>
* thread.c (update_coverage): Do not track coverage in loaded files
ext/coverage/coverage.c
#include "ruby.h"
#include "vm_core.h"
static VALUE rb_coverages = Qundef;
/*
* call-seq:
* Coverage.start => nil
......
static VALUE
rb_coverage_start(VALUE klass)
{
if (!RTEST(rb_get_coverages())) {
if (rb_coverages == Qundef) {
rb_coverages = rb_hash_new();
rb_obj_hide(rb_coverages);
}
rb_set_coverages(rb_coverages);
VALUE coverages = rb_get_coverages();
if (!RTEST(coverages)) {
coverages = rb_hash_new();
rb_obj_hide(coverages);
rb_set_coverages(coverages);
}
return Qnil;
}
/* Make coverage arrays empty so old covered files are no longer tracked. */
static int
coverage_clear_result_i(st_data_t key, st_data_t val, st_data_t h)
{
VALUE coverage = (VALUE)val;
rb_ary_clear(coverage);
return ST_CONTINUE;
}
static int
coverage_peek_result_i(st_data_t key, st_data_t val, st_data_t h)
{
......
rb_coverage_result(VALUE klass)
{
VALUE ncoverages = rb_coverage_peek_result(klass);
VALUE coverages = rb_get_coverages();
st_foreach(RHASH_TBL(coverages), coverage_clear_result_i, ncoverages);
rb_reset_coverages();
return ncoverages;
}
......
rb_define_module_function(rb_mCoverage, "start", rb_coverage_start, 0);
rb_define_module_function(rb_mCoverage, "result", rb_coverage_result, 0);
rb_define_module_function(rb_mCoverage, "peek_result", rb_coverage_peek_result, 0);
rb_gc_register_address(&rb_coverages);
}
test/coverage/test_coverage.rb
File.open("test2.rb", "w") do |f|
f.puts <<-EOS
def coverage_test_method2
:ok
:ok
end
1 + 2
EOS
end
Coverage.start
require tmp + '/test.rb'
assert_equal 3, Coverage.result[tmp + '/test.rb'].size
cov = { "#{tmp}/test.rb" => [1, 0, nil] }
assert_equal cov, Coverage.result
# Restart coverage but '/test.rb' is required before restart,
# so coverage is not recorded.
Coverage.start
coverage_test_method
assert_equal 0, Coverage.result[tmp + '/test.rb'].size
assert_equal({}, Coverage.result)
# Restart coverage and '/test2.rb' is required after restart,
# so coverage is recorded.
Coverage.start
require tmp + '/test2.rb'
assert_equal 4, Coverage.result[tmp + '/test2.rb'].size
cov = { "#{tmp}/test2.rb" => [1] }
assert_equal cov, Coverage.result
}
}
ensure
thread.c
rb_add_event_hook(update_coverage, RUBY_EVENT_COVERAGE, Qnil);
}
/* Make coverage arrays empty so old covered files are no longer tracked. */
static int
reset_coverage_i(st_data_t key, st_data_t val, st_data_t dummy)
{
VALUE coverage = (VALUE)val;
rb_ary_clear(coverage);
return ST_CONTINUE;
}
void
rb_reset_coverages(void)
{
VALUE coverages = rb_get_coverages();
st_foreach(rb_hash_tbl_raw(coverages), reset_coverage_i, 0);
GET_VM()->coverages = Qfalse;
rb_remove_event_hook(update_coverage);
}
    (1-1/1)