Add eval: true/false flag to Coverage.setup.

This commit is contained in:
Samuel Williams 2022-09-28 23:35:42 +13:00
parent ac56e5c1ab
commit 9dd902b831
No known key found for this signature in database
GPG Key ID: A0765423A44728FB
4 changed files with 21 additions and 17 deletions

View File

@ -27,7 +27,7 @@ static VALUE me2counter = Qnil;
* call-seq: * call-seq:
* Coverage.setup => nil * Coverage.setup => nil
* Coverage.setup(:all) => nil * Coverage.setup(:all) => nil
* Coverage.setup(lines: bool, branches: bool, methods: bool) => nil * Coverage.setup(lines: bool, branches: bool, methods: bool, eval: bool) => nil
* Coverage.setup(oneshot_lines: true) => nil * Coverage.setup(oneshot_lines: true) => nil
* *
* Set up the coverage measurement. * Set up the coverage measurement.
@ -53,7 +53,7 @@ rb_coverage_setup(int argc, VALUE *argv, VALUE klass)
mode = 0; /* compatible mode */ mode = 0; /* compatible mode */
} }
else if (opt == ID2SYM(rb_intern("all"))) { else if (opt == ID2SYM(rb_intern("all"))) {
mode = COVERAGE_TARGET_LINES | COVERAGE_TARGET_BRANCHES | COVERAGE_TARGET_METHODS; mode = COVERAGE_TARGET_LINES | COVERAGE_TARGET_BRANCHES | COVERAGE_TARGET_METHODS | COVERAGE_TARGET_EVAL;
} }
else { else {
mode = 0; mode = 0;
@ -71,6 +71,8 @@ rb_coverage_setup(int argc, VALUE *argv, VALUE klass)
mode |= COVERAGE_TARGET_LINES; mode |= COVERAGE_TARGET_LINES;
mode |= COVERAGE_TARGET_ONESHOT_LINES; mode |= COVERAGE_TARGET_ONESHOT_LINES;
} }
if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("eval")))))
mode |= COVERAGE_TARGET_EVAL;
} }
if (mode & COVERAGE_TARGET_METHODS) { if (mode & COVERAGE_TARGET_METHODS) {
@ -93,7 +95,6 @@ rb_coverage_setup(int argc, VALUE *argv, VALUE klass)
rb_raise(rb_eRuntimeError, "cannot change the measuring target during coverage measurement"); rb_raise(rb_eRuntimeError, "cannot change the measuring target during coverage measurement");
} }
return Qnil; return Qnil;
} }
@ -126,7 +127,7 @@ rb_coverage_resume(VALUE klass)
* call-seq: * call-seq:
* Coverage.start => nil * Coverage.start => nil
* Coverage.start(:all) => nil * Coverage.start(:all) => nil
* Coverage.start(lines: bool, branches: bool, methods: bool) => nil * Coverage.start(lines: bool, branches: bool, methods: bool, eval: bool) => nil
* Coverage.start(oneshot_lines: true) => nil * Coverage.start(oneshot_lines: true) => nil
* *
* Enables the coverage measurement. * Enables the coverage measurement.

View File

@ -20,6 +20,7 @@ struct rb_thread_struct; /* in vm_core.h */
#define COVERAGE_TARGET_BRANCHES 2 #define COVERAGE_TARGET_BRANCHES 2
#define COVERAGE_TARGET_METHODS 4 #define COVERAGE_TARGET_METHODS 4
#define COVERAGE_TARGET_ONESHOT_LINES 8 #define COVERAGE_TARGET_ONESHOT_LINES 8
#define COVERAGE_TARGET_EVAL 16
VALUE rb_obj_is_mutex(VALUE obj); VALUE rb_obj_is_mutex(VALUE obj);
VALUE rb_suppress_tracing(VALUE (*func)(VALUE), VALUE arg); VALUE rb_suppress_tracing(VALUE (*func)(VALUE), VALUE arg);

2
iseq.c
View File

@ -929,10 +929,12 @@ rb_iseq_new_main(const rb_ast_body_t *ast, VALUE path, VALUE realpath, const rb_
rb_iseq_t * rb_iseq_t *
rb_iseq_new_eval(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_iseq_t *parent, int isolated_depth) rb_iseq_new_eval(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_iseq_t *parent, int isolated_depth)
{ {
if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) {
VALUE coverages = rb_get_coverages(); VALUE coverages = rb_get_coverages();
if (RTEST(coverages) && RTEST(path) && !RTEST(rb_hash_has_key(coverages, path))) { if (RTEST(coverages) && RTEST(path) && !RTEST(rb_hash_has_key(coverages, path))) {
iseq_setup_coverage(coverages, path, ast, first_lineno - 1); iseq_setup_coverage(coverages, path, ast, first_lineno - 1);
} }
}
return rb_iseq_new_with_opt(ast, name, path, realpath, first_lineno, return rb_iseq_new_with_opt(ast, name, path, realpath, first_lineno,
parent, isolated_depth, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT); parent, isolated_depth, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT);

View File

@ -147,11 +147,11 @@ class TestCoverage < Test::Unit::TestCase
end end
assert_in_out_err(%w[-W0 -rcoverage], <<-"end;", ["[1, 1, 1, 400, nil, nil, nil, nil, nil, nil, nil]"], [], bug13305) assert_in_out_err(%w[-W0 -rcoverage], <<-"end;", ["[1, 1, 1, 400, nil, nil, nil, nil, nil, nil, nil]"], [], bug13305)
Coverage.start Coverage.start(:all)
tmp = Dir.pwd tmp = Dir.pwd
require tmp + '/test.rb' require tmp + '/test.rb'
add_method(Class.new) add_method(Class.new)
p Coverage.result[tmp + "/test.rb"] p Coverage.result[tmp + "/test.rb"][:lines]
end; end;
} }
} }
@ -159,7 +159,7 @@ class TestCoverage < Test::Unit::TestCase
def test_eval_coverage def test_eval_coverage
assert_in_out_err(%w[-rcoverage], <<-"end;", ["[1, nil, 1, nil]"], []) assert_in_out_err(%w[-rcoverage], <<-"end;", ["[1, nil, 1, nil]"], [])
Coverage.start Coverage.start(eval: true, lines: true)
eval(<<-RUBY, TOPLEVEL_BINDING, "test.rb") eval(<<-RUBY, TOPLEVEL_BINDING, "test.rb")
s = String.new s = String.new
@ -168,7 +168,7 @@ class TestCoverage < Test::Unit::TestCase
bar".freeze; end bar".freeze; end
RUBY RUBY
p Coverage.result["test.rb"] p Coverage.result["test.rb"][:lines]
end; end;
end end