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

Fix relevant lines for unloaded files #605

Merged
merged 3 commits into from
Aug 12, 2017
Merged

Fix relevant lines for unloaded files #605

merged 3 commits into from
Aug 12, 2017

Conversation

odlp
Copy link
Contributor

@odlp odlp commented Aug 9, 2017

As discussed in #604, unloaded files appear to have whitespace, comments & nocov block lines counted as relevant. This leads to the overall coverage being understated.

Before:

After:

Oli Peate added 2 commits August 9, 2017 18:04
Lines with whitespace, comments, or blocks with :nocov: should
not be relevant (nil). Other lines from unloaded files should be relevant
with zero cover (0).

Fixes #604
Copy link
Collaborator

@PragTob PragTob left a comment

Choose a reason for hiding this comment

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

Thank you for your excellent PR! 👍 💪 🎉 🚀

Comments are mostly about some more edge casey testing that I'd like to have, if you're lacking the time (as I lacked the team for a review earlier, sorry) I'm happy to take this over and get it to master :)

Ghost and Nala say thank you:

img_20170723_143705


When I open the coverage report generated with `bundle exec rspec spec`
Then I follow "lib/not_loaded.rb"
Then I should see "3 relevant lines" within ".highlighted"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you for adding an integration test! 👍

@@ -0,0 +1,29 @@
module SimpleCov
class LinesClassifier
Copy link
Collaborator

Choose a reason for hiding this comment

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

First I was like damn why do we need this, that is something Coverage should expose, but it doesn't and it's implemented in C. ☹️

A comment on the class might help though :)

lines.map do |line|
if line =~ self.class.no_cov_line
skipping = !skipping
NOT_RELEVANT
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 using constants!

if line =~ self.class.no_cov_line
skipping = !skipping
NOT_RELEVANT
elsif line =~ WHITESPACE_OR_COMMENT_LINE || skipping
Copy link
Collaborator

Choose a reason for hiding this comment

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

very minor thing, but I think skipping should be evaluated first (regexes are potentially slower) - I know probably premature optimization as I hope people don't track to many not loaded files, but better safe than sorry :)

]

expect(classified_lines.length).to eq 2
expect(classified_lines).to all be_relevant
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is this magic I've never seen before 😳 Cool stuff, thanks for teaching me!

spec/lines_classifier_spec.rb Outdated Show resolved Hide resolved
"# :nocov:",
"def hi",
"end",
"# :nocov:",
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd like to see some tests/behaviour with non closing #:nocov:

something like:

# :nocov:
foo
# :nocov:
bar
# :nocov:
lol

:)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice suggestion

it "classifies whitespace as not-relevant" do
classified_lines = subject.classify [
"",
" ",
Copy link
Collaborator

Choose a reason for hiding this comment

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

tab(s) inside the string? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call!

it "classifies comments as not-relevant" do
classified_lines = subject.classify [
"#Comment",
" # Leading space comment",
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think a test that goes and checks that puts "#{var}" is not considered a comment might be worth it :)

it "classifies each line as relevant" do
classified_lines = subject.classify [
"def foo",
"end",
Copy link
Collaborator

Choose a reason for hiding this comment

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

I might be over testing stuff, but a test with a class and a method definiton inside might be cool to make sure that all that works correctly :)

@odlp
Copy link
Contributor Author

odlp commented Aug 11, 2017

Thanks for the review @PragTob - excellent points.

I think I'll have time this weekend / Monday to make further edits addressing your feedback 😃

@PragTob
Copy link
Collaborator

PragTob commented Aug 11, 2017

That'd be great! I missed out on doing a release the last couple of weeks, but now that it's here I'd wait for this to land as it's a good bug fix :)

Adds examples for hard tabs, string interpolation and non-closing :nocov:
tokens. Also adds a descriptive comment to SimpleCov::LinesClassifier.
@odlp
Copy link
Contributor Author

odlp commented Aug 12, 2017

@PragTob I've updated the PR based on your feedback, thanks.

Copy link
Collaborator

@PragTob PragTob left a comment

Choose a reason for hiding this comment

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

Great work, thank you a lot!

Al and Nala are happy about these yummy new improvements and tests!

img_20170811_105712

expect(classified_lines).to all be_not_relevant
expect(classified_lines[0..2]).to all be_irrelevant
expect(classified_lines[3..4]).to all be_relevant
expect(classified_lines[5..7]).to all be_irrelevant
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

@PragTob PragTob merged commit f202e7e into simplecov-ruby:master Aug 12, 2017
PragTob added a commit that referenced this pull request Aug 12, 2017
* [ci skip]
* reordering in bug fixes shows my subjective perception of how many people this might affect/relevance :)
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Aug 31, 2017
0.15.0 (2017-08-14) ([changes](simplecov-ruby/simplecov@v0.14.1...v0.15.0))
=======

## Enhancements

* Ability to use regex filters for removing files from the output. See [#589](simplecov-ruby/simplecov#589) (thanks @jsteel)

## Bugfixes

* Fix merging race condition when running tests in parallel and merging
  them. See [#570](simplecov-ruby/simplecov#570) (thanks
  @jenseng)
* Fix relevant lines for unloaded files - comments, skipped code etc. are
  correctly classigied as irrelevant. See
  [#605](simplecov-ruby/simplecov#605) (thanks @odlp)
* Allow using simplecov with frozen-string-literals enabled. See
  [#590](simplecov-ruby/simplecov#590) (thanks @pat)
* Make sure Array Filter can use all other filter types. See
  [#589](simplecov-ruby/simplecov#589) (thanks @jsteel)
* Make sure file names use `Simplecov.root` as base avoiding using full
  absolute project paths. See
  [#589](simplecov-ruby/simplecov#589) (thanks @jsteel)
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.

2 participants