simplecov icon indicating copy to clipboard operation
simplecov copied to clipboard

Conditionally skipping coverage (use case: multiple ruby versions support)

Open HoneyryderChuck opened this issue 8 years ago • 5 comments

I've running into this situation in a lib where I want to support new ruby syntax, but I want to had a backwards-compatibility layer. This leads me to define methods in such a way:

...
if RUBY_VERSION < "2.3"
  def foo(n)
    bar.bang(n, oldoption)
  end
else
  def foo(n)
     bar.bang(n, oldoption)
  end
end

Depending of which ruby I run, I'll have one of those chunks marked as red. I could just :nocov: the whole block, but I also want to ensure those bits. Can I write a :nocov if RUBY_VERSION < "2.3" rule for my use-case?

HoneyryderChuck avatar Oct 18 '17 00:10 HoneyryderChuck

SimpleCov has system to merge coverage results. Ideally, you should be running your suite on multiple versions and merging the results. This should be automatic if you run each suite in a small enough amount of time. (default of 10 minutes)

https://github.com/colszowka/simplecov#merging-results

MaxLap avatar Nov 06 '17 16:11 MaxLap

:wave:

Hi, sorry I forgot to answer here. So, I think the workaround is to put one of the 2 branches into a nocov and then live with it. As for the general suggestion, executing arbitrary ruby code is hard to do for us and just opens us up to so many errors. If we were to implement this, I think we'd need to go a different/simpler route - like checking the values of environment variables or something.

I don't see this getting implemented any time soon though.

@MaxLap merging results isn't really built for that, it can/could be used that way but for instance if you run a travis build you define one ruby version to run the job with different versions won't be able to access the same files.

PragTob avatar Nov 06 '17 21:11 PragTob

In the case of travis, if you use one of the popular coverage system that link to it, they will take care of the merging. From experience, coveralls does it when you use #wear_merged!, while i gave up on codeclimate since it didn't seem able to do it.

MaxLap avatar Nov 07 '17 03:11 MaxLap

So, I think the workaround is to put one of the 2 branches into a nocov and then live with it.

I'm doing that right now. However, this means I can't rely on coverage when running against multiple rubies.

Would be nice to get some proposal to work around the issue though. Dunno, something like a special pragma, like :cov-2.4 or smth, which doesn't involve interpreting arbitrary code, but you know best about the implementation feasibility of such a feature.

HoneyryderChuck avatar Nov 07 '17 10:11 HoneyryderChuck

@HoneyryderChuck that's why I suggested environment variables, :cov-2.4 is too specific... I'd guess something along the lines of # :nocov unless MY_ENV == "mystring": or something to that tune. I'd prefer a positive spin on it (if vs. unless) but I imagine the most common use case being to do something special for rails version 4, or ruby version 1.9. So, only marking it as :nocov: for everything but the special versions. Welcome to hear feedback on that.

Whatever the case, sadly time probably won't permit me to work on it any time soon.

PragTob avatar Nov 08 '17 17:11 PragTob