From fee5b8f263211faef10ed9f3e43c1e8b34548bbd Mon Sep 17 00:00:00 2001 From: "NARUSE, Yui" Date: Wed, 25 Jan 2023 16:34:24 +0900 Subject: merge revision(s) 2c93c554019ebdc394d3c51c6d925620d3005f84,f5ea43a2e61789357e9c4b374b4bc6756abeae17: [Backport #19360] Ensure main file has default coverage if required. (#7169) * Extract common code for coverage setup. --- iseq.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) More coverage tests & specs. (#7171) * Add spec for eval and line coverage. * Add test for main file coverage. --- spec/ruby/library/coverage/start_spec.rb | 8 +++++++- test/coverage/autostart.rb | 2 ++ test/coverage/main.rb | 1 + test/coverage/test_coverage.rb | 7 +++++++ 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 test/coverage/autostart.rb create mode 100644 test/coverage/main.rb --- iseq.c | 13 +++++++++++-- spec/ruby/library/coverage/start_spec.rb | 8 +++++++- test/coverage/autostart.rb | 2 ++ test/coverage/main.rb | 1 + test/coverage/test_coverage.rb | 7 +++++++ version.h | 2 +- 6 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 test/coverage/autostart.rb create mode 100644 test/coverage/main.rb diff --git a/iseq.c b/iseq.c index 869ed4da6a..a6ba5c12bb 100644 --- a/iseq.c +++ b/iseq.c @@ -917,13 +917,20 @@ iseq_setup_coverage(VALUE coverages, VALUE path, const rb_ast_body_t *ast, int l return Qnil; } -rb_iseq_t * -rb_iseq_new_top(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent) +static inline void +iseq_new_setup_coverage(VALUE path, const rb_ast_body_t *ast, int line_offset) { VALUE coverages = rb_get_coverages(); + if (RTEST(coverages)) { iseq_setup_coverage(coverages, path, ast, 0); } +} + +rb_iseq_t * +rb_iseq_new_top(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent) +{ + iseq_new_setup_coverage(path, ast, 0); return rb_iseq_new_with_opt(ast, name, path, realpath, 0, parent, 0, ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT); @@ -932,6 +939,8 @@ rb_iseq_new_top(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath rb_iseq_t * rb_iseq_new_main(const rb_ast_body_t *ast, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt) { + iseq_new_setup_coverage(path, ast, 0); + return rb_iseq_new_with_opt(ast, rb_fstring_lit("
"), path, realpath, 0, parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE); diff --git a/spec/ruby/library/coverage/start_spec.rb b/spec/ruby/library/coverage/start_spec.rb index ddfad8b47c..a993abbf4e 100644 --- a/spec/ruby/library/coverage/start_spec.rb +++ b/spec/ruby/library/coverage/start_spec.rb @@ -2,5 +2,11 @@ require_relative '../../spec_helper' require 'coverage' describe 'Coverage.start' do - it 'needs to be reviewed for spec completeness' + ruby_version_is '3.2' do + it "can measure coverage within eval" do + Coverage.start(lines: true, eval: true) + eval("Object.new\n"*3, binding, "test.rb", 1) + Coverage.result["test.rb"].should == {lines: [1, 1, 1]} + end + end end diff --git a/test/coverage/autostart.rb b/test/coverage/autostart.rb new file mode 100644 index 0000000000..067fe0a40d --- /dev/null +++ b/test/coverage/autostart.rb @@ -0,0 +1,2 @@ +require 'coverage' +Coverage.start(:all) diff --git a/test/coverage/main.rb b/test/coverage/main.rb new file mode 100644 index 0000000000..74f1e95fa3 --- /dev/null +++ b/test/coverage/main.rb @@ -0,0 +1 @@ +puts Coverage.peek_result[__FILE__][:lines] diff --git a/test/coverage/test_coverage.rb b/test/coverage/test_coverage.rb index a2a7718a30..a2efa0d1c0 100644 --- a/test/coverage/test_coverage.rb +++ b/test/coverage/test_coverage.rb @@ -26,6 +26,13 @@ class TestCoverage < Test::Unit::TestCase end; end + def test_coverage_in_main_script + autostart_path = File.expand_path("autostart.rb", __dir__) + main_path = File.expand_path("main.rb", __dir__) + + assert_in_out_err(['-r', autostart_path, main_path], "", ["1"], []) + end + def test_coverage_running? assert_in_out_err(%w[-rcoverage], <<-"end;", ["false", "true", "true", "false"], []) p Coverage.running? diff --git a/version.h b/version.h index fb6a28ab79..86da3056a3 100644 --- a/version.h +++ b/version.h @@ -11,7 +11,7 @@ # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR #define RUBY_VERSION_TEENY 0 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR -#define RUBY_PATCHLEVEL 20 +#define RUBY_PATCHLEVEL 21 #include "ruby/version.h" #include "ruby/internal/abi.h" -- cgit v1.2.1