Repository navigation
Feature/branches coverage support - #694
Conversation
f42b76f to
93b11c9
Compare
5fdbcdd to
52827cb
Compare
This branch was never covered, detected by simplecov-ruby/simplecov#694. Now all branches are covered
|
Could we consider this PR ? It's very usefull for our project. |
|
Hey @som4ik , I got very excited after your comments in the issue and tried out your branch in my current work project. Sadly I encountered an exception after the test suite ran. Maybe the simple CallTrace can help you already; next week I might have a deeper insight with debugging. I hope that is not one specific file that caused this exception 🤣 Our docker base image is phusion passenger ruby25 EDIT: Are you sure that the For me this happens because when modules are defined in the non-nested style like this |
| def self.start(source_file_path) | ||
| return {} unless SimpleCov.branchable_report | ||
| Coverage.start(:all) | ||
| require source_file_path |
There was a problem hiding this comment.
Are you sure that the require is stable? I am receiving a loooot of LoadError errors when this is executed
There was a problem hiding this comment.
Well, thank you for being interested in this PR :), l had a look on your comments:
1 - In line 16 require is called on the absolute file path so it's safe for the file to be required. require can through and LoadError if file/module target is not located in global load path constant.
2 - About the non-nested style of defining modules, it should through another type of errors NameError.
3 - The logs that provided me with is branches_per_file.rb:17:in start': undefined method []' the line 17. This type of error is a result of totally empty ruby file which it's coverage report return an empty hash {}, I will fix it.
4 - Are you using track_file configuration on simplecov? could you please try to require the file that you have and with absolute path of it in the irb console? (This will help me to better understand the issue)
5- BranchesPerFile concern the files that are missing any coverage report (not covered by applied tests), so it will return just the branches with zero coverage result.
As a part of the solution on this case, is to write something similar to LinesClassifier that will parse the lines and give the report of branches, but this will make this PR very complicated to review. Better to have a simple solution that will return empty hash for now, and make a new PR with the new functionality of text parsing after this one will be accepted.
I appreciate your interest in this PR, hope our collaboration will make SimpleCov have this feature soon.
There was a problem hiding this comment.
1:
require is called on the absolute file path so it's safe
I do not quite understand what the file path has to do with safety of the file. My point was that with this change there is an unexpected eager loading on the execution of the test suite
3: This error appears also in a different case: execution of only a specified test or a specified directory
Example:
rspec spec/models
Randomized with seed 24924
..................................................
Finished in 1.89 seconds (files took 2.47 seconds to load)
50 examples, 0 failures
Randomized with seed 24924
Traceback (most recent call last):
11: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov/defaults.rb:27:in `block in <top (required)>'
10: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov.rb:182:in `run_exit_tasks!'
9: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov/configuration.rb:191:in `block in at_exit'
8: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov.rb:70:in `result'
7: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov.rb:310:in `process_coverage_result'
6: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov.rb:299:in `result_with_not_loaded_files'
5: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov.rb:263:in `add_not_loaded_files'
4: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov.rb:263:in `each'
3: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov.rb:265:in `block in add_not_loaded_files'
2: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov/run_file_coverage.rb:9:in `start'
1: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov/run_file_coverage.rb:26:in `call'
/Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov/branches_per_file.rb:17:in `start': undefined method `[]' for nil:NilClass (NoMethodError)
4: No, am not using track_file
Sorry, I don't have time for this at the day-to-day work anymore; so I cannot get more into the details what is wrong there.
However, I made some of the problematic examples that came to my mind in my (much simple) pet project.
Have a look at https://github.com/klyonrad/lazybar/commits/simplecov-branch-cov
the full rspec run actually works fine with the edge cases that I wrote but the the execution of a directory or a single file breaks - with a different error then what I copied in my previous example:
rspec spec/models/cocktail_recipe_spec.rb
Randomized with seed 20614
..................
Top 2 slowest examples (1.15 seconds, 55.6% of total time):
CocktailRecipe validations is not valid with only one ingredient
1.05 seconds ./spec/models/cocktail_recipe_spec.rb:21
CocktailRecipe methods about liking can show the users that like it
0.09874 seconds ./spec/models/cocktail_recipe_spec.rb:70
Finished in 2.07 seconds (files took 3.19 seconds to load)
18 examples, 0 failures
Randomized with seed 20614
Traceback (most recent call last):
13: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov/defaults.rb:27:in `block in <top (required)>'
12: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov.rb:182:in `run_exit_tasks!'
11: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov/configuration.rb:191:in `block in at_exit'
10: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov.rb:70:in `result'
9: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov.rb:310:in `process_coverage_result'
8: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov.rb:299:in `result_with_not_loaded_files'
7: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov.rb:263:in `add_not_loaded_files'
6: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov.rb:263:in `each'
5: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov.rb:265:in `block in add_not_loaded_files'
4: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov/run_file_coverage.rb:9:in `start'
3: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov/run_file_coverage.rb:26:in `call'
2: from /Users/luka/.rvm/gems/ruby-2.5.3/bundler/gems/simplecov-52827cb42b93/lib/simplecov/branches_per_file.rb:16:in `start'
1: from /Users/luka/development/lazy_bar/app/models/never_tested.rb:1:in `<top (required)>'
/Users/luka/development/lazy_bar/app/models/never_tested.rb:2:in `<class:NeverTested>': uninitialized constant NeverTested::NotExistingNamespace (NameError)
As I said: even though it would be a good thing to catch all these type of stupid programmer errors; I think an autoloading of all code is a bit unexpected behaviour
There was a problem hiding this comment.
Have to agree with @klyonrad (thanks for chiming in!) - people might have some ruby files lying around that do stuff like delete folders, or run production tasks, code base refactoring - we shouldn't just load and execute those. I think it's fine to just show unloaded files as entirely uncovered or whatever else.
If we really needed the branch structure I think we should see if we could get access to the parser that generates it.
There was a problem hiding this comment.
@PragTob see my comment above if you still not agree I will remove this class, and add it to my TODO list for next PR :)
There was a problem hiding this comment.
@som4ik I suggest remove this class, because we tested your PR and found that took 10-15 seconds for our big legacy project... After remove this class everything working fast.
There was a problem hiding this comment.
@stepozer Yep, that's what exactly I will do. We already agreed to do it at the comments above.
|
Is anyone available to review this PR ? We will be really grateful to see this feature in simplecov on our ruby2.5 projects ;) |
|
I've wanted to use support branch coverage for a while in my projects, so I tried out this pull request (and the corresponding one in simplecov-html). Here is what I found, and they may well be issues with branch support in stdlib coverage and not issues with this pull request:
I would definitely like to see branch support in simplecov. Hopefully the issues can be addressed and this can be merged. |
|
Hi everyone! Thanks @som4ik for all the work on this, everyone for their encouragements, testing this out and reporting problems 🎉 It seems like some bugs still might need to be squashed here before we can release it. I'm slowly getting back to trying to maintain simplecov and this is of course a huge piece of work. I'll aim to review and determine what might/might not need doing before we can land it and maybe release it as an "alpha" feature or what not. Please bear with me for a little while longer, this is a huge PR. @som4ik: Which one is the preferred PR to review? This here or #692 ? I'm a bit confused because #692 says "the first time I developed this" but points to this PR which was made after #692 ? Which one do you prefer to get in/which one should I review/work on? Also bunny for your efforts: Thanks, |
|
I see, thanks for the look. Aim is to take a look in the coming days, maybe even today. :) |
PragTob
left a comment
There was a problem hiding this comment.
👋
Hi there and thanks again for your awesome work 🎉 🎉 🎉
I love a lot of the changes you did (Combiners and other places where you broke up the responsibilities) 👍
Sorry for the myriad of comments, I'm a very thorough reviewer as you can see (although my concentration faded a bit). I want to make sure that I understand everything, as well I'll end up maintaining it.
On a more general level some things where I think our style just differs:
- you created some classes (combiners for instance iirc) that just transform some data, I don't think a full class with instance variables is necessary here - I think it's better to just have module method that explicitly passes on parameters (I commented this in places but I think it applies to more)
- we need to find a good setting/configuration name 😁
- the eager require for some of the files should go as we could execute random dangerous code which is definitely not a side effect someone expects from running simplecov :)
- I think some of the docs that basically state the method name aren't necessary, but I can remove those myself :)
Again, this is very good work I probably wouldn't have started working on this if it wasn't for your great contribution 👌
Also, if you don't want to change something that I mentioned that's fine, it's my fault that this has been lying around for so long. Just tell me and I'll fix it myself before merging, or afterwards.
Still this is a big change, so has to be treated with carefulness. Still I want this PR to go in relatively soon as it has been lying around for some time and merge conflicts will only get bigger.
My plan is roughly this:
- determine what's best, first merging this or #700
- do a release without those changes (so still old school SimpleCov)
- merge both those PRs
- add more testing specifically test branch coverage through cucumber tests etc (please do not add this to this PR, it's big enough as is and I wanna get it merged)
- do some alpha or beta release with the new stuff so people can easily try it out and help us find bugs
- fix bugs ;P
- do a full release but still advertise the feature as beta/use with caution/report all them bugs :)
Hope that sounds good to you, and once again thanks for all the great work 💚
|
|
||
| ```ruby | ||
| SimpleCov.start do | ||
| use_branchable_report true |
There was a problem hiding this comment.
I'm not sure about the naming. I'd expect something like: branch_coverage or use_branch_coverage but I don't yet understand all of this anyway 😁
There was a problem hiding this comment.
Or maybe we need a coverage setting that is set to :lines or :branches or :methods or :all or what not :)
There was a problem hiding this comment.
It's a perfect idea. I will apply it and add validation on it.
| # | ||
| # @return [Hash] | ||
| # | ||
| def process_coverage_result |
There was a problem hiding this comment.
small and maybe personal thing, but when it comes to order of methods I like to go high level to detail, so in this case I'd first define process_coverage_result and then the methods it calls in the order that it calls them. That way it's easier to follow along because I can read the methods in the context that they're used in :)
| # Doing something similar to https://ruby-doc.org/stdlib-2.5.3/libdoc/coverage/rdoc/Coverage.html can throw an error. | ||
| # Errors can be related to Constants, modules are included/extended inside the file but not yet loaded or even exist. | ||
| # It even can be a programmer's error. | ||
| module BranchesPerFile |
There was a problem hiding this comment.
Maybe something like "BranchDateForMissingFile" or something as the module/class name? The original name had me a bit confused.
There was a problem hiding this comment.
Or we might not need/want this see the comment chain with @klyonrad just loading ruby files seems potentially harmful to gem users.
There was a problem hiding this comment.
Frankly, somehow I agree with @klyonrad, but on the other hand, this mechanism will help us to determine the branches in the missed files, which I think is nice to have.
After the @klyonrad comment, I added a rescue to print out the error message that problematic file raises, don't you agree with this approach? it would make these modifications not harmful at all
There was a problem hiding this comment.
Hey - I'm not as worried about the errors (which we can and are catching), but more what random ruby scripts lying around might do. Imagine the following scenario:
- I wrote ruby script to that sshs into production and runs an expensive operation there, I was carless and included all the credentials. I just dropped it somewhere in my app folder
- I run my tests
- The file naturally isn't covered but gets picked up through track files or what not
- this file requires the file which causes all the ruby code to execute 😱
- Suddenly production is on fire because of that expensive operation and nobody has any idea why 🔥
I admit it's a worst case scenario. I'm certainly known to have little ruby scripts lying around that do something. They should never be executed as a side effect of a test suite run and to the best of my understanding this makes that possible. We should avoid that at all costs imo.
There was a problem hiding this comment.
Not like the ultra worst case scenario that you are painting here but at my previous job we literally had a file that upon loading was reading stuff from the database, storing into some kind of module/class variable and suddenly the information was shared between the different tests.
The lesson is: require does things 😉
| def self.start(source_file_path) | ||
| return {} unless SimpleCov.branchable_report | ||
| Coverage.start(:all) | ||
| require source_file_path |
There was a problem hiding this comment.
Have to agree with @klyonrad (thanks for chiming in!) - people might have some ruby files lying around that do stuff like delete folders, or run production tasks, code base refactoring - we shouldn't just load and execute those. I think it's fine to just show unloaded files as entirely uncovered or whatever else.
If we really needed the branch structure I think we should see if we could get access to the parser that generates it.
| # | ||
| def start_coverage_measurment | ||
| if branchable_report | ||
| Coverage.start(:all) |
There was a problem hiding this comment.
Is that API documented somewhere? https://ruby-doc.org/stdlib-2.6.3/libdoc/coverage/rdoc/Coverage.html#method-c-start doesn't seem to have anything or am I looking in the wrong place 😅
There was a problem hiding this comment.
Agree this feature is not documented, you can have a look at this article:
https://blog.bigbinary.com/2018/04/11/ruby-2-5-supports-measuring-branch-and-method-coverages.html
A comment on the other PR: #692 (comment)
There was a problem hiding this comment.
Yeah I read both of those, but having no official docs sucks. Might wanna PR against ruby. We could include at least one of those links at docs for our future selves :)
| end | ||
|
|
||
| it "has proper results for three.rb" do | ||
| expect(subject[source_fixture("three.rb")][:lines]).to eq([nil, 3, 7]) |
There was a problem hiding this comment.
I'll just believe you that all of these are correct 👼
|
|
||
| if SimpleCov.usable? | ||
| describe SimpleCov::SourceFile::Branch do | ||
| let(:results) do |
There was a problem hiding this comment.
this results confuses me a bit, I think it's only used in the other lets to do result[index] - coudln't we just inline them then? Maybe I just don't understand its purpose 😅
| end | ||
|
|
||
| it "Is a root branch" do | ||
| expect(positive_sub_branch.root?).to be false |
There was a problem hiding this comment.
description says root branch, test says no 🤷♂️
| expect(positive_sub_branch.badge).to eq "+" | ||
| end | ||
|
|
||
| it "Has badge - of negative branch" do |
There was a problem hiding this comment.
this is a bit confusing to read as the tests directly contradict each other (has negative badge, and has positive badge) - I think it might be improved by introducing describes that focus on root/positive/negative branch respectively. What do you think?
| expect(negative_sub_branch.sub_branch?).to be true | ||
| end | ||
|
|
||
| it "Has badge + of positive branch" do |
There was a problem hiding this comment.
I think usually these start lower case :)
|
Looking at #700 it seems to me that most of the relevant changes are in spec_files and that's basically an indentation change I think/hope that your merge conflicts against #700 should be relatively easy to handle. I'm tempted to merge it first, which would be bad for you but I think I can do a PR on your branch to resolve them. What do you think @som4ik ? |
|
I'll get straight on that. Thanks for checking out the review and chiming in on this! 🙏 |
|
You are welcome, I have rolled over most of the comments, will continue reviewing them this evening or next two days. I will push the updates to this branch according to the code review notices ASAP. |
|
@som4ik thank you! Don't stress out about it though, you see how long it took me to get to this 😅 We're all doing this in our free time, take time if you need it. Also feel free to tell me if you disagree with my points :) |
|
Also, all the drop rubies support of ruby xx PRs have been merged so this is ready to be rebased/have merge conflicts resolved (hopefully). As expected the merge conflicts are in not tooo many files. If tests fail on JRuby on travis, travis has wird problems installing rvm these days it seems 🤷♂️ |
|
@som4ik also on our project we found that this PR working incorrect for parallel tests (gem parallel_tests)... Right now we trying to find where an issue, will let you know. |
|
Also in HTML report I don't see new markers for branch coverage... I set up what I am doing wrong? |
|
@stepozer You probably need a version of simplecov-html installed with this pull request applied: simplecov-ruby/simplecov-html#64 |
| def missed_lines | ||
| @missed_lines ||= lines.select(&:missed?) | ||
| end | ||
| def branches_coverage_precent |
There was a problem hiding this comment.
Shouldn't it be branches_coverage_percent?
There was a problem hiding this comment.
Updated! Just in case, some of these methods are used in the default formatter and needs to be changed in both places.
| # @return [Array] | ||
| # | ||
| def covered_branches | ||
| @coverd_branches ||= root_branches.flat_map do |root_branch| |
There was a problem hiding this comment.
I guess it's better to name this ivar @covered_branches so that it matches the method name.
|
@stepozer thank you that is really helpful! 🎉 🚀 💚 |
|
@som4ik don't feel pressured to update the PR, as I said I was planning on updating it myself. I'll leave a comment here before I do though (and have to do it on another branch either way). If you get to upgrading it it's of course also welcome - it's a community effort after all :) |
Yep, although I changed some stuff :) |
4413153 to
a6987d1
Compare
|
@PragTob I have done:
@tycooon I reviewed the changes/upgrades that you have done, I like it, good job! 👍 |
|
@som4ik I will try update code on our project. I'll let you know how your new code works |
Nope, was going to add that later.
You are proposing that I add method coverage support in a later PR, am I correct? In that case we will definitely need to rename that method, so maybe it's better to rename it now, so that we don't have to care about the backward compatibility later? |
|
@som4ik Thank you so much for all this 🎉 I'm currently runnign a bunch of errands, but will re-review some time later today. @tycooon I think getting this merged is a bit more important (tiny bit) than the right configuration option and method coverage or what not to boot with. I'm fine merging this now but waiting to cut a release until we changed the naming to be better. I still like @som4ik your recent email address doesn't seem to be connected to your github account, just fyi if you care about these kinds of things. Thanks everyone! |
PragTob
left a comment
There was a problem hiding this comment.
Looking good, thank you!
Some of the code review comments aren't addressed but that's fine, I/we can address them later. I'm fine with having a slightly wonky state at master and a couple of pre/rc releases for this. For now I think it's more important to get this merged to not create more merge conflicts and so that we can parallelize the work on this and do it in smaller better reviewable chunks :)
I highlighted a couple of questions again that I'd like to know the answers to before merging this :)
| if branchable_report | ||
| Coverage.start(:all) | ||
| else | ||
| Coverage.start |
There was a problem hiding this comment.
Self mental note, I think we can't since not all ruby versions support that argument
| # | ||
| def positive? | ||
| return true if type == :when | ||
| 1 + root_id.to_i == id |
There was a problem hiding this comment.
Highlighting this once more cos I still got no idea what's happening here 😁
There was a problem hiding this comment.
For if, else conditions:
coverage returns matrices ex: [:if, 0,..] => {[:then, 1,..], [:else, 2,..]},
where positive branch always has Id equals to root branch Id incremented by 1.
There was a problem hiding this comment.
I will add more comments to this method
There was a problem hiding this comment.
Guessed as much but wanted confirmation. Thank you! I guess it also works for unless which then is the question should that still be called positive but I think so. Thank you!
| @missed_lines ||= lines.select(&:missed?) | ||
| end | ||
| def branches_coverage_percent | ||
| return 100.0 if no_branches? && no_lines? |
There was a problem hiding this comment.
still not sure why we need to check no_lines? :)
There was a problem hiding this comment.
Yep, It should be removed, I will do it!
|
Good job. Will nice if you release this and I will update gem and check. |
|
@PragTob I added more comments in some places and also done small changes that give significant upgrade for the report matrics. |
128f3aa to
1271233
Compare
|
@som4ik that looks nice! I think we could work on the presentation a bit but in a separate step :) That script looks like it gives a nice overview of branch coverage cases - I think it might be a good to include it in the repo in an integration test - I could type it all from the image but if you have, I'd appreciate it :) |
|
Ok, let's do this 🚀 |
|
|
We updated to new version, seems to be working. Will let you now if something will be wrong. |
|
Remaining work is tracked in #781 |
|
@stepozer thank you very much! 🎉 |









Add branch coverage measurement statistics (ruby ~> 2.5)
In this PR:
If config is set for ruby versions which are < 2.5, It will shows notify message and produce only lines coverage report.
{:lines => ..., :branches => ..}.Branchclass with related support methods toSourceFileRawCoveragewithRunResultsCombiner, which breaks logic of combining different results into file, line and branch combining level, this new structure adds more readability and makes it open for modification (ex: adding methods coverage or any other coverage in future)Coverage.starttake in case all loaded files including required gems, which files are not inside the project directory.[0, '-'] => [passing count, branch sign]Condition with missed branch will be marked similar to line 70.
Benefits: