Issue #19857 has been updated by ioquatix (Samuel Williams).
@jeremyevans0 I'm not proposing any change to `load`, only to `eval` and related
methods. However, I actually believe your example is equally confusing because the
coverage is not merged. If someone loads the same file several times, it's reasonable
to expect coverage to be merged (and probably unexpected that it's not).
I agree with @mame (Yusuke Endoh) that if there is no
way to ensure that the same code is evaluated in each evaluation, it is wrong to merge the
results.
We have no way to disambiguate coverage, since by its nature, coverage refers to files and
line offsets. In practice, After the program executes, coverage is displayed by loading
those files and displaying information to the user. If those file paths and line numbers
are not consistent, coverage will become meaningless. So if the user does not desire
coverage, they should simply avoid setting the path and line number. This kind of
consistency is also important for the Ruby debugger, so this is not something specific to
coverage.
I'm not necessarily opposed to merging results,
but as it is not possible to ensure result merging makes sense, it should be an option
This is already the case: it already has an option, `Coverage.start(eval: true)`.
When you select this mode, coverage is already merged. If you have several evals for the
same file, they all get merged into one coverage report.
What this bug is trying to address, is the fact that when those eval ranges overlap, they
get reset. In every case I can think of, this is undesirable and basically not reporting
coverage correctly, as lines which are previously executed, with coverage, get reset to
0.
----------------------------------------
Bug #19857: Eval coverage is reset after each `eval`.
https://bugs.ruby-lang.org/issues/19857#change-104486
* Author: ioquatix (Samuel Williams)
* Status: Open
* Priority: Normal
* Assignee: ioquatix (Samuel Williams)
* Backport: 3.0: DONTNEED, 3.1: DONTNEED, 3.2: REQUIRED
----------------------------------------
It seems like `eval` based coverage is reset every time eval is invoked.
```ruby
#!/usr/bin/env ruby
require 'coverage'
def measure(flag)
c = Class.new
c.class_eval(<<~RUBY, "foo.rb", 1)
def foo(flag)
if flag
puts "foo"
else
puts "bar"
end
end
RUBY
return c.new.foo(flag)
end
Coverage.start(lines: true, eval: true)
# Depending on the order of these two operations, different coverage is calculated,
because the evaluation of the code is considered different, even if the content/path is
the same.
measure(false)
measure(true)
p Coverage.result
```
Further investigation is required.
--
https://bugs.ruby-lang.org/