Skip to content

Commit

Permalink
merge revision(s) 2c93c55,f5ea43a2e61789357e9c4b374b4bc6756abeae17: […
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
nurse committed Jan 25, 2023
1 parent 4110137 commit fee5b8f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
13 changes: 11 additions & 2 deletions iseq.c
Expand Up @@ -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);
Expand All @@ -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("<main>"),
path, realpath, 0,
parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE);
Expand Down
8 changes: 7 additions & 1 deletion spec/ruby/library/coverage/start_spec.rb
Expand Up @@ -2,5 +2,11 @@
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
2 changes: 2 additions & 0 deletions test/coverage/autostart.rb
@@ -0,0 +1,2 @@
require 'coverage'
Coverage.start(:all)
1 change: 1 addition & 0 deletions test/coverage/main.rb
@@ -0,0 +1 @@
puts Coverage.peek_result[__FILE__][:lines]
7 changes: 7 additions & 0 deletions test/coverage/test_coverage.rb
Expand Up @@ -26,6 +26,13 @@ def test_result_with_nothing
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?
Expand Down
2 changes: 1 addition & 1 deletion version.h
Expand Up @@ -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"
Expand Down

0 comments on commit fee5b8f

Please sign in to comment.