Skip to content

add branch coverage functionality with tests - #692

Closed
som4ik wants to merge 4 commits into
simplecov-ruby:masterfrom
som4ik:feature/branch-coverage-ruby-2.5-support
Closed

som4ik wants to merge 4 commits into
simplecov-ruby:masterfrom
som4ik:feature/branch-coverage-ruby-2.5-support

Conversation

@som4ik

@som4ik som4ik commented Aug 17, 2018 •

Copy link
Copy Markdown
Contributor

Add branch coverage measurement statistics (ruby ~> 2.5)
In this PR:

  • Add configuration needed to trigger this new functionality.
SimpleCov.start do
  use_branchable_report true
end

If config is set for ruby versions which are < 2.5, It will shows notify message and produce only lines coverage report.

  • Unite the results no matter what coverage version used to be there. Now always the coverage result is a hash with keys represents the type of coverage and values represents the coverage result for each type ex: {:lines => ..., :branches => ..}.
  • Add Branch class with related support methods to SourceFile
  • On simplecov-html side we will have this visual statistics:
    branch1
    [0, '-'] => [passing count, branch sign]
    Condition with missed branch will be marked similar to line 70.

Benefits:

  • Coverage report gives more information about conditions especially the inline conditions similar to the case specified above.

Frankly, the first time I developed this feature it was a PR with 1300 diff lines, I thought it will be quite hard to review it. So this one is about 760 lines and hopes it will be approved.

@som4ik
som4ik force-pushed the feature/branch-coverage-ruby-2.5-support branch 3 times, most recently from 7f21245 to 52d21e0 Compare August 20, 2018 10:53
@som4ik som4ik changed the title add branche coverage functionality with tests fix add branch coverage functionality with tests Aug 20, 2018
@som4ik
som4ik force-pushed the feature/branch-coverage-ruby-2.5-support branch 2 times, most recently from 126b662 to 5bf996a Compare August 21, 2018 13:59
@frewsxcv

frewsxcv commented Sep 2, 2018

Copy link
Copy Markdown

Not sure if it helps, but I just found this Ruby gem which has some utilities for working with Ruby branch coverage data.

@som4ik
som4ik force-pushed the feature/branch-coverage-ruby-2.5-support branch from 5bf996a to 91c185b Compare September 3, 2018 08:28
Comment thread lib/simplecov/configuration.rb Outdated
# lines coverage report on not supportable versions.
#
def target_capability(target)
if (RUBY_VERSION.to_f < 2.5) && target

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there another way to check for this, maybe just Coverage.method(:start).arity ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, using arity looks nicer.

@bf4

bf4 commented Sep 4, 2018

Copy link
Copy Markdown
Collaborator

Thanks

@som4ik

som4ik commented Sep 13, 2018

Copy link
Copy Markdown
Contributor Author

Done, updated the ruby version check using arity.

@pelletencate

Copy link
Copy Markdown

I need this feature; is anyone ever going to pick this up?

I've tried running it on Ruby 2.6 with RSpec, it doesn't seem to work, I just get the exact same HTML I'd get with vanilla simplecov.

@som4ik

som4ik commented Feb 8, 2019

Copy link
Copy Markdown
Contributor Author

@pelletencate
The HTML changes are here in simplecov-html gem PR

@magneland magneland left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been interested in using this feature for a while now, so I'm reviewing it. Hopefully it will be well received. Please excuse any comments that are too uninformed. In general I am concerned about correctness and performance, but I don't have a way to test performance myself. I have also commented on wording and typos, but feel free to ignore those.

