Does C1 Code Coverage Analysis Exist for Ruby

Does C1 code coverage analysis exist for Ruby?

At the moment, there are no C1 coverage tools for Ruby. In fact, there aren't any coverage tools other than RCov.

Until recently, it was only possible to write tools like this by patching or extending the MRI interpreter in C. Since approximately two years ago, it is also possible to extend JRuby in Java, and there is actually since last month a port of RCov for JRuby. However, this requires both a knowledge of Ruby and C, and a pretty deep knowledge at that, because fiddling around with the internals of MRI is not for the faint at heart.

But only with Rubinius will it be possible to write dynamic analysis tools such as code coverage tools in Ruby itself, making tool writing accessible to a much larger portion of the Ruby community. My hope is that this, coupled with the substantial financial backing of tool vendors (many major IDE vendors are either working on or have already introduced Ruby IDEs, including CodeGear (ex-Borland), IntelliJ, NetBeans, Eclipse, SapphireSteel (Ruby in Steel for Visual Studio) and even Microsoft) will lead to rapid innovation in the Ruby tooling space in 2009 and we will see things like C1, C2 coverage, NPath complexity, much more fine-grained profiling and so on.

Until then, the only idea I have is to use Java tools. The JRuby guys try to emit the proper magic metadata to make their generated bytecode at least penetrable by the Java tools. So, maybe it is possible to use Java coverage tools with JRuby. However, I have no idea whether that actually works, nor if it is supposed to work.

What is Ruby Coverage.c is and what is it for?

The "number of line execution" is, as you might expect, the number of times a line has been executed by the interpreter during execution of the program.

Let's look at the example in the documentation with the results added to the original code as comments.

[foo.rb]
s = 0 # executed once
10.times do |x| # once
s += x # ten times
end # not analyzed
# not analyzed
if s == 45 # once
p :ok # once
else # not analyzed
p :ng # not executed
end # not analyzed
[EOF]

require "coverage.so"
Coverage.start
require "foo.rb"
p Coverage.result #=> {"foo.rb"=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]}

The "coverage" is the analysis of how many times each line is executed.

I assume that this module would be useful to see if you have decent "code coverage" with your tests, i.e. check if the tests actually go through all the code or if there are parts that are never executed.

Using the same example in that context, this coverage analysis tells you that the else p :ng part is not exectued and might not be tested.

Gems to assist in monitoring code quality

I use the following myself. I've found rubycritic particularly useful, and I believe it is the gem underlying a lot of the Code Climate reviews.

The sandi_meter is one I haven't used as much, but strongly encourages smaller methods.

I also strongly encourage using simplecov to help you evaluate your code coverage. Code coverage is by no means a panacea for any code problem, but it'll at least ensure that you do not miss segments of your code altogether.

The full list:

  gem 'traceroute' # Checks for undefined routes and unreachable actions.
gem 'bullet' # Checks for query optimizations.
gem 'rails_best_practices' # Checks for code optimization.
gem 'rubycritic' # Checks for code optimization.
gem 'sandi_meter' # Checks for compliance to Sandi Metz's rules for developers.
gem 'simplecov' #Enables coverage analysis of code.

Ruby source code analyzer (something like pylint)

I reviewed a bunch of Ruby tools that are available here

http://devver.wordpress.com/2008/10/03/ruby-tools-roundup/

most of the tools were mentioned by webmat, but if you want more information I go pretty in depth with examples.

I also highly recommend using Metric-Fu it gives you a on gem/plugin install of 3 of the more popular tools and is built with cruisecontrolrb integration in mind.

The creator has a great post that should help get you up and running in no time.

http://jakescruggs.blogspot.com/2008/04/dead-simple-rails-metrics-with-metricfu.html

There has been a lot of activity in Ruby tools lately which I think is a good sign of a growing and maturing language.



Related Topics



Leave a reply



Submit