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
This commit is contained in:
NARUSE, Yui 2023-01-25 16:34:24 +09:00
parent 4110137fcf
commit fee5b8f263
6 changed files with 29 additions and 4 deletions

13
iseq.c
View file

@ -917,13 +917,20 @@ iseq_setup_coverage(VALUE coverages, VALUE path, const rb_ast_body_t *ast, int l
return Qnil; return Qnil;
} }
rb_iseq_t * static inline void
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(VALUE path, const rb_ast_body_t *ast, int line_offset)
{ {
VALUE coverages = rb_get_coverages(); VALUE coverages = rb_get_coverages();
if (RTEST(coverages)) { if (RTEST(coverages)) {
iseq_setup_coverage(coverages, path, ast, 0); 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, return rb_iseq_new_with_opt(ast, name, path, realpath, 0, parent, 0,
ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT); 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_t *
rb_iseq_new_main(const rb_ast_body_t *ast, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt) 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("<main>"), return rb_iseq_new_with_opt(ast, rb_fstring_lit("<main>"),
path, realpath, 0, path, realpath, 0,
parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE); parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE);

View file

@ -2,5 +2,11 @@ require_relative '../../spec_helper'
require 'coverage' require 'coverage'
describe 'Coverage.start' do 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 end

View file

@ -0,0 +1,2 @@
require 'coverage'
Coverage.start(:all)

1
test/coverage/main.rb Normal file
View file

@ -0,0 +1 @@
puts Coverage.peek_result[__FILE__][:lines]

View file

@ -26,6 +26,13 @@ class TestCoverage < Test::Unit::TestCase
end; end;
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? def test_coverage_running?
assert_in_out_err(%w[-rcoverage], <<-"end;", ["false", "true", "true", "false"], []) assert_in_out_err(%w[-rcoverage], <<-"end;", ["false", "true", "true", "false"], [])
p Coverage.running? p Coverage.running?

View file

@ -11,7 +11,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 0 #define RUBY_VERSION_TEENY 0
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR #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/version.h"
#include "ruby/internal/abi.h" #include "ruby/internal/abi.h"