simplecov icon indicating copy to clipboard operation
simplecov copied to clipboard

allow skipping (unreachable) branch coverage only

Open akostadinov opened this issue 2 years ago • 10 comments

Hello, I would like to be able to skip some unreachable branches but I only see full coverage skipping with nocov.

For example the following code will report the else is never run but it is unreachable:

match = match = /^(\d+)([abc])$/.match(str)
raise unless match
case match[2]
when "a"
...
when "b"
...
when "c"
...
end

Is there another tag or can such be implemented that will only ignore branch coverage?

akostadinov avatar Mar 28 '23 09:03 akostadinov

You can ignore that implicit branch like this:

case match[2]
when "a"
...
when "b"
...
when "c"
...
else
  :nocov:
  nil
  :nocov:
end

tycooon avatar Mar 28 '23 09:03 tycooon

Ok, thank you! Somehow not ideal but also I don't see any better way :)

akostadinov avatar Mar 28 '23 09:03 akostadinov

Yep, you also can actually cover that branch if you want 👍

tycooon avatar Mar 28 '23 09:03 tycooon

What do you mean with cover? raise or cover as in tests?

akostadinov avatar Mar 28 '23 10:03 akostadinov

I mean you may write a unit test where that normally unreachable branch is tested (and it may contain something like raise "Invalid something whatever".

tycooon avatar Mar 28 '23 10:03 tycooon

I can't reach it because it will not match the RE and will fail earlier than the case statement.

akostadinov avatar Mar 28 '23 10:03 akostadinov

Is syntax for skipping branch correct??

:nocov:
nil
:nocov:

or am I missing something here?

sudeeptarlekar avatar Sep 19 '23 12:09 sudeeptarlekar

My bad, its

# :nocov:
nil
# :nocov:

sudeeptarlekar avatar Sep 19 '23 12:09 sudeeptarlekar

Any thoughts of allowing # :nocov: as a suffix to a line?

case match[2]
...
else
  nil # :nocov:
end

or even

case match[2]
...
else nil # :nocov:
end

This would be great to have because then there's not extra lines of code just to mark a case as unreachable. If interested, I could spend some time developing a patch for this.

win93 avatar Dec 17 '23 02:12 win93

@win93 there is a PR for that https://github.com/simplecov-ruby/simplecov/pull/1068. Unfortunately, maintainers have been mostly inactive lately.

tycooon avatar Dec 18 '23 08:12 tycooon