You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Great work on this gem. Is it possible to check code coverage in ERB templates? In a Rails app there might be an "if" condition in a view which is not covered. It would be great if it were somehow possible to check this.
Reacted by Corey Farwell, Wiktor Plaga, Markos Fragkakis, Al Bo and Gagandeep singh
No, unfortunately it's not since the underlying Ruby 1.9 Stdlib Coverage library only tracks files that are require'd after coverage starts and thus only allows .rb files (since you can only require files ending with .rb ;)
Templates on the other hand are read from a file and evaluated later on, and thus are out of the visibility of the coverage lib.
I know it is an old issue, but it might be possible to add a hook on ERB when it read a template file or it compiles it and add those calls to the result set. Does it makes sense? @colszowka ?
I were checking, and it seems that using trasepoint instead of coverage we
could track coverage of template files, I will do some testing on that,
what do you think about that approach?
El sáb., nov. 25, 2017 16:35, Dmitry Polushkin <[email protected]>
escribió:
I were trying just a bit, but found not way differentiate code that is not
being executed from lines that are being ignored.
For example, on the following code:
```
def foo bar
if bar > 2
bar
else
2
end
end
foo 3
```
I shuld have `[1,1,1,nil,0,nil,nil,nil,1]`. I have no way to distinguish
`nil` from `0` using tracepoint.
I think you'd have to patch ERB in some way to cause the template text to
be considered a file after it was parsed but before it was eval'd.
I just tried making a template.rb
<% if true %>
"true"
<%= 'true' %>
<% else %>
"false"
<% end %>
And then from the terminal
ruby -rerb -rcoverage -e 'Coverage.start; puts
ERB.new(File.read("./template.rb")).result; p Coverage.result'
"true"
true
{}
Which didn't pickup up evaluating any Ruby files, which you'd expect since
the input to `ERB` is text that happened to be read in from a file, but
the file wasn't evaluated
Activity
colszowka commentedon Apr 21, 2011
No, unfortunately it's not since the underlying Ruby 1.9 Stdlib Coverage library only tracks files that are
require'd after coverage starts and thus only allows .rb files (since you can only require files ending with .rb ;)Templates on the other hand are read from a file and evaluated later on, and thus are out of the visibility of the coverage lib.
Sorry :/
jamiew commentedon Dec 2, 2012
Very old ticket, but do you know if this is possible with any other 1.9 coverage tools?
Make test coverage data available for smart answer flows
Allow smart answer flows to be defined at runtime not load time
eloyesp commentedon Nov 23, 2017
I know it is an old issue, but it might be possible to add a hook on ERB when it read a template file or it compiles it and add those calls to the result set. Does it makes sense? @colszowka ?
dmitry commentedon Nov 25, 2017
What about HAML / Slim and friends?
eloyesp commentedon Nov 25, 2017
ioquatix commentedon Jan 26, 2018
Any update on using tracepoints?
eloyesp commentedon Jan 26, 2018
ioquatix commentedon Jan 26, 2018
Yeah I tried using trace points and came to the same conclusion. :(
bf4 commentedon Jan 28, 2018
ioquatix commentedon Jan 28, 2018
I had a look in the MRI source code and for some reason it doesn't compute coverage for
evalcode which is being parsed.https://github.com/ruby/ruby/blob/582951e2c8995d6bab5ddaf98cd3816310f8d506/parse.y#L4785
It might just be a matter if being slightly more allowing at that line, i.e. if calling eval with the optional file name argument set to something.
joevandyk commentedon May 23, 2019
Might this be doable now?
https://bugs.ruby-lang.org/issues/14888
https://bugs.ruby-lang.org/issues/15287
https://github.com/ioquatix/covered/
ioquatix commentedon May 23, 2019
It works in covered but I don’t use standard coverage library because this bug still applies.
mame commentedon Dec 3, 2022
I have created a PR that will solve this issue: #1037