summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Williams <samuel.williams@oriontransfer.co.nz>2022-09-28 23:35:42 +1300
committerSamuel Williams <samuel.williams@oriontransfer.co.nz>2022-09-29 09:44:14 +1300
commit9dd902b83186ad6f9d0a553da2ca114bac6ab7b5 (patch)
tree1567bf2f43b4cb1306409b5d25aecbde1fe2c90c
parentac56e5c1ab6eb133102c395f63101060bad6725d (diff)
Add `eval: true/false` flag to `Coverage.setup`.
-rw-r--r--ext/coverage/coverage.c21
-rw-r--r--internal/thread.h1
-rw-r--r--iseq.c8
-rw-r--r--test/coverage/test_coverage.rb8
4 files changed, 21 insertions, 17 deletions
diff --git a/ext/coverage/coverage.c b/ext/coverage/coverage.c
index 2f760c8eb6..6978bb24cb 100644
--- a/ext/coverage/coverage.c
+++ b/ext/coverage/coverage.c
@@ -25,10 +25,10 @@ static VALUE me2counter = Qnil;
/*
* call-seq:
- * Coverage.setup => nil
- * Coverage.setup(:all) => nil
- * Coverage.setup(lines: bool, branches: bool, methods: bool) => nil
- * Coverage.setup(oneshot_lines: true) => nil
+ * Coverage.setup => nil
+ * Coverage.setup(:all) => nil
+ * Coverage.setup(lines: bool, branches: bool, methods: bool, eval: bool) => nil
+ * Coverage.setup(oneshot_lines: true) => nil
*
* Set up the coverage measurement.
*
@@ -53,7 +53,7 @@ rb_coverage_setup(int argc, VALUE *argv, VALUE klass)
mode = 0; /* compatible mode */
}
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 {
mode = 0;
@@ -71,6 +71,8 @@ rb_coverage_setup(int argc, VALUE *argv, VALUE klass)
mode |= COVERAGE_TARGET_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) {
@@ -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");
}
-
return Qnil;
}
@@ -124,10 +125,10 @@ rb_coverage_resume(VALUE klass)
/*
* call-seq:
- * Coverage.start => nil
- * Coverage.start(:all) => nil
- * Coverage.start(lines: bool, branches: bool, methods: bool) => nil
- * Coverage.start(oneshot_lines: true) => nil
+ * Coverage.start => nil
+ * Coverage.start(:all) => nil
+ * Coverage.start(lines: bool, branches: bool, methods: bool, eval: bool) => nil
+ * Coverage.start(oneshot_lines: true) => nil
*
* Enables the coverage measurement.
* See the documentation of Coverage class in detail.
diff --git a/internal/thread.h b/internal/thread.h
index 63f03bb94a..6394f88d34 100644
--- a/internal/thread.h
+++ b/internal/thread.h
@@ -20,6 +20,7 @@ struct rb_thread_struct; /* in vm_core.h */
#define COVERAGE_TARGET_BRANCHES 2
#define COVERAGE_TARGET_METHODS 4
#define COVERAGE_TARGET_ONESHOT_LINES 8
+#define COVERAGE_TARGET_EVAL 16
VALUE rb_obj_is_mutex(VALUE obj);
VALUE rb_suppress_tracing(VALUE (*func)(VALUE), VALUE arg);
diff --git a/iseq.c b/iseq.c
index 0d4e014999..5b1d9de106 100644
--- a/iseq.c
+++ b/iseq.c
@@ -929,9 +929,11 @@ rb_iseq_new_main(const rb_ast_body_t *ast, VALUE path, VALUE realpath, const rb_
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)
{
- VALUE coverages = rb_get_coverages();
- if (RTEST(coverages) && RTEST(path) && !RTEST(rb_hash_has_key(coverages, path))) {
- iseq_setup_coverage(coverages, path, ast, first_lineno - 1);
+ if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) {
+ VALUE coverages = rb_get_coverages();
+ if (RTEST(coverages) && RTEST(path) && !RTEST(rb_hash_has_key(coverages, path))) {
+ iseq_setup_coverage(coverages, path, ast, first_lineno - 1);
+ }
}
return rb_iseq_new_with_opt(ast, name, path, realpath, first_lineno,
diff --git a/test/coverage/test_coverage.rb b/test/coverage/test_coverage.rb
index 1bb85b67b3..f9f9b2c04d 100644
--- a/test/coverage/test_coverage.rb
+++ b/test/coverage/test_coverage.rb
@@ -147,11 +147,11 @@ class TestCoverage < Test::Unit::TestCase
end
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
require tmp + '/test.rb'
add_method(Class.new)
- p Coverage.result[tmp + "/test.rb"]
+ p Coverage.result[tmp + "/test.rb"][:lines]
end;
}
}
@@ -159,7 +159,7 @@ class TestCoverage < Test::Unit::TestCase
def test_eval_coverage
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")
s = String.new
@@ -168,7 +168,7 @@ class TestCoverage < Test::Unit::TestCase
bar".freeze; end
RUBY
- p Coverage.result["test.rb"]
+ p Coverage.result["test.rb"][:lines]
end;
end