simplecov-ruby / simplecov

Code coverage for Ruby with a powerful configuration library and automatic merging of coverage across test suites
MIT License
4.76k stars 551 forks source link

Coverage on ERB #38

Closed ryanb closed 13 years ago

ryanb commented 13 years ago

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.

colszowka commented 13 years ago

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 commented 11 years ago

Very old ticket, but do you know if this is possible with any other 1.9 coverage tools?

eloyesp commented 6 years ago

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 commented 6 years ago

What about HAML / Slim and friends?

eloyesp commented 6 years ago

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 notifications@github.com escribió:

What about HAML / Slim and friends?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/colszowka/simplecov/issues/38#issuecomment-346961213, or mute the thread https://github.com/notifications/unsubscribe-auth/AAKm5XD1tEnfDtXIE88Hc2yvoc0F469mks5s6GvzgaJpZM4AShSg .

ioquatix commented 6 years ago

Any update on using tracepoints?

eloyesp commented 6 years ago

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.

ioquatix commented 6 years ago

Yeah I tried using trace points and came to the same conclusion. :(

bf4 commented 6 years ago

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

On Fri, Jan 26, 2018 at 2:11 PM, Samuel Williams notifications@github.com wrote:

Yeah I tried using trace points and came to the same conclusion. :(

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/colszowka/simplecov/issues/38#issuecomment-360892122, or mute the thread https://github.com/notifications/unsubscribe-auth/AAIuQlOG2h1ixJYYDuRRyokbRLg5a9BCks5tOjF9gaJpZM4AShSg .

ioquatix commented 6 years ago

I had a look in the MRI source code and for some reason it doesn't compute coverage for eval code 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 commented 5 years ago

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 commented 5 years ago

It works in covered but I don’t use standard coverage library because this bug still applies.

mame commented 1 year ago

I have created a PR that will solve this issue: https://github.com/simplecov-ruby/simplecov/pull/1037