Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverage doesn't cover begin #8173

Closed
okuramasafumi opened this issue Mar 28, 2024 · 13 comments · Fixed by #8205
Closed

Coverage doesn't cover begin #8173

okuramasafumi opened this issue Mar 28, 2024 · 13 comments · Fixed by #8205
Milestone

Comments

@okuramasafumi
Copy link

Environment Information

Provide at least:

  • JRuby version: jruby-9.4.6.0
  • Operating system and platform: ubuntu-latest and macOs on GitHub Actions

Expected Behavior

  • Coverage library will cover begin clause

Actual Behavior

Additional info

I noticed that after adding jruby to GitHub Actions matrix, the coverage percentage slightly get down. It seems coverage library doesn't cover begin clause.
CRuby does cover, so I believe JRuby could behave the same.

@enebo enebo added this to the JRuby 9.4.7.0 milestone Apr 2, 2024
@enebo
Copy link
Member

enebo commented Apr 2, 2024

This may be difficult to solve as we transform syntax to instructions and if we see no instructions executing logic we remove contiguous line instructions since we figured nothing could execute on that line. Trace instrs perhaps we can change though.

@headius
Copy link
Member

headius commented Apr 3, 2024

@enebo Yeah I suppose we'd have to add instructions indicating begin entry among other things.

@okuramasafumi FYI we generally don't trace everything unless you pass --debug or -Xdebug.fullTrace to JRuby.

@headius headius closed this as completed Apr 3, 2024
@headius
Copy link
Member

headius commented Apr 3, 2024

Oops, clicked wrong button...

@headius headius reopened this Apr 3, 2024
@okuramasafumi
Copy link
Author

@okuramasafumi
Copy link
Author

After some research, it seems to me that begin is just a Label in JRuby.

Label rBeginLabel = getNewLabel();

And we don't add coverage for a label?

If I'm correct, we can try to add coverage for labels. Probably I'm wrong so I appreciate any help.

@okuramasafumi
Copy link
Author

@Anebo @headius

Here's a minimal repro code for the difference in coverage.

# cov_result.rb
require 'coverage'
Coverage.start(lines: true)
require_relative 'cov_body'
p Coverage.result

# cov_body.rb
begin
  a = 42
end

The results:

ruby --version
# jruby 9.4.6.0 (3.1.4) 2024-02-20 576fab2c51 OpenJDK 64-Bit Server VM 21.0.2 on 21.0.2 +jit [arm64-darwin]

ruby cov_result.rb
# {"/Users/okuramasafumi/src/Playground/Ruby/cov_body.rb"=>{:lines=>[0, 1, nil]}}

ruby --version
# ruby 3.2.3 (2024-01-18 revision 52bb2ac0a6) [arm64-darwin23]

ruby cov_result.rb
# {"/Users/okuramasafumi/src/Playground/Ruby/cov_body.rb"=>{:lines=>[nil, 1, nil]}}

@enebo
Copy link
Member

enebo commented Apr 22, 2024

@okuramasafumi begin does happen to have a start label but many other things do as well so I am unsure if this will not add more coverage lines than CRuby. That fix is interesting but I think we just need to add an explicit line for begin in IRBuilder.

Note: As an aside, labels only get seen during interpretation. Once prepared for compilation they are used for constructing our graph. I do think your solution could be used even for compilation but only if we added a line number instr at that point when constructing a graph. Still IRBuilder may be the simpler fix since we can just target begin.

@enebo
Copy link
Member

enebo commented Apr 22, 2024

I just realized I misunderstood this issue. CRuby does not cover the line as much as mark it nil which does not count it as part of coverage statistics. We add it as a line which needs coverage and we do not emit a line instr so we never "cover" that line. We should NOT be marking that line as needing coverage. This problem is likely in the parser itself.

@okuramasafumi
Copy link
Author

@enebo Thank you for the fix! I think I'd like to add a test case for this issue so that we can be sure it's fixed. What do you think?

@enebo
Copy link
Member

enebo commented Apr 22, 2024

@okuramasafumi yeah that would be great! It can be a regression test for us or even better would be ruby/spec contribution under ruby/library/coverage. That is up to you though.

@okuramasafumi
Copy link
Author

@enebo Ah yes ruby/spec should include that behavior. I'm not familiar with JRuby's testing strategy, but should I add test/spec to both test directory and ruby/spec?

@enebo
Copy link
Member

enebo commented Apr 22, 2024

@okuramasafumi ruby/spec will occasionally get synced with our project tree so submitting there will get copied into us at some future point (usuallly about a month or so).

@enebo
Copy link
Member

enebo commented Apr 22, 2024

@okuramasafumi but I should have completely answered that by saying you should only contribute to ruby/spec if you are ok with adding a spec there.

okuramasafumi added a commit to okuramasafumi/rubyspec that referenced this issue Apr 22, 2024
Ref: jruby/jruby#8173
This commit is intended to make sure `Coverage` library behaves
the same for the code with `begin`. JRuby had an issue so this
change will help JRuby and other implementations to avoid it.
okuramasafumi added a commit to okuramasafumi/rubyspec that referenced this issue Apr 22, 2024
Ref: jruby/jruby#8173
This commit is intended to make sure `Coverage` library behaves
the same for the code with `begin`. JRuby had an issue so this
change will help JRuby and other implementations to avoid it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants