Using Rcov to measure the test coverage of Rails plugins

Posted by jamie Fri, 24 Aug 2007 18:44:35 GMT

To view the coverage of your plugins using Rcov, first install the rcov gem with sudo gem install rcov, then copy and paste the following onto the end of the Rakefile inside your plugin directory:

desc 'Measures test coverage using rcov'
task :rcov do
  rm_f "coverage"
  rm_f "coverage.data"
  rcov = "rcov --rails --aggregate coverage.data --text-summary -Ilib"
  system("#{rcov} --html #{Dir.glob('test/**/*_test.rb').join(' ')}")
  system("open coverage/index.html") if PLATFORM['darwin']
end

You can now simply run rake rcov from inside your plugin directory which will generate a coverage directory with the results. Open coverage/index.html (if you are on OSX this will open automatically) in a browser to view the results.

Thanks to Mike Clark for his Rcov rake task for Rails which this task is based on.

Posted in , ,  | Tags , , , ,  | 1 comment

Disabling plugin code in generators/migrations

Posted by jamie Thu, 21 Sep 2006 10:48:50 GMT

I have found on numerous occasions that I need to disable certain plugin functionality if running a generator / rake db:migrate etc.

An easy way to disable certain functionality follows:

def method_that_shouldnt_be_run_in_migrations_or_generators
  # Return if we are using a generator or migrations
  script = File.basename($0)
  return if (script == 'generate') || (script == 'rake' && ARGV[0] =~ /migrate$/)
end

Posted in ,  | Tags , ,  | 4 comments