Comment thread README.md
SimpleCov.refuse_coverage_drop
```

## Branch coverage (ruby '~> 2.5')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll start my code review here by summarizing the (not always documented) feature in Ruby core.
There are many possible types of code coverage now thanks to mame's work.

Ruby < 2.5:
https://github.com/ruby/ruby/blob/v1_9_3_374/ext/coverage/coverage.c
traditional mode (Array)

Ruby 2.5:
https://bugs.ruby-lang.org/issues/13901
https://github.com/ruby/ruby/blob/v2_5_3/ext/coverage/coverage.c
default: traditional/compatible mode (Array)
:lines - like traditional mode but using Hash
:branches
:methods
:all - same as lines + branches + methods

Ruby >= 2.6:
https://bugs.ruby-lang.org/issues/15022
https://github.com/ruby/ruby/blob/v2_6_3/ext/coverage/coverage.c
default: traditional/compatible mode (Array)
:lines - like traditional mode but using Hash
:branches
:methods
:oneshot_lines - can not be combined with lines
:all - same as lines + branches + methods

So my first question is: can the flexibility of multiple modes be exposed in Simplecov as well?

It would seem that this PR currently exposes 2 modes:
default: traditional/compatible mode (Array)
:all - same as lines + branches + methods

Since I am personally interested in :oneshot_lines in the (near) future, I wonder how that can be made compatible using a similar syntax. Or perhaps Simplecov could reflect the syntax of coverage.c?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@magneland hi there! Sorry for all the tardyness, hard at work trying to get in. I fail to find documentation for oneshot_lines in the metioned files? Can you point me somewhere?

#
def self.start(source_file_path)
return {} unless SimpleCov.branchable_report
Coverage.start(:all)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be Coverage.start(:lines, :branches)? It seems :methods is not being used for anything in this PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will handle it in the scope of change used option name.


branch1.each do |(condition, branches_inside)|
branches_inside.each do |(branch_key, branch_coverage_value)|
compared_branch_coverage = branch2[condition][branch_key]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it need a guard clause for handling missing condition in branch2? Default value should be 0 I think. Also - does it make sense to replace this code with merge_branches here? https://github.com/mame/coverage-helpers/blob/4d2a33d3dec8c8bbf51bb21a3cdaf57d2481472e/lib/coverage/helpers.rb#L24

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, you are right, I think .to_i will be ok

first_coverage.merge(second_coverage) do |_condition, first_matrices, second_matrices|
  first_matrices.merge(second_matrices) do |_condition, first_run, second_run|
    first_run + second_run.to_i
  end
end

groups[group_name] = parse_filter(filter_argument, &filter_proc)
end

# Define if the report should include any other standarts of coverage measurement except

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: standards

Comment thread lib/simplecov/result.rb Outdated
end

# Manage symbolize the keys of coverage hash.
# JSON.prase gives coverage hash with stringified keys what breaks some logics

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: logic

Comment thread spec/source_file_spec.rb
expect(subject.root_branches.size).to eq(2)
end

it "Has branch on line number 7 with report pr line" do

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: "has branch on line 17"

Comment thread spec/source_file_spec.rb
end

it "Has coverage report" do
expect(subject.branches_report).to eq(17 => [[3, "+"]], 19 => [[0, "-"]], 29 => [[3, "+"]], 31 => [[0, "-"]])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

19 and 31 should be 20 and 32? See earlier comment about zero-based keys.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this case is a multi-line branch if, else.., these branches should have badges on the line of the condition definition, that's why I have done branch.start_line - 1, otherwise, the badge statistics would appear on the first line of the branch body which is misleading from my point of view.

Comment thread spec/source_file_spec.rb
expect(subject.branches_report).to eq(17 => [[3, "+"]], 19 => [[0, "-"]], 29 => [[3, "+"]], 31 => [[0, "-"]])
end

it "Hash line 31 with missed branches" do

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be line 32?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same above.

Comment thread spec/source_file_spec.rb
expect(subject.branches_report[18]).to eq([[3, "+"], [0, "-"]])
end

it "Has branches coverage precent 50.00" do

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: percent

Comment thread spec/source_file_spec.rb
end

it "Has branches coverage precent 50.00" do
expect(subject.branches_coverage_precent).to eq(50.00)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not 50.0?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just followed the standard definition of percentage for line coverage calculation.

Comment thread spec/raw_coverage_spec.rb
source_fixture("three.rb") => [nil, 1, 1],
source_fixture("sample.rb") => {
:lines => [nil, 1, 1, 1, nil, nil, 1, 1, nil, nil],
:branches => {[:if, 3, 8, 6, 8, 36] => {[:then, 4, 8, 6, 8, 12] => 47, [:else, 5, 8, 6, 8, 36] => 24}},

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there supposed to be a corresponding fixture change in this PR? Or is this intended to be fake branch data?

@magneland magneland left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more general comment: I am taking a look at the corresponding HTML report change here: simplecov-ruby/simplecov-html#64 The interesting / unfortunate thing is that the tests for the HTML reports actually are in this repo.

@PragTob

PragTob commented Jun 26, 2019

Copy link
Copy Markdown
Collaborator

See my comment in the other PR

Basically!

  1. thanks everyone especially @som4ik for doing this!
  2. I'm slowly getting back into simplecov maintenance work and will see what I can do
  3. I'm unsure what the preferred PR to review is, this here or Feature/branches coverage support #694 (the reference to first PR but linking to the PR that was opened later is confusing to me, I might have just watched too much dark

Thanks, have a bunny:

WhatsApp Image 2019-06-26 at 08 42 20

@PragTob

PragTob commented Jul 1, 2019

Copy link
Copy Markdown
Collaborator

I reviewed #694 (on the recommendation of @som4ik ) and liked it, so it will be the "preferred" PR going forward and I will close this one. Please direct your feedback there. I'll try to take the comments made in this PR into account as well should they apply.

@PragTob PragTob closed this Jul 1, 2019
@som4ik

som4ik commented Dec 7, 2019

Copy link
Copy Markdown
Contributor Author

@magneland Thank you 👍

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 this pull request may close these issues.

6 participants