Feature #22014
openSupport `||` and `&&` operations in coverage
Description
https://bugs.ruby-lang.org/issues/13901 added support for branch coverage, but did not include || and && branches
i.e.
Currently, this is considered to be 100% covered so long as it is executed, even though some values won't result in the right hand check being executed.
From the comments on the original ticket, it sounds like || and && were planned to be supported:
https://bugs.ruby-lang.org/issues/13901?tab=history#note-6
marcandre:
I imagine you are planning on covering ||, &&, &. and ? :, right?https://bugs.ruby-lang.org/issues/13901?tab=history#note-7
mame:
? : has been already supported. Yuichiro has experimentally implemented &. coverage. I think we should support || and && too.
(Formerly, I had no intention of supporting them, but Yuichiro implemented column numbers, so now we will be able to do that.)
So I'm hoping this is just something that got forgotten about rather than having a particular unsolved technical challenge.
Related: https://github.com/simplecov-ruby/simplecov/issues/921
Updated by jduarte (José Duarte) 25 days ago
+1 on this
Branch coverage is now a CI gate in a lot of Ruby projects, and &&/|| is the one place where Coverage reports code as fully covered even though part of it never ran. For example:
If every test passes an inactive product, product.images.any? is never called. The line still counts as covered and there is no branch entry for it, so nothing in the report points at the gap. Same with memoization: with @config ||= load_config there is no way to tell whether the already-cached path was ever exercised.
The only workaround today is rewriting the code into if / return unless so Coverage can see each condition (that's what was suggested in simplecov#921). Doing that across a large codebase just to get accurate numbers isn't ideal and I don't think it will make the code better. Tools like SimpleCov can only show what Coverage reports, so this would really benefit it it came from Ruby itself.
A couple of suggestions:
- It would be great to include
||=and&&=too, not only the binary operators. - Since this adds new branch entries, existing branch percentages will go down after upgrading. It might be worth making it opt-in (something like
Coverage.setup(branches: true, conditions: true)).
I'm happy to work on a patch if the direction sounds good.