From: "jeremyevans0 (Jeremy Evans)" Date: 2022-09-22T22:44:51+00:00 Subject: [ruby-core:110011] [Ruby master Feature#19008] Introduce coverage support for `eval`. Issue #19008 has been updated by jeremyevans0 (Jeremy Evans). mame (Yusuke Endoh) wrote in #note-11: > > In the cases where I do care and I want to check coverage of evaled code, it would be best to be able to look at all evals separately. It looks like your current approach is to just override any existing coverage entry for each new eval, which does not allow for that. > > It is a fair point, but I am afraid that recording per call to eval will bloat the coverage data without limit. How about accumulating all coverage data of eval'ed code? > > ``` > 10000.times do |i| > eval <<-END, binding, __FILE__, __LINE__+1 > 1 > END > end > > Coverage.result #=> { __FILE__ => [1, nil, ...], [:eval, __FILE__] => [10000] } > ``` > > I am very afraid if the key `[:eval, __FILE__]` will break some existing coverage tools like simplecov. If we choose this design, we should introduce `Coverage.start(eval: false)` and disable it by default. I agree that there should be a keyword to enable this support, and it should be disabled by default. If you are ignoring `__LINE__` when computing coverage (I'm guessing you are, since you have a value of `[10000]`, even though `__LINE__` should be 3 and not 1), and trying to accumulate multiple calls, I think it would be problematic to use `[:eval, __FILE__]` as the key, because it wouldn't correctly handle something like: ```ruby eval <<-END, binding, __FILE__, __LINE__+1 if true something end END eval <<-END, binding, __FILE__, __LINE__+1 if false something end END ``` You could probably work around that with `[:eval, __FILE__, __LINE__]` as the key. That wouldn't handle correctly handle cases where you aren't passing a literal string and the string can vary per call: ```ruby 3.times do |i| eval codes[i], binding, __FILE__, __LINE__ end ``` However, I expect such calls are rarer, and maybe we don't need to handle them. What are your thoughts on a callable keyword argument for setting the coverage key?: ```ruby Coverage.start(eval_key: ->(file, line){"eval-#{file}:#{line}"}) ``` If this returned a key that already existed, coverage information could be accumulated (either by default or as an option). ---------------------------------------- Feature #19008: Introduce coverage support for `eval`. https://bugs.ruby-lang.org/issues/19008#change-99262 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal ---------------------------------------- I'd like to introduce coverage support for `eval`. I mostly only care about the case where an explicit path is given, and I'd even be okay to only handle the case where the line number is the default (0). https://github.com/ruby/ruby/pull/6396 This is an incredibly useful feature for computing coverage of ERB templates and other similar things. -- https://bugs.ruby-lang.org/ Unsubscribe: