
Issue #19857 has been updated by jeremyevans0 (Jeremy Evans). Merging coverage results for eval would be inconsistent with how coverage for load is handled. example code: `t.rb`: ```ruby if $a p 1 else p 2 end ``` Print coverage result: ``` # Loaded Once $ ruby -r coverage -e 'Coverage.start; $a = true; load "t.rb"; p Coverage.result' 1 {"t.rb"=>[1, 1, nil, 0, nil]} # Loaded Twice, not merged $ ruby -r coverage -e 'Coverage.start; $a = true; load "t.rb"; $a = false; load "t.rb"; p Coverage.result' 1 2 {"t.rb"=>[1, 0, nil, 1, nil]} ``` I agree with @mame that if there is no way to ensure that the same code is evaluated in each evaluation, it is wrong to merge the results. I can understand why real world code would evaluate multiple times for the same file and line: ```ruby [Array, Hash].each do |klass| klass.class_eval(RUBY, __FILE__, __LINE__+1) if self == Hash def foo; :bar end else def foo; :baz end end RUBY end ``` This is a trivial example, but at least shows why you would want to do it. In this example, it would be easy to separate the example code per class, but that is probably non-trivial and a bad idea for more complex examples. 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 and not the default behavior (assuming that @mame is open to supporting it as an option). ---------------------------------------- Bug #19857: Eval coverage is reset after each `eval`. https://bugs.ruby-lang.org/issues/19857#change-104481 * 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/